forked from stefanhendriks/Dune-II---The-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit.hpp
109 lines (81 loc) · 2.39 KB
/
Unit.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
#ifndef UNIT_H
#define UNIT_H
#include <memory>
#include "Messages.hpp"
#include <SFML/System/Time.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <Thor/Vectors/VectorAlgebra2D.hpp>
// used to calculate width of each 'tile' for a unit given a tilset
const int FACING_RIGHT = 0;
const int FACING_UP_RIGHT = 1;
const int FACING_UP = 2;
const int FACING_LEFT_UP = 3;
const int FACING_LEFT = 4;
const int FACING_DOWN_LEFT = 5;
const int FACING_DOWN = 6;
const int FACING_RIGHT_DOWN = 7;
const int FACINGS = 8;
struct TexturePack{
const sf::Texture* unit;
const sf::Texture* shadow;
const sf::Texture* selected;
};
static_assert(std::is_pod<TexturePack>::value, "Texture pack is a pod");
class Unit;
struct MoveMessage : public Message{
MoveMessage(const Unit& unit):
Message(Messages::unitMove), unit(unit)
{}
const Unit& unit;
};
struct PreMoveMessage : public Message{
PreMoveMessage(const Unit& unit):
Message(Messages::premove), unit(unit)
{}
const Unit& unit;
};
class Unit : public sf::Drawable
{
public:
enum class Type {
Trike,
Quad,
Devastator,
Carryall,
Frigate,
Soldier
};
Unit(TexturePack pack, MessageSystem& messages, const sf::Vector2f& pos, int theId);
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
void updateState(const std::vector<Unit>& units, sf::Time dt);
void orderMove(const sf::Vector2f &target);
sf::FloatRect getBounds() const;
sf::Vector2f getPosition() const;
sf::Vector2f getCenter() const;
void select();
void unselect();
bool isSelected() const;
mutable bool shouldMove;
int getViewRange() const;
private:
sf::Vector2f target; // target of interest (move/attack, etc)
sf::Sprite sprite;
sf::Sprite shadowSprite;
sf::Sprite selectedSprite; //possibly static
int viewRange;
int bodyFacing;
int desiredBodyFacing;
bool selected;
MessageSystem& messages;
void setFacing(int facing);
void turnBody();
void updateMovePosition(const std::vector<Unit> &units, sf::Time dt);
int desiredFacing() const;
bool hasTarget() const;
bool shouldTurnBody() const;
int id; //unique id for the unit
};
#endif