This is a game component that implements IUpdateable.
Inheritance: Microsoft.Xna.Framework.DrawableGameComponent
        public override void OnPlayerCollision(Player player)
        {
            if (player.RecentlyDamaged)
                return;

            Rectangle collisionArea = Rectangle.Intersect(player.AABB, this.AABB);
            bool yCollision = false;
            if (collisionArea.Width > collisionArea.Height)
                yCollision = true;

            Vector2 depth = player.AABB.GetIntersectionDepth(this.AABB);
            if (yCollision)
            {
                var origin = new Vector2(AABB.Center.X, AABB.Center.Y);
                var destination = new Vector2(AABB.Center.X, AABB.Center.X + (Texture.Height / 2));
                var normal = (destination - origin);
                normal.Normalize();
                var reflected = player.Velocity - 2 * normal * (player.Velocity * normal);
                player.Velocity = reflected;
                player.Damage();
            }
            else
            {
                player.Position = new Vector2(player.Position.X + depth.X, player.Position.Y);
                player.CollidedWithSurface(true);
            }
        }
示例#2
0
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _camera = new Camera2D(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            _player = new Player(Content, new Vector2(400, 430));
            _player.Load("Animations.xml");

            _font = Content.Load<SpriteFont>("font");

            Debug.Assert(GraphicsDevice != null, "GraphicsDevice != null");
// ReSharper disable once PossibleLossOfFraction
            _camera.MinPosition = new Vector2(x: GraphicsDevice.Viewport.Width / 2, y: 0);
            //Camera.MaxPosition = new Vector2(map.Dimensions.X - GraphicsDevice.Viewport.Width / 2, map.Dimensions.Y / 2 - 35);
            _camera.MaxPosition = new Vector2(5000, 200);

            _camera.TrackingBody = _player;

            _levelProvider = new LevelProvider(Content, _player, _camera);
            _levelProvider.LoadMaps(GraphicsDevice, "hills.tmx");

            _hud = new HUD(Content, _player);
            _hud.Initialize();
            _hud.SetMapWidth(1000);
        }
 internal void ProcessCollisions(Player player)
 {
     leftCollider.ProcessCollisions(player);
     rightCollider.ProcessCollisions(player);
     topCollider.ProcessCollisions(player);
     bottomCollider.ProcessCollisions(player);
 }
示例#4
0
 public virtual void CheckCollision(Player player)
 {
     if (Collision.CheckCollision(this, player))
     {
         OnPlayerCollision(player);
     }
 }
 public override void Update(float elapsed, Camera camera, Player player, Map map)
 {
     //			y += yDir * elapsed * FALL_SPEED;
     //			x += xDir * elapsed * MOVE_SPEED;
     base.Update (elapsed, camera, player, map);
     //rect = new Rectangle (new Point ((int)x, (int)y), new Size (WIDTH, HEIGHT + 1));
 }
示例#6
0
        public void Update(Player player)
        {
            //don't update if it's already been picked up!
            if (collected) return;

            bool collision = false;

            //check for collision with player

            //if collision
            if (collision)
            {
                collected = true;
                switch (type)
                {
                    case GemType.White:
                        //add to score
                        player.AddScore(100);
                        break;
                    case GemType.Red:
                        //add to health
                        player.AddHealth();
                        break;
                    case GemType.Gold:
                        //refill health and add 1 to max health (Zelda, anyone?)
                        player.AddToMaxHealth();
                        break;
                }
            }
        }
示例#7
0
文件: Exit.cs 项目: zmthy/play-dead
 // Switch -  the player has to be near and respond to a key press
 public override void ChangeState(Player p, KeyboardState keyState, InputManager inputManager)
 {
     RectangleF other = p.BoundingRectangle;
     Boolean touching = other.Intersects(this.BoundingRectangle);
     if (inputManager.IsNewPress(Keys.E) && touching)
         p.GotoLevel(levelIndex);
 }
