重构 hp maxhp ability

This commit is contained in:
aozhiwei 2021-06-30 12:45:45 +00:00
parent c56483e85d
commit 545939d67e
6 changed files with 57 additions and 26 deletions

View File

@ -43,8 +43,8 @@ void Car::Initialize()
SetCurrWeapon(&weapons[GUN_SLOT1]); SetCurrWeapon(&weapons[GUN_SLOT1]);
} }
born_frameno_ = room->GetFrameNo(); born_frameno_ = room->GetFrameNo();
ability.hp = hero_meta_->i->health(); SetHP(hero_meta_->i->health());
ability.max_hp = std::max(ability.hp, ability.max_hp); SetMaxHP(std::max(GetHP(), GetMaxHP()));
TryAddBuff(this, meta->car_deactive_buff_id); TryAddBuff(this, meta->car_deactive_buff_id);
cur_oil_ = meta->i->max_oil(); cur_oil_ = meta->i->max_oil();
} }
@ -270,7 +270,7 @@ void Car::OnBulletHit(Bullet* bullet)
{ {
if (!IsDead(room)) { if (!IsDead(room)) {
float dmg = bullet->GetAtk() + bullet->gun_meta->i->atk_mech(); float dmg = bullet->GetAtk() + bullet->gun_meta->i->atk_mech();
float def = ability.def * (1 + GetAbility()->GetAttrRate(kHAT_Def)) + float def = GetDef() * (1 + GetAbility()->GetAttrRate(kHAT_Def)) +
GetAbility()->GetAttrAbs(kHAT_Def); GetAbility()->GetAttrAbs(kHAT_Def);
float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K); float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K);
finaly_dmg = std::max(finaly_dmg, 0.0f); finaly_dmg = std::max(finaly_dmg, 0.0f);
@ -291,7 +291,7 @@ void Car::DecHP(float dec_hp, int killer_id, const std::string& killer_name, int
} }
float old_health = GetHP(); float old_health = GetHP();
float new_health = std::max(0.0f, GetHP() - dec_hp); float new_health = std::max(0.0f, GetHP() - dec_hp);
ability.hp = std::max(0.0f, new_health); SetHP(std::max(0.0f, new_health));
if (GetHP() <= 0.0001f && !IsDead(room)) { if (GetHP() <= 0.0001f && !IsDead(room)) {
BeKill(killer_id, killer_name, weapon_id); BeKill(killer_id, killer_name, weapon_id);
} }

View File

@ -1891,6 +1891,16 @@ float Creature::GetMaxHP()
return ability.max_hp; return ability.max_hp;
} }
float Creature::GetDef()
{
return ability.def;
}
void Creature::SetDef(float def)
{
ability.def = def;
}
void Creature::GetHitEnemys(std::set<Creature*>& enemys) void Creature::GetHitEnemys(std::set<Creature*>& enemys)
{ {
room->grid_service->TraverseCreatures room->grid_service->TraverseCreatures
@ -1923,6 +1933,16 @@ void Creature::AddHp(float hp)
} }
} }
void Creature::SetHP(float hp)
{
ability.hp = hp;
}
void Creature::SetMaxHP(float max_hp)
{
ability.max_hp = max_hp;
}
bool Creature::TryMove(const a8::Vec2& target_pos, a8::Vec2& out_pos) bool Creature::TryMove(const a8::Vec2& target_pos, a8::Vec2& out_pos)
{ {
bool move_ok = false; bool move_ok = false;

View File

@ -60,7 +60,6 @@ class Creature : public MoveableEntity
a8::Vec2 shoot_offset; a8::Vec2 shoot_offset;
int shot_hole = 0; int shot_hole = 0;
Creature* shot_passenger = nullptr; Creature* shot_passenger = nullptr;
HumanAbility ability;
a8::Vec2 target_pos; a8::Vec2 target_pos;
std::function<bool ()> on_move_collision; std::function<bool ()> on_move_collision;
bool poisoning = false; bool poisoning = false;
@ -145,6 +144,8 @@ class Creature : public MoveableEntity
Car* AsCar() { return IsCar() ? (Car*)this : nullptr; }; Car* AsCar() { return IsCar() ? (Car*)this : nullptr; };
virtual void DecHP(float dec_hp, int killer_id, const std::string& killer_name, int weapon_id) {}; virtual void DecHP(float dec_hp, int killer_id, const std::string& killer_name, int weapon_id) {};
void AddHp(float hp); void AddHp(float hp);
void SetHP(float hp);
void SetMaxHP(float max_hp);
RaceType_e GetRace() { return race_; } RaceType_e GetRace() { return race_; }
@ -190,6 +191,8 @@ class Creature : public MoveableEntity
void ClearAimingBuffs(); void ClearAimingBuffs();
float GetHP(); float GetHP();
float GetMaxHP(); float GetMaxHP();
float GetDef();
void SetDef(float def);
void GetHitEnemys(std::set<Creature*>& enemys); void GetHitEnemys(std::set<Creature*>& enemys);
bool TryMove(const a8::Vec2& target_pos, a8::Vec2& out_pos); bool TryMove(const a8::Vec2& target_pos, a8::Vec2& out_pos);
void SetInfiniteBulletMode(); void SetInfiniteBulletMode();
@ -241,6 +244,7 @@ private:
Team* team_ = nullptr; Team* team_ = nullptr;
Weapon* curr_weapon_ = nullptr; Weapon* curr_weapon_ = nullptr;
CreatureWeakPtrChunk weak_ptr_chunk_; CreatureWeakPtrChunk weak_ptr_chunk_;
HumanAbility ability;
std::shared_ptr<Ability> ability_; std::shared_ptr<Ability> ability_;
std::array<list_head, kBET_End> buff_effect_ = {}; std::array<list_head, kBET_End> buff_effect_ = {};
std::array<list_head, kBET_End> depend_effect_ = {}; std::array<list_head, kBET_End> depend_effect_ = {};

View File

@ -83,7 +83,7 @@ void Hero::OnBulletHit(Bullet* bullet)
} }
if (!IsDead(room) && (bullet->IsBomb() || bullet->sender.Get()->team_id != team_id)) { if (!IsDead(room) && (bullet->IsBomb() || bullet->sender.Get()->team_id != team_id)) {
float dmg = bullet->GetAtk(); float dmg = bullet->GetAtk();
float def = ability.def * (1 + GetAbility()->GetAttrRate(kHAT_Def)) + float def = GetDef() * (1 + GetAbility()->GetAttrRate(kHAT_Def)) +
GetAbility()->GetAttrAbs(kHAT_Def); GetAbility()->GetAttrAbs(kHAT_Def);
float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K); float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K);
finaly_dmg = std::max(finaly_dmg, 0.0f); finaly_dmg = std::max(finaly_dmg, 0.0f);
@ -202,7 +202,7 @@ void Hero::DecHP(float dec_hp, int killer_id, const std::string& killer_name, in
} }
float old_health = GetHP(); float old_health = GetHP();
float new_health = std::max(0.0f, GetHP() - dec_hp); float new_health = std::max(0.0f, GetHP() - dec_hp);
ability.hp = std::max(0.0f, new_health); SetHP(std::max(0.0f, new_health));
if (GetHP() <= 0.0001f && !IsDead(room)) { if (GetHP() <= 0.0001f && !IsDead(room)) {
BeKill(killer_id, killer_name, weapon_id); BeKill(killer_id, killer_name, weapon_id);
} }

