コード例 #1
0
ファイル: GameProcess.cs プロジェクト: klyuchnikov/Tanks
        void processProjectiles_Elapsed(object sender, ElapsedEventArgs e)
        {
            for (var i = 0; i < Projectiles.Count; i++)
            {
                bool res = false;
                var  p   = Projectiles[i].Position;
                switch (Projectiles[i].Direction)
                {
                case Direction.Up:
                    res = Map[(int)p.Y - 1, (int)p.X] == CellMap.Ground || Map[(int)p.Y - 1, (int)p.X] == CellMap.Water;
                    break;

                case Direction.Left:
                    res = Map[(int)p.Y, (int)p.X - 1] == CellMap.Ground || Map[(int)p.Y, (int)p.X - 1] == CellMap.Water;
                    break;

                case Direction.Right:
                    res = Map[(int)p.Y, (int)p.X + 1] == CellMap.Ground || Map[(int)p.Y, (int)p.X + 1] == CellMap.Water;
                    break;

                case Direction.Down:
                    res = Map[(int)p.Y + 1, (int)p.X] == CellMap.Ground || Map[(int)p.Y + 1, (int)p.X] == CellMap.Water;
                    break;
                }
                if (res)
                {
                    Projectiles[i].Move();
                }
                else
                {
                    Projectiles[i].Dispose();
                    Projectiles.RemoveAt(i);
                }
            }
        }
コード例 #2
0
 public override void Update(List <Enemy> enemies, GameTime gameTime)
 {
     base.Update(enemies, gameTime);
     foreach (Enemy enemy in enemies)
     {
         if (AttackDelay <= gameTime.TotalGameTime && Game1.CheckIfInRange(enemy.Center, enemy.Radius, Center, AttackRange))
         {
             AttackDelay = gameTime.TotalGameTime.Add(new TimeSpan(0, 0, 0, 0, (int)(AttackSpeed * 1000)));
             Attack(enemy);
             break;
         }
     }
     for (int i = 0; i < Projectiles.Count; i++)
     {
         Projectiles[i].Update();
         for (int j = 0; j < enemies.Count; j++)
         {
             if (Game1.CheckIfInRange(enemies[j].Center, enemies[j].Radius, Projectiles[i].Center, Projectiles[i].Radius))
             {
                 Projectiles[i].Effect(enemies, AttackDMG, j);//TODO kolla så att det funkar eventuellt fixa nått nytt sätt
                 break;
             }
         }
         if (Projectiles[i].IsDead)
         {
             Projectiles.RemoveAt(i);
             i--;
         }
     }
 }
コード例 #3
0
        public override void Update(GameTime gameTime)
        {
            var removeQueue = new List <BGameObject>();

            foreach (var target in mForcedTargets)
            {
                if (target == null || target.Health < 0)
                {
                    removeQueue.Add(target);
                }
            }

            foreach (var target in removeQueue)
            {
                mForcedTargets.Remove(target);
            }

            if (mForcedTargets.Count == 0)
            {
                mForceTargets = false;
            }

            // Create projectiles
            mTimeSinceLastProjectileSpawn += gameTime.ElapsedGameTime.Milliseconds;
            if (mTimeSinceLastProjectileSpawn > mProjectileSpawnCooldownInSeconds * 1000 && !mTargetToFar && mTarget != null)
            {
                var shootPosition = new Vector2
                {
                    X = Position.X + (Width / 2f / 32) + TextureOffset.X,
                    Y = Position.Y + (Height / 2f / 32) + TextureOffset.Y
                };
                var rotation = -Math.Atan2(mTargetPosition.Y - shootPosition.Y, mTargetPosition.X - shootPosition.X);

                var nextDirection = new Vector2((float)Math.Cos(rotation), -(float)Math.Sin(rotation));
                Projectiles.Add(new Projectile(mProjectileTexture, shootPosition.X, shootPosition.Y, nextDirection));
                mTimeSinceLastProjectileSpawn = 0;
            }

            foreach (var projectile in Projectiles)
            {
                projectile.Update(gameTime);
            }

            if (Projectiles.Count > MaxProjectiles)
            {
                Projectiles.RemoveAt(0);
            }

            if (mForceTargets)
            {
                SetTargets(mForcedTargets);
            }
        }
