Ejemplo n.º 1
0
        private void Collision(float dt)
        {
            var delWalls   = new List <WallViewModel>();
            var delTanks   = new List <TankViewModel>();
            var delApples  = new List <AppleViewModel>();
            var delBullets = new List <BulletViewModel>();

            if (IsGame)
            {
                DynamicEntity player = Player.Clone() as DynamicEntity;
                player.Move(dt);

                if (ObjectInScreen(player) &&
                    Rivers.Find(river => IsCollision(river, player)) == null &&
                    Walls.Find(wall => IsCollision(wall, player)) == null)
                {
                    bool isFree = true;

                    foreach (var tank in Tanks)
                    {
                        if (IsCollision(tank, player))
                        {
                            isFree = false;
                            CreateBang(player);
                            GameOver();
                        }
                    }

                    if (isFree)
                    {
                        Player.Move(dt);
                    }
                }

                Apples.ForEach(apple =>
                {
                    if (IsCollision(player, apple))
                    {
                        delApples.Add(apple);
                        Score++;
                    }
                });
            }

            Tanks.ForEach(t =>
            {
                t.Move(dt);
                if (!ObjectInScreen(t) ||
                    Walls.Find(wall => IsCollision(wall, t)) != null ||
                    Rivers.Find(river => IsCollision(river, t)) != null ||
                    Tanks.Find(tnk => tnk != t ? IsCollision(tnk, t) : false) != null)
                {
                    t.ChangeDirection();
                    t.Move(dt);
                }
            });

            Bullets.ForEach(bullet =>
            {
                bool delBullet = false;

                if (!ObjectInScreen(bullet))
                {
                    delBullet = true;
                }

                Walls.ForEach(wall =>
                {
                    if (IsCollision(bullet, wall))
                    {
                        if (wall.Destroyable)
                        {
                            delWalls.Add(wall);
                        }
                        delBullet = true;
                        CreateBang(bullet);
                    }
                });

                Tanks.ForEach(tank =>
                {
                    if (IsCollision(tank, bullet) && bullet.Shooter != tank)
                    {
                        delBullet = true;
                        delTanks.Add(tank);
                        CreateBang(tank);
                    }
                });

                if (IsGame)
                {
                    if (IsCollision(bullet, Player) && bullet.Shooter != Player)
                    {
                        delBullet = true;
                        CreateBang(Player);
                        GameOver();
                    }
                }

                if (delBullet)
                {
                    delBullets.Add(bullet);
                }
                else
                {
                    bullet.Move(dt);
                }
            });

            delWalls.ForEach(wall => Walls.Remove(wall));
            delTanks.ForEach(tank => Tanks.Remove(tank));
            delApples.ForEach(apple => Apples.Remove(apple));
            delBullets.ForEach(bullet => Bullets.Remove(bullet));
        }