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 pathCEntity.cpp
More file actions
181 lines (155 loc) · 4.88 KB
/
CEntity.cpp
File metadata and controls
181 lines (155 loc) · 4.88 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
//
// CEntity.cpp
// PacMan
//
// Created by Vsevolod Klementjev on 11/19/12.
// Copyright (c) 2012 Vsevolod Klementjev. All rights reserved.
//
#include "CEntity.h"
#include <assert.h>
CEntity::CEntity(CMap *map)
{
_map = map;
_direction = DirectionLeft;
_animator = new CAnimator();
_animator->setMaxFrames(2);
_entitySpeed = 500 / TILE_SIZE; // 500 ms per tile
_moving = false;
_pixelX = 0;
_pixelY = 0;
_targetX = 0;
_targetY = 0;
_ticks = 0;
}
CEntity::~CEntity()
{
_map = NULL; // we don't own that
delete _animator;
_animator = NULL;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 2; j++) {
delete _entitySprite[i][j];
_entitySprite[i][j] = NULL;
}
}
}
void CEntity::loadSprite(const char* filename, int direction, int frame)
{
assert(direction >= 0 && direction < 4 && frame >= 0 && frame < 2);
_entitySprite[direction][frame] = new CSprite(filename);
}
void CEntity::Draw(SDL_Surface *dest)
{
if (_moving) {
// get our frame number from animator, do animation only while moving
int frame = _animator->getCurrentFrame();
_entitySprite[_direction][frame]->setPosition(_pixelX, _pixelY);
_entitySprite[_direction][frame]->Draw(dest);
}
else {
_entitySprite[_direction][0]->setPosition(_pixelX, _pixelY);
_entitySprite[_direction][0]->Draw(dest);
}
}
void CEntity::Update()
{
_animator->Update();
if (_ticks + _entitySpeed < SDL_GetTicks()) { // checking speed
_ticks = SDL_GetTicks();
if (_moving) {
// actual speed add here
_pixelX -= sign(_pixelX - _targetX);
_pixelY -= sign(_pixelY - _targetY);
// tunnel traveling, out of bounds hack
if (_pixelX < 0) {
_pixelX = MAP_WIDTH * TILE_SIZE + _pixelX;
_targetX = MAP_WIDTH * TILE_SIZE + _targetX;
}
else if (_pixelX >= MAP_WIDTH * TILE_SIZE) {
_pixelX = _pixelX - MAP_WIDTH * TILE_SIZE;
_targetX = _targetX - MAP_WIDTH * TILE_SIZE;
}
if (_pixelY < 0) {
_pixelY = MAP_HEIGHT * TILE_SIZE + _pixelY;
_targetY = MAP_HEIGHT * TILE_SIZE + _targetY;
}
else if (_pixelY >= MAP_HEIGHT * TILE_SIZE) {
_pixelY = _pixelY - MAP_HEIGHT * TILE_SIZE;
_targetY = _targetY - MAP_HEIGHT * TILE_SIZE;
}
// stop movement if we reach target tile's center
if (_pixelX == _targetX && _pixelY == _targetY) {
_moving = false;
}
}
}
}
void CEntity::reverseDirection()
{
switch (_direction) {
case DirectionUp:
_direction = DirectionDown;
break;
case DirectionDown:
_direction = DirectionUp;
break;
case DirectionLeft:
_direction = DirectionRight;
break;
case DirectionRight:
_direction = DirectionLeft;
break;
default:
break;
}
_moving = true;
}
bool CEntity::ableToMove(int xAdd, int yAdd)
{
// checking if there is a road in an xAdd and yAdd offset tile
return _map->getObjectAt(CMap::getTileXcoordinate(_pixelX + TILE_SIZE * 0.5f * xAdd * xAdd) + xAdd,
CMap::getTileYcoordinate(_pixelY + TILE_SIZE * 0.5f * yAdd * yAdd) + yAdd) & MapObjectRoad;
}
bool CEntity::isReverse(Direction direction)
{
if (_direction == DirectionDown && direction == DirectionUp) {
return true;
}
if (_direction == DirectionUp && direction == DirectionDown) {
return true;
}
if (_direction == DirectionRight && direction == DirectionLeft) {
return true;
}
if (_direction == DirectionLeft && direction == DirectionRight) {
return true;
}
return false;
}
void CEntity::Move(Direction direction)
{
// x and y offsets representing direction in a tile grid
int xAdd = direction == DirectionRight ? 1 : direction == DirectionLeft ? -1 : 0;
int yAdd = direction == DirectionDown ? 1 : direction == DirectionUp ? -1 : 0;
if (ableToMove(xAdd, yAdd)) // if there's a road, start moving
{
_direction = direction;
_moving = true;
// setting next tile's target so we know where to stop
_targetX = CMap::getXcoordinate(CMap::getTileXcoordinate(_pixelX) + xAdd);
_targetY = CMap::getYcoordinate(CMap::getTileYcoordinate(_pixelY) + yAdd);
}
}
int CEntity::sign(int v)
{
// returns sign and an actual speed buff (should be power of 2)
return v > 0 ? 2 : (v < 0 ? -2 : 0);
}
int CEntity::getXposition()
{
return CMap::getTileXcoordinate(_pixelX);
}
int CEntity::getYposition()
{
return CMap::getTileYcoordinate(_pixelY);
}