Esempio n. 1
0
        /// <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.SteerLeft)
            {
                player.Direction -= (float)elapsedTime.TotalSeconds * MAXSTEERANGLE;
            }
            if (player.SteerRight)
            {
                player.Direction += (float)elapsedTime.TotalSeconds * MAXSTEERANGLE;
            }

            // 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 (playerCell.IsGoalTile())
            {
                Debug.Write("goal tile: " + playerCell);
                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:
                    Debug.WriteLine(player.Position.Y);
                    player.PositionRelativeToGoal = (player.Position.Y % Track.CELLSIZE >= Track.CELLSIZE / 2) ? PlayerPositionRelativeToGoal.BeforeGoal : PlayerPositionRelativeToGoal.AfterGoal;
                    break;
                }
            }
            else
            {
                player.PositionRelativeToGoal = PlayerPositionRelativeToGoal.AwayFromGoal;
            }
        }
Esempio n. 2
0
        /// <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.RoundTime += 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(player.Position);

            if (((int)playerCell & 0xC) > 0)
            {
                // Player stelt in einer Goal-Zelle
                switch (playerCell)
                {
                case TrackTile.GoalDown:
                    player.GoalFlag = (player.Position.Y % Track.CELLSIZE < Track.CELLSIZE / 2) ? GoalFlags.BeforeGoal : GoalFlags.AfterGoal;
                    break;

                case TrackTile.GoalLeft:
                    player.GoalFlag = (player.Position.X % Track.CELLSIZE >= Track.CELLSIZE / 2) ? GoalFlags.BeforeGoal : GoalFlags.AfterGoal;
                    break;

                case TrackTile.GoalRight:
                    player.GoalFlag = (player.Position.X % Track.CELLSIZE < Track.CELLSIZE / 2) ? GoalFlags.BeforeGoal : GoalFlags.AfterGoal;
                    break;

                case TrackTile.GoalUp:
                    player.GoalFlag = (player.Position.Y % Track.CELLSIZE >= Track.CELLSIZE / 2) ? GoalFlags.BeforeGoal : GoalFlags.AfterGoal;
                    break;
                }
            }
            else
            {
                player.GoalFlag = GoalFlags.None;
            }
        }