summary refs log tree commit diff stats
path: root/src/components/player_physics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/player_physics.cpp')
-rw-r--r--src/components/player_physics.cpp117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/components/player_physics.cpp b/src/components/player_physics.cpp new file mode 100644 index 0000000..1be43b6 --- /dev/null +++ b/src/components/player_physics.cpp
@@ -0,0 +1,117 @@
1#include "player_physics.h"
2#include "muxer.h"
3#include "game.h"
4
5#define JUMP_VELOCITY(h, l) (-2 * (h) / (l))
6#define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l))
7
8PlayerPhysicsComponent::PlayerPhysicsComponent()
9{
10 jump_velocity = JUMP_VELOCITY(TILE_HEIGHT*4.5, 0.3);
11 jump_gravity = JUMP_GRAVITY(TILE_HEIGHT*4.5, 0.3);
12 jump_gravity_short = JUMP_GRAVITY(TILE_HEIGHT*3.5, 0.233);
13
14 accel.second = jump_gravity_short;
15}
16
17void PlayerPhysicsComponent::receive(Game&, Entity& entity, const Message& msg)
18{
19 if (msg.type == Message::Type::walkLeft)
20 {
21 velocity.first = -90;
22 direction = -1;
23 } else if (msg.type == Message::Type::walkRight)
24 {
25 velocity.first = 90;
26 direction = 1;
27 } else if (msg.type == Message::Type::stopWalking)
28 {
29 velocity.first = 0.0;
30 direction = 0;
31 } else if (msg.type == Message::Type::stopMovingHorizontally)
32 {
33 velocity.first = 0.0;
34 } else if (msg.type == Message::Type::stopMovingVertically)
35 {
36 velocity.second = 0.0;
37 } else if (msg.type == Message::Type::hitTheGround)
38 {
39 if (isFalling)
40 {
41 playSound("../res/Randomize27.wav", 0.05);
42 isFalling = false;
43 }
44
45 velocity.second = 0.0;
46 } else if (msg.type == Message::Type::jump)
47 {
48 playSound("../res/Randomize87.wav", 0.25);
49
50 velocity.second = jump_velocity;
51 accel.second = jump_gravity;
52 } else if (msg.type == Message::Type::stopJump)
53 {
54 accel.second = jump_gravity_short;
55 } else if (msg.type == Message::Type::canDrop)
56 {
57 canDrop = true;
58 } else if (msg.type == Message::Type::cantDrop)
59 {
60 canDrop = false;
61 } else if (msg.type == Message::Type::drop)
62 {
63 if (canDrop)
64 {
65 canDrop = false;
66 } else {
67 entity.position.second = msg.dropAxis - entity.size.second;
68 velocity.second = 0;
69 }
70 } else if (msg.type == Message::Type::die)
71 {
72 frozen = true;
73 } else if (msg.type == Message::Type::stopDying)
74 {
75 frozen = false;
76 }
77}
78
79void PlayerPhysicsComponent::tick(Game& game, Entity& entity, double dt)
80{
81 // If frozen, do nothing
82 if (frozen)
83 {
84 return;
85 }
86
87 // Continue walking even if blocked earlier
88 if (velocity.first == 0)
89 {
90 if (direction < 0)
91 {
92 velocity.first = -90;
93 } else if (direction > 0)
94 {
95 velocity.first = 90;
96 }
97 }
98
99 // Increase gravity at the height of jump
100 if ((accel.second == jump_gravity) && (velocity.second >= 0))
101 {
102 accel.second = jump_gravity_short;
103 }
104
105 // Do the movement
106 std::pair<double, double> old_position = entity.position;
107 PhysicsBodyComponent::tick(game, entity, dt);
108
109 // Check for collisions
110 game.detectCollision(entity, old_position);
111
112 // Are we moving due to gravity?
113 if (velocity.second != 0.0)
114 {
115 isFalling = true;
116 }
117}