Ejemplo n.º 1
0
        public void TileCollisionCheck(AnimatedObject mo, GameTime gameTime)
        {
            bool goX = true;
            bool collided = false;
            bool goY = true;
            //int tileCollidedWith = tileManager.collisionTiles.IndexOf(tileManager.CollisionCheck(mo, CollisionTile));
            //Tile collidedTile = tileManager.CollisionCheck(mo, CollisionTile);

            int playerStandingOnThisTile = (int)(((int)((mo.Y + mo.origin.Y) / 32) * 50) + ((mo.X + mo.origin.X) / 32));

            List<Tile> CloseTiles = new List<Tile>();
            if ((mo.X < 1 || mo.Y < 1 || mo.X > worldSize.X * Tile.tileSize || mo.Y > worldSize.Y * Tile.tileSize) && mo is Shot)
            {
                projectiles.Remove((Shot)mo);
                return;
            }

            CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile]);
            if (mo.X < Tile.tileSize * worldSize.X - 1)
            {
                CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile + 1]);
            }
            if (mo.X > Tile.tileSize)
            {
                CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile - 1]);
            }
            if (playerStandingOnThisTile < worldSize.X * worldSize.Y - worldSize.X)
            {
                if (mo.X > Tile.tileSize)
                {
                    CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile + (int)worldSize.X - 1]);
                }
                CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile + (int)worldSize.X]);
                if (mo.X < Tile.tileSize * worldSize.X - 1)
                {
                    CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile + (int)worldSize.X + 1]);
                }
            }
            if (playerStandingOnThisTile > worldSize.X)
            {
                if (mo.X > Tile.tileSize)
                {
                    CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile - (int)worldSize.X - 1]);
                }
                CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile - (int)worldSize.X]);
                if (mo.X < Tile.tileSize * worldSize.X - 1)
                {
                    CloseTiles.Add(tileManager.collisionTiles[playerStandingOnThisTile - (int)worldSize.X + 1]);
                }
            }

            //foreach (Tile tile in tileManager.collisionTiles)

            if (mo is Shot)
            {
                foreach (Tile tile in CloseTiles)
                {
                    if (mo.CollisionRectangle().Intersects(tile.GraphicalRectangle()) && tile.sheetPoint == CollisionTile)
                    {
                        projectiles.Remove((Shot)mo);
                        return;
                    }
                }
                foreach (InteractableTerrain terrain in Doors)
                {
                    if (terrain.Collider[terrain.currentSprite.X] == true && mo.CollisionRectangle().Intersects(terrain.CollisionRectangleForShots()))
                    {
                        projectiles.Remove((Shot)mo);
                        return;
                    }
                }
            }

            foreach (InteractableTerrain terrain in Doors)
            {
                if (mo.GhostX.Collider().Intersects(terrain.CollisionRectangle()) && terrain.Collider[terrain.currentSprite.X] == true)
                {
                    goX = false;
                    collided = true;
                }
                if (mo.GhostY.Collider().Intersects(terrain.CollisionRectangle()) && terrain.Collider[terrain.currentSprite.X] == true)
                {
                    goY = false;
                    collided = true;
                }
            }

            foreach (Tile tile in CloseTiles)
            {
                Tile collidedTile = null;

                if (mo.GhostX.Collider().Intersects(tile.CollisionRectangle()) && tile.sheetPoint == CollisionTile)
                {
                    goX = false;
                    collided = true;
                }
                if (mo.GhostY.Collider().Intersects(tile.CollisionRectangle()) && tile.sheetPoint == CollisionTile)
                {
                    goY = false;
                    collided = true;
                }
                if (goX == true || goY == true)
                {

                }
                if (mo.CollisionRectangle().Intersects(tile.CollisionRectangle()) && tile.sheetPoint == TriggerTile && mo is Player)
                {
                    Trigger(tileManager.GetNumber(tileManager.collisionTiles.IndexOf(tile)));
                }
                if (mo.CollisionRectangle().Intersects(tile.CollisionRectangle()) && tile.sheetPoint == GoalTile && mo is Player)
                {
                    currentLevel++;
                    Load();
                    return;
                }
            }
            if (goX == true)
            {
                mo.UpdateX(gameTime, true);
            }
            else if (collided == true)
            {
                mo.UpdateX(gameTime, false);
            }
            if (goY == true)
            {
                mo.UpdateY(gameTime, true);
            }
            else if (collided == true)
            {
                mo.UpdateY(gameTime, false);
            }
        }
Ejemplo n.º 2
0
 public Enemy GetEnemyCollision(AnimatedObject mo)
 {
     foreach (Enemy enemy in enemies)
     {
         if (mo.CollisionRectangle().Intersects(enemy.CollisionRectangleForShots()))
         {
             return enemy;
         }
     }
     return null;
 }