/
ogre_obj.hpp
352 lines (289 loc) · 8.44 KB
/
ogre_obj.hpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#ifndef OGRE_OBJ_HPP
#define OGRE_OBJ_HPP
#include <cstdlib>
#include <cfloat>
#include <iostream>
#include <sstream>
#include <list>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <math.h>
#include <unistd.h> // for sleep
#include <cmath> // for round
#include <assert.h>
#ifndef PLAYER_HPP
#include "player.hpp"
#endif
#ifndef OBJECT_SIZE
#define OBJECT_SIZE 10
#endif
// Points on the shortest path, maybe change to grid pt
class PathPt: public sf::Vector2i{
public:
// cost to this point
int G;
// estimated cost to end
int F;
PathPt ();
PathPt (int x, int y);
PathPt (sf::Vector2i);
PathPt (const PathPt& old);
inline int get_G()
{
return G;
}
inline void set_G(int newG)
{
G = newG;
}
inline int get_F()
{
return F;
}
inline void set_F(int newF)
{
F = newF;
}
int diag_dist(PathPt *target)
{
int del_x = abs(x - target->x);
int del_y = abs(y - target->y);
return 4*std::min(del_x, del_y) + 10*std::max(del_x, del_y);
}
sf::Vector2f get_as_position(sf::Vector2f ratio)
{
return sf::Vector2f(x * ratio.x, y * ratio.y);
}
// where did we come from?
PathPt *parent = nullptr;
};
class OgreObject{
public:
// This represents a generic object
// Maybe future just for radius collision in future
sf::CircleShape circ;
// what do I look like?
sf::Texture picture;
// Here's my 5x10 glossy
sf::Sprite sprite;
// Where we're headed
sf::Vector2f target_position;
sf::Vector2f final_position;
sf::RectangleShape target_flag;
// how am I getting there?
std::list<PathPt*> *path;
// What's my name (or the leader's name)
std::string name;
// Info about the unit
sf::Text info_str;
// Speed class, how many steps in a round
int speed;
// How tough can I be?
int max_hp;
// because I use it to move and I'm a horrible person
int unit_type;
// Who owns this unit
OgrePlayer *owner;
// How healthy am I?
sf::Texture health_bar;
// Here's my 5x10 glossy
sf::Sprite health_bar_sprite;
// health bar background and current hp
sf::RectangleShape health_bar_bg;
sf::RectangleShape health_bar_current;
// health bar scale
float scale;
// load font
sf::Font font;
// Am I currently selected for duty?
bool select_state;
// how far can I see?
sf::CircleShape vision_aura;
OgreObject(const sf::Vector2f& p);
// Who is my general?
inline OgrePlayer *get_owner()
{
return owner;
}
// Set my general, Maybe roll this into constructor
inline void set_owner(OgrePlayer *player)
{
owner = player;
circ.setFillColor(owner->get_color());
}
// Get the current position of this unit
// For now the position of the circle
inline sf::Vector2f get_position() const
{
return circ.getPosition();
}
inline sf::Vector2f get_target_position() const
{
return target_position;
}
// Get the direction to travel this step
// This will change when we have shortest path finding
inline sf::Vector2f get_direction() const
{
return target_position - get_position();
}
// Different sense, give me the offset to a given position
template <class T>
inline sf::Vector2f get_direction(T position) const
{
return position - get_position();
}
/*
// Set some information about the object
// have to remember to update this after a battle
inline void set_info(int str, sf::Font *font, int size)
{
info_str.setString(std::to_string(str));
info_str.setFont(*font);
info_str.setCharacterSize(size);
info_str.setColor(sf::Color::Black);
//Would like the position to be outside the unit, but this doesn't seem to match...could also do
//a health bar
//info_str.setPosition(circ.getPosition() + sf::Vector2f(2*circ.getRadius(),2*circ.getRadius()));
}
// Post battle update
inline void set_info(int str)
{
info_str.setString(std::to_string(str));
}
*/
// Shout my name!
inline std::string get_name()
{
return name;
}
// What's my name?
inline void set_name(std::string object_name)
{
name = object_name;
}
// How big am I?
inline float get_size() const
{
return circ.getRadius();
}
inline bool get_select_state()
{
return select_state;
}
// Draw a ring around the object to show it's selected
inline void set_select_state(bool select)
{
select_state = select;
if (select)
{
circ.setOutlineColor(sf::Color::Green);
circ.setOutlineThickness(OBJECT_SIZE/5);
}
else
{
circ.setOutlineColor(sf::Color::Black);
circ.setOutlineThickness(OBJECT_SIZE/10);
}
}
// Set the speed in objects/time step
void set_speed(int s)
{
speed = s;
}
// Move this object one step toward its target
void move_one(int move_tile_cost);
// Get our next position to move to
void next_target(sf::Vector2f ratio);
// Move this object its speed toward its target
void move_speed(sf::Vector2f ratio,std::vector<std::vector<int>> terrain, std::vector< std::vector<int> > move_cost);
inline void draw_on(sf::RenderWindow& window)
{
window.draw(circ);
window.draw(sprite);
info_str.setPosition(get_position());
window.draw(info_str);
}
// Super simple distance to a point
// stupid c++ templates in methods
template <class T>
float distance(T v)
{
sf::Vector2f temp;
temp = sf::Vector2f(v) - get_position();
return sqrt(temp.x * temp.x + temp.y * temp.y);
}
// Set where we want to end up, locally
// We set the target with integer coordinates, but we move with float
template <class T>
void set_target_position(T p)
{
// round to nearest ten
target_position = sf::Vector2f(round(p.x/10)*10, round(p.y/10)*10);
}
template <class T>
void set_target_tile(T p, T ratio,
std::vector<std::vector<int>> terrain,
std::vector< std::vector<int> > move_cost)
{
PathPt target = PathPt(round(p.x/ratio.x), round(p.y/ratio.y));
short_path(terrain, move_cost, &target, ratio);
// maybe I will allow this, but not right now
assert(!path->empty());
set_target_position(path->front()->get_as_position(ratio));
final_position = p;
}
// Magic move after combat or other necessary things
template <class T>
void move_by(T p)
{
circ.move(p);
sprite.move(p);
vision_aura.move(p);
}
// Just a dummy
inline int get_max_hp()
{
return max_hp;
}
inline sf::Color get_life_color(int str)
{
int red, green;
float life_fraction = str/(float)get_max_hp();
if (life_fraction > 0.5) {
red = int(2*(255-255*life_fraction));
green = 255;
}
else {
red = 255;
green = int(255*life_fraction*2);
}
return sf::Color(red,green,0);
}
// Set some information about the object
inline void set_info(int str, sf::Font *font, int size)
{
info_str.setString(std::to_string(str));
info_str.setFont(*font);
info_str.setCharacterSize(size);
info_str.setColor(sf::Color::Black);
health_bar_bg.setSize(sf::Vector2f(get_max_hp()/scale, 10));
health_bar_bg.setOutlineColor(sf::Color(125,125,125));
health_bar_bg.setOutlineThickness(1);
health_bar_current.setSize(sf::Vector2f(str/scale, 10));
health_bar_current.setFillColor(get_life_color(str));
}
// Post battle update
inline void set_info(int str)
{
info_str.setString(std::to_string(str));
health_bar_bg.setSize(sf::Vector2f(get_max_hp()/scale, 10));
health_bar_bg.setOutlineColor(sf::Color(125,125,125));
health_bar_bg.setOutlineThickness(1);
health_bar_current.setSize(sf::Vector2f(str/scale, 10));
health_bar_current.setFillColor(get_life_color(str));
}
// shortest path computation prototype
void short_path(std::vector<std::vector<int>> terrain, std::vector< std::vector<int> > move_cost, PathPt *target, sf::Vector2f ratio);
};
#endif