コード例 #4
0
        public void Update(ref GameTime gameTime)
        {
            // Updates all creatures, if health is less than 0, they die. Removed from the creature less
            for (int i = Creatures.Count - 1; i >= 0; --i)
            {
                Creatures[i].Update(ref gameTime);
                if (Creatures[i].CurrentHealth <= 0)
                {
                    Creatures[i] = null;
                    Creatures.RemoveAt(i);
                }
            }

            // Updates all the items dropped on the ground
            for (int i = DroppedItems.Count - 1; i >= 0; --i)
            {
                DroppedItems[i].Update(ref gameTime);
                if (DroppedItems[i].LifeTime >= 300) // Removed after 5 mins
                {
                    DroppedItems[i] = null;
                    DroppedItems.RemoveAt(i);
                }
            }

            // Updates all the projectiles, if lifetime is less than or equal to 0, they are removed.
            for (int i = Projectiles.Count - 1; i >= 0; --i)
            {
                Projectiles[i].Update(ref gameTime);
                if (Projectiles[i].LifeTime <= 0)
                {
                    Projectiles[i] = null;
                    Projectiles.RemoveAt(i);
                }
            }

            // If the boss of the level is up then they are updates, when dead a text appears to press N to continue.
            Background.Update(gameTime);
            if (Boss.IsAlive)
            {
                Boss.Update(gameTime);
            }
            else
            {
                TradingManager.Update(gameTime);
                if (Game1.InputManager.KeyClicked(Microsoft.Xna.Framework.Input.Keys.N))
                {
                    Game1.WindowManager.GetGameplayWindow().NewLevel(NextLevel);
                }
            }
        }
コード例 #5
0
 private void UpdateBullets(GameTime gameTime)
 {
     //Update and remove non active bullets
     for (var i = Projectiles.Count - 1; i > -1; i--)
     {
         if (Projectiles[i].IsActive)
         {
             Projectiles[i].Update(gameTime);
         }
         else
         {
             scene.RemoveActor(Projectiles[i]);
             Projectiles.RemoveAt(i);
         }
     }
 }
コード例 #6
0
 public void UpdateProjectiles(float deltaTimeInSeconds)
 {
     for (int i = 0; i < Projectiles.Count; i++)
     {
         if (Projectiles[i] != null)
         {
             Projectile p = Projectiles[i];
             if (p.TimeSinceCreation > p.DespawnBulletAfter)
             {
                 Projectiles.RemoveAt(i);
             }
             else
             {
                 p.AddDeltaTime(deltaTimeInSeconds);
                 p.Translate();
             }
         }
     }
 }
コード例 #7
0
        private void TimerTask(object timerState)
        {
            List <int> projectilesToRemove = new List <int>();

            for (int e = 0; e < Projectiles.Count; e++)
            {
                if (!Projectiles[e].Update(1, MapData) || Projectiles[e].dist > 20)
                {
                    projectilesToRemove.Add(e);
                }
            }

            foreach (int e in projectilesToRemove)
            {
                try
                {
                    Projectiles.RemoveAt(e);
                }
                catch { } //could have thread collision, forget about it and try next update
            }

            Boss.Update(Players, Projectiles, _gameHub);

            List <dynamic> playersAndProjectiles = new List <dynamic>();

            playersAndProjectiles.AddRange(Players);
            try
            {
                playersAndProjectiles.AddRange(Projectiles);
                if (Boss.health > 0)
                {
                    playersAndProjectiles.Add(Boss);
                }
            }
            catch { } //could have thread collision, forget about it and try next update
            _gameHub.Clients.All.SendAsync("UpdateBros", playersAndProjectiles);
        }