示例#8
0
        public virtual void OnPlayerCollision(Player player)
        {
            Rectangle collisionArea = Rectangle.Intersect(player.AABB, this.AABB);

            float distanceY = collisionArea.Height;
            float distanceX = collisionArea.Left - (collisionArea.Left + collisionArea.Width);

            Vector2 depth = player.AABB.GetIntersectionDepth(this.AABB);

            if (depth != Vector2.Zero)
            {
                float absDepthX = Math.Abs(depth.X);
                float absDepthY = Math.Abs(depth.Y);

                if (absDepthY < absDepthX)
                {
                    var pos = new Vector2(player.Position.X, player.Position.Y + depth.Y + (player.AABB.Center.Y - player.Position.Y));
                    player.Position = pos;
                    player.CollidedWithSurface(false);
                    player.OnGround = true;
                }
                else
                {
                    player.Position = new Vector2(player.Position.X + depth.X, player.Position.Y);
                    player.CollidedWithSurface(true);
                }
            }
        }
 public override void Update(float elapsed, Camera camera, Player player, Map map)
 {
     base.Update (elapsed, camera, player, map);
     if (!dead) {
         DoAI (camera, player,map);
     }
 }
        public override void Update(float elapsed, Camera camera, Player player, Map map)
        {
            base.Update (elapsed, camera, player, map);
            if (!dead) {
                tileDirRelSprites = new List<TileDirectionRelSprite> ();
                ApplyCamera (camera);
                x += xDir * MOVE_SPEED * elapsed;
                y += yDir * FALL_SPEED * elapsed;
                if (jumping) {
                    if (yDir >= 0) {
                        jumping = false;
                        falling = true;
                    } else {
                        yDir += FALL_SPEED;
                    }
                } else if (falling) {
                    if (yDir < FALL_CAP)
                        yDir += FALL_SPEED;
                }
                gun.Update (elapsed, camera);
                if (xDir != 0) {
                    gun.left = xDir < 0;

                }
                if (gun.left) {
                    gun.x = x - gun.width;
                } else if (!gun.left) {
                    gun.x = x + width;
                }
                gun.y = y + height / 3;
            }
        }
 internal void ProcessCollisions(Player player)
 {
     //if(BoundingBox.Intersects(player.BoundingBox))
     if (player.BoundingBox.Intersects(BoundingBox))
     {
         switch (colliderType)
         {
             case ColliderType.Left:
                 if (player.Velocity.X > 0)
                 {
                     player.MoveHorizontally(0);
                 }
                 break;
             case ColliderType.Right:
                 if (player.Velocity.X < 0)
                 {
                     player.MoveHorizontally(0);
                 }
                 break;
             case ColliderType.Top:
                 player.Land();
                 player.StandOn(BoundingBox);
                 break;
             case ColliderType.Bottom:
                 if (player.Velocity.Y < 0)
                 {
                     player.MoveVertically(0);
                 }
                 break;
             default:
                 break;
         }
     }
 }
 public GrenadeExplosion(PlatformerGame game, Vector2 position, Player player)
 {
     this.game = game;
     this._player = player;
     Position = position;
     IsAlive = false;
     LoadContent();
 }
示例#13
0
        /// <summary>
        /// Constructs a new Spike
        /// </summary>
        public Knife(PlatformerGame level, Vector2 position, Player player)
        {
            this.game = level;
            this.position = position;
            this._player = player;

            LoadContent();
        }
 public override void CheckCollision(Player player)
 {
     if (Collision.CheckCollision(player, this))
     {
         AABB.GetIntersectionDepth(player.AABB);
         if (player.AABB.GetIntersectionDepth(this.AABB).X > 30)
         {
             OnPlayerCollision(player);
         }
     }
 }
示例#15
0
        public Shotgun(PlatformerGame level, Vector2 position, Player player)
        {
            this.game = level;
            Position = position;
            _player = player;

            // Initialize bullets
            _shells = new List<ShotgunShell>();
            for (int i = 0; i < MAX_SHELLS; i++)
            {
                _shells.Add(new ShotgunShell(level, position));
            }

            LoadContent();
        }
示例#16
0
        public HandGun(PlatformerGame game, Vector2 position, Player player)
        {
            this.game = game;
            Position = position;
            _player = player;

            // Initialize bullets
            _bullets = new List<HandgunBullet>();
            for (int i = 0; i < MAX_HANDGUN_BULLETS; i++)
            {
                _bullets.Add(new HandgunBullet(game, position));
            }

            LoadContent();
        }
