From 6b1dcc5df51df4a2d8b724187eb1bcdb4fd9df8b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 14 Mar 2015 19:25:23 -0400 Subject: Added sound when you hit the ground Also split up components.cpp into files for each class, fixed a bug concerning falling off the screen when you change maps, and converted collision data into doubles. --- src/components/physics_body.cpp | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/components/physics_body.cpp (limited to 'src/components/physics_body.cpp') 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 @@ +#include "physics_body.h" +#include "game.h" + +void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) +{ + if (msg.type == Message::Type::walkLeft) + { + velocity.first = -90; + } else if (msg.type == Message::Type::walkRight) + { + velocity.first = 90; + } else if (msg.type == Message::Type::stopWalking) + { + velocity.first = 0.0; + } else if (msg.type == Message::Type::stopMovingHorizontally) + { + velocity.first = 0.0; + } else if (msg.type == Message::Type::stopMovingVertically) + { + velocity.second = 0.0; + } +} + +void PhysicsBodyComponent::tick(Game&, Entity& entity, double dt) +{ + // Accelerate + velocity.first += accel.first * dt; + velocity.second += accel.second * dt; + + // Terminal velocity +#define TERMINAL_VELOCITY_X (2 * TILE_WIDTH * FRAMES_PER_SECOND) +#define TERMINAL_VELOCITY_Y (2 * TILE_HEIGHT * FRAMES_PER_SECOND) + if (velocity.first < -TERMINAL_VELOCITY_X) velocity.first = -TERMINAL_VELOCITY_X; + if (velocity.first > TERMINAL_VELOCITY_X) velocity.first = TERMINAL_VELOCITY_X; + if (velocity.second < -TERMINAL_VELOCITY_Y) velocity.second = -TERMINAL_VELOCITY_Y; + if (velocity.second > TERMINAL_VELOCITY_Y) velocity.second = TERMINAL_VELOCITY_Y; + + // Do the movement + entity.position.first += velocity.first * dt; + entity.position.second += velocity.second * dt; +} + +void PhysicsBodyComponent::detectCollision(Game& game, Entity& entity, Entity& collider, std::pair old_position) +{ + // If already colliding, do nothing! + if ((old_position.first + collider.size.first > entity.position.first) + && (old_position.first < entity.position.first + entity.size.first) + && (old_position.second + collider.size.second > entity.position.second) + && (old_position.second < entity.position.second + entity.size.second)) + { + return; + } + + // If newly colliding, SHOCK AND HORROR! + if ((collider.position.first + collider.size.first > entity.position.first) + && (collider.position.first < entity.position.first + entity.size.first) + && (collider.position.second + collider.size.second > entity.position.second) + && (collider.position.second < entity.position.second + entity.size.second)) + { + Message msg(Message::Type::collision); + msg.collisionEntity = &collider; + + entity.send(game, msg); + } +} -- cgit 1.4.1