3 #include "../graphics/const.h"
8 World::World(Vector<int> size)
10 , count(size.x * size.y)
18 void World::Update(int delta) {
19 for (int i = 0; i < delta; ++i) {
20 const float dt = 1e-3;
21 for (Entity &e : entities) {
22 e.Update(dt, gravity, terminal);
24 const AABB &b = e.Bounds();
26 // world bounds collision
27 if (b.Top() < 0) e.Move(Vector<float>(0, -b.Top()));
28 if (b.Right() > size.x) e.Move(Vector<float>(size.x - b.Right(), 0));
29 if (b.Bottom() > size.y) e.Move(Vector<float>(0, size.y - b.Bottom()));
30 if (b.Left() < 0) e.Move(Vector<float>(-b.Left(), 0));
32 const Vector<int> cBegin(b.Left(), b.Top());
33 const Vector<int> cEnd(b.Right(), b.Bottom());
35 Vector<float> topResponse;
36 for (Vector<int> pos(cBegin); pos.x < cEnd.x; ++pos.x) {
37 if (TileAt(pos).IsSolid()) {
38 topResponse = Vector<float>(0, pos.y + 1 - b.Top());
42 Vector<float> bottomResponse;
43 for (Vector<int> pos(cBegin.x, cEnd.y); pos.x < cEnd.x; ++pos.x) {
44 if (TileAt(pos).IsSolid()) {
45 bottomResponse = Vector<float>(0, pos.y - b.Bottom());
49 if (!IsZero(topResponse)) {
50 if (IsZero(bottomResponse)) {
53 } else if (!IsZero(bottomResponse)) {
54 e.Move(bottomResponse);
57 Vector<float> leftResponse;
58 for (Vector<int> pos(cBegin); pos.y < cEnd.y; ++pos.y) {
59 if (TileAt(pos).IsSolid()) {
60 leftResponse = Vector<float>(pos.x + 1 - b.Left(), 0);
64 Vector<float> rightResponse;
65 for (Vector<int> pos(cEnd.x, cBegin.y); pos.y < cEnd.y; ++pos.y) {
66 if (TileAt(pos).IsSolid()) {
67 rightResponse = Vector<float>(pos.x - b.Right(), 0);
71 if (!IsZero(leftResponse)) {
72 if (IsZero(rightResponse)) {
75 } else if (!IsZero(rightResponse)) {
76 e.Move(rightResponse);
83 Entity &World::AddEntity(const Entity &e) {
84 entities.emplace_back(e);
85 return entities.back();