-
Notifications
You must be signed in to change notification settings - Fork 5
/
livingentity.cpp
187 lines (155 loc) · 3.83 KB
/
livingentity.cpp
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
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "entity.h"
#define STAIRTRAVERSETIME 1000 //Time in milliseconds to fully traverse stairs.
LivingEntity::LivingEntity(float x, float y, Direction startingDir) : Entity(x, y)
{
_onGround = false;
_alive = true;
_fixDirection = false;
_affectedByGravity = true;
_dir = startingDir;
_stairsEntered = NULL;
//stair related
_traversal = NotMoving;
_stairTimer = STAIRTRAVERSETIME;
_overlappingStairs = NULL;
_acceleration.accelerating = false;
_acceleration.accel = 0.0f;
_acceleration.target = 0.0f;
}
void LivingEntity::setAlive(bool b)
{
_alive = b;
}
bool LivingEntity::isAlive()
{
return _alive;
}
void LivingEntity::update(unsigned int dT)
{
Entity::update(dT);
if (!_onGround && _affectedByGravity)
{
_velocity.y += GRAVITY;
}
if (_alive)
{
if (!_fixDirection)
{
if (_velocity.x > 0 && _dir == Left)
_dir = Right;
if (_velocity.x < 0 && _dir == Right)
_dir = Left;
}
}
if (isMovingThroughStairs())
{
if (_stairTimer > 0)
{
_stairTimer -= dT;
}
else
{
_stairTimer = STAIRTRAVERSETIME;
if (_overlappingStairs != NULL) //could be null if save game is loaded while moving through stairs.
{
Stairs* target = (_traversal == MovingUp) ? _overlappingStairs->getUpstairs() : _overlappingStairs->getDownstairs();
if (target != NULL)
{
arriveAtStairs(target);
}
}
_traversal = NotMoving;
}
}
if (_acceleration.accelerating)
{
if (fabs(_velocity.x - _acceleration.target) < fabs(_acceleration.accel))
{
_acceleration.accelerating = false;
_acceleration.accel = 0.0f;
_velocity.x = _acceleration.target;
}
_velocity.x += _acceleration.accel;
}
}
void LivingEntity::landOnGround()
{
_velocity.y = 0.0f;
_onGround = true;
if (!_alive)
{
_velocity.x = 0.0f;
}
}
Direction LivingEntity::getDirection()
{
return _dir;
}
void LivingEntity::reverseDirection()
{
if (_dir == Left)
_dir = Right;
else _dir = Left;
}
void LivingEntity::setStairMovement(StairTraversal st)
{
//lock bounding box to stairs only if path through stairs exists.
Stairs* target = (st == MovingUp) ? _overlappingStairs->getUpstairs() : _overlappingStairs->getDownstairs();
if (target != NULL)
{
_stairsEntered = _overlappingStairs;
_traversal = st;
setCollisionRectPosition(_overlappingStairs->getCollisionRectPosition().x, _overlappingStairs->getCollisionRectPosition().y);
setDirection(Right);
_velocity.x = 0;
_velocity.y = 0;
}
}
StairTraversal LivingEntity::getStairTraversal()
{
return _traversal;
}
void LivingEntity::arriveAtStairs(Stairs* st)
{
setCollisionRectPosition(st->getCollisionRectPosition().x, st->getCollisionRectPosition().y);
_stairsEntered = NULL;
}
bool LivingEntity::isMovingThroughStairs()
{
return _traversal != NotMoving;
}
int LivingEntity::getStairTimer()
{
return _stairTimer;
}
void LivingEntity::setDirection(Direction dir)
{
_dir = dir;
}
Acceleration* LivingEntity::getAccelerationStruct()
{
return &_acceleration;
}
Stairs* LivingEntity::getStairsEntered()
{
return _stairsEntered;
}
bool LivingEntity::isPositionBehind(float x)
{
return ((x > getCollisionRectPosition().x && _dir == Left) ||
(x < getCollisionRectPosition().x && _dir == Right));
}