-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
356 lines (292 loc) · 8.6 KB
/
Player.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
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
#include "Player.h"
#include "Graphics.h"
#include "DirectInput.h"
#include "Level.h"
#include "MeleeWeapon.h"
#include "Weapon.h"
#include "RangedWeapon.h"
#include "Loots.h"
#include "Fist.h"
Collision polyCollision(RigidBody* bodyA, RigidBody* bodyB);
Player::Player(float x, float y, int width, int height)
: Object(x, y, width, height, "imgs\\ninja.bmp")
{
// Create the animation and set state and frame
mAnimation = new Animation(62, 64, 0.1, 6, 6);
mAnimation->pause();
mAnimation->setFrame(7);
// Load head texture
mHeadTexture = gGraphics->loadTexture("imgs\\mario_head.bmp");
mHeadFlipped = false;
// Set member variables
mAcceleration = 1.20f;
mMaxVelocity = 250.0f;
mFaceDirection = RIGHT;
mHealth = 100;
mInAir = true;
mHeadRotation = 0.0f;
mWeapon = NULL;
mStunned = false;
mStunnedCounter = 0.0f;
// Set the type to PLAYER
setType(PLAYER);
// High friction
//getBody()->SetFriction(5.0f);
getBody()->SetFriction(1.8);
//Fist* fist = new Fist(getPosition().x + 50, getPosition().y);
}
Player::~Player()
{
delete mAnimation;
ReleaseCOM(mHeadTexture);
}
void Player::init()
{
// Create and equip a Fist weapon
/*ObjectData* data = getLevel()->loadObjectData("Fist");
Fist* fist = new Fist(data, getPosition().x + 50, getPosition().y);
fist->setOffset(Vector(-50, 0));
fist->setRotationAxis(getPosition() - fist->getPosition());
fist->setParent(this);
fist->setCooldown(10.0f);
mWeapon = fist;*/
if(mWeapon != NULL)
getLevel()->addObject(mWeapon);
}
void Player::update(float dt)
{
if(mStunnedCounter > 0.0f)
mStunnedCounter -= dt;
else
mStunned = false;
// Update the animation
mAnimation->animate(dt);
// Update the head rotation
updateHead();
// Update the weapon
if(mWeapon != NULL) {
mWeapon->updatePosition(getPosition());
mWeapon->incrementCooldownCounter(dt);
}
/* Update the velocity */
// Strafing sideways
if(gDInput->keyDown(DIK_A) && !mStunned && getVelocity().x > -mMaxVelocity) { // Left
getBody()->ApplyForce(Vector(-mAcceleration, 0), getPosition());
setFacingDirection(LEFT);
if(mWeapon != NULL)
mWeapon->setFlipped(true);
// If the player is on the ground, continue the animation
if(!mInAir)
mAnimation->resume();
}
else if(gDInput->keyDown(DIK_D) && !mStunned && getVelocity().x < mMaxVelocity) { // Right
getBody()->ApplyForce(Vector(mAcceleration, 0), getPosition());
setFacingDirection(RIGHT);
if(mWeapon != NULL)
mWeapon->setFlipped(false);
// If the player is on the ground, continue the animation
if(!mInAir)
mAnimation->resume();
}
// If A or D is released set the frame to 0 if the player not is in the air
if(gDInput->keyReleased(DIK_A) || gDInput->keyReleased(DIK_D)) {
mAnimation->pause();
if(!mInAir)
mAnimation->setFrame(0);
}
// Jump if the player is on the ground
if(gDInput->keyDown(DIK_SPACE) && !mInAir) {
setVelocity(getVelocity() + Vector(0, -500));
getBody()->Move(0, -1); // Move it out of collision (:HACK:)
mAnimation->setFrame(7); // Set the jumping frame
mAnimation->pause(); // Pause the animation
mInAir = true; // The player is now in the air
}
// Shooting with the left mouse button
if(gDInput->mouseButtonDown(0)) {
if(mWeapon != NULL) {
if(mWeapon->isReady()) {
mWeapon->attack();
mWeapon->setCooldownCounter(0.0f);
mWeapon->setAttacking(true);
}
}
}
// Drop the equipped weapon if the drop button was pressed
if(gDInput->keyPressed(DIK_G))
dropWeapon();
// Loot
if(gDInput->keyPressed(DIK_E))
checkLoot();
}
void Player::draw()
{
// Draw the player with the right facing direction
if(mFaceDirection == LEFT) {
gGraphics->drawTexturedShape(*getBody()->GetShape(), getTexture(), &mAnimation->getSourceRect(), true);
//gGraphics->drawTexture(mHeadTexture, getPosition().x, getPosition().y - 20, 32, 32, 0xffffffff, mHeadRotation, mHeadFlipped);
}
else {
gGraphics->drawTexturedShape(*getBody()->GetShape(), getTexture(), &mAnimation->getSourceRect(), false);
//gGraphics->drawTexture(mHeadTexture, getPosition().x, getPosition().y - 20, 32, 32, 0xffffffff, mHeadRotation, mHeadFlipped);
}
char buffer[256];
sprintf(buffer, "head rotation: %f", mHeadRotation * 57.3);
gGraphics->drawText(buffer, 10, 400);
}
bool Player::collided(Object* collider)
{
// Grab information about the collision :NOTE: Could be done in a better way, ie collided has that information as a parameter
Collision collision = polyCollision(collider->getBody(), this->getBody());
// True if the player is on the ground
// Needs to check the y velocity because the objects can still be inside each other, but on their way of separating, might take 2-4 frames for them to be 100% separated
if(collision.pushY < 0 && getVelocity().y > 0) {
// Test of collided with self or the weapon :NOTE: Check against itself shouldn't be needed
if(collider != this && collider->getParent() != this)
{
if(mInAir) {
mInAir = false;
mAnimation->setFrame(0);
}
}
}
if(collider->getType() == PARTICLE) {
mStunned = true;
mStunnedCounter = 0.30f;
}
return true;
}
void Player::setFacingDirection(Direction direction)
{
mFaceDirection = direction;
}
void Player::setLevel(Level* level)
{
// Important to set the weapons level, so it know where to add the projectiles
Object::setLevel(level);
if(mWeapon != NULL)
mWeapon->setLevel(level);
}
void Player::setVelocity(Vector velocity)
{
Object::setVelocity(velocity);
// Set the weapons velocity so it works correctly when collididing with bodies
if(mWeapon != NULL)
mWeapon->setVelocity(velocity.x, velocity.y);
}
void Player::setVelocity(float dx, float dy)
{
setVelocity(Vector(dx, dy));
}
void Player::checkLoot()
{
// Calculate loot area
Rect lootArea = getRect();
if(getWeapon() != NULL) {
// Add weapons width to loot area :NOTE: :TODO: Gives wrong result when the weapon is centered on the player etc...
if(mFaceDirection == RIGHT)
lootArea.right += getWeapon()->getWidth();
else if(mFaceDirection == LEFT)
lootArea.left -= getWeapon()->getWidth();
}
// Add some extra area
lootArea.bottom += 20;
lootArea.top -= 20;
lootArea.left -= 20;
lootArea.right += 20;
// Find loot inside loot area
Object* lootObject = getLevel()->findCollision(lootArea, getId(), LOOT);
// If an object is found
if(lootObject != NULL)
{
// If the object is loot
if(lootObject->getType() == LOOT) {
// Cast and call equip()
Loot* loot = dynamic_cast<Loot*>(lootObject);
loot->equip(this);
}
}
}
void Player::equipWeapon(Weapon* weapon)
{
// Remove the equipped weapon from the level (if there is any)
if(mWeapon != NULL)
getLevel()->removeObject(mWeapon);
// Assign the new weapon to be equipped
mWeapon = weapon;
mWeapon->setLevel(getLevel());
// Find out if it needs to be flipped
if(mFaceDirection == LEFT)
mWeapon->setFlipped(true);
// Add the new weapon to the level
getLevel()->addObject(mWeapon);
}
void Player::dropWeapon()
{
// Find out if there's any weapon to drop
if(mWeapon != NULL)
{
// Remove the equipped weapon from the level
getLevel()->removeObject(mWeapon);
mWeapon = NULL;
// Create the new loot object
SwordLoot* newLoot = new SwordLoot(getPosition().x, getPosition().y, 50, 50);
newLoot->setLevel(getLevel());
// Make it look good by a throwing effect
if(mFaceDirection == RIGHT) {
newLoot->getBody()->Move(50, -50);
newLoot->getBody()->ApplyForce(Vector(1, -1), newLoot->getPosition());
}
else if(mFaceDirection == LEFT) {
newLoot->getBody()->Move(-50, -50);
newLoot->getBody()->ApplyForce(Vector(-1, -1), newLoot->getPosition());
}
// Add the loot to the level
getLevel()->addObject(newLoot);
}
}
void Player::damage(float damage)
{
mHealth -= damage;
}
void Player::updateHead()
{
// Get mouse position
Vector mousePos = gDInput->getCursorPos();
// Get distance to the mouse
float dx = mousePos.x - getPosition().x;
float dy = mousePos.y - getPosition().y - 20;
// Point head in mouse direction - atan2 to not be limited to tans 180 degree period
float rotation = atan2f(dy, dx);
/*static float realRotation = 0.0f;
if(gDInput->keyPressed(DIK_E))
realRotation += 0.5;
else if(gDInput->keyPressed(DIK_Q))
realRotation -= 0.5;
if(realRotation > 2*PI)
realRotation -= 2*PI;
else if(realRotation < 0)
realRotation = 2*PI + realRotation;
*/
//mHeadRotation = realRotation;
mHeadRotation = rotation;
if(mHeadRotation > PI/2 || mHeadRotation < -PI/2) {
mHeadFlipped = true;
mHeadRotation -= PI;
}
else {
mHeadFlipped = false;
mHeadRotation += 2*PI;
}
}
void Player::childEvent(string eventMessage)
{
}
Direction Player::getFacingDirection()
{
return mFaceDirection;
}
Weapon* Player::getWeapon()
{
return mWeapon;
}