From 8142a9c87a13cecc7a3698e877f24d89f128c074 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 21 Apr 2018 14:50:52 -0400 Subject: Started working on prototype objects --- src/systems/mapping.cpp | 14 ++++++++ src/systems/playing.cpp | 33 +++++++++++++++++- src/systems/playing.h | 2 ++ src/systems/pondering.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++--- src/systems/pondering.h | 9 ++++- 5 files changed, 139 insertions(+), 7 deletions(-) (limited to 'src/systems') diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index a3a17ec..c7c2f9d 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp @@ -195,4 +195,18 @@ void MappingSystem::loadMap(size_t mapId) MappableComponent::Boundary::Type::danger); } } + + for (const Map::Object& object : map.getObjects()) + { + const Prototype& prototype = game_.getPrototypeManager(). + getPrototype(object.getType()); + + id_type emplacedObject = prototype.instantiate( + game_, + object.getX(), + object.getY(), + object.getItems()); + + mappable.getInstances()[object.getIndex()] = emplacedObject; + } } diff --git a/src/systems/playing.cpp b/src/systems/playing.cpp index 40d9706..5077f8a 100644 --- a/src/systems/playing.cpp +++ b/src/systems/playing.cpp @@ -5,6 +5,7 @@ #include "components/playable.h" #include "components/controllable.h" #include "components/orientable.h" +#include "components/mappable.h" #include "systems/mapping.h" #include "systems/pondering.h" #include "systems/orienting.h" @@ -84,7 +85,8 @@ void PlayingSystem::initPlayer() game_.getSystemManager().getSystem().initializeBody( player, - PonderableComponent::Type::freefalling); + PonderableComponent::BodyType::freefalling, + PonderableComponent::ColliderType::player); game_.getEntityManager().emplaceComponent(player); game_.getEntityManager().emplaceComponent(player); @@ -173,3 +175,32 @@ void PlayingSystem::die() }); } } + +void PlayingSystem::save() +{ + playSound("res/Pickup_Coin23.wav", 0.25); + + auto players = game_.getEntityManager().getEntitiesWithComponents< + TransformableComponent, + PlayableComponent>(); + + auto maps = game_.getEntityManager().getEntitiesWithComponents< + MappableComponent>(); + + auto& mappable = game_.getEntityManager(). + getComponent(*maps.begin()); + + for (id_type player : players) + { + auto& transformable = game_.getEntityManager(). + getComponent(player); + + auto& playable = game_.getEntityManager(). + getComponent(player); + + playable.checkpointMapId = mappable.getMapId(); + playable.checkpointX = transformable.getX(); + playable.checkpointY = transformable.getY(); + playable.checkpointObjectActivated = false; + } +} diff --git a/src/systems/playing.h b/src/systems/playing.h index ff16808..d7f5072 100644 --- a/src/systems/playing.h +++ b/src/systems/playing.h @@ -23,6 +23,8 @@ public: void die(); + void save(); + }; #endif /* end of include guard: PLAYING_H_70A54F7D */ diff --git a/src/systems/pondering.cpp b/src/systems/pondering.cpp index 02d5cfc..9115988 100644 --- a/src/systems/pondering.cpp +++ b/src/systems/pondering.cpp @@ -39,7 +39,7 @@ void PonderingSystem::tick(double dt) ponderable.setVelocityY( ponderable.getVelocityY() + ponderable.getAccelY() * dt); - if ((ponderable.getType() == PonderableComponent::Type::freefalling) + if ((ponderable.getBodyType() == PonderableComponent::BodyType::freefalling) && (ponderable.getVelocityY() > TERMINAL_VELOCITY)) { ponderable.setVelocityY(TERMINAL_VELOCITY); @@ -58,7 +58,7 @@ void PonderingSystem::tick(double dt) std::priority_queue collisions; - // Find collisions + // Find map collisions for (id_type mapEntity : maps) { auto& mappable = game_.getEntityManager(). @@ -366,6 +366,51 @@ void PonderingSystem::tick(double dt) } } + // Find body collisions + for (id_type body : entities) + { + // Can't collide with self + if (body == entity) + { + continue; + } + + // Make sure the body is collidable + auto& colliderPonderable = + game_.getEntityManager().getComponent(body); + + if (!colliderPonderable.isCollidable()) + { + continue; + } + + // Test if the body was already colliding + auto& colliderTransformable = + game_.getEntityManager().getComponent(body); + + if ((oldRight > colliderTransformable.getX()) + && (oldX < colliderTransformable.getX() + colliderTransformable.getW()) + && (oldBottom > colliderTransformable.getY()) + && (oldY < colliderTransformable.getY() + colliderTransformable.getH())) + { + continue; + } + + // Test if there is a new collision + if (!((newX + transformable.getW() > colliderTransformable.getX()) + && (newX < colliderTransformable.getX() + colliderTransformable.getW()) + && (newY + transformable.getH() > colliderTransformable.getY()) + && (newY < + colliderTransformable.getY() + colliderTransformable.getH()))) + { + continue; + } + + // Process the collision + processBodyCollision(entity, body); + processBodyCollision(body, entity); + } + // Move transformable.setX(newX); transformable.setY(newY); @@ -399,13 +444,46 @@ void PonderingSystem::tick(double dt) void PonderingSystem::initializeBody( id_type entity, - PonderableComponent::Type type) + PonderableComponent::BodyType bodyType, + PonderableComponent::ColliderType colliderType) { auto& ponderable = game_.getEntityManager(). - emplaceComponent(entity, type); + emplaceComponent(entity, bodyType, colliderType); - if (type == PonderableComponent::Type::freefalling) + if (bodyType == PonderableComponent::BodyType::freefalling) { ponderable.setAccelY(NORMAL_GRAVITY); } } + +void PonderingSystem::processBodyCollision(id_type body, id_type collider) +{ + auto& bodyPonderable = game_.getEntityManager(). + getComponent(body); + + auto& colliderPonderable = game_.getEntityManager(). + getComponent(collider); + + switch (colliderPonderable.getColliderType()) + { + case PonderableComponent::ColliderType::event: + { + auto& callback = colliderPonderable. + getEventCallback(bodyPonderable.getColliderType()); + + if (callback) + { + callback(game_); + } + + break; + } + + default: + { + // Do nothing. + + break; + } + } +} diff --git a/src/systems/pondering.h b/src/systems/pondering.h index d70525b..7e342df 100644 --- a/src/systems/pondering.h +++ b/src/systems/pondering.h @@ -14,7 +14,14 @@ public: void tick(double dt); - void initializeBody(id_type entity, PonderableComponent::Type type); + void initializeBody( + id_type entity, + PonderableComponent::BodyType bodyType, + PonderableComponent::ColliderType colliderType); + +private: + + void processBodyCollision(id_type body, id_type collider); }; -- cgit 1.4.1