示例#17
0
        public void Think(float elapsed, Player player)
        {
            switch (monsterType)
            {
                case MonsterType.MonsterA:

                    //idle for 1 seconds
                    if (idleTime < 1)
                    {
                        xInput = 0;
                        idleTime += elapsed;
                    }
                    //wander in a random direction for 2 seconds
                    else if (walkTime < 2)
                    {
                        if (walkTime == 0)
                        {
                            Random r = new Random();
                            int x = r.Next(1, 2);
                            if (x == 1) speed = -speed;
                        }
                        xInput = speed;
                        walkTime += elapsed;
                    }
                    //start over
                    else
                    {
                        idleTime = 0f;
                        walkTime = 0f;
                    }
                    break;

                case MonsterType.MonsterB:
                    //just walks
                    xInput = speed;
                    break;

                case MonsterType.MonsterC:
                    //idles until player gets close, then chases
                    break;

                case MonsterType.MonsterD:

                    break;
            }
        }
示例#18
0
        public void OnPlayerCollision(Player player)
        {
            if (player.AABB.Y + player.AABB.Height - 10 < AABB.Y)
            {
                Console.WriteLine("Enemy Killed!");
                this.Kill();
            }
            else
            {
                Console.WriteLine("Player Killed");
                player.Damage();
            }

            var origin = new Vector2(AABB.Center.X, AABB.Center.Y);
            var destination = new Vector2(AABB.Center.X, AABB.Center.X + (Texture.Height / 2));
            var normal = (destination - origin);
            normal.Normalize();
            var reflected = player.Velocity - 2 * normal * (player.Velocity * normal);
            player.Velocity = reflected;
        }
示例#19
0
文件: Switch.cs 项目: zmthy/play-dead
        // Switch -  the player has to be near and respond to a key press
        public override void ChangeState(Player p, KeyboardState keyState, InputManager inputManager)
        {
            RectangleF other = p.BoundingRectangle;
            Boolean touching = other.Intersects(this.BoundingRectangle);
            if (inputManager.IsNewPress(Keys.E) && touching)
            {
                on = !on;

                if (on)
                {
                    buttonDown.Play();
                }
                else
                {
                    buttonUp.Play();
                }

                foreach (IActivatable responder in list)
                {
                    responder.ChangeState();
                }
            }
        }
 public virtual void Update(float elapsed, Camera camera, Player player, Map map)
 {
     rect = new Rectangle (new Point ((int)x, (int)y), new Size (width, height));
     if (health <= 0) {
         dead = true;
     }
     if (dead) {
         if (system == null) {
             ParticleOptions ops = (new EffectDie (colour, (KillableSprite)this)).template.Clone();
             ops.xPosRange = new Vector (new Point ((int)x, (int)x + width));
             ops.yPosRange = new Vector (new Point ((int)y, (int)y + height));
             system = new ParticleSystem (ops);
         }
         system.Update (elapsed, camera);
         return;
     }
 }
示例#21
0
        /// <summary>
        /// Called when a gem is collected.
        /// </summary>
        /// <param name="gem">The gem that was collected.</param>
        /// <param name="collectedBy">The player who collected this gem.</param>
        private void OnGemCollected(Gem gem, Player collectedBy)
        {
            score += Gem.PointValue;

            gem.OnCollected(collectedBy);
        }
示例#22
0
        /// <summary>
        /// Instantiates a player, puts him in the level, and remembers where to put him when he is resurrected.
        /// </summary>
        private Tile LoadStartTile(int x, int y)
        {
            if (Player != null)
                throw new NotSupportedException("A level may only have one starting point.");

            start = RectangleExtensions.GetBottomCenter(GetBounds(x, y));
            player = new Player(this, start);

            return new Tile(null, TileCollision.Passable);
        }
 /// <summary>
 /// Sets the player to correct situation at level loadup
 /// </summary>
 /// <param name="player">The player which to fit correctly</param>
 public void SetupPlayer(Player player)
 {
     player.Legs = HasLegs;
     player.Laser = HasGun;
 }
