forked from stefanhendriks/Dune-II---The-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.cpp
184 lines (138 loc) · 4.31 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include "Unit.hpp"
#include <Thor/Math.hpp>
#include <Thor/Shapes.hpp>
Unit::Unit(TexturePack pack, MessageSystem &messages, const sf::Vector2f& pos, int theId):
shouldMove(false),
sprite(*pack.unit),
shadowSprite(*pack.shadow),
selectedSprite(*pack.selected),
viewRange(10),
selected(false),
messages(messages),
id(theId)
{
setFacing(FACING_UP);
desiredBodyFacing = bodyFacing;
sprite.setPosition(pos);
shadowSprite.setPosition(pos);
shadowSprite.setColor(sf::Color(255, 255, 255, 128));
target = getCenter();
selectedSprite.setPosition(pos);
//send a fake move message to remove shroud on creation
messages.triggerEvent(MoveMessage(*this));
}
void Unit::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(shadowSprite, sf::BlendAlpha);
target.draw(sprite);
if (selected)
target.draw(selectedSprite);
if (hasTarget())
target.draw(thor::Arrow(getCenter(), this->target - getCenter(), sf::Color(51,255,51,125)), sf::BlendAdd);
}
void Unit::select() {
selected = true;
}
void Unit::unselect() {
selected = false;
}
bool Unit::isSelected() const {
return selected;
}
int Unit::getViewRange() const
{
return viewRange;
}
bool Unit::hasTarget() const {
return getCenter() != target;
}
bool Unit::shouldTurnBody() const {
return desiredBodyFacing != bodyFacing;
}
void Unit::orderMove(const sf::Vector2f& target) {
this->target = target;
desiredBodyFacing = desiredFacing();
}
void Unit::setFacing(int facing) {
bodyFacing = facing;
int size = sprite.getTexture()->getSize().x / FACINGS;
sprite.setTextureRect({size * bodyFacing, 0, size, size});
shadowSprite.setTextureRect({size * bodyFacing, 0, size, size});
}
int Unit::desiredFacing() const {
//if (position == next_move_position) return body_facing;
int nx = target.x;
int ny = target.y;
int x = getCenter().x;
int y = getCenter().y;
if (nx > x && (ny-y)<0.1) return FACING_RIGHT;
if (nx < x && (ny-y)<0.1) return FACING_LEFT;
if ((nx-x)<0.1 && ny > y) return FACING_DOWN;
if ((nx-x)<0.1 && ny < y) return FACING_UP;
if (nx > x && ny > y) return FACING_RIGHT_DOWN;
if (nx < x && ny > y) return FACING_DOWN_LEFT;
if (nx > x && ny < y) return FACING_UP_RIGHT;
if (nx < x && ny < y) return FACING_LEFT_UP;
return bodyFacing;
}
void Unit::turnBody() {
int body_facing = this->bodyFacing;
int desired = desiredFacing();
int turning_left = (body_facing + FACINGS) - desired;
if (turning_left > (FACINGS - 1)) turning_left -= FACINGS;
int turning_right = abs(turning_left - FACINGS);
if (turning_right < turning_left) {
body_facing++;
} else if (turning_left < turning_right) {
body_facing--;
} else {
thor::random(0, 10) < 5 ? body_facing-- : body_facing++;
}
// wrap around
if (body_facing < 0) body_facing += FACINGS;
if (body_facing > 7) body_facing -= FACINGS;
setFacing(body_facing);
}
void Unit::updateMovePosition(const std::vector<Unit>& units, sf::Time dt) {
if (hasTarget()) {
float speed = dt.asSeconds()*250.f;
sf::Vector2f direction = target - getCenter();
sf::Vector2f unitDirection = thor::unitVector(direction);
float distance = thor::length(direction);
if (distance < speed) speed = distance;
sprite.move(speed*unitDirection);
messages.triggerEvent(PreMoveMessage(*this));
if (shouldMove){
shouldMove = false;
//collision detection with units still here
for (const auto& unit : units){
if (id == unit.id) continue;
if (sprite.getGlobalBounds().intersects(unit.sprite.getGlobalBounds())){
sprite.move(-speed*unitDirection);
return;
}
}
shadowSprite.move(speed*unitDirection);
selectedSprite.move(speed*unitDirection);
messages.triggerEvent(MoveMessage(*this));
}else{
sprite.move(-speed*unitDirection);
}
}
}
sf::FloatRect Unit::getBounds() const {
return sprite.getGlobalBounds();
}
sf::Vector2f Unit::getPosition() const {
return sprite.getPosition();
}
sf::Vector2f Unit::getCenter() const {
sf::FloatRect spriteRect(sprite.getGlobalBounds());
return (sprite.getPosition() + sf::Vector2f(spriteRect.width/2, spriteRect.height/2));
}
void Unit::updateState(const std::vector<Unit> &units, sf::Time dt) {
updateMovePosition(units, dt);
if (shouldTurnBody())
turnBody();
}