This repository was archived by the owner on Jun 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentities.h
More file actions
257 lines (180 loc) · 5.37 KB
/
Copy pathentities.h
File metadata and controls
257 lines (180 loc) · 5.37 KB
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
/////////////////////////////////////////////////////////////////////////////
// Name: entities.h
// Description: ...
// Author: Alexey Orlov (https://github.com/m110h)
// Last modification: 18/08/2020
// Licence: MIT licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _ENTITIES_H_
#define _ENTITIES_H_
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <vector>
#include <tuple>
#include "gene.h"
#include "properties.h"
#include "constants.h"
#include "random.h"
#include "config.h"
namespace ProtoPuddle
{
class Entity;
class World
{
public:
World(wxFrame* _parentFrame, GlobalProperties* _properties);
~World();
void Step();
void Draw(wxDC* dc);
void SetProperties(GlobalProperties* _properties);
GlobalProperties* GetProperties();
Entity* GetEntityByPosition(const wxPoint& worldPosition);
Entity* GetEntityById(int id);
int GetEntityIdByPosition(const wxPoint& worldPosition);
void SelectEntityByPosition(const wxPoint& worldPosition);
Entity* GetSelectedEntity();
// returns plants, meat, cells
std::tuple<int, int, int> GetEntitiesQuantity();
// returns total, used, peak
std::tuple<std::size_t, std::size_t, std::size_t> GetMemoryInfo();
bool IsInside(const wxPoint& worldPosition);
int GetNextId();
int GetTopId();
int GetSteps();
void New();
void SetPanelSize(const wxSize& size);
wxPoint WorldToPanel(const wxPoint& position);
wxPoint PanelToWorld(const wxPoint& position);
wxSize GetFieldSize(const wxRect& board);
wxRect GetBoardBoundingBox();
void AddEntity(Entity* e);
bool MoveEntity(Entity* e, const wxPoint& newPosition);
std::tuple<bool, wxString> OpenFromFile(const wxString& filename);
bool SaveToFile(const wxString& filename);
bool LeaseEmptyPoint(const wxPoint& point);
wxPoint LeaseRandomEmptyPoint();
void ReleasePoint(const wxPoint& point);
private:
void StepEntities();
void DrawBoard(wxDC* dc);
void DrawEntities(wxDC* dc);
void GenerateEmptyPoints();
void GenerateEntities(int type, int quantity);
void DeathHandle();
void ClearEntitiesTable();
void ClearEmptyPoints();
private:
std::vector<wxPoint> emptyPoints;
int nextId {0};
int selectedId {-1};
int steps {0};
int plantsCounter {0};
int meatCounter {0};
int cellsCounter {0};
int paddingX {0};
int paddingY {0};
GlobalProperties* properties {nullptr};
wxSize worldSize {wxSize(0,0)};
wxSize panelSize {wxSize(0,0)};
wxFrame* parentFrame {nullptr};
};
class Entity
{
public:
Entity(World* _world);
virtual ~Entity();
virtual void Step();
virtual void Draw(wxDC* dc) = 0;
virtual void DrawSelected(wxDC* dc) = 0;
bool IsDead();
void Die();
int GetId();
void SetColor(const wxColor& _color);
const wxColor& GetColor() const;
void SetPosition(const wxPoint& _position);
const wxPoint& GetPosition() const;
int GetType();
void SetEnergy(int _energy);
int GetEnergy();
virtual wxString Get(const wxString& name);
// types for entities
enum
{
TYPE_PLANT = 1,
TYPE_MEAT,
TYPE_CELL,
TYPE_WALL
};
protected:
void SetSelectedBrushAndPen(wxDC* dc);
void SetNormalBrushAndPen(wxDC* dc);
void DrawCircle(wxDC* dc);
void DrawRectangle(wxDC* dc);
protected:
int type {0};
int id {-1};
int age {0};
int lifeTime {0};
int energy {1};
World* world {nullptr};
wxColor color {wxColor(0,0,0)};
wxPoint position {wxPoint(-1,-1)};
};
class Plant: public Entity
{
public:
Plant(World* _world);
virtual ~Plant();
void Draw(wxDC* dc) final;
void DrawSelected(wxDC* dc) final;
};
class Meat: public Entity
{
public:
Meat(World* _world);
virtual ~Meat();
void Draw(wxDC* dc) final;
void DrawSelected(wxDC* dc) final;
};
class Cell: public Entity
{
public:
Cell(World* _world, const wxString& geneName);
Cell(World* _world, int _divEnergy, int _damage, int _mutationProbability, const wxColor& _color, const Gene& _gene);
virtual ~Cell();
void Step() final;
void Draw(wxDC* dc) final;
void DrawSelected(wxDC* dc) final;
bool Attack(Cell* victim);
void SetAttacked(bool _attacked);
const Gene& GetGene();
void SetGene(const Gene& _gene);
wxString Get(const wxString& name) override;
private:
void Clone();
void Execute(int cmd);
Gene GenerateGene(const wxString& name);
Gene MutateGene(const Gene& _gene);
wxColor GenerateColor();
wxPoint GenerateDirection();
bool CanDivide();
void DrawDirection(wxDC* dc);
int NormalizeCoord(int x);
void SetLastBehavior(const wxString& behavior);
private:
wxPoint direction {wxPoint(1,0)};
Gene gen1;
int divEnergy {0};
int damage {0};
int childrenCounter {0};
int killsCounter {0};
int mutationProbability {0};
int eatenPlantsCounter {0};
int eatenMeatCounter {0};
bool attacked {false};
wxString lastBehavior {""};
};
}
#endif