Deep RTS
Unit.h
Go to the documentation of this file.
1//
2// Created by Per-Arne on 24.02.2017.
3//
4
5#pragma once
6
7#include <vector>
8#include <memory>
9#include <map>
10#include <set>
11#include <deque>
12#include "Constants.h"
13#include "Tile.h"
14#include "state/StateManager.h"
15#include "util/Position.h"
16#include "Config.h"
17#include "Constants.h"
18
19class Game;
20class Player;
21class BaseState;
22
23
24
25class Unit{
26private:
27 std::deque<std::pair<Constants::Action, int>> actionQueue;
28
29
30public:
32
33 const Config & config;
34
36 explicit Unit(Player &player);
37
38
44
46 int id;
47
49 std::string nameID;
50
53
55 int health;
56
59
62
63
65
67
68 int damageMin = -1;
69 int damageMax = -1;
70 int damageRange = -1;
72 int armor = -1;
73
74 float lumberCarry = 0;
75 float goldCarry = 0;
76 float stoneCarry = 0;
77 float carryCapacity = 10;
78 std::vector<Constants::Unit> buildInventory;
79 int speed;
80 int sight;
81
84 bool canMove;
87 bool recallable = false;
88
92
95
96 int width;
97 int height;
98
99 std::string name;
100
101
103
104
105 // State attributes
106
107 // Spawning
109 int spawnTileID = -1;
110 int builtByID = -1;
111
112
113
114
115
116 [[nodiscard]] Unit &getBuiltBy() const;
117 [[nodiscard]] Tile &getSpawnTile() const;
118
119
120 // Harvesting
121 double harvestInterval = .5; // Must be set in constructor
125 [[nodiscard]] Tile *getTile(int tileID) const;
126 [[maybe_unused]] [[nodiscard]] std::set<int> getVisionTileIDs() const;
127
128 // Building
129 int spawnTimer = 0;
131 [[nodiscard]] Unit &getBuildEntity() const;
132 int buildTimer = 0;
133
134 // Combat
136 [[nodiscard]] Unit *getCombatTarget() const;
137 double combatInterval = 1; // Must be set in constructor
138 double combatTimer = 0;
139
140
141 // Walking
143 std::vector<Tile *> walking_path;
145 int stepsLeft = 0; // If value is 0 it is ignored. However, Walking is transitioned when stepsLeft = 1 (Which decrements it to 0)
146 double walking_interval = 1; // Must be set in constructor
147
148 // Animation
150
151
152 Tile *tile = nullptr;
154 std::shared_ptr<BaseState> state = nullptr;
155
156 std::vector<Constants::State> stateList;
157
158
159 Tile *getNextTile();
160 bool build(int idx);
161 bool build(int idx, bool ignoreCost);
162 void spawn(Tile &x, int initValue);
163 void despawn();
164 void update();
165 [[nodiscard]] Tile * centerTile() const;
166 void enqueueState(const std::shared_ptr<BaseState>& state);
167 void transitionState();
168 void transitionState(std::shared_ptr<BaseState> nextState);
169 void setPosition(Tile &tile);
170 void rightClick(Tile &tile);
171 void move(Tile &targetTile);
172 void attack(Tile &tile);
173 void harvest(Tile &tile);
174 int distance(Tile &tile) const;
175 int distance(Unit & unit) const;
176 Position distanceVector(Tile &target) const;
177
178
179 void clearTiles();
180
181 [[nodiscard]] Unit *closestRecallBuilding() const;
182
183
184 bool isDead();
185
186 void afflictDamage(int dmg_);
187
188 int getDamage(Unit &target) const;
189
191
192 void setDirection(int newX, int newY);
193 void setDirection(Position &dir);
194
195 void moveRelative(int x, int y);
196
197 void rightClickRelative(int x, int y);
198
199 bool removedFromGame = false;
200
201 bool operator==(int id) const;
202
203 void tryMove(int16_t x, int16_t y);
204 void tryAttack();
205 void tryHarvest();
206
207 Player &getPlayer();
208
209
210 [[nodiscard]] bool position_in_bounds(int x, int y) const;
211
212
214 void setStateForTile(Tile * t);
215 void clearStateForTile(Tile *t);
216
217};
218
Definition: BaseState.h:14
Definition: Config.h:8
Definition: Game.h:23
Definition: Player.h:18
Definition: StateManager.h:15
Definition: Tile.h:21
Definition: Unit.h:25
std::string name
Definition: Unit.h:99
StateManager * stateManager
Definition: Unit.h:153
int foodConsumption
Definition: Unit.h:94
bool military
Definition: Unit.h:85
int combatTargetID
Definition: Unit.h:135
int damageMin
Definition: Unit.h:68
Unit & getBuildEntity() const
Definition: Unit.cpp:537
Tile * getNextTile()
Definition: Unit.cpp:388
float lumberCarry
Definition: Unit.h:74
int harvestTimer
Definition: Unit.h:122
bool build(int idx)
Definition: Unit.cpp:118
int height
Definition: Unit.h:97
int distance(Tile &tile) const
Definition: Unit.cpp:317
bool canAttack
Definition: Unit.h:83
bool canMove
Definition: Unit.h:84
int spawnTimer
Definition: Unit.h:129
Tile * tile
Definition: Unit.h:152
int stoneCost
Definition: Unit.h:91
Unit * getCombatTarget() const
Definition: Unit.cpp:542
Position worldPosition
Definition: Unit.h:190
int buildEntityID
Definition: Unit.h:130
bool structure
Definition: Unit.h:86
int animationCounter
Definition: Unit.h:149
Tile * centerTile() const
Definition: Unit.cpp:106
Game * game
Definition: Unit.h:31
int damageRange
Definition: Unit.h:70
Player & player_
Definition: Unit.h:102
int health_max
Unit Max Health.
Definition: Unit.h:58
double walking_interval
Definition: Unit.h:146
int harvestTargetID
Definition: Unit.h:124
int armor
Definition: Unit.h:72
void attack(Tile &tile)
Definition: Unit.cpp:250
void rightClick(Tile &tile)
Definition: Unit.cpp:224
void transitionState()
Definition: Unit.cpp:283
bool isDead()
Definition: Unit.cpp:369
double harvestInterval
Definition: Unit.h:121
int builtByID
Definition: Unit.h:110
void setPosition(Tile &tile)
Definition: Unit.cpp:75
float goldCarry
Definition: Unit.h:75
void rightClickRelative(int x, int y)
Definition: Unit.cpp:49
void enqueueState(const std::shared_ptr< BaseState > &state)
Definition: Unit.cpp:279
int stepsLeft
Definition: Unit.h:145
Constants::Unit typeId
Unit Type.
Definition: Unit.h:52
void update()
Definition: Unit.cpp:102
bool position_in_bounds(int x, int y) const
Definition: Unit.cpp:573
int id
Unit ID.
Definition: Unit.h:46
Position distanceVector(Tile &target) const
Definition: Unit.cpp:337
int spawnTileID
Definition: Unit.h:109
int goldCost
Definition: Unit.h:90
const Config & config
Definition: Unit.h:33
double combatTimer
Definition: Unit.h:138
void despawn()
Definition: Unit.cpp:203
float stoneCarry
Definition: Unit.h:76
int walkingGoalID
Definition: Unit.h:144
int waterUnit
Definition: Unit.h:66
int getDamage(Unit &target) const
Definition: Unit.cpp:373
int groundUnit
Definition: Unit.h:64
void harvest(Tile &tile)
Definition: Unit.cpp:270
void setDirection(int newX, int newY)
Definition: Unit.cpp:395
bool recallable
Definition: Unit.h:87
Unit & getBuiltBy() const
Definition: Unit.cpp:532
int width
Definition: Unit.h:96
void moveRelative(int x, int y)
Definition: Unit.cpp:38
Unit(Player &player)
Constructor.
Definition: Unit.cpp:13
int walking_timer
Definition: Unit.h:142
Unit * closestRecallBuilding() const
Definition: Unit.cpp:344
void clearStateForTile(Tile *t)
Definition: Unit.cpp:582
int health
Unit Current Health.
Definition: Unit.h:55
void move(Tile &targetTile)
Definition: Unit.cpp:59
double combatInterval
Definition: Unit.h:137
std::vector< Tile * > walking_path
Definition: Unit.h:143
std::vector< Constants::State > stateList
Definition: Unit.h:156
std::set< int > getVisionTileIDs() const
Definition: Unit.cpp:550
int lumberCost
Definition: Unit.h:89
int damageMax
Definition: Unit.h:69
void tryAttack()
Definition: Unit.cpp:447
int spawnDuration
Definition: Unit.h:108
Tile * getTile(int tileID) const
Definition: Unit.cpp:525
int speed
Definition: Unit.h:79
void tryMove(int16_t x, int16_t y)
Definition: Unit.cpp:465
std::shared_ptr< BaseState > state
Definition: Unit.h:154
void clearTiles()
Definition: Unit.cpp:216
Tile & getSpawnTile() const
Definition: Unit.cpp:520
std::string nameID
Unit Name ID (i.e Peasant1)
Definition: Unit.h:49
void afflictDamage(int dmg_)
Definition: Unit.cpp:359
int foodProduction
Definition: Unit.h:93
int sight
Definition: Unit.h:80
bool removedFromGame
Definition: Unit.h:199
int buildTimer
Definition: Unit.h:132
void tryHarvest()
Definition: Unit.cpp:502
int damagePiercing
Definition: Unit.h:71
Player & getPlayer()
Definition: Unit.cpp:578
std::vector< Constants::Unit > buildInventory
Definition: Unit.h:78
int harvestIterator
Definition: Unit.h:123
void spawn(Tile &x, int initValue)
Definition: Unit.cpp:29
int direction
Unit Direction.
Definition: Unit.h:61
float carryCapacity
Definition: Unit.h:77
bool operator==(int id) const
Definition: Unit.cpp:442
bool canHarvest
Definition: Unit.h:82
void setStateForTile(Tile *t)
State alterations.
Definition: Unit.cpp:593
Unit
Definition: Constants.h:20
Definition: Position.h:8