5 #include "../graphics/const.h"
15 World::World(Vector<int> size)
17 , count(size.x * size.y)
26 void World::Update(float dt) {
27 for (Entity *e : entities) {
29 e->Update(dt, Vector<float>(), terminal);
32 e->Update(dt, gravity, terminal);
35 BoundsCollision(*e, dt);
36 TileCollision(*e, dt);
41 void World::BoundsCollision(Entity &e, float dt) {
42 if (e.vbox.Top() < 0) {
43 e.Move(Vector<float>(0, -e.vbox.Top()));
48 if (e.vbox.Bottom() > size.y) {
49 e.Move(Vector<float>(0, size.y - e.vbox.Bottom()));
55 if (e.hbox.Right() > size.x) {
56 e.Move(Vector<float>(size.x - e.hbox.Right(), 0));
61 if (e.hbox.Left() < 0) {
62 e.Move(Vector<float>(-e.hbox.Left(), 0));
69 void World::TileCollision(Entity &e, float dt) {
70 Vector<float> response;
73 for (int x = e.vbox.Left(), y = e.vbox.Top(),
74 end = std::ceil(e.vbox.Right()); x < end; ++x) {
75 const Tile &tile = TileAt(Vector<int>(x, y));
77 response.y = y + 1 - e.vbox.Top();
86 for (int x = e.vbox.Left(), y = e.vbox.Bottom(),
87 end = std::ceil(e.vbox.Right()); x < end; ++x) {
88 const Tile &tile = TileAt(Vector<int>(x, y));
90 response.y = y - e.vbox.Bottom();
98 if (response.y <= 0) {
100 // due to the hbox's huge bottom gap
101 for (int x = e.vbox.Left(), y = e.vbox.Bottom() - 1,
102 end = std::ceil(e.vbox.Right()); x < end; ++x) {
103 const Tile &tile = TileAt(Vector<int>(x, y));
104 if (tile.IsSolid()) {
116 for (int y = e.hbox.Top(), x = e.hbox.Left(),
117 end = std::ceil(e.hbox.Bottom()); y < end; ++y) {
118 const Tile &tile = TileAt(Vector<int>(x, y));
119 if (tile.IsSolid()) {
120 response.x = x + 1 - e.hbox.Left();
129 for (int y = e.hbox.Top(), x = e.hbox.Right(),
130 end = std::ceil(e.hbox.Bottom()); y < end; ++y) {
131 const Tile &tile = TileAt(Vector<int>(x, y));
132 if (tile.IsSolid()) {
133 response.x = x - e.hbox.Right();
141 if (response.x > fixSpeed * dt) {
142 response.x = fixSpeed * dt;
143 } else if (response.x < -fixSpeed * dt) {
144 response.x = -fixSpeed * dt;
146 if (response.y > fixSpeed * dt) {
147 response.y = fixSpeed * dt;
148 } else if (response.y < -fixSpeed * dt) {
149 response.y = -fixSpeed * dt;
154 void World::EntityCollision() {
155 if (entities.size() <= 1) return;
157 auto firstEnd(entities.end());
159 for (auto first(entities.begin()); first != firstEnd; ++first) {
162 for (auto secondEnd(entities.end()); second != secondEnd; ++second) {
163 if ((*first)->bounds.Intersects((*second)->bounds)) {
164 // damage + knockback