Skip to content

Commit 92e2a53

Browse files
Владимир СеверовВладимир Северов
authored andcommitted
17.05.23 15:36 - Struct edited
1 parent f003e95 commit 92e2a53

File tree

111 files changed

+451
-300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+451
-300
lines changed

HW-2/Classes/AliveObject.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include "AliveObject.h"
2+
#include "GameScene.h"
3+
4+
aad::AliveObject::AliveObject()
5+
: parent_(nullptr)
6+
, health_(0.f)
7+
, maxHealth_(0.f)
8+
, armor_(0.f)
9+
, maxArmor_(0.f)
10+
, healthLabel_(nullptr)
11+
, armorLabel_(nullptr)
12+
{}
13+
14+
aad::AliveObject::AliveObject(GameObject const* parent, float maxHealth, float maxArmor)
15+
: parent_(nullptr)
16+
, health_(0.f)
17+
, maxHealth_(maxHealth)
18+
, armor_(0.f)
19+
, maxArmor_(maxArmor)
20+
, healthLabel_(nullptr)
21+
, armorLabel_(nullptr)
22+
{
23+
SetParent(parent);
24+
SetHealth(maxHealth_);
25+
SetArmor(0.f);
26+
}
27+
28+
void aad::AliveObject::Damage(float power)
29+
{
30+
if (power > GetArmor()) {
31+
SetArmor(0);
32+
SetHealth(GetHealth() - (power - GetArmor()));
33+
if (GetHealth() < 0) {
34+
SetHealth(0);
35+
}
36+
}
37+
else {
38+
SetArmor(GetArmor() - power);
39+
}
40+
}
41+
42+
void aad::AliveObject::SetParent(GameObject const * parent)
43+
{
44+
parent_ = parent;
45+
LabelsInit_();
46+
}
47+
48+
void aad::AliveObject::SetHealth(float health)
49+
{
50+
health_ = health;
51+
if (healthLabel_ != nullptr) {
52+
healthLabel_->setString(std::to_string(static_cast<int>(ceil(health_))));
53+
}
54+
}
55+
56+
void aad::AliveObject::SetArmor(float armor)
57+
{
58+
armor_ = armor;
59+
if (armorLabel_ != nullptr) {
60+
if (armor_ == 0) {
61+
armorLabel_->setString("");
62+
}
63+
else {
64+
armorLabel_->setString(std::to_string(static_cast<int>(ceil(armor_))));
65+
}
66+
}
67+
}
68+
69+
aad::GameObject const * aad::AliveObject::GetParent()
70+
{
71+
return parent_;
72+
}
73+
74+
float aad::AliveObject::GetHealth() const
75+
{
76+
return health_;
77+
}
78+
79+
float aad::AliveObject::GetMaxHealth() const
80+
{
81+
return maxHealth_;
82+
}
83+
84+
float aad::AliveObject::GetArmor() const
85+
{
86+
return armor_;
87+
}
88+
89+
float aad::AliveObject::GetMaxArmor() const
90+
{
91+
return maxArmor_;
92+
}
93+
94+
cocos2d::CCLabelTTF* aad::AliveObject::GetHealthLabel() const
95+
{
96+
return healthLabel_;
97+
}
98+
99+
cocos2d::CCLabelTTF* aad::AliveObject::GetArmorLabel() const
100+
{
101+
return armorLabel_;
102+
}
103+
104+
void aad::AliveObject::LabelsInit_()
105+
{
106+
if (parent_ != nullptr) {
107+
if (healthLabel_ == nullptr) {
108+
healthLabel_ = cocos2d::CCLabelTTF::create("", "Helvetica", HEALTH_FONT_SIZE, cocos2d::Size(parent_->GetW(), HEALTH_FONT_SIZE));
109+
}
110+
else {
111+
healthLabel_->setContentSize(cocos2d::Size(parent_->GetW(), HEALTH_FONT_SIZE));
112+
}
113+
if (armorLabel_ == nullptr) {
114+
armorLabel_ = cocos2d::CCLabelTTF::create("", "Helvetica", HEALTH_FONT_SIZE, cocos2d::Size(parent_->GetW(), HEALTH_FONT_SIZE));
115+
}
116+
else {
117+
armorLabel_->setContentSize(cocos2d::Size(parent_->GetW(), HEALTH_FONT_SIZE));
118+
}
119+
120+
OnXUpdate_();
121+
OnYUpdate_();
122+
OnRightAlignmentUpdate_();
123+
}
124+
}
125+
126+
void aad::AliveObject::OnXUpdate_()
127+
{
128+
if (parent_ != nullptr) {
129+
if (healthLabel_ != nullptr) {
130+
healthLabel_->setPositionX(parent_->GetSprite()->getPositionX());
131+
}
132+
if (armorLabel_ != nullptr) {
133+
armorLabel_->setPositionX(parent_->GetSprite()->getPositionX());
134+
}
135+
}
136+
}
137+
138+
void aad::AliveObject::OnYUpdate_()
139+
{
140+
if (parent_ != nullptr) {
141+
if (healthLabel_ != nullptr) {
142+
healthLabel_->setPositionY(parent_->GetY() + parent_->GetH() + MARGIN_SIZE);
143+
}
144+
if (armorLabel_ != nullptr) {
145+
armorLabel_->setPositionY(parent_->GetY() + parent_->GetH() + 2 * MARGIN_SIZE + HEALTH_FONT_SIZE);
146+
}
147+
}
148+
}
149+
150+
void aad::AliveObject::OnRightAlignmentUpdate_()
151+
{
152+
if (parent_ != nullptr) {
153+
if (healthLabel_ != nullptr) {
154+
healthLabel_->setAnchorPoint(cocos2d::Vec2(parent_->IsRightAlignment() ? 1 : 0, 0));
155+
}
156+
if (armorLabel_ != nullptr) {
157+
armorLabel_->setAnchorPoint(cocos2d::Vec2(parent_->IsRightAlignment() ? 1 : 0, 0));
158+
}
159+
}
160+
}

