]> git.localhorst.tv Git - orbi.git/blob - src/world/World.cpp
some cleanup
[orbi.git] / src / world / World.cpp
1 #include "World.h"
2
3 #include "Collision.h"
4 #include "../graphics/const.h"
5
6
7 namespace orbi {
8
9 World::World(Vector<int> size)
10 : size(size)
11 , count(size.x * size.y)
12 , gravity(0, 5)
13 , terminal(50, 50)
14 , tiles(count) {
15
16 }
17
18
19 void World::Update(float dt) {
20         for (Entity &e : entities) {
21                 e.Update(dt, gravity, terminal);
22                 e.onGround = false;
23
24                 const AABB &b = e.bounds;
25
26                 // world bounds collision
27                 if (b.Top() < 0) {
28                         e.Move(Vector<float>(0, -b.Top()));
29                         e.vel.y = 0;
30                 }
31                 if (b.Right() > size.x) {
32                         e.Move(Vector<float>(size.x - b.Right(), 0));
33                         e.vel.x = 0;
34                 }
35                 if (b.Bottom() > size.y) {
36                         e.Move(Vector<float>(0, size.y - b.Bottom()));
37                         e.vel.y = 0;
38                         e.onGround = true;
39                 }
40                 if (b.Left() < 0) {
41                         e.Move(Vector<float>(-b.Left(), 0));
42                         e.vel.x = 0;
43                 }
44
45                 const Vector<int> cBegin(b.Left(), b.Top());
46                 const Vector<int> cEnd(b.Right() + 1, b.Bottom() + 1);
47
48                 Vector<float> min;
49                 Vector<float> max;
50
51                 for (Vector<int> pos(cBegin); pos.y < cEnd.y; ++pos.y) {
52                         for (pos.x = cBegin.x; pos.x < cEnd.x; ++pos.x) {
53                                 if (!TileAt(pos).IsSolid()) continue;
54                                 const AABB &tBounds = TileShapeAt(pos);
55                                 Collision coll;
56                                 if (!e.bounds.Intersects(tBounds, coll)) {
57                                         continue;
58                                 }
59                                 if (coll.depth.x < min.x) min.x = coll.depth.x;
60                                 if (coll.depth.x > max.x) max.x = coll.depth.x;
61                                 if (coll.depth.y < min.y) min.y = coll.depth.y;
62                                 if (coll.depth.y > max.y) max.y = coll.depth.y;
63                         }
64                 }
65
66                 Vector<float> resp;
67                 if (min.x != 0) {
68                         if (max.x == 0) {
69                                 resp.x = min.x;
70                         }
71                 } else {
72                         resp.x = max.x;
73                 }
74                 if (min.y != 0) {
75                         if (max.y == 0) {
76                                 resp.y = min.y;
77                         }
78                 } else {
79                         resp.y = max.y;
80                 }
81                 e.Move(resp);
82                 if (resp.x != 0) {
83                         e.vel.x = 0;
84                 }
85                 if (resp.y != 0) {
86                         e.vel.y = 0;
87                         if (resp.y < 0) {
88                                 e.onGround = true;
89                         }
90                 }
91         }
92 }
93
94
95 const AABB &World::TileShapeAt(Vector<int> pos) const {
96         tileShape = AABB(pos, Vector<float>(1, 1));
97         return tileShape;
98 }
99
100
101 Entity &World::AddEntity(const Entity &e) {
102         entities.emplace_back(e);
103         return entities.back();
104 }
105
106 }