View File

@ -66,12 +66,15 @@ void Human::Initialize()
RecalcSelfCollider(); RecalcSelfCollider();
volume_ = meta->volume; volume_ = meta->volume;
observers_.insert(this); observers_.insert(this);
ability.hp = meta->i->health(); SetHP(meta->i->health());
for (auto& weapon : spec_weapons) { for (auto& weapon : spec_weapons) {
if (weapon.meta) { if (weapon.meta) {
ability.hp += weapon.meta ? weapon.GetAttrValue(kHAT_MaxHp) : 0; float hp = GetHP();
hp += weapon.meta ? weapon.GetAttrValue(kHAT_MaxHp) : 0;
SetHP(hp);
} }
} }
SetMaxHP(GetHP());
} }
float Human::GetSpeed() float Human::GetSpeed()
@ -784,7 +787,7 @@ void Human::BeKill(int killer_id, const std::string& killer_name, int weapon_id)
} else { } else {
dead = true; dead = true;
downed = false; downed = false;
ability.hp = 0.0f; SetHP(0.0f);
dead_frameno = room->GetFrameNo(); dead_frameno = room->GetFrameNo();
++dead_times; ++dead_times;
if (HasBuffEffect(kBET_Camouflage)) { if (HasBuffEffect(kBET_Camouflage)) {
@ -859,7 +862,7 @@ void Human::DecHP(float dec_hp, int killer_id, const std::string& killer_name, i
float old_health = GetHP(); float old_health = GetHP();
float new_health = std::max(0.0f, GetHP() - dec_hp); float new_health = std::max(0.0f, GetHP() - dec_hp);
AdjustDecHp(old_health, new_health); AdjustDecHp(old_health, new_health);
ability.hp = std::max(0.0f, new_health); SetHP(std::max(0.0f, new_health));
if (GetHP() - old_health > 0.001f) { if (GetHP() - old_health > 0.001f) {
stats.damage_amount_in += GetHP() - old_health; stats.damage_amount_in += GetHP() - old_health;
} }
@ -874,7 +877,7 @@ void Human::DecHP(float dec_hp, int killer_id, const std::string& killer_name, i
SyncAroundPlayers(__FILE__, __LINE__, __func__); SyncAroundPlayers(__FILE__, __LINE__, __func__);
} else { } else {
if (HasNoDownedTeammate()) { if (HasNoDownedTeammate()) {
ability.hp = MetaMgr::Instance()->GetSysParamAsInt("downed_recover_hp"); SetHP(MetaMgr::Instance()->GetSysParamAsInt("downed_recover_hp"));
downed = true; downed = true;
if (HasBuffEffect(kBET_Camouflage)) { if (HasBuffEffect(kBET_Camouflage)) {
RemoveBuffByEffectId(kBET_Camouflage); RemoveBuffByEffectId(kBET_Camouflage);
@ -1326,16 +1329,17 @@ void Human::RecalcVolume()
void Human::RecalcBaseAttr() void Human::RecalcBaseAttr()
{ {
ability.def = meta->i->def();
MetaData::Equip* chest_meta = MetaMgr::Instance()->GetEquip(chest); MetaData::Equip* chest_meta = MetaMgr::Instance()->GetEquip(chest);
float def = meta->i->def();
if (chest_meta) { if (chest_meta) {
ability.def += chest_meta->i->def(); def += chest_meta->i->def();
} }
MetaData::Equip* helmet_meta = MetaMgr::Instance()->GetEquip(helmet); MetaData::Equip* helmet_meta = MetaMgr::Instance()->GetEquip(helmet);
if (helmet_meta) { if (helmet_meta) {
ability.def += helmet_meta->i->def(); def += helmet_meta->i->def();
} }
ability.max_hp = std::max(ability.hp, ability.max_hp); SetDef(def);
SetMaxHP(std::max(GetHP(), GetMaxHP()));
} }
int Human::GetVolume(int slot_id) int Human::GetVolume(int slot_id)
@ -1349,8 +1353,10 @@ int Human::GetVolume(int slot_id)
void Human::RecoverHp(int inc_hp) void Human::RecoverHp(int inc_hp)
{ {
if (!dead) { if (!dead) {
ability.hp += inc_hp; float hp = GetHP();
ability.hp = std::max(GetHP(), GetMaxHP()); hp += inc_hp;
SetHP(hp);
SetHP(std::min(GetHP(), GetMaxHP()));
} }
} }
@ -2586,7 +2592,7 @@ void Human::Revive()
dead = false; dead = false;
downed = false; downed = false;
real_dead = false; real_dead = false;
ability.hp = GetMaxHP(); SetHP(GetMaxHP());
ClearBuffList(); ClearBuffList();
{ {
MetaData::Buff* buff_meta = MetaMgr::Instance()->GetBuff(kREVIVE_BUFF_ID); MetaData::Buff* buff_meta = MetaMgr::Instance()->GetBuff(kREVIVE_BUFF_ID);
@ -2878,12 +2884,13 @@ void Human::OnMetaChange()
SetCurrWeapon(&weapons[0]); SetCurrWeapon(&weapons[0]);
} }
} }
ability.hp = meta->i->health(); float hp = meta->i->health();
for (auto& weapon : spec_weapons) { for (auto& weapon : spec_weapons) {
if (weapon.meta) { if (weapon.meta) {
ability.hp += weapon.meta ? weapon.GetAttrValue(kHAT_MaxHp) : 0; hp += weapon.meta ? weapon.GetAttrValue(kHAT_MaxHp) : 0;
} }
} }
SetHP(hp);
room->frame_event.AddHpChg(GetWeakPtrRef()); room->frame_event.AddHpChg(GetWeakPtrRef());
RecalcBaseAttr(); RecalcBaseAttr();
ClearSkill(); ClearSkill();
@ -3063,7 +3070,7 @@ void Human::ProcUseItemAction()
{ {
if (!dead) { if (!dead) {
if (downed) { if (downed) {
ability.hp = MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp"); SetHP(MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp"));
downed = false; downed = false;
if (downed_timer) { if (downed_timer) {
room->xtimer.DeleteTimer(downed_timer); room->xtimer.DeleteTimer(downed_timer);
@ -3096,7 +3103,7 @@ void Human::ProcReliveAction()
return; return;
} }
if (!hum->dead && hum->downed) { if (!hum->dead && hum->downed) {
hum->ability.hp = MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp"); SetHP(MetaMgr::Instance()->GetSysParamAsInt("downed_relive_recover_hp"));
hum->downed = false; hum->downed = false;
if (hum->downed_timer) { if (hum->downed_timer) {
room->xtimer.DeleteTimer(hum->downed_timer); room->xtimer.DeleteTimer(hum->downed_timer);
@ -3483,7 +3490,7 @@ void Human::OnBulletHit(Bullet* bullet)
} }
if (!dead && (bullet->IsBomb() || bullet->sender.Get()->team_id != team_id)) { if (!dead && (bullet->IsBomb() || bullet->sender.Get()->team_id != team_id)) {
float dmg = bullet->GetAtk(); float dmg = bullet->GetAtk();
float def = ability.def * (1 + GetAbility()->GetAttrRate(kHAT_Def)) + float def = GetDef() * (1 + GetAbility()->GetAttrRate(kHAT_Def)) +
GetAbility()->GetAttrAbs(kHAT_Def); GetAbility()->GetAttrAbs(kHAT_Def);
float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K); float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K);
finaly_dmg = std::max(finaly_dmg, 0.0f); finaly_dmg = std::max(finaly_dmg, 0.0f);
@ -3530,7 +3537,7 @@ void Human::OnExplosionHit(Explosion* e)
return; return;
} }
float dmg = e->GetDmg(); float dmg = e->GetDmg();
float def = ability.def * (1 + GetAbility()->GetAttrRate(kHAT_Def)) + float def = GetDef() * (1 + GetAbility()->GetAttrRate(kHAT_Def)) +
GetAbility()->GetAttrAbs(kHAT_Def); GetAbility()->GetAttrAbs(kHAT_Def);
float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K); float finaly_dmg = dmg * (1 - def/MetaMgr::Instance()->K);
finaly_dmg = std::max(finaly_dmg, 0.0f); finaly_dmg = std::max(finaly_dmg, 0.0f);

View File

@ -1389,7 +1389,7 @@ void Player::_CMRevive(f8::MsgHdr& hdr, const cs::CMRevive& msg)
real_dead = false; real_dead = false;
downed = false; downed = false;
FreeDownedTimer(); FreeDownedTimer();
ability.hp = GetMaxHP(); SetHP(GetMaxHP());
SyncAroundPlayers(__FILE__, __LINE__, __func__); SyncAroundPlayers(__FILE__, __LINE__, __func__);
FreeReviveTimer(); FreeReviveTimer();
#if 1 #if 1