diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-06 11:40:48 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-06 11:40:48 -0500 |
commit | 478bc11eec70e6127161ff360cd77d6893a81c42 (patch) | |
tree | 0b9dee46b3f2111acce17475f309487fc8759cb4 /src/transform_system.cpp | |
parent | 64ad64ae11a9497f36258e1c83d23f7964bbf885 (diff) | |
download | tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.gz tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.tar.bz2 tanetane-478bc11eec70e6127161ff360cd77d6893a81c42.zip |
Moved some collision stuff into the TransformSystem
Diffstat (limited to 'src/transform_system.cpp')
-rw-r--r-- | src/transform_system.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index 0e602e0..ad7947f 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp | |||
@@ -1,5 +1,6 @@ | |||
1 | #include "transform_system.h" | 1 | #include "transform_system.h" |
2 | #include "game.h" | 2 | #include "game.h" |
3 | #include "map.h" | ||
3 | 4 | ||
4 | void TransformSystem::initSprite(int spriteId, vec2i loc) { | 5 | void TransformSystem::initSprite(int spriteId, vec2i loc) { |
5 | Sprite& sprite = game_.getSprite(spriteId); | 6 | Sprite& sprite = game_.getSprite(spriteId); |
@@ -18,3 +19,69 @@ void TransformSystem::moveSprite(int spriteId, vec2i newLoc) { | |||
18 | spritesByY_.emplace(newLoc.y(), spriteId); | 19 | spritesByY_.emplace(newLoc.y(), spriteId); |
19 | } | 20 | } |
20 | } | 21 | } |
22 | |||
23 | CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Direction dir) { | ||
24 | CollisionResult result; | ||
25 | |||
26 | Sprite& sprite = game_.getSprite(spriteId); | ||
27 | |||
28 | const Map& map = game_.getMap(); | ||
29 | bool blocked = false; | ||
30 | |||
31 | const vec2i UL_COL_BOX = { 8, 8 }; | ||
32 | const vec2i DR_COL_BOX = { 4, 0 }; | ||
33 | vec2i oldColPosUL = (sprite.loc - UL_COL_BOX) / map.getTileSize(); | ||
34 | vec2i newColPosUL = (newLoc - UL_COL_BOX) / map.getTileSize(); | ||
35 | vec2i oldColPosDR = (sprite.loc + DR_COL_BOX) / map.getTileSize(); | ||
36 | vec2i newColPosDR = (newLoc + DR_COL_BOX) / map.getTileSize(); | ||
37 | |||
38 | if (dirHasDir(sprite.dir, Direction::right) && | ||
39 | newColPosDR.x() > oldColPosDR.x()) { | ||
40 | for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { | ||
41 | if (map.isBlocked(newColPosDR.x(), y)) { | ||
42 | result.horiz.blocked = true; | ||
43 | result.horiz.dir = Direction::right; | ||
44 | |||
45 | break; | ||
46 | } | ||
47 | } | ||
48 | } | ||
49 | |||
50 | if (dirHasDir(sprite.dir, Direction::left) && | ||
51 | newColPosUL.x() < oldColPosUL.x()) { | ||
52 | for (int y = newColPosUL.y(); y <= newColPosDR.y(); y++) { | ||
53 | if (map.isBlocked(newColPosUL.x(), y)) { | ||
54 | result.horiz.blocked = true; | ||
55 | result.horiz.dir = Direction::left; | ||
56 | |||
57 | break; | ||
58 | } | ||
59 | } | ||
60 | } | ||
61 | |||
62 | if (dirHasDir(sprite.dir, Direction::down) && | ||
63 | newColPosDR.y() > oldColPosDR.y()) { | ||
64 | for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { | ||
65 | if (map.isBlocked(x, newColPosDR.y())) { | ||
66 | result.vert.blocked = true; | ||
67 | result.vert.dir = Direction::down; | ||
68 | |||
69 | break; | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | |||
74 | if (dirHasDir(sprite.dir, Direction::up) && | ||
75 | newColPosUL.y() < oldColPosUL.y()) { | ||
76 | for (int x = newColPosUL.x(); x <= newColPosDR.x(); x++) { | ||
77 | if (map.isBlocked(x, newColPosUL.y())) { | ||
78 | result.vert.blocked = true; | ||
79 | result.vert.dir = Direction::up; | ||
80 | |||
81 | break; | ||
82 | } | ||
83 | } | ||
84 | } | ||
85 | |||
86 | return result; | ||
87 | } | ||