forked from Yao-class-cpp-studio/battle_game_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.cpp
127 lines (112 loc) · 3.41 KB
/
unit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "battle_game/core/unit.h"
#include "battle_game/core/game_core.h"
namespace battle_game {
namespace {
uint32_t life_bar_model_index = 0xffffffffu;
} // namespace
Unit::Unit(GameCore *game_core, uint32_t id, uint32_t player_id)
: Object(game_core, id) {
player_id_ = player_id;
lifebar_offset_ = {0.0f, 1.0f};
background_lifebar_color_ = {1.0f, 0.0f, 0.0f, 0.9f};
front_lifebar_color_ = {0.0f, 1.0f, 0.0f, 0.9f};
fadeout_lifebar_color_ = {1.0f, 1.0f, 1.0f, 0.5f};
fadeout_health_ = 1;
if (!~life_bar_model_index) {
auto mgr = AssetsManager::GetInstance();
life_bar_model_index = mgr->RegisterModel(
{{{-0.5f, 0.08f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}},
{{-0.5f, -0.08f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}},
{{0.5f, 0.08f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}},
{{0.5f, -0.08f}, {0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f}}},
{0, 1, 2, 1, 2, 3});
}
}
void Unit::SetPosition(glm::vec2 position) {
position_ = position;
}
void Unit::SetRotation(float rotation) {
rotation_ = rotation;
}
float Unit::GetSpeedScale() const {
return 1.0f;
}
float Unit::GetDamageScale() const {
return 1.0f;
}
float Unit::BasicMaxHealth() const {
return 100.0f;
}
float Unit::GetHealthScale() const {
return 1.0f;
}
void Unit::SetLifeBarLength(float new_length) {
lifebar_length_ = std::min(new_length, 0.0f);
}
void Unit::SetLifeBarOffset(glm::vec2 new_offset) {
lifebar_offset_ = new_offset;
}
void Unit::SetLifeBarFrontColor(glm::vec4 new_color) {
front_lifebar_color_ = new_color;
}
void Unit::SetLifeBarBackgroundColor(glm::vec4 new_color) {
background_lifebar_color_ = new_color;
}
void Unit::SetLifeBarFadeoutColor(glm::vec4 new_color) {
fadeout_lifebar_color_ = new_color;
}
float Unit::GetLifeBarLength() {
return lifebar_length_;
}
glm::vec2 Unit::GetLifeBarOffset() {
return lifebar_offset_;
}
glm::vec4 Unit::GetLifeBarFrontColor() {
return front_lifebar_color_;
}
glm::vec4 Unit::GetLifeBarBackgroundColor() {
return background_lifebar_color_;
}
glm::vec4 Unit::GetLifeBarFadeoutColor() {
return fadeout_lifebar_color_;
}
void Unit::ShowLifeBar() {
lifebar_display_ = true;
}
void Unit::HideLifeBar() {
lifebar_display_ = false;
}
void Unit::RenderLifeBar() {
if (lifebar_display_) {
auto parent_unit = game_core_->GetUnit(id_);
auto pos = parent_unit->GetPosition() + lifebar_offset_;
auto health = parent_unit->GetHealth();
SetTransformation(pos, 0.0f, {lifebar_length_, 1.0f});
SetColor(background_lifebar_color_);
SetTexture(0);
DrawModel(life_bar_model_index);
glm::vec2 shift = {(float)lifebar_length_ * (1 - health) / 2, 0.0f};
SetTransformation(pos - shift, 0.0f, {lifebar_length_ * health, 1.0f});
SetColor(front_lifebar_color_);
DrawModel(life_bar_model_index);
if (std::fabs(health - fadeout_health_) >= 0.01f) {
fadeout_health_ = health + (fadeout_health_ - health) * 0.93;
shift = {lifebar_length_ * (health + fadeout_health_ - 1) / 2, 0.0f};
SetTransformation(pos + shift, 0.0f,
{lifebar_length_ * (health - fadeout_health_), 1.0f});
SetColor(fadeout_lifebar_color_);
DrawModel(life_bar_model_index);
} else {
fadeout_health_ = health;
}
}
}
void Unit::RenderHelper() {
}
const char *Unit::UnitName() const {
return "Unknown Unit";
}
const char *Unit::Author() const {
return "Unknown Author";
}
} // namespace battle_game