示例#24
0
        private void OnItemCollected(AbstractItem item, Player collectedBy)
        {
            // Check what was collected
            Console.WriteLine("Found item of type:" + item.GetType());
            if (item is GunItem)
            {
                EvolutionManager.Instance.HasGun = true;
                // Update the player
                EvolutionManager.Instance.SetupPlayer(player);
                player.LoadContent();

            }
            else if (item is LegsItem)
            {
                // Set legs to true
                EvolutionManager.Instance.HasLegs = true;
                // Update the player
                EvolutionManager.Instance.SetupPlayer(player);
                player.LoadContent();
            }

            item.OnCollected(collectedBy);
        }
示例#25
0
 /// <summary>
 /// Called when this gem has been collected by a player and removed from the level.
 /// </summary>
 /// <param name="collectedBy">
 /// The player who collected this gem. Although currently not used, this parameter would be
 /// useful for creating special powerup gems. For example, a gem could make the player invincible.
 /// </param>
 public void OnCollected(Player collectedBy)
 {
     collectedSound.Play();
 }
示例#26
0
        public void Collision(Player player)
        {
            var near = from t in Tiles
                       where Vector2.Distance(player.Position, t.Position) < 150 && t.CheckForCollision
                       select t;

            foreach (Tile t in near)
            {
                t.CheckCollision(player);
            }
        }
示例#27
0
        public void Update(GameTime time, Player player)
        {
            if (player.AABB.X < 0)
            {
                player.Position =  new Vector2(player.AABB.Width / 2, player.Position.Y);
            }
            else if (player.AABB.Right > Dimensions.X)
            {
                player.Position = new Vector2(Dimensions.X - player.AABB.Width / 2, player.Position.Y);
            }

            Collision(player);

            /*
            var portal = Tiles.Find(x => x is ExitPortal) as ExitPortal;
            if (portal.Triggered)
            {
                Completed = true;
            }
            */
            Tiles.RemoveAll(x => x.FlagForRemove == true);
        }
示例#28
0
 public void OnKilled(Player killedBy)
 {
     IsAlive = false;
 }
示例#29
0
        public void UpdatePlayerScreen(Player player, Player player2)
        {
            float minX;
            float maxX;

            if (player.VirtualScreen == VirtualScreen.ONE)
            {
                minX = -_blockScreenSize.X / 2.0f;
                maxX = _blockScreenSize.X / 2.0f;
            }
            else
            {
                minX = (3 * _blockScreenSize.X) / 2.0f;
                maxX = (5 * _blockScreenSize.X) / 2.0f;
            }

            if (player.Position.X < minX)
            {
                if (player.CurrentScreen.X > 0)
                {
                    player.NextScreen -= new Vector2(1, 0);
                    player.X = maxX;
                }
                else
                {
                    player.X = minX;
                    player.MainFixture.Body.LinearVelocity *= new Vector2(0, 1);
                }

                if (player is PlayerOne)
                {
                    foreach (DynamicObject dObject in player._dynamicObjects)
                    {
                        if (dObject is Bullet)
                            dObject.Dispose();
                    }
                }
            }
            else if (player.Position.X > maxX)
            {
                if (player.CurrentScreen.X < MAX_LEVEL_X - 1)
                {
                    player.NextScreen += new Vector2(1, 0);
                    player.X = minX;
                }
                else
                {
                    player.X = maxX;
                    player.MainFixture.Body.LinearVelocity *= new Vector2(0, 1);
                }

                if (player is PlayerOne)
                {
                    foreach (DynamicObject dObject in player._dynamicObjects)
                    {
                        if (dObject is Bullet)
                            dObject.Dispose();
                    }
                }
            }

            if (player.Position.Y < -_blockScreenSize.Y / 2)
            {
                if (player.CurrentScreen.Y < MAX_LEVEL_Y - 1)
                {
                    player.NextScreen += new Vector2(0, 1);
                    player.Y = _blockScreenSize.Y / 2;
                }
                else
                {
                    player.Y = -_blockScreenSize.Y / 2;
                    player.MainFixture.Body.LinearVelocity *= new Vector2(1, 0);
                }
            }
            else if (player.Position.Y > _blockScreenSize.Y / 2)
            {
                if (player.CurrentScreen.Y > 0)
                {
                    player.NextScreen -= new Vector2(0, 1);
                    player.Y = -_blockScreenSize.Y / 2;
                }
                else
                {
                    player.Y = _blockScreenSize.Y / 2;
                    player.MainFixture.Body.LinearVelocity *= new Vector2(1, 0);
                }
            }
        }
