This repository was archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCEnemy.cpp
More file actions
198 lines (172 loc) · 6.31 KB
/
CEnemy.cpp
File metadata and controls
198 lines (172 loc) · 6.31 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
//
// CEnemy.cpp
// PacMan
//
// Created by Vsevolod Klementjev on 11/19/12.
// Copyright (c) 2012 Vsevolod Klementjev. All rights reserved.
//
#include "CEnemy.h"
#include <math.h>
inline int min(int a, int b)
{
return a < b ? a : b; // returns minimal number from the given
}
CEnemy::CEnemy(CMap* map) : CEntity(map)
{
// loading frightened ghost
_frightenedSprite[0] = new CSprite("fright1.png");
_frightenedSprite[1] = new CSprite("fright2.png");
// loading eyes
char directions[4] = { 'u', 'd', 'l', 'r' };
char filename[50];
for (int i = 0; i < 4; i++) {
sprintf(filename, "eyes_%c.png", directions[i]);
_eyesSprite[i] = new CSprite(filename);
}
}
bool CEnemy::ableToMove(int xAdd, int yAdd)
{
// inside the house
bool gate = _map->getObjectAt(CMap::getTileXcoordinate(_pixelX + TILE_SIZE * 0.5f),
CMap::getTileYcoordinate(_pixelY + TILE_SIZE * 0.5f)) & MapObjectGate;
bool house = (xAdd != 0) && _map->getObjectAt(CMap::getTileXcoordinate(_pixelX + TILE_SIZE * 0.5f) + xAdd,
CMap::getTileYcoordinate(_pixelY + TILE_SIZE * 0.5f)) & MapObjectGate;
// road and checking gate and house
bool able = CEntity::ableToMove(xAdd, yAdd) || (gate && yAdd < 0) || (house && !gate);
if (_state == EnemyStateFrighten) {
return able;
}
else if (_state == EnemyStateEyes) {
// a little bit different house situation when returning as eyes
house = _map->getObjectAt(CMap::getTileXcoordinate(_pixelX + TILE_SIZE * 0.5f) + xAdd,
CMap::getTileYcoordinate(_pixelY + TILE_SIZE * 0.5f) + yAdd) & MapObjectGate;
return CEntity::ableToMove(xAdd, yAdd) || (gate && yAdd > 0) || house;
}
else {
// there are 4 tiles where ghost can't go upwards
bool restriction = yAdd < 0 && _map->getObjectAt(CMap::getTileXcoordinate(_pixelX + TILE_SIZE * 0.5f),
CMap::getTileYcoordinate(_pixelY + TILE_SIZE * 0.5f)) & MapObjectRestriction;
return able && !restriction;
}
}
int CEnemy::distance(int targetx, int targety, int x, int y)
{
// plain distance, non euclidean
return (int)sqrtf(powf(targetx - x, 2) + powf(targety - y, 2));
}
Direction CEnemy::chooseDirection(int targetx, int targety)
{
int currentTileX = getXposition();
int currentTileY = getYposition();
// gathering distances, adding bonus distance for invert directions so they can't turn back, adding bonus if we can't move there aswell
int upWay = distance(targetx, targety, currentTileX, currentTileY - 1) + (_direction == DirectionDown ? 1000 : 0) + (ableToMove(0, -1) ? 0 : 1000);
int leftWay = distance(targetx, targety, currentTileX - 1, currentTileY) + (_direction == DirectionRight ? 1000 : 0) + (ableToMove(-1, 0) ? 0 : 1000);
int downWay = distance(targetx, targety, currentTileX, currentTileY + 1) + (_direction == DirectionUp ? 1000 : 0) + (ableToMove(0, 1) ? 0 : 1000);
int rightWay = distance(targetx, targety, currentTileX + 1, currentTileY) + (_direction == DirectionLeft ? 1000 : 0) + (ableToMove(1, 0) ? 0 : 1000);
// finding best distance, ie shortest one, ie without added bonus distances aswell
int bestDistance = min(upWay, min(leftWay, min(downWay, rightWay)));
if (upWay == bestDistance) {
return DirectionUp;
}
else if (leftWay == bestDistance) {
return DirectionLeft;
}
else if (downWay == bestDistance) {
return DirectionDown;
}
return DirectionRight;
}
void CEnemy::Update()
{
CEntity::Update(); // animation and movement stuff
// handling new movement in different states
if (!_moving && _state == EnemyStateScatter)
{
Move(chooseDirection(_scatterX, _scatterY)); // try to move to our scatter corner
}
else if (!_moving && _state == EnemyStateChase)
{
Move(chooseDirection(_chaseX, _chaseY)); // try to move to our chase position (different for every ghost)
}
else if (!_moving && _state == EnemyStateFrighten)
{
while (true) { // just move in random direction
int direction = rand() % 4;
if (!isReverse((Direction)direction)) {
Move((Direction)direction);
if (_moving) {
break;
}
}
}
}
else if (!_moving && _state == EnemyStateEyes)
{
if (getXposition() == _startX && getYposition() == _startY) {
Reset(); // reset our states when home reached
_state = EnemyStateChase;
Update(); // start chase over
}
else {
Move(chooseDirection(_startX, _startY)); // go home
}
}
}
void CEnemy::RetreatHome()
{
if (_state != EnemyStateEyes) {
changeState(EnemyStateEyes); // we got eaten
Move(chooseDirection(_startX, _startY));
}
}
void CEnemy::Draw(SDL_Surface *dest)
{
if (_state == EnemyStateFrighten) {
// draw frightened ghost instead
int frame = _animator->getCurrentFrame();
_frightenedSprite[frame]->setPosition(_pixelX, _pixelY);
_frightenedSprite[frame]->Draw(dest);
}
else if (_state == EnemyStateEyes) {
// draw eyes instead, they have no animation
_eyesSprite[_direction]->setPosition(_pixelX, _pixelY);
_eyesSprite[_direction]->Draw(dest);
}
else {
CEntity::Draw(dest);
}
}
EnemyState CEnemy::currentState()
{
return _state;
}
void CEnemy::changeState(EnemyState newState)
{
// reversing direction if we were not frightened or inactive
if (_state != EnemyStateFrighten && _state != EnemyStateNone) {
reverseDirection();
}
_state = newState;
}
void CEnemy::Reset()
{
// reseting all positions to default
_state = EnemyStateNone;
_direction = DirectionUp;
_moving = false;
_chaseX = 0;
_chaseY = 0;
_pixelX = CMap::getXcoordinate(_startX) + TILE_SIZE * 0.5f;
_pixelY = CMap::getYcoordinate(_startY);
}
CEnemy::~CEnemy()
{
for (int i = 0; i < 2; i++) {
delete _frightenedSprite[i];
_frightenedSprite[i] = NULL;
}
for (int i = 0; i < 4; i++) {
delete _eyesSprite[i];
_eyesSprite[i] = NULL;
}
}