forked from thoth-tech/build-a-game-team
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflymachine.h
203 lines (164 loc) · 3.84 KB
/
flymachine.h
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
#include "splashkit.h"
#include "player.h"
#include "machinehelper.h"
#include <memory>
#include <vector>
#include <random>
class FlyMachine;
class FlyMachineState
{
protected:
FlyMachine *fly;
string fly_state;
public:
virtual ~FlyMachineState()
{};
void set_state(FlyMachine *fly, string fly_state)
{
this->fly = fly;
this->fly_state = fly_state;
};
string get_type()
{
return this->fly_state;
};
virtual void update() = 0;
};
class FlyMachine
{
private:
FlyMachineState *state;
sprite enemy_sprite;
bool facing_left;
bool flying_up = true;
public:
FlyMachine(FlyMachineState *state, sprite enemy_sprite) : state(nullptr)
{
this->enemy_sprite = enemy_sprite;
sprite_start_animation(enemy_sprite, "LeftFly");
this->change_state(state, "Fly");
};
~FlyMachine()
{
delete state;
};
void change_state(FlyMachineState *new_state, string type)
{
if (this->state != nullptr)
delete this->state;
this->state = new_state;
this->state->set_state(this, type);
};
void update()
{
this->state->update();
};
bool get_facing_left()
{
return facing_left;
};
void set_facing_left(bool new_value)
{
this->facing_left = new_value;
};
sprite get_sprite()
{
return this->enemy_sprite;
};
string get_state_type()
{
return this->state->get_type();
};
void set_flying_up(bool new_value)
{
this->flying_up = new_value;
};
bool get_flying_up()
{
return this->flying_up;
};
};
class FlySide : public FlyMachineState
{
private:
bool run_once = false;
public:
FlySide(){};
~FlySide(){};
void update() override;
};
class FlyVertical : public FlyMachineState
{
private:
bool run_once = false;
public:
FlyVertical(){};
~FlyVertical(){};
void update() override;
};
class FlyRandom : public FlyMachineState
{
private:
bool run_once = false;
public:
FlyRandom(){};
~FlyRandom(){};
void update() override;
};
void FlySide::update()
{
sprite fly_sprite = this->fly->get_sprite();
if(this->fly->get_facing_left())
{
sprite_set_dx(fly_sprite, 4);
set_proper_direction(fly_sprite, "LeftFly");
}
else
{
sprite_set_dx(fly_sprite, -4);
set_proper_direction(fly_sprite, "RightFly");
}
}
void FlyVertical::update()
{
sprite fly_sprite = this->fly->get_sprite();
if(this->fly->get_flying_up())
{
sprite_set_dy(fly_sprite, -4);
}
else
{
sprite_set_dy(fly_sprite, 4);
}
if(this->fly->get_facing_left())
set_proper_direction(fly_sprite, "LeftFly");
else
set_proper_direction(fly_sprite, "RightFly");
}
void FlyRandom::update()
{
sprite fly_sprite = this->fly->get_sprite();
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<double> dist(1.0, 7.0);
double rand_vert = dist(mt);
double rand_side = dist(mt);
if(this->fly->get_flying_up())
{
sprite_set_dy(fly_sprite, -rand_vert);
}
else
{
sprite_set_dy(fly_sprite, rand_vert);
}
if(this->fly->get_facing_left())
{
sprite_set_dx(fly_sprite, rand_side);
set_proper_direction(fly_sprite, "LeftFly");
}
else
{
sprite_set_dx(fly_sprite, -rand_side);
set_proper_direction(fly_sprite, "RightFly");
}
}