diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-16 12:12:42 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2019-02-16 12:12:42 -0500 |
commit | 9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7 (patch) | |
tree | 72d0331ec91b4fb193c5733ddddc953d19eda9d1 /src | |
parent | a34396730c8d993fea84a454690bd13ea9a9b403 (diff) | |
download | dispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.tar.gz dispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.tar.bz2 dispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.zip |
Pushing works now
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/simulation.cpp | 166 | ||||
-rw-r--r-- | src/simulation.h | 15 |
3 files changed, 160 insertions, 27 deletions
diff --git a/src/main.cpp b/src/main.cpp index 4dce6a5..c041cf8 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -15,21 +15,19 @@ int main(int, char**) | |||
15 | 15 | ||
16 | Simulation::id_type player = sim.emplaceEntity(); | 16 | Simulation::id_type player = sim.emplaceEntity(); |
17 | Entity& entity = sim.getEntity(player); | 17 | Entity& entity = sim.getEntity(player); |
18 | entity.gridPos.x() = 1; | ||
19 | entity.gridPos.y() = 5; | ||
20 | entity.size = TILE_SIZE; | 18 | entity.size = TILE_SIZE; |
21 | entity.speed = 3.0; | 19 | entity.speed = 3.0; |
22 | entity.controllable = true; | 20 | entity.controllable = true; |
23 | entity.colliderType = ColliderType::player; | 21 | entity.colliderType = ColliderType::player; |
24 | entity.colorVal = 180; | 22 | entity.colorVal = 180; |
23 | sim.setGridPos(player, vec2s { 1, 5 } ); | ||
25 | 24 | ||
26 | Simulation::id_type crateId = sim.emplaceEntity(); | 25 | Simulation::id_type crateId = sim.emplaceEntity(); |
27 | Entity& crate = sim.getEntity(crateId); | 26 | Entity& crate = sim.getEntity(crateId); |
28 | crate.gridPos.x() = 4; | ||
29 | crate.gridPos.y() = 5; | ||
30 | crate.size = TILE_SIZE; | 27 | crate.size = TILE_SIZE; |
31 | crate.speed = 4.0; | 28 | crate.speed = 4.0; |
32 | crate.playerCanPush = true; | 29 | crate.playerCanPush = true; |
30 | sim.setGridPos(crateId, vec2s { 4, 5 } ); | ||
33 | 31 | ||
34 | bool quit = false; | 32 | bool quit = false; |
35 | 33 | ||
diff --git a/src/simulation.cpp b/src/simulation.cpp index 7dfc83c..328b4f4 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp | |||
@@ -16,40 +16,39 @@ void Simulation::tick( | |||
16 | !entity.moving) | 16 | !entity.moving) |
17 | { | 17 | { |
18 | if (keystate[SDL_SCANCODE_LEFT] && | 18 | if (keystate[SDL_SCANCODE_LEFT] && |
19 | entity.gridPos.x() > 0 && | 19 | moveEntityOnGrid(id, Direction::left, true)) |
20 | level_.getTileset().canPlayerMoveTo( | ||
21 | level_.at(entity.gridPos - vec2s { 1, 0 }))) | ||
22 | { | 20 | { |
23 | entity.moving = true; | 21 | entity.shouldMoveTo = Direction::left; |
24 | entity.destPos = entity.gridPos - vec2s { 1, 0 }; | ||
25 | } | 22 | } |
26 | else if (keystate[SDL_SCANCODE_UP] && | 23 | else if (keystate[SDL_SCANCODE_UP] && |
27 | entity.gridPos.y() > 0 && | 24 | moveEntityOnGrid(id, Direction::up, true)) |
28 | level_.getTileset().canPlayerMoveTo( | ||
29 | level_.at(entity.gridPos - vec2s { 0, 1 }))) | ||
30 | { | 25 | { |
31 | entity.moving = true; | 26 | entity.shouldMoveTo = Direction::up; |
32 | entity.destPos = entity.gridPos - vec2s { 0, 1 }; | 27 | } |
33 | } else if (keystate[SDL_SCANCODE_RIGHT] && | 28 | else if (keystate[SDL_SCANCODE_RIGHT] && |
34 | entity.gridPos.x() < (level_.getSize().w() - 1) && | 29 | moveEntityOnGrid(id, Direction::right, true)) |
35 | level_.getTileset().canPlayerMoveTo( | ||
36 | level_.at(entity.gridPos + vec2s { 1, 0 }))) | ||
37 | { | 30 | { |
38 | entity.moving = true; | 31 | entity.shouldMoveTo = Direction::right; |
39 | entity.destPos = entity.gridPos + vec2s { 1, 0 }; | ||
40 | } | 32 | } |
41 | else if (keystate[SDL_SCANCODE_DOWN] && | 33 | else if (keystate[SDL_SCANCODE_DOWN] && |
42 | entity.gridPos.y() < (level_.getSize().h() - 1) && | 34 | moveEntityOnGrid(id, Direction::down, true)) |
43 | level_.getTileset().canPlayerMoveTo( | 35 | { |
44 | level_.at(entity.gridPos + vec2s { 0, 1 }))) | 36 | entity.shouldMoveTo = Direction::down; |
37 | } | ||
38 | else | ||
39 | { | ||
40 | entity.shouldMoveTo = Direction::none; | ||
41 | } | ||
42 | |||
43 | if (entity.shouldMoveTo != Direction::none) | ||
45 | { | 44 | { |
46 | entity.moving = true; | 45 | moveEntityOnGrid(id, entity.shouldMoveTo); |
47 | entity.destPos = entity.gridPos + vec2s { 0, 1 }; | ||
48 | } | 46 | } |
49 | 47 | ||
48 | |||
50 | if (entity.moving) | 49 | if (entity.moving) |
51 | { | 50 | { |
52 | entity.movementTween = 0.0; | 51 | |
53 | } | 52 | } |
54 | } | 53 | } |
55 | 54 | ||
@@ -69,7 +68,7 @@ void Simulation::tick( | |||
69 | if (entity.movementTween >= 1.0) | 68 | if (entity.movementTween >= 1.0) |
70 | { | 69 | { |
71 | entity.moving = false; | 70 | entity.moving = false; |
72 | entity.gridPos = entity.destPos; | 71 | setGridPos(id, entity.destPos); |
73 | } | 72 | } |
74 | } | 73 | } |
75 | 74 | ||
@@ -113,3 +112,124 @@ void Simulation::deleteEntity(id_type id) | |||
113 | available_.push_back(id); | 112 | available_.push_back(id); |
114 | active_.erase(id); | 113 | active_.erase(id); |
115 | } | 114 | } |
115 | |||
116 | void Simulation::setGridPos(id_type id, vec2s pos) | ||
117 | { | ||
118 | Entity& entity = entities_.at(id); | ||
119 | |||
120 | size_t oldPosIndex = | ||
121 | entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w(); | ||
122 | gridCache_[oldPosIndex].erase(id); | ||
123 | |||
124 | entity.gridPos = pos; | ||
125 | |||
126 | size_t newPosIndex = | ||
127 | entity.gridPos.x() + entity.gridPos.y() * level_.getSize().w(); | ||
128 | gridCache_[newPosIndex].insert(id); | ||
129 | } | ||
130 | |||
131 | const std::unordered_set<Simulation::id_type>& | ||
132 | Simulation::getGridEntities(vec2s pos) const | ||
133 | { | ||
134 | size_t posIndex = pos.x() + pos.y() * level_.getSize().w(); | ||
135 | |||
136 | return gridCache_[posIndex]; | ||
137 | } | ||
138 | |||
139 | bool Simulation::moveEntityOnGrid( | ||
140 | id_type id, | ||
141 | Direction moveDir, | ||
142 | bool validate) | ||
143 | { | ||
144 | Entity& entity = entities_.at(id); | ||
145 | |||
146 | vec2s shouldMoveTo = entity.gridPos; | ||
147 | |||
148 | switch (moveDir) | ||
149 | { | ||
150 | case Direction::left: | ||
151 | { | ||
152 | if (entity.gridPos.x() == 0) | ||
153 | { | ||
154 | return false; | ||
155 | } | ||
156 | |||
157 | shouldMoveTo -= vec2s { 1, 0 }; | ||
158 | |||
159 | break; | ||
160 | } | ||
161 | |||
162 | case Direction::right: | ||
163 | { | ||
164 | if (entity.gridPos.x() == level_.getSize().w() - 1) | ||
165 | { | ||
166 | return false; | ||
167 | } | ||
168 | |||
169 | shouldMoveTo += vec2s { 1, 0 }; | ||
170 | |||
171 | break; | ||
172 | } | ||
173 | |||
174 | case Direction::up: | ||
175 | { | ||
176 | if (entity.gridPos.y() == 0) | ||
177 | { | ||
178 | return false; | ||
179 | } | ||
180 | |||
181 | shouldMoveTo -= vec2s { 0, 1 }; | ||
182 | |||
183 | break; | ||
184 | } | ||
185 | |||
186 | case Direction::down: | ||
187 | { | ||
188 | if (entity.gridPos.y() == level_.getSize().h() - 1) | ||
189 | { | ||
190 | return false; | ||
191 | } | ||
192 | |||
193 | shouldMoveTo += vec2s { 0, 1 }; | ||
194 | |||
195 | break; | ||
196 | } | ||
197 | } | ||
198 | |||
199 | if (entity.colliderType == ColliderType::player) | ||
200 | { | ||
201 | if (!level_.getTileset().canPlayerMoveTo(level_.at(shouldMoveTo))) | ||
202 | { | ||
203 | return false; | ||
204 | } | ||
205 | |||
206 | for (id_type blockId : getGridEntities(shouldMoveTo)) | ||
207 | { | ||
208 | Entity& block = entities_.at(blockId); | ||
209 | |||
210 | if (!block.playerCanPush) | ||
211 | { | ||
212 | return false; | ||
213 | } | ||
214 | |||
215 | if (!moveEntityOnGrid(blockId, moveDir, validate)) | ||
216 | { | ||
217 | return false; | ||
218 | } | ||
219 | } | ||
220 | |||
221 | } | ||
222 | |||
223 | |||
224 | |||
225 | |||
226 | |||
227 | if (!validate) | ||
228 | { | ||
229 | entity.moving = true; | ||
230 | entity.destPos = shouldMoveTo; | ||
231 | entity.movementTween = 0.0; | ||
232 | } | ||
233 | |||
234 | return true; | ||
235 | } | ||
diff --git a/src/simulation.h b/src/simulation.h index bc47642..2f80f9f 100644 --- a/src/simulation.h +++ b/src/simulation.h | |||
@@ -6,6 +6,8 @@ | |||
6 | #include <vector> | 6 | #include <vector> |
7 | #include <deque> | 7 | #include <deque> |
8 | #include <set> | 8 | #include <set> |
9 | #include <unordered_map> | ||
10 | #include <unordered_set> | ||
9 | 11 | ||
10 | class Level; | 12 | class Level; |
11 | 13 | ||
@@ -45,13 +47,26 @@ public: | |||
45 | return level_; | 47 | return level_; |
46 | } | 48 | } |
47 | 49 | ||
50 | void setGridPos(id_type id, vec2s pos); | ||
51 | |||
48 | private: | 52 | private: |
49 | 53 | ||
54 | |||
55 | |||
56 | const std::unordered_set<id_type>& getGridEntities(vec2s pos) const; | ||
57 | |||
58 | bool moveEntityOnGrid( | ||
59 | id_type id, | ||
60 | Direction moveDir, | ||
61 | bool validate = false); | ||
62 | |||
50 | const Level& level_; | 63 | const Level& level_; |
51 | 64 | ||
52 | std::vector<Entity> entities_; | 65 | std::vector<Entity> entities_; |
53 | std::deque<id_type> available_; | 66 | std::deque<id_type> available_; |
54 | std::set<id_type> active_; | 67 | std::set<id_type> active_; |
68 | |||
69 | mutable std::unordered_map<size_t, std::unordered_set<id_type>> gridCache_; | ||
55 | }; | 70 | }; |
56 | 71 | ||
57 | #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ | 72 | #endif /* end of include guard: SIMULATION_H_7BF6EEA4 */ |