public void ParseShipInputs(long inputs, int i, out float heading, out float thrust, out int fire) { var ship = _ships[i]; GGPORunner.LogGame($"parsing ship {i} inputs: {inputs}."); if ((inputs & INPUT_ROTATE_RIGHT) != 0) { heading = (ship.heading - ROTATE_INCREMENT) % 360; } else if ((inputs & INPUT_ROTATE_LEFT) != 0) { heading = (ship.heading + ROTATE_INCREMENT + 360) % 360; } else { heading = ship.heading; } if ((inputs & INPUT_THRUST) != 0) { thrust = SHIP_THRUST; } else if ((inputs & INPUT_BREAK) != 0) { thrust = -SHIP_THRUST; } else { thrust = 0; } fire = (int)(inputs & INPUT_FIRE); }
public void MoveShip(int index, float heading, float thrust, int fire) { var ship = _ships[index]; GGPORunner.LogGame($"calculation of new ship coordinates: (thrust:{thrust} heading:{heading})."); ship.heading = heading; if (ship.cooldown == 0) { if (fire != 0) { GGPORunner.LogGame("firing bullet."); for (int i = 0; i < ship.bullets.Length; i++) { float dx = Mathf.Cos(DegToRad(ship.heading)); float dy = Mathf.Sin(DegToRad(ship.heading)); if (!ship.bullets[i].active) { ship.bullets[i].active = true; ship.bullets[i].position.x = ship.position.x + (ship.radius * dx); ship.bullets[i].position.y = ship.position.y + (ship.radius * dy); ship.bullets[i].velocity.x = ship.velocity.x + (BULLET_SPEED * dx); ship.bullets[i].velocity.y = ship.velocity.y + (BULLET_SPEED * dy); ship.cooldown = BULLET_COOLDOWN; break; } } } } if (thrust != 0) { float dx = thrust * Mathf.Cos(DegToRad(heading)); float dy = thrust * Mathf.Sin(DegToRad(heading)); ship.velocity.x += dx; ship.velocity.y += dy; float mag = Mathf.Sqrt(ship.velocity.x * ship.velocity.x + ship.velocity.y * ship.velocity.y); if (mag > SHIP_MAX_THRUST) { ship.velocity.x = (ship.velocity.x * SHIP_MAX_THRUST) / mag; ship.velocity.y = (ship.velocity.y * SHIP_MAX_THRUST) / mag; } } GGPORunner.LogGame($"new ship velocity: (dx:{ship.velocity.x} dy:{ship.velocity.y})."); ship.position.x += ship.velocity.x; ship.position.y += ship.velocity.y; GGPORunner.LogGame($"new ship position: (dx:{ship.position.x} dy:{ship.position.y})."); if (ship.position.x - ship.radius < _bounds.xMin || ship.position.x + ship.radius > _bounds.xMax) { ship.velocity.x *= -1; ship.position.x += (ship.velocity.x * 2); } if (ship.position.y - ship.radius < _bounds.yMin || ship.position.y + ship.radius > _bounds.yMax) { ship.velocity.y *= -1; ship.position.y += (ship.velocity.y * 2); } for (int i = 0; i < ship.bullets.Length; i++) { if (ship.bullets[i].active) { ship.bullets[i].position.x += ship.bullets[i].velocity.x; ship.bullets[i].position.y += ship.bullets[i].velocity.y; if (ship.bullets[i].position.x <_bounds.xMin || ship.bullets[i].position.y <_bounds.yMin || ship.bullets[i].position.x> _bounds.xMax || ship.bullets[i].position.y> _bounds.yMax) { ship.bullets[i].active = false; } else { for (int j = 0; j < _ships.Length; j++) { var other = _ships[j]; if (Distance(ship.bullets[i].position, other.position) < other.radius) { ship.score++; other.health -= BULLET_DAMAGE; ship.bullets[i].active = false; break; } } } } } }