コード例 #1
0
ファイル: Program.cs プロジェクト: TheMaikXX/DrunkenMonk
        /// <summary>
        /// Checks if next position will be collision and handles it
        /// </summary>
        /// <param name="context"></param>
        /// <param name="lastPosition"></param>
        /// <param name="newPlayerPosition"></param>
        /// <param name="newPlayerDirection"></param>
        /// <param name="brush"></param>
        private static void TripAndCollisionLogic(
            GameContext context,
            Position lastPosition,
            Position newPlayerPosition,
            Direction newPlayerDirection,
            PaintBrush brush)
        {
            Random random = new Random(DateTime.Now.Millisecond);

            bool collided = newPlayerPosition.PredictCollision(context.Enemies.Select(enemy => enemy.Position));

            bool tripped = false;

            if (!collided)
            {
                tripped = random.Next(1, 5) == 1;                 // 25% chance
            }

            if (collided)
            {
                SimulationResult simulationResult = null;

                do
                {
                    // TODO: Move constnat numbres to App.config
                    Simulation simulation = (simulationResult?.Obstacle ?? newPlayerPosition)
                                            .SimulateCollision(
                        lastPosition,
                        newPlayerDirection,
                        3, 4);

                    newPlayerDirection = newPlayerDirection.Reverse();

                    simulationResult = context.Square.ExecuteSimulation(simulation, newPosition =>
                    {
                        return(!newPosition.PredictCollision(context.Enemies.Select(enemy => enemy.Position)));
                    }, brush);

                    lastPosition = simulationResult.LastSafePosition;
                } while (!simulationResult.HasSuccessfulyFinished);

                context.Player.Position = simulationResult.LastSafePosition;
            }
            else if (tripped)
            {
                SimulationResult simulationResult = null;

                do
                {
                    Simulation simulation;

                    // TODO: Move constnat numbres to App.config
                    if (!simulationResult?.HasSuccessfulyFinished ?? false)
                    {
                        simulation = lastPosition
                                     .SimulateCollision(
                            newPlayerPosition,
                            newPlayerDirection,
                            3, 4);

                        newPlayerDirection = newPlayerDirection.Reverse();
                    }
                    else
                    {
                        simulation = newPlayerPosition
                                     .SimulateTrip(
                            simulationResult?.LastSafePosition ?? lastPosition,
                            newPlayerDirection,
                            2, 4);
                    }

                    simulationResult = context.Square.ExecuteSimulation(simulation, newPosition =>
                    {
                        return(!newPosition.PredictCollision(context.Enemies.Select(enemy => enemy.Position)) ||
                               newPosition.Y < 0 || newPosition.X < 0);                            // TODO: Finish validation
                    }, brush, true);

                    newPlayerPosition = simulationResult.Obstacle;
                    lastPosition      = simulationResult.LastSafePosition;
                } while (!simulationResult.HasSuccessfulyFinished);

                context.Player.Position = simulationResult.LastSafePosition;
            }
            else
            {
                brush.Derender(context.Square, context.Player.Position);

                // Update position and direction
                context.Player.Direction = newPlayerDirection;
                context.Player.Position  = newPlayerPosition;

                brush.Render(context.Square, context.Player.Position, Player.BodyCharacter);
            }
        }