4 #include "../graphics/const.h"
14 World::World(Vector<int> size)
16 , count(size.x * size.y)
25 void World::Update(float dt) {
26 for (Entity &e : entities) {
28 e.Update(dt, Vector<float>(), terminal);
31 e.Update(dt, gravity, terminal);
34 BoundsCollision(e, dt);
40 void World::BoundsCollision(Entity &e, float dt) {
41 if (e.vbox.Top() < 0) {
42 e.Move(Vector<float>(0, -e.vbox.Top()));
47 if (e.vbox.Bottom() > size.y) {
48 e.Move(Vector<float>(0, size.y - e.vbox.Bottom()));
54 if (e.hbox.Right() > size.x) {
55 e.Move(Vector<float>(size.x - e.hbox.Right(), 0));
60 if (e.hbox.Left() < 0) {
61 e.Move(Vector<float>(-e.hbox.Left(), 0));
68 void World::TileCollision(Entity &e, float dt) {
69 Vector<float> response;
72 for (int x = e.vbox.Left(), y = e.vbox.Top(),
73 end = std::ceil(e.vbox.Right()); x < end; ++x) {
74 const Tile &tile = TileAt(Vector<int>(x, y));
76 response.y = y + 1 - e.vbox.Top();
85 for (int x = e.vbox.Left(), y = e.vbox.Bottom(),
86 end = std::ceil(e.vbox.Right()); x < end; ++x) {
87 const Tile &tile = TileAt(Vector<int>(x, y));
89 response.y = y - e.vbox.Bottom();
97 if (response.y <= 0) {
99 // due to the hbox's huge bottom gap
100 for (int x = e.vbox.Left(), y = e.vbox.Bottom() - 1,
101 end = std::ceil(e.vbox.Right()); x < end; ++x) {
102 const Tile &tile = TileAt(Vector<int>(x, y));
103 if (tile.IsSolid()) {
115 for (int y = e.hbox.Top(), x = e.hbox.Left(),
116 end = std::ceil(e.hbox.Bottom()); y < end; ++y) {
117 const Tile &tile = TileAt(Vector<int>(x, y));
118 if (tile.IsSolid()) {
119 response.x = x + 1 - e.hbox.Left();
128 for (int y = e.hbox.Top(), x = e.hbox.Right(),
129 end = std::ceil(e.hbox.Bottom()); y < end; ++y) {
130 const Tile &tile = TileAt(Vector<int>(x, y));
131 if (tile.IsSolid()) {
132 response.x = x - e.hbox.Right();
140 if (response.x > fixSpeed * dt) {
141 response.x = fixSpeed * dt;
142 } else if (response.x < -fixSpeed * dt) {
143 response.x = -fixSpeed * dt;
145 if (response.y > fixSpeed * dt) {
146 response.y = fixSpeed * dt;
147 } else if (response.y < -fixSpeed * dt) {
148 response.y = -fixSpeed * dt;
153 void World::EntityCollision() {
154 if (entities.size() <= 1) return;
156 auto firstEnd(entities.end());
158 for (auto first(entities.begin()); first != firstEnd; ++first) {
161 for (auto secondEnd(entities.end()); second != secondEnd; ++second) {
162 if (first->bounds.Intersects(second->bounds)) {
163 // damage + knockback
170 Entity &World::AddEntity(const Entity &e) {
171 entities.emplace_back(e);
172 return entities.back();