X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2FWorld.cpp;h=a198ccc77a2f06b17ea49c6b4806f392f3d6448a;hb=a73a6dd63407b5f5ef5b0c635551ad27b27c95d6;hp=088712acae45ac2720322954a7f68eac3216fc25;hpb=7cf057b5b3a28c3896af27cb725fbf0b4f1459c2;p=orbi.git diff --git a/src/world/World.cpp b/src/world/World.cpp index 088712a..a198ccc 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,6 +1,7 @@ #include "World.h" #include "Collision.h" +#include "Entity.h" #include "../graphics/const.h" #include @@ -23,16 +24,16 @@ World::World(Vector size) void World::Update(float dt) { - for (Entity &e : entities) { - if (e.onGround) { - e.Update(dt, Vector(), terminal); - e.onGround = false; + for (Entity *e : entities) { + if (e->onGround) { + e->Update(dt, Vector(), terminal); + e->onGround = false; } else { - e.Update(dt, gravity, terminal); + e->Update(dt, gravity, terminal); } - BoundsCollision(e, dt); - TileCollision(e, dt); + BoundsCollision(*e, dt); + TileCollision(*e, dt); } } @@ -40,20 +41,28 @@ void World::Update(float dt) { void World::BoundsCollision(Entity &e, float dt) { if (e.vbox.Top() < 0) { e.Move(Vector(0, -e.vbox.Top())); - e.vel.y = 0; + if (e.vel.y < 0) { + e.vel.y = 0; + } } if (e.vbox.Bottom() > size.y) { e.Move(Vector(0, size.y - e.vbox.Bottom())); - e.vel.y = 0; + if (e.vel.y > 0) { + e.vel.y = 0; + } e.onGround = true; } if (e.hbox.Right() > size.x) { e.Move(Vector(size.x - e.hbox.Right(), 0)); - e.vel.x = 0; + if (e.vel.x > 0) { + e.vel.x = 0; + } } if (e.hbox.Left() < 0) { e.Move(Vector(-e.hbox.Left(), 0)); - e.vel.x = 0; + if (e.vel.x < 0) { + e.vel.x = 0; + } } } @@ -66,7 +75,9 @@ void World::TileCollision(Entity &e, float dt) { const Tile &tile = TileAt(Vector(x, y)); if (tile.IsSolid()) { response.y = y + 1 - e.vbox.Top(); - e.vel.y = 0; + if (e.vel.y < 0) { + e.vel.y = 0; + } break; } } @@ -78,7 +89,9 @@ void World::TileCollision(Entity &e, float dt) { if (tile.IsSolid()) { response.y = y - e.vbox.Bottom(); e.onGround = true; - e.vel.y = 0; + if (e.vel.y > 0) { + e.vel.y = 0; + } break; } } @@ -91,7 +104,9 @@ void World::TileCollision(Entity &e, float dt) { if (tile.IsSolid()) { response.y = -1; e.onGround = true; - e.vel.y = 0; + if (e.vel.y > 0) { + e.vel.y = 0; + } break; } } @@ -103,7 +118,9 @@ void World::TileCollision(Entity &e, float dt) { const Tile &tile = TileAt(Vector(x, y)); if (tile.IsSolid()) { response.x = x + 1 - e.hbox.Left(); - e.vel.x = 0; + if (e.vel.x < 0) { + e.vel.x = 0; + } break; } } @@ -114,7 +131,9 @@ void World::TileCollision(Entity &e, float dt) { const Tile &tile = TileAt(Vector(x, y)); if (tile.IsSolid()) { response.x = x - e.hbox.Right(); - e.vel.x = 0; + if (e.vel.x > 0) { + e.vel.x = 0; + } break; } } @@ -141,7 +160,7 @@ void World::EntityCollision() { auto second(first); ++second; for (auto secondEnd(entities.end()); second != secondEnd; ++second) { - if (first->bounds.Intersects(second->bounds)) { + if ((*first)->bounds.Intersects((*second)->bounds)) { // damage + knockback } } @@ -149,9 +168,4 @@ void World::EntityCollision() { } -Entity &World::AddEntity(const Entity &e) { - entities.emplace_back(e); - return entities.back(); -} - }