From 5c82f052c26303318e81ddd76475c1d188cc74f4 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 8 May 2018 21:09:36 -0400 Subject: Simplified positions/sizes with vectors Positions and sizes are now stored as vectors (of doubles and ints, respectively). This allows for at least minor code simplification in many places, and cleans up the CollisionParams code in PonderingSystem quite a bit. --- src/components/automatable.h | 4 +- src/components/playable.h | 4 +- src/components/ponderable.h | 10 +- src/components/realizable.h | 4 +- src/components/transformable.h | 13 +- src/systems/animating.cpp | 8 +- src/systems/automating.cpp | 3 +- src/systems/orienting.cpp | 14 +- src/systems/playing.cpp | 19 +-- src/systems/playing.h | 4 +- src/systems/pondering.cpp | 294 +++++++++++++++-------------------------- src/systems/pondering.h | 34 +++-- src/systems/realizing.cpp | 38 +++--- src/vector.h | 111 ++++++++++++++++ 14 files changed, 285 insertions(+), 275 deletions(-) create mode 100644 src/vector.h diff --git a/src/components/automatable.h b/src/components/automatable.h index b37945f..c1fd1a3 100644 --- a/src/components/automatable.h +++ b/src/components/automatable.h @@ -4,6 +4,7 @@ #include "component.h" #include #include +#include "vector.h" class AutomatableComponent : public Component { public: @@ -18,8 +19,7 @@ public: * The horizontal and vertical speed, in pixels/sec, that the entity should * move at. */ - double speedX; - double speedY; + vec2d speed; /** * The duration of the action in seconds. diff --git a/src/components/playable.h b/src/components/playable.h index 94d4326..b8af0f2 100644 --- a/src/components/playable.h +++ b/src/components/playable.h @@ -3,6 +3,7 @@ #include "component.h" #include "entity_manager.h" +#include "vector.h" class PlayableComponent : public Component { public: @@ -24,8 +25,7 @@ public: * @managed_by PlayingSystem */ size_t checkpointMapId; - double checkpointX; - double checkpointY; + vec2d checkpointPos; }; diff --git a/src/components/ponderable.h b/src/components/ponderable.h index eff20e9..c0312b4 100644 --- a/src/components/ponderable.h +++ b/src/components/ponderable.h @@ -4,6 +4,7 @@ #include #include "component.h" #include "entity_manager.h" +#include "vector.h" class PonderableComponent : public Component { public: @@ -45,14 +46,12 @@ public: /** * The velocity of the body. */ - double velX = 0.0; - double velY = 0.0; + vec2d vel = { 0.0, 0.0 }; /** * The acceleration of the body. */ - double accelX = 0.0; - double accelY = 0.0; + vec2d accel = { 0.0, 0.0 }; /** * The type of physical body that the entity is meant to assume. The body will @@ -89,8 +88,7 @@ public: * * @managed_by PonderingSystem */ - double relX; - double relY; + vec2d rel = { 0.0, 0.0 }; /** * The bodies that are being ferried by this body. diff --git a/src/components/realizable.h b/src/components/realizable.h index 0858e7a..b749aeb 100644 --- a/src/components/realizable.h +++ b/src/components/realizable.h @@ -5,6 +5,7 @@ #include #include #include "entity_manager.h" +#include "vector.h" class RealizableComponent : public Component { public: @@ -31,8 +32,7 @@ public: * @managed_by RealizingSystem */ int startingMapId; - int startingX; - int startingY; + vec2i startingPos; /** * The set of map entities loaded by this entity. It is only intended for diff --git a/src/components/transformable.h b/src/components/transformable.h index 6f3c2ef..bb21996 100644 --- a/src/components/transformable.h +++ b/src/components/transformable.h @@ -2,6 +2,7 @@ #define LOCATABLE_H_39E526CA #include "component.h" +#include "vector.h" class TransformableComponent : public Component { public: @@ -15,24 +16,20 @@ public: * so, use PonderingSystem::unferry on the body to ensure that it is not * ferried. */ - double x; - double y; + vec2d pos; /** * The size of the entity. */ - int w; - int h; + vec2i size; /** * For prototypes, the original coordinates and size of the entity. * * @managed_by RealizingSystem */ - double origX; - double origY; - int origW; - int origH; + vec2d origPos; + vec2i origSize; }; #endif /* end of include guard: LOCATABLE_H_39E526CA */ diff --git a/src/systems/animating.cpp b/src/systems/animating.cpp index 8543ba2..50a32fc 100644 --- a/src/systems/animating.cpp +++ b/src/systems/animating.cpp @@ -64,10 +64,10 @@ void AnimatingSystem::render(Texture& texture) } Rectangle dstrect { - static_cast(transform.x), - static_cast(transform.y), - transform.w, - transform.h}; + static_cast(transform.pos.x()), + static_cast(transform.pos.y()), + transform.size.w(), + transform.size.h()}; const AnimationSet& aset = sprite.animationSet; game_.getRenderer().blit( diff --git a/src/systems/automating.cpp b/src/systems/automating.cpp index 0d85957..61b97d9 100644 --- a/src/systems/automating.cpp +++ b/src/systems/automating.cpp @@ -54,8 +54,7 @@ void AutomatingSystem::tick(double dt) auto& ponderable = game_.getEntityManager(). getComponent(entity); - ponderable.velX = curAction.speedX; - ponderable.velY = curAction.speedY; + ponderable.vel = curAction.speed; automatable.remaining -= dt; } diff --git a/src/systems/orienting.cpp b/src/systems/orienting.cpp index 206ebf6..d73ddd2 100644 --- a/src/systems/orienting.cpp +++ b/src/systems/orienting.cpp @@ -24,27 +24,27 @@ void OrientingSystem::tick(double) { case OrientableComponent::WalkState::still: { - ponderable.velX = 0.0; + ponderable.vel.x() = 0.0; break; } case OrientableComponent::WalkState::left: { - ponderable.velX = -WALK_SPEED; + ponderable.vel.x() = -WALK_SPEED; break; } case OrientableComponent::WalkState::right: { - ponderable.velX = WALK_SPEED; + ponderable.vel.x() = WALK_SPEED; break; } } - if (orientable.isJumping() && (ponderable.velY > 0)) + if (orientable.isJumping() && (ponderable.vel.y() > 0)) { orientable.setJumping(false); } @@ -122,8 +122,8 @@ void OrientingSystem::jump(id_type entity) playSound("res/Randomize87.wav", 0.25); - ponderable.velY = JUMP_VELOCITY; - ponderable.accelY = JUMP_GRAVITY; + ponderable.vel.y() = JUMP_VELOCITY; + ponderable.accel.y() = JUMP_GRAVITY; auto& animating = game_.getSystemManager().getSystem(); if (orientable.isFacingRight()) @@ -147,7 +147,7 @@ void OrientingSystem::stopJumping(id_type entity) auto& ponderable = game_.getEntityManager(). getComponent(entity); - ponderable.accelY = NORMAL_GRAVITY; + ponderable.accel.y() = NORMAL_GRAVITY; } } diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp index acec4e7..dabc9a5 100644 --- a/src/systems/playing.cpp +++ b/src/systems/playing.cpp @@ -42,10 +42,9 @@ void PlayingSystem::initPlayer() auto& transformable = game_.getEntityManager(). emplaceComponent(player); - transformable.x = realizable.startingX; - transformable.y = realizable.startingY; - transformable.w = 10; - transformable.h = 12; + transformable.pos = realizable.startingPos; + transformable.size.w() = 10; + transformable.size.h() = 12; game_.getSystemManager().getSystem().initializeBody( player, @@ -59,8 +58,7 @@ void PlayingSystem::initPlayer() playable.mapId = realizable.activeMap; playable.checkpointMapId = realizable.startingMapId; - playable.checkpointX = realizable.startingX; - playable.checkpointY = realizable.startingY; + playable.checkpointPos = realizable.startingPos; realizing.enterActiveMap(player); @@ -70,8 +68,7 @@ void PlayingSystem::initPlayer() void PlayingSystem::changeMap( id_type player, size_t mapId, - double x, - double y) + vec2d warpPos) { auto& playable = game_.getEntityManager(). getComponent(player); @@ -103,8 +100,7 @@ void PlayingSystem::changeMap( pondering.unferry(player); - transformable.x = x; - transformable.y = y; + transformable.pos = warpPos; if (realizable.activePlayer == player) { @@ -139,8 +135,7 @@ void PlayingSystem::die(id_type player) changeMap( player, playable.checkpointMapId, - playable.checkpointX, - playable.checkpointY); + playable.checkpointPos); animatable.frozen = false; animatable.flickering = false; diff --git a/src/systems/playing.h b/src/systems/playing.h index 9ba403b..31f79ab 100644 --- a/src/systems/playing.h +++ b/src/systems/playing.h @@ -2,6 +2,7 @@ #define PLAYING_H_70A54F7D #include "system.h" +#include "vector.h" class PlayingSystem : public System { public: @@ -15,8 +16,7 @@ public: void changeMap( id_type player, size_t mapId, - double x, - double y); + vec2d warpPos); void die(id_type player); diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 7b3ab2d..ec83270 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -47,7 +47,7 @@ void PonderingSystem::initializeBody( if (type == PonderableComponent::Type::freefalling) { - ponderable.accelY = NORMAL_GRAVITY; + ponderable.accel.y() = NORMAL_GRAVITY; } } @@ -56,10 +56,10 @@ void PonderingSystem::initPrototype(id_type prototype) auto& ponderable = game_.getEntityManager(). getComponent(prototype); - ponderable.velX = 0.0; - ponderable.velY = 0.0; - ponderable.accelX = 0.0; - ponderable.accelY = 0.0; + ponderable.vel.x() = 0.0; + ponderable.vel.y() = 0.0; + ponderable.accel.x() = 0.0; + ponderable.accel.y() = 0.0; ponderable.grounded = false; ponderable.frozen = false; ponderable.collidable = true; @@ -102,19 +102,17 @@ void PonderingSystem::tickBody( // Accelerate if (!ponderable.frozen) { - ponderable.velX += ponderable.accelX * dt; - ponderable.velY += ponderable.accelY * dt; + ponderable.vel += ponderable.accel * dt; if ((ponderable.type == PonderableComponent::Type::freefalling) - && (ponderable.velY > TERMINAL_VELOCITY)) + && (ponderable.vel.y() > TERMINAL_VELOCITY)) { - ponderable.velY = TERMINAL_VELOCITY; + ponderable.vel.y() = TERMINAL_VELOCITY; } } // Move - double newX = transformable.x; - double newY = transformable.y; + vec2d newPos = transformable.pos; if (!ponderable.frozen) { @@ -123,19 +121,13 @@ void PonderingSystem::tickBody( auto& ferryTrans = game_.getEntityManager(). getComponent(ponderable.ferry); - newX = ferryTrans.x + ponderable.relX; - newY = ferryTrans.y + ponderable.relY; + newPos = ferryTrans.pos + ponderable.rel; } - newX += ponderable.velX * dt; - newY += ponderable.velY * dt; + newPos += ponderable.vel * dt; } - CollisionResult result = - moveBody( - entity, - newX, - newY); + CollisionResult result = moveBody(entity, newPos); // Perform cleanup for orientable entites bool groundedChanged = (ponderable.grounded != result.grounded); @@ -194,8 +186,7 @@ void PonderingSystem::tickBody( auto& ferryTrans = game_.getEntityManager(). getComponent(ponderable.ferry); - ponderable.relX = transformable.x - ferryTrans.x; - ponderable.relY = transformable.y - ferryTrans.y; + ponderable.rel = transformable.pos - ferryTrans.pos; } // Handle ferry passengers @@ -212,35 +203,34 @@ void PonderingSystem::tickBody( // Move to an adjacent map, if necessary if (result.adjacentlyWarping) { - double warpX = result.newX; - double warpY = result.newY; + vec2d warpPos = result.pos; switch (result.adjWarpDir) { case Direction::left: { - warpX = GAME_WIDTH + WALL_GAP - transformable.w; + warpPos.x() = GAME_WIDTH + WALL_GAP - transformable.size.w(); break; } case Direction::right: { - warpX = -WALL_GAP; + warpPos.x() = -WALL_GAP; break; } case Direction::up: { - warpY = MAP_HEIGHT * TILE_HEIGHT - transformable.h; + warpPos.y() = MAP_HEIGHT * TILE_HEIGHT - transformable.size.h(); break; } case Direction::down: { - warpY = -WALL_GAP; + warpPos.y() = -WALL_GAP; break; } @@ -250,15 +240,13 @@ void PonderingSystem::tickBody( changeMap( entity, result.adjWarpMapId, - warpX, - warpY); + warpPos); } } -CollisionResult PonderingSystem::moveBody( +PonderingSystem::CollisionResult PonderingSystem::moveBody( id_type entity, - double x, - double y) + vec2d newPos) { auto& ponderable = game_.getEntityManager(). getComponent(entity); @@ -266,26 +254,29 @@ CollisionResult PonderingSystem::moveBody( auto& transformable = game_.getEntityManager(). getComponent(entity); - const double oldX = transformable.x; - const double oldY = transformable.y; - const double oldRight = oldX + transformable.w; - const double oldBottom = oldY + transformable.h; - CollisionResult result; if (ponderable.collidable) { - result = detectCollisions(entity, x, y); + result = detectCollisions(entity, newPos); } else { - result.newX = x; - result.newY = y; + result.pos = newPos; } // Move if (!ponderable.frozen) { - transformable.x = result.newX; - transformable.y = result.newY; + transformable.pos = result.pos; + + if (result.blockedHoriz) + { + ponderable.vel.x() = 0.0; + } + + if (result.blockedVert) + { + ponderable.vel.y() = 0.0; + } } return result; @@ -311,21 +302,14 @@ namespace CollisionParams { return (colliderAxis > entityAxis); } - inline static double OldAxis(const TransformableComponent& transformable) + inline static double EntityAxis(const vec2d& pos, const vec2i& size) { - return HorizVert::AxisOldLower(transformable); + return HorizVert::AxisLower(pos); } - inline static double NewAxis( - const CollisionResult& result, - const TransformableComponent&) + inline static double ObjectAxis(const vec2d& pos, const vec2i& size) { - return HorizVert::AxisNewLower(result); - } - - inline static double ObjectAxis(const TransformableComponent& transformable) - { - return HorizVert::AxisOldUpper(transformable); + return HorizVert::AxisUpper(pos, size); } inline static bool Closer(double left, double right) @@ -352,21 +336,14 @@ namespace CollisionParams { return (colliderAxis < entityAxis); } - inline static double OldAxis(const TransformableComponent& transformable) + inline static double EntityAxis(const vec2d& pos, const vec2i& size) { - return HorizVert::AxisOldUpper(transformable); + return HorizVert::AxisUpper(pos, size); } - inline static double NewAxis( - const CollisionResult& result, - const TransformableComponent& transformable) + inline static double ObjectAxis(const vec2d& pos, const vec2i& size) { - return HorizVert::AxisNewUpper(result, transformable); - } - - inline static double ObjectAxis(const TransformableComponent& transformable) - { - return HorizVert::AxisOldLower(transformable); + return HorizVert::AxisLower(pos); } inline static bool Closer(double left, double right) @@ -375,121 +352,62 @@ namespace CollisionParams { } }; - class Horizontal { + template + class HorizVert { public: - inline static double AxisOldLower( - const TransformableComponent& transformable) + inline static double AxisLower(const vec2d& pos) { - return transformable.x; + return pos.coords[Axis]; } - inline static double AxisOldUpper( - const TransformableComponent& transformable) + inline static double AxisUpper(const vec2d& pos, const vec2i& size) { - return transformable.x + transformable.w; + return pos.coords[Axis] + size.coords[Axis]; } - inline static double AxisNewLower(const CollisionResult& result) + inline static double NonAxisLower(const vec2d& pos) { - return result.newX; + return pos.coords[NonAxis]; } - inline static double AxisNewUpper( - const CollisionResult& result, - const TransformableComponent& transformable) + inline static double NonAxisUpper(const vec2d& pos, const vec2i& size) { - return result.newX + transformable.w; + return pos.coords[NonAxis] + size.coords[NonAxis]; } - inline static double NonAxisOldLower( - const TransformableComponent& transformable) - { - return transformable.y; - } - - inline static double NonAxisOldUpper( - const TransformableComponent& transformable) - { - return transformable.y + transformable.h; - } - - inline static double NonAxisNewLower( - const CollisionResult& result, - const TransformableComponent& transformable) - { - return result.newY; - } - - inline static double NonAxisNewUpper( - const CollisionResult& result, - const TransformableComponent& transformable) - { - return result.newY + transformable.h; - } }; - class Vertical { - public: - - inline static double AxisOldLower( - const TransformableComponent& transformable) - { - return transformable.y; - } - - inline static double AxisOldUpper( - const TransformableComponent& transformable) - { - return transformable.y + transformable.h; - } + using Horizontal = HorizVert<0, 1>; + using Vertical = HorizVert<1, 0>; - inline static double AxisNewLower(const CollisionResult& result) - { - return result.newY; - } + template + class DetectCollisions : public AscDesc { + public: - inline static double AxisNewUpper( - const CollisionResult& result, - const TransformableComponent& transformable) - { - return result.newY + transformable.h; - } + static const Direction Dir = dir; - inline static double NonAxisOldLower( - const TransformableComponent& transformable) + inline static double EntityAxis(const vec2d& pos, const vec2i& size) { - return transformable.x; + return AscDesc::EntityAxis(pos, size); } - inline static double NonAxisOldUpper( - const TransformableComponent& transformable) + inline static double ObjectAxis(const vec2d& pos, const vec2i& size) { - return transformable.x + transformable.w; + return AscDesc::ObjectAxis(pos, size); } - inline static double NonAxisNewLower( - const CollisionResult& result, - const TransformableComponent& transformable) + inline static double EntityAxis(const TransformableComponent& transformable) { - return result.newX; + return AscDesc::EntityAxis(transformable.pos, transformable.size); } - inline static double NonAxisNewUpper( - const CollisionResult& result, - const TransformableComponent& transformable) + inline static double ObjectAxis(const TransformableComponent& transformable) { - return result.newX + transformable.w; + return AscDesc::ObjectAxis(transformable.pos, transformable.size); } }; - template - class DetectCollisions : public AscDesc { - public: - - static const Direction Dir = dir; - }; - class Left : public DetectCollisions> { public: @@ -531,23 +449,22 @@ namespace CollisionParams { }; }; -CollisionResult PonderingSystem::detectCollisions( +PonderingSystem::CollisionResult PonderingSystem::detectCollisions( id_type entity, - double x, - double y) + vec2d newPos) { auto& transformable = game_.getEntityManager(). getComponent(entity); CollisionResult result; - result.newX = x; - result.newY = transformable.y; + result.pos.x() = newPos.x(); + result.pos.y() = transformable.pos.y(); // Find horizontal collisions. - if (result.newX < transformable.x) + if (result.pos.x() < transformable.pos.x()) { detectCollisionsInDirection(entity, result); - } else if (result.newX > transformable.x) + } else if (result.pos.x() > transformable.pos.x()) { detectCollisionsInDirection(entity, result); } @@ -555,13 +472,13 @@ CollisionResult PonderingSystem::detectCollisions( // Find vertical collisions if (!result.stopProcessing) { - result.newY = y; + result.pos.y() = newPos.y(); result.touchedWall = false; - if (result.newY < transformable.y) + if (result.pos.y() < transformable.pos.y()) { detectCollisionsInDirection(entity, result); - } else if (result.newY > transformable.y) + } else if (result.pos.y() > transformable.pos.y()) { detectCollisionsInDirection(entity, result); } @@ -586,25 +503,25 @@ void PonderingSystem::detectCollisionsInDirection( getComponent(mapEntity); // Get old location. - auto& transformable = game_.getEntityManager(). + auto& transform = game_.getEntityManager(). getComponent(entity); bool boundaryCollision = false; auto boundaries = Param::MapBoundaries(mappable); - auto it = boundaries.lower_bound(Param::OldAxis(transformable)); + auto it = boundaries.lower_bound(Param::EntityAxis(transform)); // Find the axis distance of the closest environmental boundary. for (; (it != std::end(boundaries)) && Param::AtLeastInAxisSweep( it->first, - Param::NewAxis(result, transformable)); + Param::EntityAxis(result.pos, transform.size)); it++) { // Check that the boundary is in range for the other axis. - if ((Param::NonAxisNewUpper(result, transformable) > it->second.lower) && - (Param::NonAxisNewLower(result, transformable) < it->second.upper)) + if ((Param::NonAxisUpper(result.pos, transform.size) > it->second.lower) && + (Param::NonAxisLower(result.pos) < it->second.upper)) { // We have a collision! boundaryCollision = true; @@ -644,16 +561,16 @@ void PonderingSystem::detectCollisionsInDirection( // Check if the entity would move into the potential collider, if (Param::IsPastAxis( Param::ObjectAxis(colliderTrans), - Param::NewAxis(result, transformable)) && + Param::EntityAxis(result.pos, transform.size)) && // that it wasn't already colliding, !Param::IsPastAxis( Param::ObjectAxis(colliderTrans), - Param::OldAxis(transformable)) && + Param::EntityAxis(transform)) && // that the position on the non-axis is in range, - (Param::NonAxisOldUpper(colliderTrans) > - Param::NonAxisNewLower(result, transformable)) && - (Param::NonAxisOldLower(colliderTrans) < - Param::NonAxisNewUpper(result, transformable)) && + (Param::NonAxisUpper(colliderTrans.pos, colliderTrans.size) > + Param::NonAxisLower(result.pos)) && + (Param::NonAxisLower(colliderTrans.pos) < + Param::NonAxisUpper(result.pos, transform.size)) && // and that the collider is not farther away than the environmental // boundary. (!boundaryCollision || @@ -688,7 +605,7 @@ void PonderingSystem::detectCollisionsInDirection( // Check if the entity would still move into the potential collider. if (!Param::IsPastAxis( Param::ObjectAxis(colliderTrans), - Param::NewAxis(result, transformable))) + Param::EntityAxis(result.pos, transform.size))) { break; } @@ -702,8 +619,8 @@ void PonderingSystem::detectCollisionsInDirection( Param::Dir, colliderPonder.colliderType, Param::ObjectAxis(colliderTrans), - Param::NonAxisOldLower(colliderTrans), - Param::NonAxisOldUpper(colliderTrans), + Param::NonAxisLower(colliderTrans.pos), + Param::NonAxisUpper(colliderTrans.pos, colliderTrans.size), result); if (result.stopProcessing) @@ -724,8 +641,8 @@ void PonderingSystem::detectCollisionsInDirection( (it->first == boundaryAxis); it++) { - if ((Param::NonAxisNewUpper(result, transformable) > it->second.lower) && - (Param::NonAxisNewLower(result, transformable) < it->second.upper)) + if ((Param::NonAxisLower(result.pos) < it->second.upper) && + (Param::NonAxisUpper(result.pos, transform.size) > it->second.lower)) { processCollision( entity, @@ -756,9 +673,6 @@ void PonderingSystem::processCollision( double upper, CollisionResult& result) { - auto& ponderable = game_.getEntityManager(). - getComponent(entity); - auto& transformable = game_.getEntityManager(). getComponent(entity); @@ -823,29 +737,29 @@ void PonderingSystem::processCollision( { case Direction::left: { - result.newX = GAME_WIDTH + WALL_GAP - transformable.w; + result.pos.x() = GAME_WIDTH + WALL_GAP - transformable.size.w(); break; } case Direction::right: { - result.newX = -WALL_GAP; + result.pos.x() = -WALL_GAP; break; } case Direction::up: { - result.newY = - MAP_HEIGHT * TILE_HEIGHT + WALL_GAP - transformable.h; + result.pos.y() = + MAP_HEIGHT * TILE_HEIGHT + WALL_GAP - transformable.pos.h(); break; } case Direction::down: { - result.newY = -WALL_GAP; + result.pos.y() = -WALL_GAP; break; } @@ -905,33 +819,33 @@ void PonderingSystem::processCollision( { case Direction::left: { - result.newX = axis; - ponderable.velX = 0.0; + result.pos.x() = axis; + result.blockedHoriz = true; break; } case Direction::right: { - result.newX = axis - transformable.w; - ponderable.velX = 0.0; + result.pos.x() = axis - transformable.size.w(); + result.blockedHoriz = true; break; } case Direction::up: { - result.newY = axis; - ponderable.velY = 0.0; + result.pos.y() = axis; + result.blockedVert = true; break; } case Direction::down: { - result.newY = axis - transformable.h; + result.pos.y() = axis - transformable.size.h(); + result.blockedVert = true; result.groundEntity = collider; - ponderable.velY = 0.0; result.grounded = true; break; diff --git a/src/systems/pondering.h b/src/systems/pondering.h index abc6db2..273db67 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -4,19 +4,7 @@ #include "system.h" #include "components/ponderable.h" #include "direction.h" - -struct CollisionResult -{ - double newX; - double newY; - bool stopProcessing = false; - bool touchedWall = false; - bool adjacentlyWarping = false; - Direction adjWarpDir; - size_t adjWarpMapId; - bool grounded = false; - EntityManager::id_type groundEntity; -}; +#include "vector.h" class PonderingSystem : public System { public: @@ -47,7 +35,19 @@ public: private: - + struct CollisionResult + { + vec2d pos; + bool stopProcessing = false; + bool touchedWall = false; + bool blockedHoriz = false; + bool blockedVert = false; + bool adjacentlyWarping = false; + Direction adjWarpDir; + size_t adjWarpMapId; + bool grounded = false; + id_type groundEntity; + }; void tickBody( id_type entity, @@ -56,13 +56,11 @@ private: CollisionResult moveBody( id_type entity, - double x, - double y); + vec2d newPos); CollisionResult detectCollisions( id_type entity, - double x, - double y); + vec2d newPos); template void detectCollisionsInDirection( diff --git a/src/systems/realizing.cpp b/src/systems/realizing.cpp index 8e670ac..f9285ad 100644 --- a/src/systems/realizing.cpp +++ b/src/systems/realizing.cpp @@ -93,20 +93,20 @@ void parseAI( if (direction == "left") { - action.speedX = -speed; - action.speedY = 0; + action.speed.x() = -speed; + action.speed.y() = 0; } else if (direction == "right") { - action.speedX = speed; - action.speedY = 0; + action.speed.x() = speed; + action.speed.y() = 0; } else if (direction == "up") { - action.speedX = 0; - action.speedY = -speed; + action.speed.x() = 0; + action.speed.y() = -speed; } else if (direction == "down") { - action.speedX = 0; - action.speedY = speed; + action.speed.x() = 0; + action.speed.y() = speed; } action.dur = length / speed; @@ -186,11 +186,11 @@ EntityManager::id_type RealizingSystem::initSingleton( } key = getProp(top, "startx"); - realizable.startingX = atoi(reinterpret_cast(key)); + realizable.startingPos.x() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(top, "starty"); - realizable.startingY = atoi(reinterpret_cast(key)); + realizable.startingPos.y() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(top, "startmap"); @@ -260,11 +260,11 @@ EntityManager::id_type RealizingSystem::initSingleton( emplaceComponent(mapObject); key = getProp(mapNode, "x"); - transformable.origX = atoi(reinterpret_cast(key)); + transformable.origPos.x() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(mapNode, "y"); - transformable.origY = atoi(reinterpret_cast(key)); + transformable.origPos.y() = atoi(reinterpret_cast(key)); xmlFree(key); // Set the sprite and size using the prototype definition. @@ -273,17 +273,17 @@ EntityManager::id_type RealizingSystem::initSingleton( xmlFree(key); key = getProp(prototypeNode, "width"); - transformable.origW = atoi(reinterpret_cast(key)); + transformable.origSize.w() = atoi(reinterpret_cast(key)); xmlFree(key); key = getProp(prototypeNode, "height"); - transformable.origH = atoi(reinterpret_cast(key)); + transformable.origSize.h() = atoi(reinterpret_cast(key)); xmlFree(key); AnimationSet objectAnim( spritePath.c_str(), - transformable.origW, - transformable.origH, + transformable.origSize.w(), + transformable.origSize.h(), 1); objectAnim.emplaceAnimation("static", 0, 1, 1); @@ -503,10 +503,8 @@ void RealizingSystem::loadMap(id_type mapEntity) auto& transformable = game_.getEntityManager(). getComponent(prototype); - transformable.x = transformable.origX; - transformable.y = transformable.origY; - transformable.w = transformable.origW; - transformable.h = transformable.origH; + transformable.pos = transformable.origPos; + transformable.size = transformable.origSize; } if (game_.getEntityManager().hasComponent(prototype)) diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 0000000..3abd98a --- /dev/null +++ b/src/vector.h @@ -0,0 +1,111 @@ +#ifndef COORDINATES_H_A45D34FB +#define COORDINATES_H_A45D34FB + +template +class vec2 { +public: + + T coords[2]; + + vec2() = default; + + vec2(double x, double y) : coords{x, y} + { + } + + inline T& x() + { + return coords[0]; + } + + inline const T& x() const + { + return coords[0]; + } + + inline T& w() + { + return coords[0]; + } + + inline const T& w() const + { + return coords[0]; + } + + inline T& y() + { + return coords[1]; + } + + inline const T& y() const + { + return coords[1]; + } + + inline T& h() + { + return coords[1]; + } + + inline const T& h() const + { + return coords[1]; + } + + template + operator vec2() const + { + return vec2(x(), y()); + } + + vec2 operator+(const vec2& other) const + { + return vec2(x() + other.x(), y() + other.y()); + } + + vec2& operator+=(const vec2& other) + { + x() += other.x(); + y() += other.y(); + + return *this; + } + + vec2 operator-(const vec2& other) const + { + return vec2(x() - other.x(), y() - other.y()); + } + + vec2 operator-=(const vec2& other) + { + x() -= other.x(); + y() -= other.y(); + + return *this; + } + + vec2 operator-() const + { + return vec2(-x(), -y()); + } + + vec2 operator*(double s) const + { + return vec2(x() * s, y() * s); + } + + vec2& operator*=(double s) + { + x() *= s; + y() *= s; + + return *this; + } + +}; + +using vec2d = vec2; +using vec2i = vec2; + +#endif /* end of include guard: COORDINATES_H_A45D34FB */ -- cgit 1.4.1