重构 UpdateAction

This commit is contained in:
aozhiwei 2021-05-13 15:11:00 +08:00
parent 66f93388bd
commit 78e32712c6
2 changed files with 104 additions and 91 deletions

View File

@ -1567,11 +1567,9 @@ void Human::SendRollMsg(const std::string& msg, int killer_id, bool killer_team_
void Human::UpdateAction()
{
int duration = std::max(0,
action_duration -
(int)((room->GetFrameNo() - action_frameno) * 1.0f / SERVER_FRAME_RATE) * 1000
);
if (duration <= 0) {
int passed_time = (room->GetFrameNo() - action_frameno) * FRAME_RATE_MS;
int left_time = std::max(0, action_duration - passed_time);
if (left_time <= 0) {
switch (action_type) {
case AT_Reload:
{
@ -1580,99 +1578,17 @@ void Human::UpdateAction()
break;
case AT_UseItem:
{
switch (action_item_id) {
case IS_HEALTHKIT:
{
MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id);
if (item_meta){
if (GetInventory(item_meta->i->_inventory_slot()) > 0) {
float old_health = GetHP();
ability.hp += item_meta->i->heal();
ability.hp = std::min(GetHP(), GetMaxHP());
stats.heal_amount += GetHP() - old_health;
DecInventory(item_meta->i->_inventory_slot(), 1);
need_sync_active_player = true;
SyncAroundPlayers(__FILE__, __LINE__, __func__);
}
}
}
break;
case IS_PAIN_KILLER:
{
MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id);
if (item_meta){
if (GetInventory(item_meta->i->_inventory_slot()) > 0) {
if (pain_killer_timer) {
int passed_time = (room->GetFrameNo() - pain_killer_frameno) * FRAME_RATE_MS;
int left_time = std::max(0, pain_killer_lastingtime * 1000 - passed_time);
int anodyne_max_time = MetaMgr::Instance()->GetSysParamAsInt("anodyne_max_time");
left_time = std::min(left_time, anodyne_max_time * 1000);
pain_killer_lastingtime += std::min(item_meta->i->time() * 1000, anodyne_max_time * 1000 - left_time) / 1000;
need_sync_active_player = true;
} else {
pain_killer_frameno = room->GetFrameNo();
pain_killer_lastingtime = item_meta->i->time();
pain_killer_timer = room->xtimer.AddRepeatTimerAndAttach(
SERVER_FRAME_RATE,
a8::XParams()
.SetSender(this)
.SetParam1(item_meta->i->heal()),
[] (const a8::XParams& param)
{
Human* hum = (Human*)param.sender.GetUserData();
float old_health = hum->GetHP();
hum->ability.hp += param.param1.GetDouble();
hum->ability.hp = std::min(hum->GetHP(), hum->GetMaxHP());
hum->stats.heal_amount += hum->GetHP() - old_health;
hum->SyncAroundPlayers(__FILE__, __LINE__, __func__);
if (hum->room->GetFrameNo() - hum->pain_killer_frameno > hum->pain_killer_lastingtime * SERVER_FRAME_RATE) {
hum->room->xtimer.DeleteTimer(hum->pain_killer_timer);
hum->pain_killer_timer = nullptr;
}
},
&xtimer_attacher.timer_list_
);
}
DecInventory(item_meta->i->_inventory_slot(), 1);
need_sync_active_player = true;
}
}
}
break;
default:
{
}
break;
}
ProcUseItemAction();
}
break;
case AT_Relive:
{
Entity* entity = room->GetEntityByUniId(action_target_id);
if (!entity->IsEntityType(ET_Player)) {
return;
}
Human* hum = (Human*)entity;
if (hum->action_type == AT_Rescue) {
hum->CancelAction();
return;
}
if (!hum->dead && hum->downed) {
hum->ability.hp = MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp");
hum->downed = false;
if (hum->downed_timer) {
room->xtimer.DeleteTimer(hum->downed_timer);
hum->downed_timer = nullptr;
}
++hum->stats.rescue_member;
if (hum->guild_id != 0 && hum->guild_id == guild_id) {
++hum->stats.rescue_guild_member;
}
}
hum->SyncAroundPlayers(__FILE__, __LINE__, __func__);
ProcReliveAction();
}
break;
default:
{
}
break;
}
ResetAction();
@ -3234,6 +3150,101 @@ void Human::ProcReloadAction()
}
}
void Human::ProcUseItemAction()
{
switch (action_item_id) {
case IS_HEALTHKIT:
{
MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id);
if (item_meta){
if (GetInventory(item_meta->i->_inventory_slot()) > 0) {
float old_health = GetHP();
ability.hp += item_meta->i->heal();
ability.hp = std::min(GetHP(), GetMaxHP());
stats.heal_amount += GetHP() - old_health;
DecInventory(item_meta->i->_inventory_slot(), 1);
need_sync_active_player = true;
SyncAroundPlayers(__FILE__, __LINE__, __func__);
}
}
}
break;
case IS_PAIN_KILLER:
{
MetaData::Equip* item_meta = MetaMgr::Instance()->GetEquipBySlotId(action_item_id);
if (item_meta){
if (GetInventory(item_meta->i->_inventory_slot()) > 0) {
if (pain_killer_timer) {
int passed_time = (room->GetFrameNo() - pain_killer_frameno) * FRAME_RATE_MS;
int left_time = std::max(0, pain_killer_lastingtime * 1000 - passed_time);
int anodyne_max_time = MetaMgr::Instance()->GetSysParamAsInt("anodyne_max_time");
left_time = std::min(left_time, anodyne_max_time * 1000);
pain_killer_lastingtime += std::min(item_meta->i->time() * 1000, anodyne_max_time * 1000 - left_time) / 1000;
need_sync_active_player = true;
} else {
pain_killer_frameno = room->GetFrameNo();
pain_killer_lastingtime = item_meta->i->time();
pain_killer_timer = room->xtimer.AddRepeatTimerAndAttach
(
SERVER_FRAME_RATE,
a8::XParams()
.SetSender(this)
.SetParam1(item_meta->i->heal()),
[] (const a8::XParams& param)
{
Human* hum = (Human*)param.sender.GetUserData();
float old_health = hum->GetHP();
hum->ability.hp += param.param1.GetDouble();
hum->ability.hp = std::min(hum->GetHP(), hum->GetMaxHP());
hum->stats.heal_amount += hum->GetHP() - old_health;
hum->SyncAroundPlayers(__FILE__, __LINE__, __func__);
if (hum->room->GetFrameNo() - hum->pain_killer_frameno > hum->pain_killer_lastingtime * SERVER_FRAME_RATE) {
hum->room->xtimer.DeleteTimer(hum->pain_killer_timer);
hum->pain_killer_timer = nullptr;
}
},
&xtimer_attacher.timer_list_
);
}
DecInventory(item_meta->i->_inventory_slot(), 1);
need_sync_active_player = true;
}
}
}
break;
default:
{
}
break;
}
}
void Human::ProcReliveAction()
{
Entity* entity = room->GetEntityByUniId(action_target_id);
if (!entity->IsEntityType(ET_Player)) {
return;
}
Human* hum = (Human*)entity;
if (hum->action_type == AT_Rescue) {
hum->CancelAction();
return;
}
if (!hum->dead && hum->downed) {
hum->ability.hp = MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp");
hum->downed = false;
if (hum->downed_timer) {
room->xtimer.DeleteTimer(hum->downed_timer);
hum->downed_timer = nullptr;
}
++hum->stats.rescue_member;
if (hum->guild_id != 0 && hum->guild_id == guild_id) {
++hum->stats.rescue_guild_member;
}
}
hum->SyncAroundPlayers(__FILE__, __LINE__, __func__);
}
void Human::OnBuffRemove(Buff& buff)
{
switch (buff.meta->i->buff_effect()) {

View File

@ -278,6 +278,8 @@ private:
void OnMetaChange();
void OnChgToTerminator();
void ProcReloadAction();
void ProcUseItemAction();
void ProcReliveAction();
void OnLand();
void NextReload(int prev_weapon_id, int prev_weapon_idx);
void DoGetOnWithLoot(Loot* loot_entity);