Example #1
0
 public void Update(GameTime gameTime, TileMap map)
 {
     bool hit;
     m_vec.X = m_vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
     Vector2 vel = map.Collide(this, out hit);
     if (hit || m_rect.X < 0 || m_rect.X > 1024)
     {
         m_valid = false;
     }
     else
     {
         m_rect.X += m_vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
     }
 }
Example #2
0
        public void Update(GameTime gameTime, TileMap map, ContentManager Content2)
        {
            Content = Content2;
            m_vibrate -= gameTime.ElapsedGameTime.TotalSeconds;
            if (m_vibrate < 0)
                GamePad.SetVibration(m_index, 0, 0);
            m_rect.X = m_pos.X;
            m_rect.Y = m_pos.Y;
            int ladder_x = 0, ladder_y = 0;

            bool onLadder = map.OnLadder(this, out ladder_x, out ladder_y);

            this.m_dir.X = GamePad.GetState(m_index).ThumbSticks.Left.X;
            if (Keyboard.GetState().IsKeyDown(m_controls[0]))
            {
                m_dir.X = -1;
            }
            else if (Keyboard.GetState().IsKeyDown(m_controls[1]))
            {
                m_dir.X = 1;
            }
            if (onLadder)
            {
                this.m_dir.Y = -GamePad.GetState(m_index).ThumbSticks.Left.Y;
                if (Keyboard.GetState().IsKeyDown(m_controls[2]))
                {
                    m_dir.Y = -1;
                }
                else if (Keyboard.GetState().IsKeyDown(m_controls[3]))
                {
                    m_dir.Y = 1;
                }
                if (Math.Abs(m_dir.Y) > Math.Abs(m_dir.X))
                    m_pos.X = (float)ladder_x;
            }
            else
                m_dir.Y = 0;
            if (m_dir.LengthSquared() > 0)
                m_dir.Normalize();

            // stuck wall hack
            bool unstuck = false;
            if (!onLadder)
            {
                float grav = m_gravity.Y * (float)(gameTime.ElapsedGameTime.TotalSeconds);
                m_vec = m_gravity * (float)(gameTime.ElapsedGameTime.TotalSeconds);
                m_vec = map.Collide(this);
                if (m_vec.Y == grav)
                    unstuck = true;
            }
            if(!unstuck && m_atype != Animation.AnimationType.SHOOTING)
                m_vec.X = m_dir.X * (float)(m_xspeed * gameTime.ElapsedGameTime.TotalSeconds);
            m_vec.Y = m_dir.Y * (float)(m_yspeed * gameTime.ElapsedGameTime.TotalSeconds);
            if (!onLadder)
            {
                m_vec += m_gravity * (float)(gameTime.ElapsedGameTime.TotalSeconds);
            }
            else
            {
                if (Math.Abs(m_vec.Y) < 0.20f) m_atype = Animation.AnimationType.IDLING;
                else m_atype = Animation.AnimationType.CLIMBING;
            }
            m_vec = map.Collide(this);

            m_pos.X += m_vec.X;
            m_pos.Y += m_vec.Y;

            if (onLadder && Math.Abs(m_dir.Y) > Math.Abs(m_dir.X))
            {
                m_rect.X = m_pos.X;
                m_rect.Y = m_pos.Y;
                int dummyx, dummyy;
                if (!map.OnLadder(this, out dummyx, out dummyy))
                {
                    m_pos.Y = ladder_y - m_rect.Height;
                }
            }

            if (m_vec.X > 0)
            {
                m_bulletDir.X = 2.0f;
                flipped = false;
            }
            else if (m_vec.X < 0)
            {
                m_bulletDir.X = -2.0f;
                flipped = true;
            }

            if (m_atype != Animation.AnimationType.SHOOTING && m_atype != Animation.AnimationType.DYING)
            {
                if (Math.Abs(m_vec.X) > 0.20f && Math.Abs(m_vec.Y) < 0.20f)
                {
                    m_atype = Animation.AnimationType.RUNNING;
                }
                if (Math.Abs(m_vec.Y) > 0.20f && Math.Abs(m_vec.X) < 0.20f && onLadder == true)
                {
                    m_atype = Animation.AnimationType.CLIMBING;
                }
                if (Math.Abs(m_vec.X) < 0.20f && !onLadder)
                {
                    m_atype = Animation.AnimationType.IDLING;
                }
            }

            // go back to idle animation after SHOOTING animation is done
            if (m_atype == Animation.AnimationType.SHOOTING && m_sprite.IsCurrentAnimationDone() == true)
            {
                m_atype = Animation.AnimationType.IDLING;
            }

            // go back to idle animation after SPAWNING animation is done
            if (m_atype == Animation.AnimationType.SPAWNING && m_sprite.IsCurrentAnimationDone() == true)
            {
                m_atype = Animation.AnimationType.IDLING;
            }

            if (dyingAnimationIsDone)
            {
                dyingAnimationIsDone = false;
                Respawn();
            }

            // if dying animation completed, respawn
            if (m_atype == Animation.AnimationType.DYING && m_sprite.IsCurrentAnimationDone() == true)
            {
                dyingAnimationIsDone = true;
            }

            m_sprite.Update(gameTime, m_pos + new Vector2(8, 0), Content2);
            //m_sprite.PlayAnimation(Animation.AnimationType.RUNNING); // start in "idling animation" state

            // bullets!
            m_bulletWait -= gameTime.ElapsedGameTime.TotalSeconds;
            if (m_bulletWait < 0) m_bulletWait = 0;
            if((GamePad.GetState(m_index).Buttons.A == ButtonState.Pressed || (Keyboard.GetState().IsKeyDown(m_controls[4])))
                && m_bullets.Count() < 1 && m_bulletWait <= 0)
            {
                m_atype = Animation.AnimationType.SHOOTING;
                m_bulletWait = 0.5f;
                 m_bullets.AddLast(new Bullet(m_content,
                    new floatRectangle(m_pos.X + 8, m_pos.Y + 24, 8, 8),
                    m_bulletDir * 320.0f,
                    m_index, m_team));
            }
            LinkedList<Bullet> garbage = new LinkedList<Bullet>();
            foreach (Bullet b in m_bullets)
            {
                if (!b.IsValid())
                    garbage.AddLast(b);
                else
                    b.Update(gameTime, map);
            }
            foreach (Bullet b in garbage)
            {
                m_bullets.Remove(b);
            }

            switch (m_atype)
            {
                case Animation.AnimationType.DYING: m_sprite.PlayAnimation(Animation.AnimationType.DYING); break;
                case Animation.AnimationType.RUNNING: m_sprite.PlayAnimation(Animation.AnimationType.RUNNING); break;
                case Animation.AnimationType.CLIMBING: m_sprite.PlayAnimation(Animation.AnimationType.CLIMBING); break;
                case Animation.AnimationType.IDLING: m_sprite.PlayAnimation(Animation.AnimationType.IDLING); break;
                case Animation.AnimationType.PANTING: m_sprite.PlayAnimation(Animation.AnimationType.PANTING); break;
                case Animation.AnimationType.SHOOTING: m_sprite.PlayAnimation(Animation.AnimationType.SHOOTING); break;
                case Animation.AnimationType.SPAWNING: m_sprite.PlayAnimation(Animation.AnimationType.SPAWNING); break;
                default: break;
            }
        }