static void Tick(Player player, List <Enemy> enemies, List <Bullet> bullets, float timeDelta) { //najpierw ruch player.Tick(timeDelta); for (int i = 0; i < enemies.Count; i++) { enemies[i].Tick(timeDelta); } for (int i = 0; i < bullets.Count; i++) { bullets[i].Tick(timeDelta); } //potem polecenia wystrzalu if (player.PlayerShoot) { Bullet bullet = new Bullet(player.X, player.Y, EOwner.PLAYER, true); if (!bullet.deleteThis) { bullets.Add(bullet); } } for (int i = 0; i < enemies.Count; i++) { if (enemies[i].EnemyShoot) { Bullet bullet = new Bullet(enemies[i].X, enemies[i].Y, EOwner.ENEMY, false); if (!bullet.deleteThis) { bullets.Add(bullet); } } } //kolizje kul for (int i = 0; i < bullets.Count; i++) { for (int j = 0; j < enemies.Count; j++) { if (enemies[j].CheckCollision(bullets[i])) { bullets[i].Kill(enemies[j]); } } if (player.CheckCollision(bullets[i])) { bullets[i].Kill(player); } } //sprzatamy smieci for (int i = 0; i < enemies.Count; i++) { if (enemies[i].IsDead()) { player.pointsScored += enemies[i].points; enemies.Remove(enemies[i]); i--; } } for (int i = 0; i < bullets.Count; i++) { if (bullets[i].deleteThis) { bullets.Remove(bullets[i]); i--; } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } if (!player.Disposing && playerBullet == default(Bullet) && Keyboard.GetState().IsKeyDown(Keys.Space)) { playerBullet = new Bullet(player); bullets.Add(playerBullet); } if (enemyBullet == default(Bullet)) { enemyBullet = enemyGrid.GetBullet(); if (enemyBullet != null) { bullets.Add(enemyBullet); } } enemyGrid.Update(gameTime); player.Update(gameTime); foreach (var bullet in bullets) { bullet.Update(gameTime); } base.Update(gameTime); foreach (var bullet in bullets) { player.CheckCollision(bullet, this); enemyGrid.CheckCollision(bullet, this); if (bullet.Disposing) { removableBullets.Add(bullet); } } foreach (var bullet in removableBullets) { bullets.Remove(bullet); } removableBullets.Clear(); if (playerBullet?.Disposing == true) { playerBullet = default; } if (enemyBullet?.Disposing == true) { enemyBullet = default; } elapsedTime += gameTime.ElapsedGameTime; if (elapsedTime > TimeSpan.FromSeconds(1)) { elapsedTime -= TimeSpan.FromSeconds(1); frameRate = frameCounter; frameCounter = 0; } if (player.Disposing) { Reset(); } }