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