Cleanup of ExecutingAttack, removing TargetData, and reducing the number of allocations needed.
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@@ -37,24 +37,10 @@ namespace CreatureLib::Battling {
|
||||
inline void SetType(uint8_t value) noexcept { _type = value; }
|
||||
};
|
||||
|
||||
class TargetData {
|
||||
bool _isHit = true;
|
||||
List<HitData> _hits;
|
||||
|
||||
public:
|
||||
explicit TargetData(uint8_t numberOfHits) : _hits(numberOfHits) { _hits.Resize(numberOfHits, HitData()); }
|
||||
TargetData() = default;
|
||||
TargetData& operator=(const TargetData&) = delete;
|
||||
|
||||
HitData& GetHit(uint8_t index) { return _hits[index]; }
|
||||
|
||||
uint8_t GetNumberOfHits() const noexcept { return _hits.Count(); }
|
||||
|
||||
bool IsHit() const noexcept { return _isHit; }
|
||||
};
|
||||
|
||||
private:
|
||||
Dictionary<Creature*, TargetData> _targets;
|
||||
List<Creature*> _targets;
|
||||
uint8_t _numberHits;
|
||||
List<HitData> _hits;
|
||||
Creature* _user;
|
||||
LearnedAttack* _attack;
|
||||
Script* _script;
|
||||
@@ -62,21 +48,30 @@ namespace CreatureLib::Battling {
|
||||
public:
|
||||
ExecutingAttack(const List<Creature*>& targets, uint8_t numberHits, Creature* user, LearnedAttack* attack,
|
||||
Script* script)
|
||||
: _targets(targets.Count()), _user(user), _attack(attack), _script(script) {
|
||||
: _targets(targets.Count()), _numberHits(numberHits), _hits(targets.Count() * numberHits), _user(user),
|
||||
_attack(attack), _script(script) {
|
||||
AssertNotNull(user)
|
||||
AssertNotNull(attack)
|
||||
for (auto target : targets) {
|
||||
_targets.Insert(target, TargetData(numberHits));
|
||||
_targets.Append(target);
|
||||
_hits.Resize(numberHits);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~ExecutingAttack() noexcept { delete _script; };
|
||||
|
||||
TargetData& GetAttackDataForTarget(Creature* creature) { return _targets[creature]; }
|
||||
HitData& GetHitData(Creature* creature, uint8_t hit) {
|
||||
for (size_t i = 0; i < _targets.Count(); i++) {
|
||||
if (_targets[i] == creature) {
|
||||
return _hits[i * _numberHits + hit];
|
||||
}
|
||||
}
|
||||
throw CreatureException("Invalid target requested.");
|
||||
}
|
||||
|
||||
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.Has(creature); }
|
||||
|
||||
Dictionary<Creature*, TargetData>& GetTargets() noexcept { return _targets; }
|
||||
bool IsCreatureTarget(Creature* creature) noexcept { return _targets.IndexOf(creature) != -1; }
|
||||
const List<Creature*>& GetTargets() noexcept { return _targets; }
|
||||
uint8_t GetNumberOfHits() const noexcept { return _numberHits; }
|
||||
|
||||
Creature* GetUser() noexcept { return _user; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user