private void UpdatePlayer(TimeSpan totalTime, TimeSpan elapsedTime,Player player) { // Lenkung if (player.WheelLeft) player.Direction -= (float)elapsedTime.TotalSeconds * 100; if (player.WheelRight) player.Direction += (float)elapsedTime.TotalSeconds * 100; // Beschleunigung & Bremse float targetSpeed = 0f; if (player.Acceleration) targetSpeed = 100f; if (player.Break) targetSpeed = -50f; int cellX = (int)(player.Position.X / Track.CELLSIZE); int cellY = (int)(player.Position.Y / Track.CELLSIZE); cellX = Math.Min(Track.Tiles.GetLength(0) - 1, Math.Max(0, cellX)); cellY = Math.Min(Track.Tiles.GetLength(1) - 1, Math.Max(0, cellY)); TrackTile tile = Track.Tiles[cellX, cellY]; switch (tile) { case TrackTile.Dirt: targetSpeed *= 0.2f; break; case TrackTile.Gras: targetSpeed *= 0.8f; break; case TrackTile.Road: targetSpeed *= 1f; break; case TrackTile.Sand: targetSpeed *= 0.4f; break; } // Beschleunigung if (targetSpeed > player.Velocity) { player.Velocity += 80f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Min(targetSpeed, player.Velocity); } else if (targetSpeed < player.Velocity) { player.Velocity -= 100f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Max(targetSpeed, player.Velocity); } // Positionsveränderung float direction = (float)(player.Direction * Math.PI) / 180f; Vector velocity = new Vector( Math.Sin(direction) * player.Velocity * elapsedTime.TotalSeconds, -Math.Cos(direction) * player.Velocity * elapsedTime.TotalSeconds); player.Position += velocity; }
public Game(Track track) { Track = track; State = GameState.CountDown; CountDown = TimeSpan.FromSeconds(3); Vector goal = (Vector)Track.GoalPosition; Vector startOffset1 = default(Vector); Vector startOffset2 = default(Vector); float startRotation = 0f; switch (Track.GetTileByIndex((int)goal.X, (int)goal.Y)) { case TrackTile.GoalDown: startOffset1 = new Vector(0.75f, 0.25f); startOffset2 = new Vector(0.25f, 0.25f); startRotation = 180f; break; case TrackTile.GoalLeft: startOffset1 = new Vector(0.75f, 0.75f); startOffset2 = new Vector(0.75f, 0.25f); startRotation = -90f; break; case TrackTile.GoalRight: startOffset1 = new Vector(0.25f, 0.25f); startOffset2 = new Vector(0.25f, 0.75f); startRotation = 90f; break; case TrackTile.GoalUp: startOffset1 = new Vector(0.25f, 0.75f); startOffset2 = new Vector(0.75f, 0.75f); break; } Player1 = new Player() { Position = (Point)((goal + startOffset1) * Track.CELLSIZE), Direction = startRotation }; }
/// <summary> /// Aktualisiert den gegebenen Spieler. /// </summary> /// <param name="totalTime">Die total abgelaufene Zeit.</param> /// <param name="elapsedTime">Die abgelaufene Zeit seit dem letzten Update.</param> /// <param name="player">Der Spieler.</param> private void UpdatePlayer(TimeSpan totalTime, TimeSpan elapsedTime, Player player) { // Lenkung if (player.WheelLeft) player.Direction -= (float)elapsedTime.TotalSeconds * 100; if (player.WheelRight) player.Direction += (float)elapsedTime.TotalSeconds * 100; // Beschleunigung & Bremse var targetSpeed = 0f; if (player.Acceleration) targetSpeed = 100f; if (player.Break) targetSpeed = -50f; // Anpassung je nach Untergrund targetSpeed *= Track.GetSpeedByPosition(player.Position); // Beschleunigung if (targetSpeed > player.Velocity) { player.Velocity += 80f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Min(targetSpeed, player.Velocity); } else if (targetSpeed < player.Velocity) { player.Velocity -= 100f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Max(targetSpeed, player.Velocity); } // Positionsveränderung var direction = (float)(player.Direction * Math.PI) / 180f; var velocity = new Vector( Math.Sin(direction) * player.Velocity * elapsedTime.TotalSeconds, -Math.Cos(direction) * player.Velocity * elapsedTime.TotalSeconds); player.Position += velocity; }
public Game() { State = GameState.CountDown; CountDown = TimeSpan.FromSeconds(3); Track = Track.LoadFromTxt("./Tracks/Track1.txt"); Vector goal = Track.GetGoalPosition(); Vector startOffset1 = new Vector(); Vector startOffset2 = new Vector(); float startRotation = 0f; switch (Track.GetTileByIndex((int)goal.X, (int)goal.Y)) { case TrackTile.GoalDown: startOffset1 = new Vector(0.75f, 0.25f); startOffset2 = new Vector(0.25f, 0.25f); startRotation = 180f; break; case TrackTile.GoalLeft: startOffset1 = new Vector(0.75f, 0.75f); startOffset2 = new Vector(0.75f, 0.25f); startRotation = -90f; break; case TrackTile.GoalRight: startOffset1 = new Vector(0.25f, 0.25f); startOffset2 = new Vector(0.25f, 0.75f); startRotation = 90f; break; case TrackTile.GoalUp: startOffset1 = new Vector(0.25f, 0.75f); startOffset2 = new Vector(0.75f, 0.75f); startRotation = 0f; break; } Player1 = new Player() { Position = (goal + startOffset1) * Track.CELLSIZE, Direction = startRotation }; }
public Game() { Track = Track.LoadFromTxt("./Tracks/Track1.txt"); Player1 = new Player(); }
public Game() { Player1 = new Player(); }
/// <summary> /// Aktualisiert den gegebenen Spieler. /// </summary> /// <param name="totalTime">Die total abgelaufene Zeit.</param> /// <param name="elapsedTime">Die abgelaufene Zeit seit dem letzten Update.</param> /// <param name="player">Der Spieler.</param> private void UpdatePlayer(TimeSpan totalTime, TimeSpan elapsedTime, Player player) { player.TotalTime += elapsedTime; player.LapTime += elapsedTime; // Lenkung if (player.WheelLeft) player.Direction -= (float)elapsedTime.TotalSeconds * 100; if (player.WheelRight) player.Direction += (float)elapsedTime.TotalSeconds * 100; // Beschleunigung & Bremse var targetSpeed = 0f; if (player.Acceleration) targetSpeed = 100f; if (player.Break) targetSpeed = -50f; // Anpassung je nach Untergrund targetSpeed *= Track.GetSpeedByPosition(player.Position); // Beschleunigung if (targetSpeed > player.Velocity) { player.Velocity += 80f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Min(targetSpeed, player.Velocity); } else if (targetSpeed < player.Velocity) { player.Velocity -= 100f * (float)elapsedTime.TotalSeconds; player.Velocity = Math.Max(targetSpeed, player.Velocity); } // Positionsveränderung var direction = (float)(player.Direction * Math.PI) / 180f; var velocity = new Vector( Math.Sin(direction) * player.Velocity * elapsedTime.TotalSeconds, -Math.Cos(direction) * player.Velocity * elapsedTime.TotalSeconds); player.Position += velocity; // Goal State ermitteln var playerCell = Track.GetTileByPosition((Point)player.Position); if (((int)playerCell & 0xC) > 0) { // Player stelt in einer Goal-Zelle switch (playerCell) { case TrackTile.GoalDown: player.PositionRelativeToGoal = (player.Position.Y % Track.CELLSIZE < Track.CELLSIZE / 2) ? PlayerPositionRelativeToGoal.BeforeGoal : PlayerPositionRelativeToGoal.AfterGoal; break; case TrackTile.GoalLeft: player.PositionRelativeToGoal = (player.Position.X % Track.CELLSIZE >= Track.CELLSIZE / 2) ? PlayerPositionRelativeToGoal.BeforeGoal : PlayerPositionRelativeToGoal.AfterGoal; break; case TrackTile.GoalRight: player.PositionRelativeToGoal = (player.Position.X % Track.CELLSIZE < Track.CELLSIZE / 2) ? PlayerPositionRelativeToGoal.BeforeGoal : PlayerPositionRelativeToGoal.AfterGoal; break; case TrackTile.GoalUp: player.PositionRelativeToGoal = (player.Position.Y % Track.CELLSIZE >= Track.CELLSIZE / 2) ? PlayerPositionRelativeToGoal.BeforeGoal : PlayerPositionRelativeToGoal.AfterGoal; break; } } else { player.PositionRelativeToGoal = PlayerPositionRelativeToGoal.AwayFromGoal; } }