summary refs log tree commit diff stats
path: root/src/simulation.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-16 12:12:42 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-16 12:12:42 -0500
commit9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7 (patch)
tree72d0331ec91b4fb193c5733ddddc953d19eda9d1 /src/simulation.cpp
parenta34396730c8d993fea84a454690bd13ea9a9b403 (diff)
downloaddispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.tar.gz
dispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.tar.bz2
dispatcher-9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7.zip
Pushing works now
Diffstat (limited to 'src/simulation.cpp')
-rw-r--r--src/simulation.cpp166
1 files changed, 143 insertions, 23 deletions
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
116void 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
131const 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
139bool 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}