]> git.localhorst.tv Git - orbi.git/blobdiff - src/world/World.cpp
switch world entities from instance to pointer
[orbi.git] / src / world / World.cpp
index c76f8608bb82b30c4aaadda76413658df2fe0999..a198ccc77a2f06b17ea49c6b4806f392f3d6448a 100644 (file)
@@ -1,11 +1,14 @@
 #include "World.h"
 
 #include "Collision.h"
+#include "Entity.h"
 #include "../graphics/const.h"
 
 #include <algorithm>
 #include <cmath>
 
+using namespace std;
+
 
 namespace orbi {
 
@@ -21,12 +24,16 @@ World::World(Vector<int> size)
 
 
 void World::Update(float dt) {
-       for (Entity &e : entities) {
-               e.Update(dt, gravity, terminal);
-               e.onGround = false;
+       for (Entity *e : entities) {
+               if (e->onGround) {
+                       e->Update(dt, Vector<float>(), terminal);
+                       e->onGround = false;
+               } else {
+                       e->Update(dt, gravity, terminal);
+               }
 
-               BoundsCollision(e, dt);
-               TileCollision(e, dt);
+               BoundsCollision(*e, dt);
+               TileCollision(*e, dt);
 
        }
 }
@@ -34,20 +41,28 @@ void World::Update(float dt) {
 void World::BoundsCollision(Entity &e, float dt) {
        if (e.vbox.Top() < 0) {
                e.Move(Vector<float>(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<float>(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<float>(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<float>(-e.hbox.Left(), 0));
-               e.vel.x = 0;
+               if (e.vel.x < 0) {
+                       e.vel.x = 0;
+               }
        }
 }
 
@@ -60,7 +75,9 @@ void World::TileCollision(Entity &e, float dt) {
                const Tile &tile = TileAt(Vector<int>(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;
                }
        }
@@ -72,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;
                }
        }
@@ -85,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;
                        }
                }
@@ -97,7 +118,9 @@ void World::TileCollision(Entity &e, float dt) {
                const Tile &tile = TileAt(Vector<int>(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;
                }
        }
@@ -108,7 +131,9 @@ void World::TileCollision(Entity &e, float dt) {
                const Tile &tile = TileAt(Vector<int>(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;
                }
        }
@@ -126,16 +151,21 @@ void World::TileCollision(Entity &e, float dt) {
        e.Move(response);
 }
 
-
-const AABB &World::TileShapeAt(Vector<int> pos) const {
-       tileShape = AABB(pos, Vector<float>(1, 1));
-       return tileShape;
+void World::EntityCollision() {
+       if (entities.size() <= 1) return;
+
+       auto firstEnd(entities.end());
+       --firstEnd;
+       for (auto first(entities.begin()); first != firstEnd; ++first) {
+               auto second(first);
+               ++second;
+               for (auto secondEnd(entities.end()); second != secondEnd; ++second) {
+                       if ((*first)->bounds.Intersects((*second)->bounds)) {
+                               // damage + knockback
+                       }
+               }
+       }
 }
 
 
-Entity &World::AddEntity(const Entity &e) {
-       entities.emplace_back(e);
-       return entities.back();
-}
-
 }