-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameUnit.cpp
More file actions
412 lines (361 loc) · 13.1 KB
/
Copy pathGameUnit.cpp
File metadata and controls
412 lines (361 loc) · 13.1 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "GameUnit.hpp"
#include "GlobalUnit.hpp"
#include "MsxMath.hpp"
#include "SoundUnit.hpp"
#include <algorithm>
#include <cmath>
TBase::TBase(TBaseList* aList, TRealTimeModel* aModel)
: position({0,0,0}), angle(0), speed(0), size(1.0f), modelsize(1.0f), life(4), dead(false), pushable(true), list(aList) {
anim = std::make_unique<TAnimationHandler>(aModel);
// Randomize starting animation offset to desync units
anim->Update((float)(rand() % 100) / 100.0f);
anim->Start(ANIM_STAND, 3.0f);
}
void TBase::Update(TFloat delta) {
TVector displacement = {0, speed * delta, 0};
// Rotate 2D-style (on floor)
TFloat s = std::sin(angle);
TFloat c = std::cos(angle);
TVector rotated = { displacement.x * c + displacement.y * s, -displacement.x * s + displacement.y * c, 0 };
position.x += rotated.x;
position.y += rotated.y;
}
void TBase::DoAttack(TFloat delta) {
for (auto& obj : list->objects) {
if (obj.get() != this && IsValidTarget(obj.get())) {
obj->DoHit();
}
}
}
bool TBase::IsValidTarget(TBase* a) {
// Ignore targets that are already dead or in the process of dying (life <= 0)
if (a->dead || a->life <= 0) return false;
TFloat dx = position.x - a->position.x;
TFloat dy = position.y - a->position.y;
TFloat dist = std::sqrt(dx * dx + dy * dy);
if (dist < 5.0f) {
// Calculate "front" target point based on size and angle
TVector targetOffset = {0, size + 1.0f, 0};
TFloat s = std::sin(angle);
TFloat c = std::cos(angle);
TVector targetPos = {
position.x + (targetOffset.x * c + targetOffset.y * s),
position.y + (-targetOffset.x * s + targetOffset.y * c),
0
};
TFloat tdx = a->position.x - targetPos.x;
TFloat tdy = a->position.y - targetPos.y;
TFloat tdist = std::sqrt(tdx * tdx + tdy * tdy);
return tdist < a->size;
}
return false;
}
void TViking::Update(TFloat delta) {
if (dead) return;
if (global.keystate->left) angle += 4.0f * delta;
if (global.keystate->right) angle -= 4.0f * delta;
// Only allow state changes if the player isn't busy with a non-interruptible animation
int curAnim = anim->GetCurrentAnim();
if (curAnim != ANIM_HIT && curAnim != ANIM_DIE && curAnim != ANIM_ATTACK && curAnim != ANIM_ATTACKB) {
if (global.keystate->ctrl || global.keystate->space) {
speed = 0;
if (curAnim != ANIM_ATTACK && curAnim != ANIM_ATTACKB) {
if (rand() % 2 == 1) anim->Start(ANIM_ATTACK, 0.6f);
else anim->Start(ANIM_ATTACKB, 0.6f);
}
} else if (global.keystate->up) {
speed = 6.5f;
if (curAnim != ANIM_WALK) anim->Start(ANIM_WALK, 1.0f);
} else if (global.keystate->down) {
speed = -6.5f;
if (curAnim != ANIM_WALK) anim->Start(ANIM_WALK, 1.0f);
} else {
speed = 0;
if (curAnim != ANIM_STAND) anim->Start(ANIM_STAND, 3.0f);
}
}
// Handle attack triggers
if (anim->GetCurrentAnim() == ANIM_ATTACK || anim->GetCurrentAnim() == ANIM_ATTACKB) {
if (anim->Trigger()) {
global.man->getSoundByName("swhoosh0")->play();
DoAttack(delta);
}
}
if (anim->Update(delta)) {
int finishedAnim = anim->GetCurrentAnim();
if (finishedAnim == ANIM_HIT && life <= 0) {
anim->Start(ANIM_DIE, 1.0f);
} else if (finishedAnim == ANIM_DIE) {
dead = true;
speed = 0;
return; // Exit early to ensure no movement or further state changes occur
} else {
anim->Start(ANIM_STAND, 3.0f);
}
}
if (!dead) TBase::Update(delta);
}
void TViking::DoHit() {
if (dead || life <= 0) return;
if (anim->GetCurrentAnim() != ANIM_HIT && anim->GetCurrentAnim() != ANIM_DIE) {
speed = 0;
anim->Start(ANIM_HIT, 0.3f);
life--;
global.man->getSoundByName("hit0")->play();
if (life == 0) global.man->getSoundByName("scream")->play();
}
}
void TSheep::Update(TFloat delta) {
if (anim->GetCurrentAnim() == ANIM_WALK) angle -= 0.2f * delta;
if (anim->Update(delta)) {
if (anim->GetCurrentAnim() == ANIM_HIT) {
if (life <= 0) anim->Start(ANIM_DIE, 0.3f);
else ChooseActivity();
}
else if (anim->GetCurrentAnim() == ANIM_DIE) {
dead = true;
list->score += 5;
} else {
ChooseActivity();
}
}
TBase::Update(delta);
}
void TSheep::ChooseActivity() {
if (rand() % 10 == 9) {
anim->Start(ANIM_STAND, 2.0f);
speed = 0;
} else {
anim->Start(ANIM_WALK, 1.0f);
speed = 2.0f;
}
}
void TSheep::DoHit() {
if (anim->GetCurrentAnim() != ANIM_HIT && anim->GetCurrentAnim() != ANIM_DIE) {
speed = 0;
anim->Start(ANIM_HIT, 0.3f);
life--;
global.man->getSoundByName("hit0")->play();
if (life == 0) global.man->getSoundByName("sheep")->play();
}
}
void TWolf::Update(TFloat delta) {
int curAnim = anim->GetCurrentAnim();
// Aggression check: If idling, check if player is close enough to wake up
TFloat wolfDistFromHome = Distance(position, patrol);
TFloat playerDistFromHome = Distance(list->player->position, patrol);
// Predator behavior: Wake up if player enters territory
if (curAnim == ANIM_STAND && playerDistFromHome <= patrolarea) {
anim->Start(ANIM_WALK, 1.0f);
speed = 2.0f;
curAnim = ANIM_WALK;
}
if (curAnim == ANIM_WALK) {
TVector target;
// Chase if player is in territory, otherwise return to patrol
if (wolfDistFromHome <= patrolarea) {
target = list->player->position;
} else {
target = patrol;
}
// Ensure movement speed is set to walk speed
if (speed == 0) speed = 2.0f;
// Calculate target angle and turn towards it
TFloat targetAngle = atan2(target.x - position.x, target.y - position.y);
TFloat aDist = targetAngle - angle;
while (aDist > M_PI) aDist -= 2 * M_PI;
while (aDist < -M_PI) aDist += 2 * M_PI;
TFloat d = (aDist > 0 ? 1 : -1) * delta;
if (std::abs(d) > std::abs(aDist)) d = aDist;
angle += d;
if (IsValidTarget(list->player)) {
anim->Start(ANIM_ATTACK, 1.0f);
speed = 0;
}
}
if (anim->GetCurrentAnim() == ANIM_ATTACK) {
if (anim->Trigger()) {
global.man->getSoundByName("rambite")->play();
DoAttack(delta);
}
}
if (anim->Update(delta)) {
if (anim->GetCurrentAnim() == ANIM_HIT) {
if (life <= 0) anim->Start(ANIM_DIE, 0.3f);
else ChooseActivity();
} else if (anim->GetCurrentAnim() == ANIM_DIE) {
dead = true;
list->score += 15;
} else {
ChooseActivity();
}
}
TBase::Update(delta);
}
void TWolf::DoHit() {
if (anim->GetCurrentAnim() != ANIM_HIT && anim->GetCurrentAnim() != ANIM_DIE) {
speed = 0;
anim->Start(ANIM_HIT, 0.3f);
life--;
global.man->getSoundByName("hit0")->play();
if (life == 0) global.man->getSoundByName("ramdie")->play();
}
}
void TWolf::ChooseActivity() {
if (rand() % 10 == 9) {
anim->Start(ANIM_STAND, 2.0f);
speed = 0;
} else {
anim->Start(ANIM_WALK, 1.0f);
speed = 2.0f;
}
}
TBaseList::TBaseList() : level(0), score(0), numSheep(0) {
animViking = new TRealTimeModel();
animViking->load("media/viking_weapon.model", global.man);
animSheep = new TRealTimeModel();
animSheep->load("media/sheep.model", global.man);
animTree = new TRealTimeModel();
animTree->load("media/tree1.model", global.man);
animWolf = new TRealTimeModel();
animWolf->load("media/ram.model", global.man);
animFence = new TRealTimeModel();
animFence->load("media/fence.model", global.man);
player = nullptr;
}
TBaseList::~TBaseList() {
delete animViking;
delete animSheep;
delete animTree;
delete animWolf;
delete animFence;
}
void TBaseList::Restart() {
player = nullptr; // Reset pointer first so NextLevel defaults to starting health
level = 0;
score = 0;
numSheep = 0;
NextLevel(); // NextLevel calls objects.clear() internally
}
void TBaseList::NextLevel() {
int lastLevelLife = 5;
if (player != nullptr) {
lastLevelLife = player->life;
}
objects.clear();
numSheep = 0;
level++;
auto vPlayer = std::make_unique<TViking>(this, animViking);
vPlayer->position.x = rand() % (TILESIZE * AREASIZE);
vPlayer->position.y = rand() % (TILESIZE * AREASIZE);
vPlayer->size = 1.1f;
vPlayer->modelsize = 2.0f;
vPlayer->life = lastLevelLife;
player = vPlayer.get();
Add(std::move(vPlayer));
for (int i = 0; i < 4; ++i) {
auto tree = std::make_unique<TTree>(this, animTree);
tree->position.x = rand() % (TILESIZE * AREASIZE);
tree->position.y = rand() % (TILESIZE * AREASIZE);
tree->size = 1.1f;
tree->modelsize = 4.0f;
tree->pushable = false;
tree->speed = 0;
Add(std::move(tree));
}
for (int i = 0; i < level * 2 + 2; ++i) {
auto sheep = std::make_unique<TSheep>(this, animSheep);
sheep->position.x = rand() % (TILESIZE * AREASIZE);
sheep->position.y = rand() % (TILESIZE * AREASIZE);
sheep->size = 1.4f;
sheep->modelsize = 1.8f;
sheep->angle = (rand() % 360) / 180.0f * M_PI;
Add(std::move(sheep));
}
for (int i = 0; i <= level; ++i) {
auto wolf = std::make_unique<TWolf>(this, animWolf);
wolf->position.x = rand() % (TILESIZE * AREASIZE);
wolf->position.y = rand() % (TILESIZE * AREASIZE);
wolf->patrol.x = rand() % (TILESIZE * AREASIZE);
wolf->patrol.y = rand() % (TILESIZE * AREASIZE);
wolf->patrol.z = 0.0f; // Initialize Z to ensure distance checks are 2D/reliable
wolf->patrolarea = 20.0f;
wolf->size = 1.4f;
wolf->modelsize = 1.8f;
wolf->pushable = true;
wolf->life = 8;
wolf->speed = 0;
wolf->angle = (rand() % 360) / 180.0f * M_PI;
Add(std::move(wolf));
}
if (objects.size() >= 2) {
TWolf* w1 = dynamic_cast<TWolf*>(objects[objects.size() - 1].get());
if (w1) w1->patrolarea = 1000.0f;
TWolf* w2 = dynamic_cast<TWolf*>(objects[objects.size() - 2].get());
if (w2) w2->patrolarea = 1000.0f;
}
}
void TBaseList::Add(std::unique_ptr<TBase> item) {
if (dynamic_cast<TSheep*>(item.get())) numSheep++;
objects.push_back(std::move(item));
}
void TBaseList::Update(TFloat delta) {
for (auto& obj : objects) {
obj->Update(delta);
// Boundary checks from Pascal: avoid exit from the play area
TFloat bounds = TILESIZE * AREASIZE;
if (obj->position.x < obj->size) obj->position.x = obj->size;
if (obj->position.x > bounds - obj->size) obj->position.x = bounds - obj->size;
if (obj->position.y < obj->size) obj->position.y = obj->size;
if (obj->position.y > bounds - obj->size) obj->position.y = bounds - obj->size;
}
// Simple O(N^2) collision
for (size_t i = 0; i < objects.size(); ++i) {
for (size_t j = i + 1; j < objects.size(); ++j) {
TBase* a = objects[i].get();
TBase* b = objects[j].get();
// Use 2D Distance (X, Y) to match Pascal vector2d logic
TFloat dx = a->position.x - b->position.x;
TFloat dy = a->position.y - b->position.y;
TFloat dist = std::sqrt(dx * dx + dy * dy);
TFloat minDist = a->size + b->size;
if (dist < minDist) {
AdjustCollision(a, b, minDist - dist);
}
}
}
// Cleanup dead
objects.erase(std::remove_if(objects.begin(), objects.end(),
[this](const std::unique_ptr<TBase>& b) {
if (b->dead && b.get() != this->player) {
if (dynamic_cast<TSheep*>(b.get())) numSheep--;
return true;
}
return false;
}), objects.end());
}
void TBaseList::AdjustCollision(TBase* a, TBase* b, TFloat comp) {
// Direction vector on ground (X, Y in logic)
float dx = a->position.x - b->position.x;
float dy = a->position.y - b->position.y;
float mag = std::sqrt(dx * dx + dy * dy);
if (mag < 0.0001f) { dx = 1.0f; dy = 0.0f; mag = 1.0f; }
dx /= mag; dy /= mag;
if (a->pushable == b->pushable) {
// Both pushable or both static: split the repulsion
float move = (comp / 2.0f);
a->position.x += dx * move;
a->position.y += dy * move;
b->position.x -= dx * move;
b->position.y -= dy * move;
} else {
// Only one is pushable: move only the pushable one by the full amount
if (a->pushable) {
a->position.x += dx * comp;
a->position.y += dy * comp;
} else {
b->position.x -= dx * comp;
b->position.y -= dy * comp;
}
}
}