HW-2/Classes/AliveObject.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef __ALIVE_OBJECT_INCLUDED__
2+
#define __ALIVE_OBJECT_INCLUDED__
3+
4+
#include "cocos2d.h"
5+
#include <string>
6+
#include "GameObject.h"
7+
8+
namespace aad {
9+
10+
class AliveObject abstract
11+
{
12+
public:
13+
AliveObject();
14+
AliveObject(GameObject const* parent, float maxHealth, float maxArmor);
15+
16+
virtual ~AliveObject() = default;
17+
18+
virtual void Damage(float power);
19+
20+
void SetParent(GameObject const* parent);
21+
void SetHealth(float health);
22+
void SetArmor(float armor);
23+
24+
GameObject const* GetParent();
25+
float GetHealth() const;
26+
float GetMaxHealth() const;
27+
float GetArmor() const;
28+
float GetMaxArmor() const;
29+
cocos2d::CCLabelTTF * GetHealthLabel() const;
30+
cocos2d::CCLabelTTF * GetArmorLabel() const;
31+
32+
protected:
33+
virtual void OnXUpdate_();
34+
virtual void OnYUpdate_();
35+
virtual void OnRightAlignmentUpdate_();
36+
37+
private:
38+
GameObject const* parent_;
39+
float health_;
40+
float maxHealth_;
41+
float armor_;
42+
float maxArmor_;
43+
cocos2d::CCLabelTTF * healthLabel_;
44+
cocos2d::CCLabelTTF * armorLabel_;
45+
46+
void LabelsInit_();
47+
};
48+
49+
}
50+
51+
#endif // __ALIVE_OBJECT_INCLUDED__

HW-2/Classes/AttackerObject.cpp

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33

44
aad::AttackerObject::AttackerObject()
55
: DynamicObject()
6-
, maxHealth_(0.f)
6+
, AliveObject()
77
, power_(0.f)
8-
{
9-
InitLabel_();
10-
SetHealth(maxHealth_);
11-
}
8+
{}
129

13-
aad::AttackerObject::AttackerObject(float speed, float maxHealth, float power, std::string const& fileName)
10+
aad::AttackerObject::AttackerObject(float maxHealth, float power, float speed, std::string const& fileName)
1411
: DynamicObject(MARGIN_SIZE, GROUND_HEIGHT, speed, 0.f, fileName)
15-
, maxHealth_(maxHealth)
12+
, AliveObject(this, maxHealth, 0.f)
1613
, power_(power)
17-
{
18-
InitLabel_();
19-
SetHealth(maxHealth_);
20-
}
14+
{}
2115

2216
void aad::AttackerObject::Update(Game* const scene)
2317
{
@@ -26,15 +20,15 @@ void aad::AttackerObject::Update(Game* const scene)
2620
for (std::shared_ptr<AttackerObject>& attacker : scene->getAttackers(!IsRightAlignment())) {
2721
float distance = scene->getContentSize().width - attacker->GetX() - attacker->GetW() - GetW() - GetX();
2822
if (distance < ATTACKER_DISTANCE) {
29-
SetX(GetX() - (ATTACKER_DISTANCE - distance));
23+
SetX_(GetX() - (ATTACKER_DISTANCE - distance));
3024
}
3125
}
3226

3327
auto& castle = scene->getCastle(!IsRightAlignment());
3428
if (castle) {
3529
float distance = scene->getContentSize().width - castle->GetX() - castle->GetW() - GetW() - GetX();
3630
if (distance < ATTACKER_DISTANCE) {
37-
SetX(GetX() - (ATTACKER_DISTANCE - distance));
31+
SetX_(GetX() - (ATTACKER_DISTANCE - distance));
3832
}
3933
}
4034
}
@@ -62,67 +56,30 @@ void aad::AttackerObject::Attack(Game* const scene)
6256
}
6357
}
6458

