-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
370 lines (298 loc) · 9.17 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Player.cpp
*
* Created on: 4 maj 2010
* Author: meros
*/
#include "Player.h"
#include "Rope.h"
#define MAXFALLSPEED 5
#define JUMP_SPEED_BEG -4
#define JUMP_SPEED_END -3
#define JUMP_SPEED_STOP -2
#define JUMP_MAX_LEN 0.7
#define COLLISION_HEIGHT 0.26f
#define COLLISION_WIDTH 0.16f
#define RUN_BREAK_FACTOR 0.2
#define RUN_STOP_THRESHOLD 0.1
#define RUN_ACC 0.6
#define MAX_RUN_SPEED 3
#define MID_AIR_STEER_ACC 0.4
#define MID_AIR_SLOWDOWN_FACTOR 0.1
#define MID_AIR_STOP_THRESHOLD 0.01
#define ROPE_STEER_ACC 0.2
#define ROPE_SLOWDOWN_FACTOR 0
#include <iostream>
#include <cmath>
using namespace std;
Player::Player(b2World& aWorld, float aXStartPos, float aYStartPos) :
myNewlyTouchedRopeBody(NULL), myNewlyTouchedRope(NULL), myRopeJoint(
NULL), myRope(NULL), myWorld(aWorld), myIsBreaking(false), myTouchingRoofContacts(
0), myJumpStartY(0.0f) {
myType = UserData::Player;
myDX = 0;
myDY = 0;
myAnimationCounter = 0;
myTouchingGroundContacts = 0;
myDirectionIsRight = true;
myState = STATE_MidAir;
myJumpBtnReleased = true;
myStandRightSprite.LoadTGA("data/apa_stand_right.tga");
myBreakRightSprite.LoadTGA("data/mario_break_right.tga");
myWalk1RightSprite.LoadTGA("data/apa_walk_right_1.tga");
myWalk2RightSprite.LoadTGA("data/apa_walk_right_2.tga");
myJumpRightSprite.LoadTGA("data/apa_jump_right.tga");
myStandLeftSprite.LoadTGA("data/apa_stand_left.tga");
myBreakLeftSprite.LoadTGA("data/mario_break_left.tga");
myClimbLeftSprite.LoadTGA("data/mario_jump_left.tga");
myClimbRightSprite.LoadTGA("data/mario_jump_left.tga");
myClimbRightSprite.Mirror();
myWalk1LeftSprite.LoadTGA("data/apa_walk_right_1.tga");
myWalk1LeftSprite.Mirror();
myWalk2LeftSprite.LoadTGA("data/apa_walk_right_2.tga");
myWalk2LeftSprite.Mirror();
myJumpLeftSprite.LoadTGA("data/apa_jump_left.tga");
// Set up collision box
b2BodyDef dropboxDef;
dropboxDef.type = b2_dynamicBody;
dropboxDef.position = b2Vec2(aXStartPos, aYStartPos);
b2PolygonShape dropboxShape;
float roundof = 0.02;
b2Vec2 verts[8];
// verts[0] = b2Vec2(-COLLISION_WIDTH/2, -COLLISION_HEIGHT/2);
//verts[1] = b2Vec2(COLLISION_WIDTH/2, -COLLISION_HEIGHT/2);
verts[0] = b2Vec2(-COLLISION_WIDTH / 2, -COLLISION_HEIGHT / 2 + roundof);
verts[1] = b2Vec2(-COLLISION_WIDTH / 2 + roundof, -COLLISION_HEIGHT / 2);
verts[2] = b2Vec2(COLLISION_WIDTH / 2 - roundof, -COLLISION_HEIGHT / 2);
verts[3] = b2Vec2(COLLISION_WIDTH / 2, -COLLISION_HEIGHT / 2 + roundof);
verts[4] = b2Vec2(COLLISION_WIDTH / 2, COLLISION_HEIGHT / 2 - roundof);
verts[5] = b2Vec2(COLLISION_WIDTH / 2 - roundof, COLLISION_HEIGHT / 2);
verts[6] = b2Vec2(-COLLISION_WIDTH / 2 + roundof, COLLISION_HEIGHT / 2);
verts[7] = b2Vec2(-COLLISION_WIDTH / 2, COLLISION_HEIGHT / 2 - roundof);
dropboxShape.Set(verts, 8);
//dropboxShape.SetAsBox(COLLISION_WIDTH/2, COLLISION_HEIGHT/2);
myCollisionBody = aWorld.CreateBody(&dropboxDef);
myCollisionBody->SetFixedRotation(true);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dropboxShape;
fixtureDef.density = 0.1f;
fixtureDef.friction = 0.4f;
myCollisionBody->CreateFixture(&fixtureDef)->SetUserData((UserData*) this);
}
Player::~Player() {
// TODO Auto-generated destructor stub
}
void Player::Update() {
myDY = myCollisionBody->GetLinearVelocity().y;
myDX = myCollisionBody->GetLinearVelocity().x;
if (myState == STATE_MidAir && myNewlyTouchedRopeBody) {
b2RevoluteJointDef jointDef;
b2Vec2 anchor = (myNewlyTouchedRopeBody->GetPosition()
+ myCollisionBody->GetPosition());
anchor *= 0.5;
jointDef.Initialize(myNewlyTouchedRopeBody, myCollisionBody, anchor);
jointDef.collideConnected = false;
myRope = myNewlyTouchedRope;
myRopeJoint = myWorld.CreateJoint(&jointDef);
myState = STATE_Rope;
}
if (PrivDoJump() && myState == STATE_Rope) {
myWorld.DestroyJoint(myRopeJoint);
myRopeJoint = NULL;
myJumpBtnReleased = false;
myState = STATE_MidJump;
myDY = JUMP_SPEED_BEG;
myJumpStartY = myCollisionBody->GetPosition().y;
myState = STATE_MidJump;
}
myNewlyTouchedRopeBody = NULL;
if (myTouchingGroundContacts > 0) {
if (myState == STATE_MidAir) {
myState = STATE_Ground;
}
} else {
if (myDY > 0.1 && myState == STATE_Ground) {
myState = STATE_MidAir;
}
}
myCollisionBody->SetAwake(true);
if (!PrivDoJump()) {
myJumpBtnReleased = true;
}
myIsBreaking = false;
//Swinging
if (myState == STATE_Rope) {
if (myDX > 0 && PrivDoRight()) {
myDX += ROPE_STEER_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
} else if (myDX < 0 && PrivDoLeft()) {
myDX -= ROPE_STEER_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
} else {
//Mid air slowing down
myDX -= myDX * ROPE_SLOWDOWN_FACTOR;
}
if (PrivDoUp()) {
myDY = 1;
myRope->MoveJoint(myRopeJoint, 1);
} else if (PrivDoDown()) {
myDY = -1;
myRope->MoveJoint(myRopeJoint, -1);
}
}
if (myState == STATE_MidAir || myState == STATE_MidJump) {
//Mid air steering
if (PrivDoRight()) {
myDX += MID_AIR_STEER_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
} else if (PrivDoLeft()) {
myDX -= MID_AIR_STEER_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
} else {
//Mid air slowing down
myDX -= myDX * MID_AIR_SLOWDOWN_FACTOR;
if (abs(myDX) < MID_AIR_STOP_THRESHOLD) {
myDX = 0;
}
}
}
//Falling maxfallspeed
if (myDY > MAXFALLSPEED)
myDY = MAXFALLSPEED;
switch (myState) {
case STATE_MidAir: {
break;
}
case STATE_Ground:
//Begin jump?
if (PrivDoJump() && myJumpBtnReleased) {
myJumpBtnReleased = false;
myState = STATE_MidJump;
myDY = JUMP_SPEED_BEG;
myJumpStartY = myCollisionBody->GetPosition().y;
} else {
//Normal running
if (PrivDoRight()) {
if (myDX < -0.1) {
myIsBreaking = true;
myDX -= myDX * RUN_BREAK_FACTOR;
} else
myDX += RUN_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
} else if (PrivDoLeft()) {
if (myDX > 0.1) {
myIsBreaking = true;
myDX -= myDX * RUN_BREAK_FACTOR;
} else
myDX -= RUN_ACC * (1 - (abs(myDX) / MAX_RUN_SPEED));
}
if (abs(myDX) < RUN_STOP_THRESHOLD) {
myDX = 0;
}
}
break;
case STATE_MidJump:
if (!PrivDoJump()
|| (myJumpStartY - myCollisionBody->GetPosition().y)
> JUMP_MAX_LEN || myTouchingRoofContacts != 0) {
myState = STATE_MidAir;
myDY = JUMP_SPEED_STOP;
} else {
myDY = ((JUMP_MAX_LEN
- (myJumpStartY - myCollisionBody->GetPosition().y))
/ JUMP_MAX_LEN) * JUMP_SPEED_BEG
+ ((myJumpStartY - myCollisionBody->GetPosition().y)
/ JUMP_MAX_LEN) * JUMP_SPEED_END;
}
break;
case STATE_Rope:
break;
default:
myDY = 0;
break;
}
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::A))
myCollisionBody->SetLinearVelocity(b2Vec2(myDX, myDY));
if (myDX > 0.1)
myDirectionIsRight = true;
if (myDX < -0.1)
myDirectionIsRight = false;
myTouchingGroundContacts = 0;
myTouchingRoofContacts = 0;
}
void Player::Draw(sf::RenderTarget& aTarget, float aXScale, float aYScale,
float aXTranslate, float aYTranslate) {
myAnimationCounter++;
float x = (myCollisionBody->GetPosition().x - aXTranslate) * aXScale;
float y = (myCollisionBody->GetPosition().y - aYTranslate) * aYScale;
//FIXME: matrix matrix matrix
Sprite* spriteToDraw = NULL;
if (myState == STATE_Ground) {
if (myDirectionIsRight) {
if (myIsBreaking) {
spriteToDraw = &myBreakRightSprite;
} else if (myDX < RUN_STOP_THRESHOLD) {
spriteToDraw = &myStandRightSprite;
} else {
if ((myAnimationCounter / 4) % 2 == 0)
spriteToDraw = &myWalk1RightSprite;
else
spriteToDraw = &myWalk2RightSprite;
}
} else {
if (myIsBreaking) {
spriteToDraw = &myBreakLeftSprite;
} else if (-myDX < RUN_STOP_THRESHOLD) {
spriteToDraw = &myStandLeftSprite;
} else {
if ((myAnimationCounter / 4) % 2 == 0)
spriteToDraw = &myWalk1LeftSprite;
else
spriteToDraw = &myWalk2LeftSprite;
}
}
} else if (myState == STATE_Rope) {
if (myDirectionIsRight) {
spriteToDraw = &myClimbRightSprite;
} else {
spriteToDraw = &myClimbLeftSprite;
}
} else {
if (myDirectionIsRight) {
spriteToDraw = &myJumpRightSprite;
} else {
spriteToDraw = &myJumpLeftSprite;
}
}
if (spriteToDraw) {
spriteToDraw->DrawGroundCenterRelative(aTarget, x,
y + (COLLISION_HEIGHT * aYScale / 2.0f),
COLLISION_HEIGHT * aYScale * 2,
myCollisionBody->GetTransform().q.GetAngle());
}
}
void Player::SetPosition(float aX, float aY) {
}
Point Player::GetPosition() {
Point res;
res.myX = myCollisionBody->GetPosition().x;
res.myY = myCollisionBody->GetPosition().y;
return res;
}
void Player::PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) {
b2WorldManifold manifold;
contact->GetWorldManifold(&manifold);
cout << endl;
if (manifold.points[0].y
>= myCollisionBody->GetPosition().y + (COLLISION_HEIGHT / 2) * 0.9
&& abs(manifold.points[0].x - myCollisionBody->GetPosition().x)
<= (COLLISION_HEIGHT / 2) * 0.95) {
cout << "ground!" << endl;
myTouchingGroundContacts++;
}
if (manifold.points[0].y
<= myCollisionBody->GetPosition().y + -(COLLISION_HEIGHT / 2) * 0.9
&& abs(manifold.points[0].x - myCollisionBody->GetPosition().x)
<= (COLLISION_HEIGHT / 2) * 0.95) {
myTouchingRoofContacts++;
}
}
void Player::TouchedRope(Rope* aRope, b2Body* aBody) {
myNewlyTouchedRopeBody = aBody;
myNewlyTouchedRope = aRope;
}