diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-06 13:03:01 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-06 13:03:01 -0500 |
commit | 1abc8894c2378596542e5772cd7594492eeecb27 (patch) | |
tree | c905dc3c96ff7b1d64aaf3212214f0731305bf82 | |
parent | 4526d347961f7a5df110ef94bd38b80008e3110d (diff) | |
download | tanetane-1abc8894c2378596542e5772cd7594492eeecb27.tar.gz tanetane-1abc8894c2378596542e5772cd7594492eeecb27.tar.bz2 tanetane-1abc8894c2378596542e5772cd7594492eeecb27.zip |
Added collision with other sprites
-rw-r--r-- | src/main.cpp | 1 | ||||
-rw-r--r-- | src/transform_system.cpp | 130 | ||||
-rw-r--r-- | src/transform_system.h | 3 |
3 files changed, 105 insertions, 29 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4d8f52d..8363995 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -49,6 +49,7 @@ void loop(Renderer& renderer) { | |||
49 | 49 | ||
50 | int flintSprite = game.emplaceSprite(); | 50 | int flintSprite = game.emplaceSprite(); |
51 | game.getSystem<TransformSystem>().initSprite(flintSprite, {35*16, 19*16}); | 51 | game.getSystem<TransformSystem>().initSprite(flintSprite, {35*16, 19*16}); |
52 | game.getSystem<TransformSystem>().setUpCollision(flintSprite, {-8, -8}, {12, 8}); | ||
52 | game.getSystem<AnimationSystem>().initSprite(flintSprite, "../res/sprites/flint_anim.txt", renderer); | 53 | game.getSystem<AnimationSystem>().initSprite(flintSprite, "../res/sprites/flint_anim.txt", renderer); |
53 | 54 | ||
54 | game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite); | 55 | game.getSystem<CameraSystem>().setFollowingSprite(lucasSprite); |
diff --git a/src/transform_system.cpp b/src/transform_system.cpp index d6df5fa..3d65acb 100644 --- a/src/transform_system.cpp +++ b/src/transform_system.cpp | |||
@@ -54,50 +54,122 @@ CollisionResult TransformSystem::checkCollision(int spriteId, vec2i newLoc, Dire | |||
54 | vec2i oldTileDR = oldColDR / map.getTileSize(); | 54 | vec2i oldTileDR = oldColDR / map.getTileSize(); |
55 | vec2i newTileDR = newColDR / map.getTileSize(); | 55 | vec2i newTileDR = newColDR / map.getTileSize(); |
56 | 56 | ||
57 | if (dirHasDir(sprite.dir, Direction::right) && | 57 | if (dirHasDir(sprite.dir, Direction::right)) { |
58 | newTileDR.x() > oldTileDR.x()) { | 58 | if (newTileDR.x() > oldTileDR.x()) { |
59 | for (int y = newTileUL.y(); y <= newTileDR.y(); y++) { | 59 | for (int y = newTileUL.y(); y <= newTileDR.y(); y++) { |
60 | if (map.isBlocked(newTileDR.x(), y)) { | 60 | if (map.isBlocked(newTileDR.x(), y)) { |
61 | result.horiz.blocked = true; | 61 | result.horiz.blocked = true; |
62 | result.horiz.dir = Direction::right; | 62 | result.horiz.dir = Direction::right; |
63 | 63 | ||
64 | break; | 64 | break; |
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | if (!result.horiz.blocked) { | ||
70 | auto it = rightCollidables_.lower_bound({oldColDR.x(), INT_MAX}); | ||
71 | for (; | ||
72 | (it != std::end(rightCollidables_) && | ||
73 | std::get<0>(it->first) <= newColDR.x()); | ||
74 | it++) { | ||
75 | if (newColDR.y() >= it->second.lower && | ||
76 | newColUL.y() <= it->second.upper) { | ||
77 | result.horiz.blocked = true; | ||
78 | result.horiz.dir = Direction::right; | ||
79 | result.horiz.colliderSprite = std::get<1>(it->first); | ||
80 | |||
81 | break; | ||
82 | } | ||
65 | } | 83 | } |
66 | } | 84 | } |
67 | } | 85 | } |
68 | 86 | ||
69 | if (dirHasDir(sprite.dir, Direction::left) && | 87 | if (dirHasDir(sprite.dir, Direction::left)) { |
70 | newTileUL.x() < oldTileUL.x()) { | 88 | if (newTileUL.x() < oldTileUL.x()) { |
71 | for (int y = newTileUL.y(); y <= newTileDR.y(); y++) { | 89 | for (int y = newTileUL.y(); y <= newTileDR.y(); y++) { |
72 | if (map.isBlocked(newTileUL.x(), y)) { | 90 | if (map.isBlocked(newTileUL.x(), y)) { |
73 | result.horiz.blocked = true; | 91 | result.horiz.blocked = true; |
74 | result.horiz.dir = Direction::left; | 92 | result.horiz.dir = Direction::left; |
93 | |||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | } | ||
75 | 98 | ||
76 | break; | 99 | if (!result.horiz.blocked) { |
100 | auto it = leftCollidables_.lower_bound({oldColUL.x(), 0}); | ||
101 | for (; | ||
102 | (it != std::end(leftCollidables_) && | ||
103 | std::get<0>(it->first) >= newColUL.x()); | ||
104 | it++) { | ||
105 | if (newColDR.y() >= it->second.lower && | ||
106 | newColUL.y() <= it->second.upper) { | ||
107 | result.horiz.blocked = true; | ||
108 | result.horiz.dir = Direction::left; | ||
109 | result.horiz.colliderSprite = std::get<1>(it->first); | ||
110 | |||
111 | break; | ||
112 | } | ||
77 | } | 113 | } |
78 | } | 114 | } |
79 | } | 115 | } |
80 | 116 | ||
81 | if (dirHasDir(sprite.dir, Direction::down) && | 117 | if (dirHasDir(sprite.dir, Direction::down)) { |
82 | newTileDR.y() > oldTileDR.y()) { | 118 | if (newTileDR.y() > oldTileDR.y()) { |
83 | for (int x = newTileUL.x(); x <= newTileDR.x(); x++) { | 119 | for (int x = newTileUL.x(); x <= newTileDR.x(); x++) { |
84 | if (map.isBlocked(x, newTileDR.y())) { | 120 | if (map.isBlocked(x, newTileDR.y())) { |
85 | result.vert.blocked = true; | 121 | result.vert.blocked = true; |
86 | result.vert.dir = Direction::down; | 122 | result.vert.dir = Direction::down; |
87 | 123 | ||
88 | break; | 124 | break; |
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | if (!result.vert.blocked) { | ||
130 | auto it = downCollidables_.lower_bound({oldColDR.y(), INT_MAX}); | ||
131 | for (; | ||
132 | (it != std::end(downCollidables_) && | ||
133 | std::get<0>(it->first) <= newColDR.y()); | ||
134 | it++) { | ||
135 | if (newColDR.x() >= it->second.lower && | ||
136 | newColUL.x() <= it->second.upper) { | ||
137 | result.vert.blocked = true; | ||
138 | result.vert.dir = Direction::down; | ||
139 | result.vert.colliderSprite = std::get<1>(it->first); | ||
140 | |||
141 | break; | ||
142 | } | ||
89 | } | 143 | } |
90 | } | 144 | } |
91 | } | 145 | } |
92 | 146 | ||
93 | if (dirHasDir(sprite.dir, Direction::up) && | 147 | if (dirHasDir(sprite.dir, Direction::up)) { |
94 | newTileUL.y() < oldTileUL.y()) { | 148 | if (newTileUL.y() < oldTileUL.y()) { |
95 | for (int x = newTileUL.x(); x <= newTileDR.x(); x++) { | 149 | for (int x = newTileUL.x(); x <= newTileDR.x(); x++) { |
96 | if (map.isBlocked(x, newTileUL.y())) { | 150 | if (map.isBlocked(x, newTileUL.y())) { |
97 | result.vert.blocked = true; | 151 | result.vert.blocked = true; |
98 | result.vert.dir = Direction::up; | 152 | result.vert.dir = Direction::up; |
153 | |||
154 | break; | ||
155 | } | ||
156 | } | ||
157 | } | ||
99 | 158 | ||
100 | break; | 159 | if (!result.vert.blocked) { |
160 | auto it = upCollidables_.lower_bound({oldColUL.y(), 0}); | ||
161 | for (; | ||
162 | (it != std::end(upCollidables_) && | ||
163 | std::get<0>(it->first) >= newColUL.y()); | ||
164 | it++) { | ||
165 | if (newColDR.x() >= it->second.lower && | ||
166 | newColUL.x() <= it->second.upper) { | ||
167 | result.vert.blocked = true; | ||
168 | result.vert.dir = Direction::up; | ||
169 | result.vert.colliderSprite = std::get<1>(it->first); | ||
170 | |||
171 | break; | ||
172 | } | ||
101 | } | 173 | } |
102 | } | 174 | } |
103 | } | 175 | } |
diff --git a/src/transform_system.h b/src/transform_system.h index 1ad661c..a7294ce 100644 --- a/src/transform_system.h +++ b/src/transform_system.h | |||
@@ -14,6 +14,7 @@ class Game; | |||
14 | struct AxisResult { | 14 | struct AxisResult { |
15 | Direction dir; | 15 | Direction dir; |
16 | bool blocked = false; | 16 | bool blocked = false; |
17 | int colliderSprite = -1; | ||
17 | }; | 18 | }; |
18 | 19 | ||
19 | struct CollisionResult { | 20 | struct CollisionResult { |
@@ -54,6 +55,8 @@ private: | |||
54 | Collidable(int lower, int upper) : lower(lower), upper(upper) {} | 55 | Collidable(int lower, int upper) : lower(lower), upper(upper) {} |
55 | }; | 56 | }; |
56 | 57 | ||
58 | // The way these work is that the key is (axis position, entity ID). The | ||
59 | // entity ID being there makes the mapping unique and makes removal easy. | ||
57 | using asc_collidables_type = | 60 | using asc_collidables_type = |
58 | std::map< | 61 | std::map< |
59 | std::tuple<int, int>, | 62 | std::tuple<int, int>, |