Ejemplo n.º 1
0
 public void add(PongMove m)
 {
     moves.AddLast(m);
 }
Ejemplo n.º 2
0
        public GameState Evolve(KeyboardDevice keyboard)
        {
            if (pause)
            {
                return(this);
            }

            yPhysics.Instance.Beat();
            gameevents.newTime(yPhysics.Instance.FrameN);

            fieldlist.newTime(yPhysics.Instance.FrameN, this);

            // GET INPUT
            Keypress[] move0 = KeyMap.KeyMap_system.translateInput(this, keyboard);

            PongMove move1 = player1.getInput(this, keyboard);
            PongMove move2 = player2.getInput(this, keyboard);

            //move2 = AI.SuggestForce(1, lvl, balls, pads[1]);

            // APPLY INPUT
            move1.Apply(this, player1);
            move2.Apply(this, player2);

            // MOVE PADS
            float force_constant      = 400f * pads[0].Mass;
            float resistence_constant = 18.0f;

            pads[0].verlet(yPhysics.Instance.DT, resistence_constant);
            pads[1].verlet(yPhysics.Instance.DT, resistence_constant);

            // MOVE BALLS
            AgeBallsAndKillThem();

            for (int b = 0; b < balls.Length; b++)
            {
                balls[b].verlet(yPhysics.Instance.DT);
            }


            // CALCULATE COLLISIONS
            CollisionInformations[] collisions = CollisionDetection.CheckCollisions(lvl, pads, balls);

            // slow balls, for they tend to spin fast.
            const float max_ball_speed = 17.0f;

            for (int b = 0; b < balls.Length; b++)
            {
                if (balls[b].Speed.Length > max_ball_speed)
                {
                    balls[b].Speed = balls[b].Speed * max_ball_speed / balls[b].Speed.Length;
                }
            }


            // HP loss, deaths, etc...

            for (int i = 0; i < collisions.Length; i++)
            {
                if (collisions[i].CollidingObj is Ball)
                {
                    Ball theball = ((Ball)collisions[i].CollidingObj);

                    if (collisions[i].TargetObj.Equals(lvl.BoxLeft))
                    {
                        theball.hitWall(this, lvl.BoxLeft);
                        player1.Healthbar.increase(-7);
                    }
                    else if (collisions[i].TargetObj.Equals(lvl.BoxRight))
                    {
                        theball.hitWall(this, lvl.BoxRight);
                        player2.Healthbar.increase(-7);
                    }


                    else if (collisions[i].TargetObj.Equals(player1.thePad))
                    {
                        theball.hitPlayer(this, player1);
                        KillExpiredBalls();
                    }
                    else if (collisions[i].TargetObj.Equals(player2.thePad))
                    {
                        theball.hitPlayer(this, player2);
                        KillExpiredBalls();
                    }
                }
            }


            /*
             *          if (collisions.Length > 0)
             *          {
             * //                pause = true;
             * //                Console.WriteLine("Collisions done: " + collisions.Length);
             *
             *              for (int i = 0; i < collisions.Length; i++)
             *              {
             *                  if (collisions[i].CollidingObj is Ball)
             *                      Console.Write("ball ");
             *                  else if (collisions[i].CollidingObj is Pad)
             *                      Console.Write("pad ");
             *                  else if (collisions[i].CollidingObj is Box)
             *                      Console.Write("wall ");
             *
             *                  if (collisions[i].TargetObj is Ball)
             *                      Console.WriteLine("ball");
             *                  else if (collisions[i].TargetObj is Pad)
             *                      Console.WriteLine("pad");
             *                  else if (collisions[i].TargetObj is Box)
             *                      Console.WriteLine("wall");
             *              }
             *
             *          }
             */


            bool changes = false;

            if (player1.Healthbar.HP <= 0)
            {
                won_p2++;
                changes = true;
            }
            if (player2.Healthbar.HP <= 0)
            {
                won_p1++;
                changes = true;
            }

            if (MatchEnded)
            {
                int winner = Won;
                won_p1 = won_p2 = 0;
            }

            if (matchtype != MatchType.FREEPLAY && changes)
            {
                ResetRound();
            }


            return(this);
        }