示例#30
0
 public void UpdateView(Player player, Player player2)
 {
     if (player.NextScreen.X < player2.NextScreen.X)
     {
         _leftViewPort.Height = _graphics.PreferredBackBufferHeight;
         _rightViewPort.Height = _graphics.PreferredBackBufferHeight;
         _leftViewPort.Width = _graphics.PreferredBackBufferWidth / 2;
         _rightViewPort.Width = _graphics.PreferredBackBufferWidth / 2;
         _leftViewPort.X = 0;
         _rightViewPort.X = _graphics.PreferredBackBufferWidth / 2;
         _leftViewPort.Y = 0;
         _rightViewPort.Y = 0;
         _splitScreenMode = SplitScreenMode.HORIZONTAL;
         proj = Matrix.CreateOrthographic(_blockScreenSize.X / 2, _blockScreenSize.Y, 0, 1);
     }
     else if (player.NextScreen.X > player2.NextScreen.X)
     {
         _leftViewPort.Height = _graphics.PreferredBackBufferHeight;
         _rightViewPort.Height = _graphics.PreferredBackBufferHeight;
         _leftViewPort.Width = _graphics.PreferredBackBufferWidth / 2;
         _rightViewPort.Width = _graphics.PreferredBackBufferWidth / 2;
         _leftViewPort.X = _graphics.PreferredBackBufferWidth / 2;
         _rightViewPort.X = 0;
         _leftViewPort.Y = 0;
         _rightViewPort.Y = 0;
         _splitScreenMode = SplitScreenMode.HORIZONTAL;
         proj = Matrix.CreateOrthographic(_blockScreenSize.X / 2, _blockScreenSize.Y, 0, 1);
     }
     else if (player.NextScreen.Y < player2.NextScreen.Y)
     {
         _leftViewPort.Height = _graphics.PreferredBackBufferHeight / 2;
         _rightViewPort.Height = _graphics.PreferredBackBufferHeight / 2;
         _leftViewPort.Width = _graphics.PreferredBackBufferWidth;
         _rightViewPort.Width = _graphics.PreferredBackBufferWidth;
         _leftViewPort.X = 0;
         _rightViewPort.X = 0;
         _leftViewPort.Y = 0;
         _rightViewPort.Y = _graphics.PreferredBackBufferHeight / 2;
         _splitScreenMode = SplitScreenMode.VERTICAL;
         proj = Matrix.CreateOrthographic(_blockScreenSize.X, _blockScreenSize.Y / 2, 0, 1);
     }
     else if (player.NextScreen.Y > player2.NextScreen.Y)
     {
         _leftViewPort.Height = _graphics.PreferredBackBufferHeight / 2;
         _rightViewPort.Height = _graphics.PreferredBackBufferHeight / 2;
         _leftViewPort.Width = _graphics.PreferredBackBufferWidth;
         _rightViewPort.Width = _graphics.PreferredBackBufferWidth;
         _leftViewPort.X = 0;
         _rightViewPort.X = 0;
         _leftViewPort.X = 0;
         _rightViewPort.X = 0;
         _leftViewPort.Y = _graphics.PreferredBackBufferHeight / 2;
         _rightViewPort.Y = 0;
         _splitScreenMode = SplitScreenMode.VERTICAL;
         proj = Matrix.CreateOrthographic(_blockScreenSize.X, _blockScreenSize.Y / 2, 0, 1);
     }
     else
     {
         _splitScreenMode = SplitScreenMode.NONE;
         proj = Matrix.CreateOrthographic(_blockScreenSize.X, _blockScreenSize.Y, 0, 1);
     }
 }