]> git.localhorst.tv Git - orbi.git/commitdiff
switch world entities from instance to pointer
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 7 May 2014 18:34:37 +0000 (20:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 7 May 2014 18:34:55 +0000 (20:34 +0200)
src/app/Application.cpp
src/orbi.cpp
src/world/World.cpp
src/world/World.h

index 76ded2db2c9b15bc35ba637bf5567ccdfee369d4..3ba8485db63554d012f05fd0de5a56ca9b37f283 100644 (file)
@@ -194,29 +194,29 @@ void Application::RenderEntities() {
        constexpr Color vboxColor(0xFA, 0xFA, 0x00);
        constexpr Color hboxColor(0x00, 0xFA, 0x00);
 
-       for (const Entity &e : world.Entities()) {
+       for (const Entity *e : world.Entities()) {
                canvas.SetColor(boundsColor);
                canvas.OutlineRect(
-                       cam.ToScreen(Vector<float>(e.bounds.Left(), e.bounds.Top())),
-                       cam.ToScale(Vector<float>(e.bounds.Size()))
+                       cam.ToScreen(Vector<float>(e->bounds.Left(), e->bounds.Top())),
+                       cam.ToScale(Vector<float>(e->bounds.Size()))
                );
                canvas.SetColor(vboxColor);
                canvas.Line(
-                       cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Top())),
-                       cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Top())) - Vector<int>(1, 0)
+                       cam.ToScreen(Vector<float>(e->vbox.Left(), e->vbox.Top())),
+                       cam.ToScreen(Vector<float>(e->vbox.Right(), e->vbox.Top())) - Vector<int>(1, 0)
                );
                canvas.Line(
-                       cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Bottom())) - Vector<int>(0, 1),
-                       cam.ToScreen(Vector<float>(e.vbox.Right(), e.vbox.Bottom())) - Vector<int>(1, 1)
+                       cam.ToScreen(Vector<float>(e->vbox.Left(), e->vbox.Bottom())) - Vector<int>(0, 1),
+                       cam.ToScreen(Vector<float>(e->vbox.Right(), e->vbox.Bottom())) - Vector<int>(1, 1)
                );
                canvas.SetColor(hboxColor);
                canvas.Line(
-                       cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Top())),
-                       cam.ToScreen(Vector<float>(e.hbox.Left(), e.hbox.Bottom())) - Vector<int>(0, 1)
+                       cam.ToScreen(Vector<float>(e->hbox.Left(), e->hbox.Top())),
+                       cam.ToScreen(Vector<float>(e->hbox.Left(), e->hbox.Bottom())) - Vector<int>(0, 1)
                );
                canvas.Line(
-                       cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Top())) - Vector<int>(1, 0),
-                       cam.ToScreen(Vector<float>(e.hbox.Right(), e.hbox.Bottom())) - Vector<int>(1, 1)
+                       cam.ToScreen(Vector<float>(e->hbox.Right(), e->hbox.Top())) - Vector<int>(1, 0),
+                       cam.ToScreen(Vector<float>(e->hbox.Right(), e->hbox.Bottom())) - Vector<int>(1, 1)
                );
        }
 }
index 91974974704d67bd939023a8c9fe468af36fa733..67409e6888f9122a0aeca50389ed24ba44c1f59b 100644 (file)
@@ -57,7 +57,7 @@ int main(int argc, const char *argv[]) {
        e.vbox = AABB(.1, 0, 1.8, 3);
        e.hbox = AABB(0, .1, 2, 1.8);
        e.Move(Vector<float>(5, 0));
-       Entity &player = world.AddEntity(e);
+       world.AddEntity(e);
 
        Entity mob;
        mob.bounds = AABB(0, 0, 2, 1.5);
@@ -67,7 +67,7 @@ int main(int argc, const char *argv[]) {
        world.AddEntity(mob);
 
        Application app(canv, world, tiles);
-       app.Control(player);
+       app.Control(e);
        app.Run();
 
        return 0;
index faf7da910b5a34420bf5fdc13c55d5c7f625ed36..a198ccc77a2f06b17ea49c6b4806f392f3d6448a 100644 (file)
@@ -1,6 +1,7 @@
 #include "World.h"
 
 #include "Collision.h"
+#include "Entity.h"
 #include "../graphics/const.h"
 
 #include <algorithm>
@@ -23,16 +24,16 @@ World::World(Vector<int> size)
 
 
 void World::Update(float dt) {
-       for (Entity &e : entities) {
-               if (e.onGround) {
-                       e.Update(dt, Vector<float>(), 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);
+                       e->Update(dt, gravity, terminal);
                }
 
-               BoundsCollision(e, dt);
-               TileCollision(e, dt);
+               BoundsCollision(*e, dt);
+               TileCollision(*e, dt);
 
        }
 }
@@ -159,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
                        }
                }
@@ -167,9 +168,4 @@ void World::EntityCollision() {
 }
 
 
-Entity &World::AddEntity(const Entity &e) {
-       entities.emplace_back(e);
-       return entities.back();
-}
-
 }
index 6369757743d66c4d9d61a2ad04d1912e861995b0..66d0d18c7b88fafe93e31346acfa30bdeb54eed8 100644 (file)
@@ -1,8 +1,6 @@
 #ifndef ORBI_WORLD_H_
 #define ORBI_WORLD_H_
 
-#include "AABB.h"
-#include "Entity.h"
 #include "Tile.h"
 #include "../graphics/Vector.h"
 
@@ -12,6 +10,8 @@
 
 namespace orbi {
 
+class Entity;
+
 class World {
 
 public:
@@ -32,8 +32,8 @@ public:
        const Tile &TileAt(Vector<int> pos) const { return tiles[Index(pos)]; }
        void SetTile(Vector<int> pos, const Tile &t) { tiles[Index(pos)] = t; }
 
-       const std::list<Entity> &Entities() const { return entities; }
-       Entity &AddEntity(const Entity &);
+       const std::list<Entity *> &Entities() const { return entities; }
+       void AddEntity(Entity &e) { entities.push_back(&e); }
 
 private:
        void BoundsCollision(Entity &, float dt);
@@ -50,9 +50,7 @@ private:
 
        std::vector<Tile> tiles;
 
-       std::list<Entity> entities;
-
-       mutable AABB tileShape;
+       std::list<Entity *> entities;
 
 };