コード例 #8
0
        public void CheckCollisions(Player shooter)
        {
            // 2 sets of loops, because we cannot modify the list while we are iterating through it
            List <int> indexesToRemove = new List <int>();

            var collidables = GameState.GetInstance().Collidables;
            var players     = PlayerRepository.Players;


            for (int i = 0; i < Projectiles.Count; i++)
            {
                for (int j = 0; j < players.Count; j++)
                {
                    if (!players[j].Name.Equals(shooter.Name) &&
                        !players[j].IsDead &&
                        CollisionTester.BoundingBoxTest(players[j], Projectiles[i].ProjectileSprite))
                    {
                        indexesToRemove.Add(i);

                        if (shooter.IsMainPlayer)
                        {
                            players[j].AddHealth(-this.Damage);
                            //GameState.GetInstance().ConnectionManager.Connection.SendAsync("UpdateScoresServer", shooter.ToDTO(), players[j].ToDTO());
                            GameState.GetInstance().ConnectionManagerProxy.Connection.SendAsync("UpdateScoresServer", shooter.ToDTO(), players[j].ToDTO());
                        }
                    }
                }

                for (int j = 0; j < collidables.Count; j++)
                {
                    if (CollisionTester.BoundingBoxTest(collidables[j], Projectiles[i].ProjectileSprite))
                    {
                        indexesToRemove.Add(i);

                        if (collidables[j] is HealthCrate)
                        {
                            Pickupable health = ((HealthCrate)collidables[j]).Pickupable;
                            //explosion.ExplosionCheck();
                            health.Position = collidables[j].Position;
                            //GameState.GetInstance().Pickupables.Add(health);
                            GameState.GetInstance().PickupableRep.GetIterator().Add(health);
                            collidables.RemoveAt(j);
                        }
                        else if (collidables[j] is ItemCrate)
                        {
                            Pickupable gun = ((ItemCrate)collidables[j]).Pickupable;
                            gun.Position = collidables[j].Position;
                            //GameState.GetInstance().Pickupables.Add(gun);
                            GameState.GetInstance().PickupableRep.GetIterator().Add(gun);
                            collidables.RemoveAt(j);
                        }
                    }
                }
            }

            bool OptimisedCollisionBullet(Sprite sprite, Sprite bullet)
            {
                Vector2f colidablePos = new Vector2f(sprite.Position.X + 32, sprite.Position.Y + 32);

                double squaredDist = VectorUtils.GetSquaredDistance(colidablePos, bullet.Position);

                if (squaredDist < 4096)
                {
                    return(CollisionTester.BoundingBoxTest(sprite, bullet));
                }

                return(false);
            }

            // Edge case: remove duplicate collisions
            indexesToRemove = indexesToRemove.Distinct().ToList();

            // iterate from the end because indexes shift when we modify the list
            for (int i = indexesToRemove.Count - 1; i >= 0; i--)
            {
                if (Projectiles.Count != 0)
                {
                    Projectiles.RemoveAt(indexesToRemove[i]);
                }
            }
        }
