]> git.localhorst.tv Git - orbi.git/blob - src/world/World.cpp
e390914fae7eb34adccc08de26649c697fe15165
[orbi.git] / src / world / World.cpp
1 #include "World.h"
2
3 #include "../graphics/const.h"
4
5
6 namespace orbi {
7
8 World::World(Vector<int> size)
9 : size(size)
10 , count(size.x * size.y)
11 , gravity(0, 5)
12 , terminal(50, 50)
13 , tiles(count) {
14
15 }
16
17
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);
23
24                         const AABB &b = e.Bounds();
25
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));
31
32                         const Vector<int> cBegin(b.Left(), b.Top());
33                         const Vector<int> cEnd(b.Right(), b.Bottom());
34
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());
39                                         break;
40                                 }
41                         }
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());
46                                         break;
47                                 }
48                         }
49                         if (!IsZero(topResponse)) {
50                                 if (IsZero(bottomResponse)) {
51                                         e.Move(topResponse);
52                                 }
53                         } else if (!IsZero(bottomResponse)) {
54                                 e.Move(bottomResponse);
55                         }
56
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);
61                                         break;
62                                 }
63                         }
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);
68                                         break;
69                                 }
70                         }
71                         if (!IsZero(leftResponse)) {
72                                 if (IsZero(rightResponse)) {
73                                         e.Move(leftResponse);
74                                 }
75                         } else if (!IsZero(rightResponse)) {
76                                 e.Move(rightResponse);
77                         }
78                 }
79         }
80 }
81
82
83 Entity &World::AddEntity(const Entity &e) {
84         entities.emplace_back(e);
85         return entities.back();
86 }
87
88 }