150 } |
151 } |
151 |
152 |
152 static void move_spaceship(AscBehavior *behavior) { |
153 static void move_spaceship(AscBehavior *behavior) { |
153 AscSceneNode *node = behavior->node; |
154 AscSceneNode *node = behavior->node; |
154 Spaceship *spaceship = node->user_data; |
155 Spaceship *spaceship = node->user_data; |
155 float speed = 32.f * spaceship->speed * asc_context.frame_factor; |
156 const float ts = (float) game_field_tile_size; |
156 asc_vec2f movement = asc_vec2f_scale(directions[spaceship->direction], speed); |
157 const float speed = ts * spaceship->speed * asc_context.frame_factor; |
157 asc_scene_node_move2f(node, movement); |
158 const asc_vec2f dvec = directions[spaceship->direction]; |
158 asc_scene_node_set_rotation(node, rotations[spaceship->direction]); |
159 const asc_vec2f movement = asc_vec2f_scale(dvec, speed); |
|
160 if (spaceship->direction == spaceship->target_direction) { |
|
161 // no change of direction - keep moving |
|
162 asc_scene_node_move2f(node, movement); |
|
163 } else { |
|
164 // identify the tile we are currently in |
|
165 int x = (int) (node->position.x / ts); |
|
166 int y = (int) (node->position.y / ts); |
|
167 const asc_vec2f tcenter = ASC_VEC2F(x*ts, y*ts); |
|
168 // calculate the (squared) distance to the tile's center |
|
169 float cdist = asc_vec2f_sqrlen(asc_vec2f_sub(tcenter, ASC_VEC2F(node->position.x, node->position.y))); |
|
170 // if it is less than squared length of the movement vector, snap to the center and rotate |
|
171 // TODO: think about if we should allow changing direction only when we haven't passed the center yet |
|
172 if (cdist < asc_vec2f_sqrlen(dvec)) { |
|
173 asc_scene_node_set_position2f(node, tcenter); |
|
174 spaceship->direction = spaceship->target_direction; |
|
175 asc_scene_node_set_rotation(node, rotations[spaceship->direction]); |
|
176 // TODO: this is losing some speed - improve the calculation and add partial movement after rotation |
|
177 } else { |
|
178 // too far away from center, continue normal movement |
|
179 asc_scene_node_move2f(node, movement); |
|
180 } |
|
181 } |
159 } |
182 } |
160 |
183 |
161 static Spaceship *create_spaceship(void) { |
184 static Spaceship *create_spaceship(void) { |
162 AscSceneNode *sprite = asc_sprite( |
185 AscSceneNode *sprite = asc_sprite( |
163 .name = "Player", |
186 .name = "Player", |
283 } |
306 } |
284 |
307 |
285 // player rotation |
308 // player rotation |
286 if (asc_key_pressed(ASC_KEY(LEFT))) { |
309 if (asc_key_pressed(ASC_KEY(LEFT))) { |
287 if (spaceship->direction != MOVE_RIGHT) { |
310 if (spaceship->direction != MOVE_RIGHT) { |
288 spaceship->direction = MOVE_LEFT; |
311 spaceship->target_direction = MOVE_LEFT; |
289 } |
312 } |
290 } |
313 } |
291 if (asc_key_pressed(ASC_KEY(RIGHT))) { |
314 if (asc_key_pressed(ASC_KEY(RIGHT))) { |
292 if (spaceship->direction != MOVE_LEFT) { |
315 if (spaceship->direction != MOVE_LEFT) { |
293 spaceship->direction = MOVE_RIGHT; |
316 spaceship->target_direction = MOVE_RIGHT; |
294 } |
317 } |
295 } |
318 } |
296 if (asc_key_pressed(ASC_KEY(UP))) { |
319 if (asc_key_pressed(ASC_KEY(UP))) { |
297 if (spaceship->direction != MOVE_DOWN) { |
320 if (spaceship->direction != MOVE_DOWN) { |
298 spaceship->direction = MOVE_UP; |
321 spaceship->target_direction = MOVE_UP; |
299 } |
322 } |
300 } |
323 } |
301 if (asc_key_pressed(ASC_KEY(DOWN))) { |
324 if (asc_key_pressed(ASC_KEY(DOWN))) { |
302 if (spaceship->direction != MOVE_UP) { |
325 if (spaceship->direction != MOVE_UP) { |
303 spaceship->direction = MOVE_DOWN; |
326 spaceship->target_direction = MOVE_DOWN; |
304 } |
327 } |
305 } |
328 } |
306 |
329 |
307 // debug-key for clearing the shader registry |
330 // debug-key for clearing the shader registry |
308 if (asc_key_pressed(ASC_KEY(S))) { |
331 if (asc_key_pressed(ASC_KEY(S))) { |