}
void Application::RenderEntities() {
- constexpr Color vboxColor(0xFA, 0x00, 0x00);
+ constexpr Color boundsColor(0xFA, 0x00, 0x00);
+ constexpr Color vboxColor(0xFA, 0xFA, 0x00);
constexpr Color hboxColor(0x00, 0xFA, 0x00);
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()))
+ );
canvas.SetColor(vboxColor);
canvas.Line(
cam.ToScreen(Vector<float>(e.vbox.Left(), e.vbox.Top())),
world.SetTile(Vector<int>(3, 9), Tile(0));
Entity e;
- e.vbox = AABB(Vector<float>(.1, 0), Vector<float>(1.8, 3));
- e.hbox = AABB(Vector<float>(0, .1), Vector<float>(2, 1.8));
+ e.bounds = AABB(0, 0, 2, 3);
+ 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);
+ Entity mob;
+ mob.bounds = AABB(0, 0, 2, 1.5);
+ mob.vbox = mob.bounds;
+ mob.hbox = mob.bounds;
+ mob.Move(Vector<float>(1, 0));
+ world.AddEntity(mob);
+
Application app(canv, world, tiles);
app.Control(player);
app.Run();
namespace orbi {
-bool AABB::Intersects(const AABB &other, Collision &coll) const {
+bool AABB::Intersects(const AABB &other) const {
if (Bottom() < other.Top()) return false;
if (other.Bottom() < Top()) return false;
if (Right() < other.Left()) return false;
if (other.Right() < Left()) return false;
+ return true;
+}
+
+bool AABB::Intersects(const AABB &other, Collision &coll) const {
+ if (!Intersects(other)) return false;
AABB diff;
diff.lt = max(lt, other.lt);
void Move(Vector<float> delta) { lt += delta; rb += delta; }
void Resize(Vector<float> size) { *this = AABB(lt, size); }
+ bool Intersects(const AABB &other) const;
bool Intersects(const AABB &other, Collision &) const;
private:
}
void Entity::Move(Vector<float> delta) {
+ bounds.Move(delta);
vbox.Move(delta);
hbox.Move(delta);
}
void Move(Vector<float> delta);
public:
+ AABB bounds;
AABB vbox;
AABB hbox;
Vector<float> vel;
#include <algorithm>
#include <cmath>
+using namespace std;
+
namespace orbi {
e.Move(response);
}
+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
+ }
+ }
+ }
+}
+
const AABB &World::TileShapeAt(Vector<int> pos) const {
tileShape = AABB(pos, Vector<float>(1, 1));
private:
void BoundsCollision(Entity &, float dt);
void TileCollision(Entity &, float dt);
+ void EntityCollision();
private:
Vector<int> size;