Ejemplo n.º 1
0
        public void applyPhysics(Map map, Player player, Enemy[] enemies, GameTime gametime)
        {
            float x = 1.5f;
            player.Speed += new Vector2(0, 0.15f * x);

            mapCollisionPlayer(map, player);

            foreach (Enemy e in enemies)
            {
                e.Speed += new Vector2(0, 0.15f * x);
                if (e.IsAlive && e.IsOnScreen)
                {
                    mapCollisionEnemy(map, e);
                }
            }
        }
Ejemplo n.º 2
0
        private void mapCollisionEnemy(Map map, Enemy enemy)
        {
            Vector2 moveAmount = enemy.Speed;

            //foreach (Tile tile in tileList)
            //{
            //    moveAmount = horizontalCollisionTest(moveAmount, player, tile);
            //    moveAmount = verticalCollisionTest(moveAmount, player, tile);
            //}

            moveAmount = horizontalCollisionTest(moveAmount, enemy, map);
            moveAmount = verticalCollisionTestEnemy(moveAmount, enemy, map);

            Vector2 newPosition = enemy.Position + moveAmount;

            newPosition = new Vector2(
                MathHelper.Clamp(newPosition.X, 0,
                  map.WidthInPixel - enemy.collisionRect.Width),
                MathHelper.Clamp(newPosition.Y, 2 * (map.TileHeight),
                  map.HeightInPixel - enemy.collisionRect.Height));
            enemy.Position = newPosition;
        }
Ejemplo n.º 3
0
 public void synchEnemies(Enemy[] enemies, GameTime gameTime, Rectangle clientBounds)
 {
     foreach (Enemy e in enemies)
         e.PositionOffsetX = -(int)mapOffset.X;
 }
Ejemplo n.º 4
0
        private void initializeEnemy()
        {
            Texture2D enemyTex;
            Vector2 enemyPosition;
            Point enemyFrameSize;
            Point enemyCurrentFrame;
            Point enemySheetSize;
            Vector2 enemySpeed;

            enemyTex = Content.Load<Texture2D>(@"Images\gegner");
            enemyPosition = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 4);
            enemyFrameSize = new Point(40, 48);
            enemyCurrentFrame = new Point(0, 0);
            enemySheetSize = new Point(2, 1);
            enemySpeed = new Vector2(1, 0);
            enemies[0] = new Enemy(enemyTex, enemyPosition, enemyFrameSize, 0, enemyCurrentFrame, enemySheetSize, enemySpeed, 250);

            enemyTex = Content.Load<Texture2D>(@"Images\gegner");
            enemyPosition = new Vector2(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
            enemyFrameSize = new Point(40, 48);
            enemyCurrentFrame = new Point(0, 0);
            enemySheetSize = new Point(2, 1);
            enemySpeed = new Vector2(2, 0);
            enemies[1] = new Enemy(enemyTex, enemyPosition, enemyFrameSize, 0, enemyCurrentFrame, enemySheetSize, enemySpeed, 500);
        }
Ejemplo n.º 5
0
        private Vector2 verticalCollisionTestEnemy(Vector2 moveAmount, Enemy player, Map map)
        {
            if (moveAmount.Y == 0)
                return moveAmount;

            Rectangle afterMoveRect = player.collisionRect;
            afterMoveRect.Offset((int)moveAmount.X, (int)moveAmount.Y);
            Vector2 corner1, corner2;

            if (moveAmount.Y < 0)
            {
                corner1 = new Vector2(afterMoveRect.Left + 1,
                                      afterMoveRect.Top);
                corner2 = new Vector2(afterMoveRect.Right - 1,
                                      afterMoveRect.Top);
            }
            else
            {
                corner1 = new Vector2(afterMoveRect.Left + 1,
                                      afterMoveRect.Bottom);
                corner2 = new Vector2(afterMoveRect.Right - 1,
                                      afterMoveRect.Bottom);
            }

            Tile mapCell1 = map.getTile((int)(corner1.X / map.TileHeight), (int)(corner1.Y / map.TileWidth));
            Tile mapCell2 = map.getTile((int)(corner2.X / map.TileHeight), (int)(corner2.Y / map.TileWidth));

            if ((!mapCell1.IsBackground && !mapCell1.IsDestructible) ||
                (!mapCell2.IsBackground && !mapCell2.IsDestructible))
            {
                //if (moveAmount.Y > 0)
                //    player.IsOnGround = true;
                moveAmount.Y = 0;
                player.Speed = new Vector2(player.Speed.X, 0);
            }

            return moveAmount;
        }