Example #1
0
SHIP *
ship_update(SHIP *ship, bool keys[], ALLEGRO_TIMER *timer)
{
  LIST *head = NULL;

  /* move forward */
  if(keys[KEY_UP])
    ship_accelerate(ship);

  /* rotate */
  if(keys[KEY_LEFT])
    ship_rotate(ship, -3);
  if(keys[KEY_RIGHT])
    ship_rotate(ship, 3);

  /* hyperspace */
  if(keys[KEY_LCONTROL])
    ship_hyperspace(ship);

  /* shoot */
  if(keys[KEY_SPACE])
    ship_fire(ship, timer);

  if(ship->explosion) {
    animation_update(ship->explosion);

    /* if the animation is complete, create a new ship */
    if(ship->explosion->current_frame >= ship->explosion->n_frames - 1) {
      /* FIXME: need preemptive collision detection, wait() */
      SHIP *old = ship;
      ship = ship_create();
      ship_free(old);
    }

    return ship;
  }

  /* ship missile positions */
  head = list_first(ship->missiles);
  while(head) {
    missile_update((MISSILE *) head->data, timer);
    head = head->next;
  }

  ship->position->x += ship->velocity->x;
  ship->position->y += ship->velocity->y;
  wrap_position(ship->position);

  /* slow down over time */
  ship_drag(ship);

  return ship;
}
Example #2
0
void apply_velocity(Object* obj)
{
	X_y temp;

	temp = Vector_to_X_y(obj->velocity);

	obj->pos.x += temp.x;
	obj->pos.y += -temp.y; // apply negative y velocity due to screen coordinates

	// wrap screen
	wrap_position(obj);
}