コード例 #9
0
        public void RefreshDrawing()
        {
            DForm.Data.ResetBuffers();
            DForm.Data.UpdateItem(GameDataType.Player, 0, CurrentPlayer.CurrentWeapons, CurrentPlayer.ImageIndex, (int)CurrentPlayer.GunRotation, CurrentPlayer.Facing, 3, CurrentPlayer.Location.ToPoint(), CurrentPlayer.Paint);
            List <int>    Indices      = new List <int>();
            List <int>    ArtIndices   = new List <int>();
            List <int>    ImageIndices = new List <int>();
            List <int>    Other        = new List <int>();
            List <double> Angles       = new List <double>();
            List <double> Sizes        = new List <double>();
            List <Color>  Colors       = new List <Color>();
            List <Point>  Locations    = new List <Point>();

            for (int i = 0; i < Enemies.Count; i++)
            {
                if (Enemies[i].Location != null)
                {
                    Indices.Add(i);
                    ArtIndices.Add(Enemies[i].ArtIndex);
                    ImageIndices.Add(Enemies[i].ImageIndex);
                    Other.Add(0);
                    Colors.Add(Enemies[i].Paint);
                    Sizes.Add(Enemies[i].Scale);
                    Locations.Add(Enemies[i].Location.ToPoint());
                    if (this._CurrentBoss != null && this._CurrentBoss.Auxes.Contains(Enemies[i]))
                    {
                        Angles.Add(DrawForm.GetAngleDegree((CurrentPlayer.Location).ToPoint(), this._CurrentBoss.Location.ToPoint()));
                    }
                    else
                    {
                        Angles.Add(DrawForm.GetAngleDegree((CurrentPlayer.Location).ToPoint(), Enemies[i].Location.ToPoint()));
                    }
                }
            }
            for (int i = Enemies.Count - 1; i >= 0; i--)
            {
                if (!(Enemies[i].Location != null))
                {
                    Enemies.RemoveAt(i);
                }
            }
            while (DForm.Data.Working)
            {
                ;
            }
            DForm.Data.UpdateItems(GameDataType.Enemy, Indices.ToArray(), ArtIndices.ToArray(), ImageIndices.ToArray(), Other.ToArray(), Angles.ToArray(), Sizes.ToArray(), Locations.ToArray(), Colors.ToArray());
            Indices      = new List <int>();
            ArtIndices   = new List <int>();
            ImageIndices = new List <int>();
            Angles       = new List <double>();
            Sizes        = new List <double>();
            Locations    = new List <Point>();
            Colors       = new List <Color>();
            Other        = new List <int>();
            for (int i = 0; i < Projectiles.Count; i++)
            {
                if (Projectiles[i].Location != null)
                {
                    Indices.Add(i);
                    ArtIndices.Add(Projectiles[i].ArtIndex);
                    ImageIndices.Add(Projectiles[i].ImageIndex);
                    Angles.Add(Projectiles[i].Facing);
                    Sizes.Add(Projectiles[i].Scale);
                    Other.Add(Projectiles[i].Spin);
                    Colors.Add(Projectiles[i].Paint);
                    Locations.Add(Projectiles[i].Location.ToPoint());
                }
            }
            for (int i = Projectiles.Count - 1; i >= 0; i--)
            {
                if (!(Projectiles[i].Location != null))
                {
                    Projectiles.RemoveAt(i);
                }
            }
            while (DForm.Data.Working)
            {
                ;
            }
            DForm.Data.UpdateItems(GameDataType.Projectile, Indices.ToArray(), ArtIndices.ToArray(), ImageIndices.ToArray(), Other.ToArray(), Angles.ToArray(), Sizes.ToArray(), Locations.ToArray(), Colors.ToArray());
            Indices      = new List <int>();
            ArtIndices   = new List <int>();
            ImageIndices = new List <int>();
            Angles       = new List <double>();
            Sizes        = new List <double>();
            Locations    = new List <Point>();
            Colors       = new List <Color>();
            Other        = new List <int>();
            for (int i = 0; i < Effects.Count; i++)
            {
                if (Effects[i].Location != null)
                {
                    Indices.Add(i);
                    ArtIndices.Add(Effects[i].ArtIndex);
                    ImageIndices.Add(Effects[i].ImageIndex);
                    Angles.Add(0);
                    Sizes.Add(1);
                    Other.Add(0);
                    Colors.Add(Color.White);
                    Locations.Add(Effects[i].Location.ToPoint());
                }
            }
            for (int i = Effects.Count - 1; i >= 0; i--)
            {
                if (!(Effects[i].Location != null))
                {
                    Effects.RemoveAt(i);
                }
            }
            while (DForm.Data.Working)
            {
                ;
            }
            DForm.Data.UpdateItems(GameDataType.Effect, Indices.ToArray(), ArtIndices.ToArray(), ImageIndices.ToArray(), Other.ToArray(), Angles.ToArray(), Sizes.ToArray(), Locations.ToArray(), Colors.ToArray());
            Indices      = new List <int>();
            ArtIndices   = new List <int>();
            ImageIndices = new List <int>();
            Angles       = new List <double>();
            Sizes        = new List <double>();
            Locations    = new List <Point>();
            Colors       = new List <Color>();
            Other        = new List <int>();
            for (int i = 0; i < PowerUps.Count; i++)
            {
                if (PowerUps[i].Location != null)
                {
                    Indices.Add(i);
                    ArtIndices.Add((int)(PowerUps[i].Type));
                    ImageIndices.Add(0);
                    Angles.Add(0);
                    Sizes.Add(1);
                    Other.Add(0);
                    Colors.Add(Color.White);
                    Locations.Add(PowerUps[i].Location.ToPoint());
                }
            }
            for (int i = PowerUps.Count - 1; i >= 0; i--)
            {
                if (!(PowerUps[i].Location != null))
                {
                    PowerUps.RemoveAt(i);
                }
            }
            while (DForm.Data.Working)
            {
                ;
            }
            DForm.Data.UpdateItems(GameDataType.PowerUp, Indices.ToArray(), ArtIndices.ToArray(), ImageIndices.ToArray(), Other.ToArray(), Angles.ToArray(), Sizes.ToArray(), Locations.ToArray(), Colors.ToArray());
            DForm.Data.SwapBuffers();
            DForm.DrawDataReady = true;
        }