From aa28d59d492c490194e8e8f7af086fcb531d170a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 7 May 2014 20:34:37 +0200 Subject: [PATCH] switch world entities from instance to pointer --- src/app/Application.cpp | 22 +++++++++++----------- src/orbi.cpp | 4 ++-- src/world/World.cpp | 22 +++++++++------------- src/world/World.h | 12 +++++------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 76ded2d..3ba8485 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -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(e.bounds.Left(), e.bounds.Top())), - cam.ToScale(Vector(e.bounds.Size())) + cam.ToScreen(Vector(e->bounds.Left(), e->bounds.Top())), + cam.ToScale(Vector(e->bounds.Size())) ); canvas.SetColor(vboxColor); canvas.Line( - cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Top())), - cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Top())) - Vector(1, 0) + cam.ToScreen(Vector(e->vbox.Left(), e->vbox.Top())), + cam.ToScreen(Vector(e->vbox.Right(), e->vbox.Top())) - Vector(1, 0) ); canvas.Line( - cam.ToScreen(Vector(e.vbox.Left(), e.vbox.Bottom())) - Vector(0, 1), - cam.ToScreen(Vector(e.vbox.Right(), e.vbox.Bottom())) - Vector(1, 1) + cam.ToScreen(Vector(e->vbox.Left(), e->vbox.Bottom())) - Vector(0, 1), + cam.ToScreen(Vector(e->vbox.Right(), e->vbox.Bottom())) - Vector(1, 1) ); canvas.SetColor(hboxColor); canvas.Line( - cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Top())), - cam.ToScreen(Vector(e.hbox.Left(), e.hbox.Bottom())) - Vector(0, 1) + cam.ToScreen(Vector(e->hbox.Left(), e->hbox.Top())), + cam.ToScreen(Vector(e->hbox.Left(), e->hbox.Bottom())) - Vector(0, 1) ); canvas.Line( - cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Top())) - Vector(1, 0), - cam.ToScreen(Vector(e.hbox.Right(), e.hbox.Bottom())) - Vector(1, 1) + cam.ToScreen(Vector(e->hbox.Right(), e->hbox.Top())) - Vector(1, 0), + cam.ToScreen(Vector(e->hbox.Right(), e->hbox.Bottom())) - Vector(1, 1) ); } } diff --git a/src/orbi.cpp b/src/orbi.cpp index 9197497..67409e6 100644 --- a/src/orbi.cpp +++ b/src/orbi.cpp @@ -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(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; diff --git a/src/world/World.cpp b/src/world/World.cpp index faf7da9..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); } } @@ -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(); -} - } diff --git a/src/world/World.h b/src/world/World.h index 6369757..66d0d18 100644 --- a/src/world/World.h +++ b/src/world/World.h @@ -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 pos) const { return tiles[Index(pos)]; } void SetTile(Vector pos, const Tile &t) { tiles[Index(pos)] = t; } - const std::list &Entities() const { return entities; } - Entity &AddEntity(const Entity &); + const std::list &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 tiles; - std::list entities; - - mutable AABB tileShape; + std::list entities; }; -- 2.39.2