Esempio n. 1
0
 public Player(Game1 game)
 {
     exit         = new ExitCommand(game);
     reset        = new ResetCommand(this);
     camMoveUp    = new CamMoveUp(this);
     camMoveDown  = new CamMoveDown(this);
     camMoveLeft  = new CamMoveLeft(this);
     camMoveRight = new CamMoveRight(this);
     stateMachine = new PlayerStateMachine(this);
     facing.Add(Dir.Down, LinkSpriteFactory.Instance.CreateMoveDown(1, 2));
     facing.Add(Dir.Up, LinkSpriteFactory.Instance.CreateMoveUp(1, 2));
     facing.Add(Dir.Left, LinkSpriteFactory.Instance.CreateMoveLeft(1, 2));
     facing.Add(Dir.Right, LinkSpriteFactory.Instance.CreateMoveRight(1, 2));
     facing.Add(Dir.DownSword, LinkSpriteFactory.Instance.CreateDownSword(1, 2));
     facing.Add(Dir.UpSword, LinkSpriteFactory.Instance.CreateUpSword(1, 2));
     facing.Add(Dir.LeftSword, LinkSpriteFactory.Instance.CreateLeftSword(1, 2));
     facing.Add(Dir.RightSword, LinkSpriteFactory.Instance.CreateRightSword(1, 2));
     facing.Add(Dir.Dead, LinkSpriteFactory.Instance.CreateDeadLink(1, 4));
     anim = facing[direction];
 }
Esempio n. 2
0
        public void Update(GameTime gameTime)
        {
            if (health <= 0)
            {
                direction = Dir.Dead;
            }

            if (Blocks.Blocks.inwater(position, length, width))
            {
                speed = 50;
            }
            else
            {
                speed = 200;
            }
            stateMachine.Update(gameTime);
            direction = stateMachine.getDirection();
            anim      = facing[direction];
            if (stateMachine.ifIsMoving() || stateMachine.ifIsSwording())
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(1);
            }
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (healthTimer > 0)
            {
                healthTimer -= dt;
                ColorTimer  -= dt;
            }
            if (ColorTimer > 0)
            {
                ColorTimer -= dt;
            }
            if (stateMachine.ifIsMoving())
            {
                Vector2 tempPos = position;
                tempPos.X = position.X + 20;
                tempPos.Y = position.Y + 10;
                switch (direction)
                {
                case Dir.Right:
                    tempPos.X += speed * dt;
                    if (!Blocks.Blocks.didCollide(tempPos, length, width))
                    {
                        position.X += speed * dt;
                    }

                    break;

                case Dir.Left:
                    tempPos.X -= speed * dt;
                    if (!Blocks.Blocks.didCollide(tempPos, length, width))
                    {
                        position.X -= speed * dt;
                    }

                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Blocks.Blocks.didCollide(tempPos, length, width))
                    {
                        position.Y -= speed * dt;
                    }

                    break;

                case Dir.Down:
                    tempPos.Y += speed * dt;
                    if (!Blocks.Blocks.didCollide(tempPos, length, width))
                    {
                        position.Y += speed * dt;
                    }

                    break;

                default:
                    break;
                }
            }



            KeyboardState kState = Keyboard.GetState();

            if (kState.IsKeyDown(Keys.D1) && previous.IsKeyUp(Keys.D1))
            {
                BombProj.bomb.Add(new BombProj(position));
            }
            if (kState.IsKeyDown(Keys.D2) && previous.IsKeyUp(Keys.D2))
            {
                if (direction == Dir.Down)
                {
                    ArrowProj.arrowDown.Add(new ArrowProj(position, direction));
                }
                if (direction == Dir.Up)
                {
                    ArrowProj.arrowUp.Add(new ArrowProj(position, direction));
                }
                if (direction == Dir.Left)
                {
                    ArrowProj.arrowLeft.Add(new ArrowProj(position, direction));
                }
                if (direction == Dir.Right)
                {
                    ArrowProj.arrowRight.Add(new ArrowProj(position, direction));
                }
            }
            if (kState.IsKeyDown(Keys.D3) && previous.IsKeyUp(Keys.D3))
            {
                BoomerangProj.boomerang.Add(new BoomerangProj(position, direction));
            }

            if (kState.IsKeyDown(Keys.F) && previous.IsKeyUp(Keys.F))
            {
                camMoveUp.Execute();
            }
            if (kState.IsKeyDown(Keys.V) && previous.IsKeyUp(Keys.V))
            {
                camMoveDown.Execute();
            }
            if (kState.IsKeyDown(Keys.C) && previous.IsKeyUp(Keys.C))
            {
                camMoveLeft.Execute();
            }
            if (kState.IsKeyDown(Keys.B) && previous.IsKeyUp(Keys.B))
            {
                camMoveRight.Execute();
            }
            previous = kState;
            if (kState.IsKeyDown(Keys.Q))
            {
                exit.Execute();
            }
            if (kState.IsKeyDown(Keys.R))
            {
                reset.Execute();
            }
            if (kState.IsKeyDown(Keys.P))
            {
                MediaPlayer.Pause();
            }
            if (kState.IsKeyDown(Keys.O))
            {
                MediaPlayer.Resume();
            }
        }