private void MoveSprite(ref GunSprite gS, TileDirectionRelSprite tile, Rectangle rectangle)
 {
     if (tile.below && !gS.jumping && rectangle.IntersectsWith(new Rectangle(new Point((int)tile.tile.x, (int)tile.tile.y), new Size(Tile.WIDTH, 7))))
     {
         gS.y       = tile.tile.y - gS.height - 1;
         gS.falling = false;
         gS.yDir    = 0;
     }
     else if (tile.left)
     {
         gS.x = tile.tile.x + Tile.WIDTH;
         if (gS.gun.left)
         {
             gS.x += gS.gun.width + 1;
         }
     }
     else if (tile.right)
     {
         gS.x = tile.tile.x - gS.width - 1;
         if (!gS.gun.left)
         {
             gS.x -= gS.gun.width;
         }
     }
 }
        public void Update(float elapsed, ref Player player, Camera _camera, ref List <Enemy> enemies)
        {
            overallCamera.x += -1 * _camera.x;
            overallCamera.y += -1 * _camera.y;

            foreach (List <Tile> r in map)
            {
                foreach (Tile c in r)
                {
                    c.Update(elapsed, _camera);
                    if (c.tileType == TileType.Air || player == null)
                    {
                        continue;
                    }
                    GunSprite gS = (GunSprite)player;
                    CollisionBullets(c, ref gS);
                    Collision(ref gS, c, elapsed);

                    foreach (Enemy enemy in enemies)
                    {
                        gS = (GunSprite)enemy;
                        CollisionBullets(c, ref gS);
                        Collision(ref gS, c, elapsed);
                    }
                }
            }

            if (player == null || enemies == null)
            {
                return;
            }
            if (player.tileDirRelSprites.Count == 0)
            {
                player.falling = true;
            }
            else
            {
                foreach (TileDirectionRelSprite t in player.tileDirRelSprites)
                {
                    GunSprite gS = (GunSprite)player;
                    MoveSprite(ref gS, t, gS.rectBot);
                }
            }

            foreach (Enemy enemy in enemies)
            {
                if (enemy.tileDirRelSprites.Count == 0)
                {
                    enemy.falling = true;
                }
                else
                {
                    foreach (TileDirectionRelSprite t in enemy.tileDirRelSprites)
                    {
                        GunSprite gS = (GunSprite)enemy;
                        MoveSprite(ref gS, t, gS.rectBot);
                    }
                }
            }
        }
        private void Collision(ref GunSprite gS, Tile tile, float elapsed)
        {
//			Tile mBLeft, mBRight;
//			mBLeft = map [(int)((gS.y + overallCamera.y) / Tile.HEIGHT) + 1] [(int)((gS.x + overallCamera.x) /
//				Tile.WIDTH)];
//			mBRight = map [(int)((gS.y + overallCamera.y) / Tile.HEIGHT) + 1] [(int)((gS.width + gS.x +
//				overallCamera.x) / Tile.WIDTH)];
//
//			if (mBLeft.tileType != TileType.Air || mBRight.tileType != TileType.Air) {
//				gS.falling = false;
//			} else if (gS.falling == false && !gS.jumping) {
//				gS.falling = true;
//			}

            Rectangle rectLeft, rectRight, rectTop, rectBot;

            if (gS != null)
            {
                float xSpeed = gS.xDir * elapsed * gS.MOVE_SPEED;
                float ySpeed = gS.yDir * elapsed * gS.FALL_SPEED;
                rectLeft = new Rectangle(new Point((int)(gS.x + xSpeed), (int)(gS.y + ySpeed)),
                                         new Size(1, gS.height));
                rectRight = new Rectangle(new Point((int)(gS.x + xSpeed + gS.width), (int)(gS.y +
                                                                                           ySpeed)), new Size(1, gS.height));
                rectTop = new Rectangle(new Point((int)(gS.x + xSpeed), ((int)(gS.y + ySpeed))),
                                        new Size(Player.WIDTH, 1));
                rectBot = new Rectangle(new Point((int)(gS.x + xSpeed), (int)(gS.y + gS.height +
                                                                              ySpeed + 1)), new Size(gS.width, 1));
                gS.rectBot = rectBot;
                if (gS.gun.left)
                {
                    rectLeft.X -= gS.gun.width;
                }
                else
                {
                    rectRight.X += gS.gun.width;
                }
            }

            if (tile.outOfSight || gS == null)
            {
                return;
            }
            TileDirectionRelSprite t = new TileDirectionRelSprite();

            t.tile  = tile;
            t.below = tile.rect.IntersectsWith(rectBot);
            t.above = tile.rect.IntersectsWith(rectTop);
            t.left  = tile.rect.IntersectsWith(rectLeft);
            t.right = tile.rect.IntersectsWith(rectRight);
            if (t.below || t.above || t.left || t.right)
            {
                if (t.tile.tileType == TileType.Fire)
                {
                    gS.dead = true;
                }
                gS.tileDirRelSprites.Add(t);
            }
        }
 public Gun(GunType _gunType, GunSprite _parent)
 {
     bullets = new List <Bullet> ();
     parent  = _parent;
     gunType = _gunType;
     width   = Tile.WIDTH;
     height  = Tile.HEIGHT;
     ChangeGun();
 }
 private void CollisionBullets(Tile c, ref GunSprite gS)
 {
     if (gS.gun.bullets != null)
     {
         foreach (Bullet b in gS.gun.bullets)
         {
             if (c.rect.IntersectsWith(b.rect))
             {
                 gS.gun.bullets [gS.gun.bullets.IndexOf(b)].dead = true;
             }
         }
     }
 }
 private void MoveSprite(ref GunSprite gS, TileDirectionRelSprite tile, Rectangle rectangle)
 {
     if (tile.below && !gS.jumping && rectangle.IntersectsWith (new Rectangle (new Point ((int)tile.tile.x, (int)tile.tile.y), new Size (Tile.WIDTH, 7)))) {
         gS.y = tile.tile.y - gS.height - 1;
         gS.falling = false;
         gS.yDir = 0;
     } else if (tile.left) {
         gS.x = tile.tile.x + Tile.WIDTH;
         if (gS.gun.left) {
             gS.x += gS.gun.width + 1;
         }
     } else if (tile.right) {
         gS.x = tile.tile.x - gS.width - 1;
         if (!gS.gun.left) {
             gS.x -= gS.gun.width;
         }
     }
 }
 private void CollisionBullets(Tile c, ref GunSprite gS)
 {
     if (gS.gun.bullets != null) {
         foreach (Bullet b in gS.gun.bullets) {
             if (c.rect.IntersectsWith (b.rect)) {
                 gS.gun.bullets [gS.gun.bullets.IndexOf (b)].dead = true;
             }
         }
     }
 }
        private void Collision(ref GunSprite gS, Tile tile, float elapsed)
        {
            //			Tile mBLeft, mBRight;
            //			mBLeft = map [(int)((gS.y + overallCamera.y) / Tile.HEIGHT) + 1] [(int)((gS.x + overallCamera.x) /
            //				Tile.WIDTH)];
            //			mBRight = map [(int)((gS.y + overallCamera.y) / Tile.HEIGHT) + 1] [(int)((gS.width + gS.x +
            //				overallCamera.x) / Tile.WIDTH)];
            //
            //			if (mBLeft.tileType != TileType.Air || mBRight.tileType != TileType.Air) {
            //				gS.falling = false;
            //			} else if (gS.falling == false && !gS.jumping) {
            //				gS.falling = true;
            //			}

            Rectangle rectLeft, rectRight, rectTop, rectBot;
            if (gS != null) {
                float xSpeed = gS.xDir * elapsed * gS.MOVE_SPEED;
                float ySpeed = gS.yDir * elapsed * gS.FALL_SPEED;
                rectLeft = new Rectangle (new Point ((int)(gS.x + xSpeed), (int)(gS.y + ySpeed)),
                    new Size (1, gS.height));
                rectRight = new Rectangle (new Point ((int)(gS.x + xSpeed + gS.width), (int)(gS.y +
                    ySpeed)), new Size (1, gS.height));
                rectTop = new Rectangle (new Point ((int)(gS.x + xSpeed), ((int)(gS.y + ySpeed))),
                    new Size (Player.WIDTH, 1));
                rectBot = new Rectangle (new Point ((int)(gS.x + xSpeed), (int)(gS.y + gS.height +
                    ySpeed + 1)), new Size (gS.width, 1));
                gS.rectBot = rectBot;
                if (gS.gun.left) {
                    rectLeft.X -= gS.gun.width;
                } else {
                    rectRight.X += gS.gun.width;
                }
            }

            if (tile.outOfSight || gS == null) {
                return;
            }
            TileDirectionRelSprite t = new TileDirectionRelSprite ();
            t.tile = tile;
            t.below = tile.rect.IntersectsWith (rectBot);
            t.above = tile.rect.IntersectsWith (rectTop);
            t.left = tile.rect.IntersectsWith (rectLeft);
            t.right = tile.rect.IntersectsWith (rectRight);
            if (t.below || t.above || t.left || t.right) {
                if (t.tile.tileType == TileType.Fire) {
                    gS.dead = true;
                }
                gS.tileDirRelSprites.Add (t);
            }
        }
 public Gun(GunType _gunType, GunSprite _parent)
 {
     bullets = new List<Bullet> ();
     parent = _parent;
     gunType = _gunType;
     width = Tile.WIDTH;
     height = Tile.HEIGHT;
     ChangeGun ();
 }