diff options
Diffstat (limited to 'src/components/physics_body.cpp')
-rw-r--r-- | src/components/physics_body.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/components/physics_body.cpp b/src/components/physics_body.cpp new file mode 100644 index 0000000..72f2fd8 --- /dev/null +++ b/src/components/physics_body.cpp | |||
@@ -0,0 +1,65 @@ | |||
1 | #include "physics_body.h" | ||
2 | #include "game.h" | ||
3 | |||
4 | void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) | ||
5 | { | ||
6 | if (msg.type == Message::Type::walkLeft) | ||
7 | { | ||
8 | velocity.first = -90; | ||
9 | } else if (msg.type == Message::Type::walkRight) | ||
10 | { | ||
11 | velocity.first = 90; | ||
12 | } else if (msg.type == Message::Type::stopWalking) | ||
13 | { | ||
14 | velocity.first = 0.0; | ||
15 | } else if (msg.type == Message::Type::stopMovingHorizontally) | ||
16 | { | ||
17 | velocity.first = 0.0; | ||
18 | } else if (msg.type == Message::Type::stopMovingVertically) | ||
19 | { | ||
20 | velocity.second = 0.0; | ||
21 | } | ||
22 | } | ||
23 | |||
24 | void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) | ||
25 | { | ||
26 | // Accelerate | ||
27 | velocity.first += accel.first * dt; | ||
28 | velocity.second += accel.second * dt; | ||
29 | |||
30 | // Terminal velocity | ||
31 | #define TERMINAL_VELOCITY_X (2 * TILE_WIDTH * FRAMES_PER_SECOND) | ||
32 | #define TERMINAL_VELOCITY_Y (2 * TILE_HEIGHT * FRAMES_PER_SECOND) | ||
33 | if (velocity.first < -TERMINAL_VELOCITY_X) velocity.first = -TERMINAL_VELOCITY_X; | ||
34 | if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; | ||
35 | if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; | ||
36 | if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; | ||
37 | |||
38 | // Do the movement | ||
39 | entity.position.first += velocity.first * dt; | ||
40 | entity.position.second += velocity.second * dt; | ||
41 | } | ||
42 | |||
43 | void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair<double, double> old_position) | ||
44 | { | ||
45 | // If already colliding, do nothing! | ||
46 | if ((old_position.first + collider.size.first > entity.position.first) | ||
47 | && (old_position.first < entity.position.first + entity.size.first) | ||
48 | && (old_position.second + collider.size.second > entity.position.second) | ||
49 | && (old_position.second < entity.position.second + entity.size.second)) | ||
50 | { | ||
51 | return; | ||
52 | } | ||
53 | |||
54 | // If newly colliding, SHOCK AND HORROR! | ||
55 | if ((collider.position.first + collider.size.first > entity.position.first) | ||
56 | && (collider.position.first < entity.position.first + entity.size.first) | ||
57 | && (collider.position.second + collider.size.second > entity.position.second) | ||
58 | && (collider.position.second < entity.position.second + entity.size.second)) | ||
59 | { | ||
60 | Message msg(Message::Type::collision); | ||
61 | msg.collisionEntity = &collider; | ||
62 | |||
63 | entity.send(game, msg); | ||
64 | } | ||
65 | } | ||