summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2019-02-16 12:39:24 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2019-02-16 12:39:24 -0500
commit7514077a07076403f29050e57fa87f24fc614122 (patch)
treec51a8d20923a93e5cef9fcc55436602fa1fdd387 /src
parent9d9fe5b1d8ac5f8c7fe03e0d77591e71acf41af7 (diff)
downloaddispatcher-7514077a07076403f29050e57fa87f24fc614122.tar.gz
dispatcher-7514077a07076403f29050e57fa87f24fc614122.tar.bz2
dispatcher-7514077a07076403f29050e57fa87f24fc614122.zip
Shift-pushing
Diffstat (limited to 'src')
-rw-r--r--src/entity.h2
-rw-r--r--src/simulation.cpp118
-rw-r--r--src/simulation.h2
3 files changed, 78 insertions, 44 deletions
diff --git a/src/entity.h b/src/entity.h index 8d28b98..0ff31e1 100644 --- a/src/entity.h +++ b/src/entity.h
@@ -42,7 +42,7 @@ public:
42 42
43 bool playerCanPush = false; 43 bool playerCanPush = false;
44 bool trainCanPush = false; 44 bool trainCanPush = false;
45 45
46 // Temp 46 // Temp
47 int colorVal = 25; 47 int colorVal = 25;
48 48
diff --git a/src/simulation.cpp b/src/simulation.cpp index 328b4f4..ca6ca3d 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp
@@ -15,40 +15,68 @@ void Simulation::tick(
15 if (entity.controllable && 15 if (entity.controllable &&
16 !entity.moving) 16 !entity.moving)
17 { 17 {
18 if (keystate[SDL_SCANCODE_LEFT] && 18 if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT])
19 moveEntityOnGrid(id, Direction::left, true))
20 { 19 {
21 entity.shouldMoveTo = Direction::left; 20 Direction lookDir = Direction::none;
22 } 21
23 else if (keystate[SDL_SCANCODE_UP] && 22 if (keystate[SDL_SCANCODE_LEFT])
24 moveEntityOnGrid(id, Direction::up, true)) 23 {
25 { 24 lookDir = Direction::left;
26 entity.shouldMoveTo = Direction::up; 25 }
27 } 26 else if (keystate[SDL_SCANCODE_UP])
28 else if (keystate[SDL_SCANCODE_RIGHT] && 27 {
29 moveEntityOnGrid(id, Direction::right, true)) 28 lookDir = Direction::up;
30 { 29 }
31 entity.shouldMoveTo = Direction::right; 30 else if (keystate[SDL_SCANCODE_RIGHT])
32 } 31 {
33 else if (keystate[SDL_SCANCODE_DOWN] && 32 lookDir = Direction::right;
34 moveEntityOnGrid(id, Direction::down, true)) 33 }
35 { 34 else if (keystate[SDL_SCANCODE_DOWN])
36 entity.shouldMoveTo = Direction::down; 35 {
37 } 36 lookDir = Direction::down;
38 else 37 }
39 { 38
40 entity.shouldMoveTo = Direction::none; 39 vec2s lookPos = posInDir(entity.gridPos, lookDir);
41 } 40
42 41 for (id_type blockId : getGridEntities(lookPos))
43 if (entity.shouldMoveTo != Direction::none) 42 {
44 { 43 Entity& block = entities_.at(blockId);
45 moveEntityOnGrid(id, entity.shouldMoveTo); 44
46 } 45 if (!block.moving && block.playerCanPush)
47 46 {
48 47 moveEntityOnGrid(blockId, lookDir);
49 if (entity.moving) 48 }
50 { 49 }
51 50 } else {
51 if (keystate[SDL_SCANCODE_LEFT] &&
52 moveEntityOnGrid(id, Direction::left, true))
53 {
54 entity.shouldMoveTo = Direction::left;
55 }
56 else if (keystate[SDL_SCANCODE_UP] &&
57 moveEntityOnGrid(id, Direction::up, true))
58 {
59 entity.shouldMoveTo = Direction::up;
60 }
61 else if (keystate[SDL_SCANCODE_RIGHT] &&
62 moveEntityOnGrid(id, Direction::right, true))
63 {
64 entity.shouldMoveTo = Direction::right;
65 }
66 else if (keystate[SDL_SCANCODE_DOWN] &&
67 moveEntityOnGrid(id, Direction::down, true))
68 {
69 entity.shouldMoveTo = Direction::down;
70 }
71 else
72 {
73 entity.shouldMoveTo = Direction::none;
74 }
75
76 if (entity.shouldMoveTo != Direction::none)
77 {
78 moveEntityOnGrid(id, entity.shouldMoveTo);
79 }
52 } 80 }
53 } 81 }
54 82
@@ -143,7 +171,7 @@ bool Simulation::moveEntityOnGrid(
143{ 171{
144 Entity& entity = entities_.at(id); 172 Entity& entity = entities_.at(id);
145 173
146 vec2s shouldMoveTo = entity.gridPos; 174 vec2s shouldMoveTo = posInDir(entity.gridPos, moveDir);
147 175
148 switch (moveDir) 176 switch (moveDir)
149 { 177 {
@@ -154,8 +182,6 @@ bool Simulation::moveEntityOnGrid(
154 return false; 182 return false;
155 } 183 }
156 184
157 shouldMoveTo -= vec2s { 1, 0 };
158
159 break; 185 break;
160 } 186 }
161 187
@@ -166,8 +192,6 @@ bool Simulation::moveEntityOnGrid(
166 return false; 192 return false;
167 } 193 }
168 194
169 shouldMoveTo += vec2s { 1, 0 };
170
171 break; 195 break;
172 } 196 }
173 197
@@ -178,8 +202,6 @@ bool Simulation::moveEntityOnGrid(
178 return false; 202 return false;
179 } 203 }
180 204
181 shouldMoveTo -= vec2s { 0, 1 };
182
183 break; 205 break;
184 } 206 }
185 207
@@ -190,8 +212,6 @@ bool Simulation::moveEntityOnGrid(
190 return false; 212 return false;
191 } 213 }
192 214
193 shouldMoveTo += vec2s { 0, 1 };
194
195 break; 215 break;
196 } 216 }
197 } 217 }
@@ -207,7 +227,7 @@ bool Simulation::moveEntityOnGrid(
207 { 227 {
208 Entity& block = entities_.at(blockId); 228 Entity& block = entities_.at(blockId);
209 229
210 if (!block.playerCanPush) 230 if (block.moving || !block.playerCanPush)
211 { 231 {
212 return false; 232 return false;
213 } 233 }
@@ -233,3 +253,15 @@ bool Simulation::moveEntityOnGrid(
233 253
234 return true; 254 return true;
235} 255}
256
257vec2s Simulation::posInDir(vec2s orig, Direction dir)
258{
259 switch (dir)
260 {
261 case Direction::left: return orig - vec2s { 1, 0 };
262 case Direction::right: return orig + vec2s { 1, 0 };
263 case Direction::up: return orig - vec2s { 0, 1 };
264 case Direction::down: return orig + vec2s { 0, 1 };
265 case Direction::none: return orig;
266 }
267}
diff --git a/src/simulation.h b/src/simulation.h index 2f80f9f..4d41b49 100644 --- a/src/simulation.h +++ b/src/simulation.h
@@ -49,6 +49,8 @@ public:
49 49
50 void setGridPos(id_type id, vec2s pos); 50 void setGridPos(id_type id, vec2s pos);
51 51
52 static vec2s posInDir(vec2s orig, Direction dir);
53
52private: 54private:
53 55
54 56