65-
void aad::AttackerObject::Damage(float power)
66-
{
67-
SetHealth(GetHealth() - power);
68-
if (GetHealth() < 0) {
69-
SetHealth(0);
70-
}
71-
}
72-
73-
void aad::AttackerObject::SetHealth(float health)
74-
{
75-
health_ = health;
76-
char buf[10];
77-
_itoa_s(ceil(health_), buf, 10);
78-
label_->setString(buf);
79-
}
80-
81-
float aad::AttackerObject::GetHealth() const
82-
{
83-
return health_;
84-
}
85-
86-
float aad::AttackerObject::GetMaxHealth() const
87-
{
88-
return maxHealth_;
89-
}
90-
9159
float aad::AttackerObject::GetPower() const
9260
{
9361
return power_;
9462
}
9563

96-
cocos2d::CCLabelTTF * aad::AttackerObject::GetLabel() const
97-
{
98-
return label_;
99-
}
100-
10164
size_t aad::AttackerObject::GetCost() const
10265
{
10366
return 0;
10467
}
10568

106-
void aad::AttackerObject::onPositionUpdate_()
69+
void aad::AttackerObject::OnXUpdate_()
10770
{
108-
GameObject::onPositionUpdate_();
109-
UpdateLabelPosition();
71+
DynamicObject::OnXUpdate_();
72+
AliveObject::OnXUpdate_();
11073
}
11174

112-
void aad::AttackerObject::InitLabel_()
75+
void aad::AttackerObject::OnYUpdate_()
11376
{
114-
label_ = cocos2d::CCLabelTTF::create("", "Helvetica", 30, cocos2d::Size(GetW(), 30));
115-
UpdateLabelPosition();
77+
DynamicObject::OnYUpdate_();
78+
AliveObject::OnYUpdate_();
11679
}
11780

118-
void aad::AttackerObject::UpdateLabelPosition()
81+
void aad::AttackerObject::OnRightAlignmentUpdate_()
11982
{
120-
if (isRightAlignment_) {
121-
label_->setPosition(frameWidth_ - GetX(), GROUND_HEIGHT + GetH() + MARGIN_SIZE);
122-
label_->setAnchorPoint(cocos2d::Vec2(1, 0));
123-
}
124-
else {
125-
label_->setPosition(GetX(), GROUND_HEIGHT + GetH() + MARGIN_SIZE);
126-
label_->setAnchorPoint(cocos2d::Vec2(0, 0));
127-
}
83+
DynamicObject::OnRightAlignmentUpdate_();
84+
AliveObject::OnRightAlignmentUpdate_();
12885
}

HW-2/Classes/AttackerObject.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
11
#ifndef __ATTACKER_OBJECT_INCLUDED__
22
#define __ATTACKER_OBJECT_INCLUDED__
33

4+
#include <string>
45
#include "DynamicObject.h"
6+
#include "AliveObject.h"
57

68
namespace aad {
79

810
class AttackerObject abstract
9-
: public DynamicObject
11+
: public DynamicObject, public AliveObject
1012
{
1113
public:
1214
AttackerObject();
13-
AttackerObject(float speed, float maxHealth, float power, std::string const& fileName);
15+
AttackerObject(float maxHealth, float power, float speed, std::string const& fileName);
1416

1517
virtual ~AttackerObject() = default;
1618

1719
virtual void Update(Game* const scene) override;
1820
void Attack(Game* const scene);
1921

20-
virtual void Damage(float power) override;
21-
22-
void SetHealth(float health);
23-
24-
float GetHealth() const;
25-
float GetMaxHealth() const;
2622
float GetPower() const;
27-
cocos2d::CCLabelTTF * GetLabel() const;
2823

2924
virtual size_t GetCost() const;
3025

31-
protected:
32-
float health_;
33-
float maxHealth_;
34-
float power_;
35-
cocos2d::CCLabelTTF * label_;
36-
37-
virtual void onPositionUpdate_() override;
26+
protected:
27+
virtual void OnXUpdate_() override;
28+
virtual void OnYUpdate_() override;
29+
virtual void OnRightAlignmentUpdate_() override;
3830

39-
void InitLabel_();
40-
void UpdateLabelPosition();
31+
private:
32+
float power_;
4133
};
4234

4335
}

HW-2/Classes/BlockObject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __BLOCK_OBJECT_INCLUDED__
22
#define __BLOCK_OBJECT_INCLUDED__
33

4+
#include <string>
45
#include "StaticObject.h"
56

67
namespace aad {

0 commit comments

Comments
 (0)