public override void Update(GameTime gameTime) { base.Update(gameTime); // Helpers if (_stageCompleted) { _stageCompletedHelper.Update(gameTime); } _backgroundHelper.Update(_camera); _pauseHelper.Update(gameTime); if (_pauseHelper.Paused) { return; } _time += gameTime.ElapsedGameTime.Duration(); DebugValues["Timer"] = _time.ToString(); var bossCollapsing = GameMap.Instance.CurrentMapId == 5 && _enemies.Count > 0 && ((Boss)_enemies[0]).Collapsing; _player.Update(gameTime, _stageCompleted || bossCollapsing); if (_player.RequestRespawn) { HandlePlayerRespawn(); } foreach (var projectile in _projectiles) { projectile.Update(gameTime); if (projectile.Subject == ProjectileSubject.FromEnemy && projectile.BoundingBox.Intersects(_player.BoundingRectangle)) { var lastHearts = _player.HP; _player.ReceiveAttack(projectile.Damage, projectile.LastPosition); if (lastHearts - _player.HP > 0) { _heartsLost += lastHearts - _player.HP; } } if (projectile.RequestErase) { _projectilesToRemove.Add(projectile); } } foreach (var enemy in _enemies) { enemy.Update(gameTime); if (enemy.HasViewRange && enemy.ViewRangeCooldown <= 0f && _camera.Contains(enemy.BoundingRectangle) != ContainmentType.Disjoint && enemy.ViewRange.Intersects(_player.BoundingRectangle)) { enemy.PlayerOnSight(_player.Position); } if (!enemy.Dying && enemy.ContactDamageEnabled && enemy.BoundingRectangle.Intersects(_player.BoundingRectangle)) { var lastHearts = _player.HP; _player.ReceiveAttackWithRect(1, enemy.BoundingRectangle); if (lastHearts - _player.HP > 0) { _heartsLost += lastHearts - _player.HP; } } foreach (var projectile in _projectiles) { if (projectile.Subject == ProjectileSubject.FromPlayer) { if (!enemy.Dying && !enemy.IsImunity && enemy.CanReceiveAttacks && projectile.BoundingBox.Intersects(enemy.BoundingRectangle)) { if (enemy.EnemyType == EnemyType.TurtleWheel && enemy.InWheelMode) { projectile.Acceleration = new Vector2(projectile.Acceleration.X * -1.7f, _rand.Next(-4, 5)); _deflectSe.PlaySafe(); CreateSparkParticle(projectile.Position); projectile.Subject = ProjectileSubject.FromEnemy; } else { enemy.ReceiveAttack(projectile.Damage, projectile.LastPosition); projectile.Destroy(); } } } else if (projectile.BoundingBox.Intersects(_player.BoundingRectangle)) { var lastHearts = _player.HP; _player.ReceiveAttack(projectile.Damage, projectile.LastPosition); if (lastHearts - _player.HP > 0) { _heartsLost += lastHearts - _player.HP; } projectile.Destroy(); } if (projectile.RequestErase) { _projectilesToRemove.Add(projectile); } } if (enemy is Boss) { var boss = (Boss)enemy; if (boss.RequestingHatDrop) { _player.PerformHatDrop(); } if (boss.Collapsing) { SoundManager.SetBgmVolume(0.3f); if (_ambienceSe != null) { _ambienceSe.Volume = 0.3f; } } } if (enemy.RequestErase) { _enemiesToRemove.Add(enemy); _enemiesDefeated++; } } foreach (var spike in GameMap.Instance.Spikes) { if (_player.BoundingRectangle.Intersects(spike) && !_player.TouchedSpikes) { _heartsLost += _player.HP; _player.CharacterSprite.RemoveImmunity(); _player.ReceiveAttackWithRect(999, spike); _player.TouchedSpikes = true; } } foreach (var coin in _coins) { coin.Update(gameTime); var sprite = coin.CoinSprite; if (sprite.TextureRegion.Name.IndexOf("CoinSparkle") > 0 && sprite.Looped) { _coinsToRemove.Add(coin); } else if (sprite.TextureRegion.Name.IndexOf("CoinSparkle") < 0 && _player.BoundingRectangle.Intersects(sprite.BoundingBox)) { _coinsCollected += 1; PlayerManager.Instance.AddCoins(1); sprite.SetTexture(ImageManager.loadMisc("CoinSparkle"), false); sprite.SetDelay(80); _coinsSe.PlaySafe(); } } foreach (var shop in _shops) { if (shop.IsActive && !_player.BoundingRectangle.Intersects(shop.BoundingRectangle)) { shop.SetActive(false); } else if (!shop.IsActive && _player.BoundingRectangle.Intersects(shop.BoundingRectangle)) { shop.SetActive(true); } if (shop.IsActive && ((_player.IsAttacking && !shop.IsDenied()) || shop.NeedDeny())) { shop.SetArrowDenyState(); } else if (shop.IsDenied() && !_player.IsAttacking) { shop.SetArrowNormalState(); } shop.Update(gameTime); } foreach (var checkpoint in _checkpoints) { if (!checkpoint.IsChecked && _player.BoundingRectangle.Intersects(checkpoint.BoundingBox)) { if (checkpoint.IsEndFlag) { FinishStage(false); } else { checkpoint.OnPlayerCheck(); var checkpointData = new CheckpointData() { Activated = true, Checkpoint = checkpoint, MapEnemies = new List <Enemy>(_enemies.Select(enemy => enemy.Clone <Enemy>())), MapCoins = new List <GameCoin>(_coins.Select(coin => coin.Clone())), Ammo = PlayerManager.Instance.Ammo, Coins = PlayerManager.Instance.Coins, CoinsCollected = _coinsCollected }; _lastCheckpoint = checkpointData; } } checkpoint.Update(gameTime); } foreach (var enemy in _enemiesToRemove) { enemy.DisposeEnemyTextures(); enemy.Dispose(); _enemies.Remove(enemy); } foreach (var projectile in _projectilesToRemove) { _projectiles.Remove(projectile); } foreach (var coin in _coinsToRemove) { _coins.Remove(coin); } _enemiesToRemove.Clear(); _projectilesToRemove.Clear(); _coinsToRemove.Clear(); UpdateCamera(); }
public override void Update(GameTime gameTime) { base.Update(gameTime); // Helpers if (_stageCompleted) { _stageCompletedHelper.Update(gameTime); } _backgroundHelper.Update(_camera); _pauseHelper.Update(gameTime); if (_pauseHelper.Paused) { return; } _time += gameTime.ElapsedGameTime.Duration(); DebugValues["Timer"] = _time.ToString(); _player.Update(gameTime, _stageCompleted); if (_player.RequestRespawn) { HandlePlayerRespawn(); } for (var i = 0; i < _projectiles.Count; i++) { _projectiles[i].Update(gameTime); if (_projectiles[i].Subject == ProjectileSubject.FromEnemy && _projectiles[i].BoundingBox.Intersects(_player.BoundingRectangle)) { _player.ReceiveAttack(_projectiles[i].Damage, _projectiles[i].LastPosition); } if (_projectiles[i].RequestErase) { _projectiles.Remove(_projectiles[i]); } } for (var i = 0; i < _enemies.Count; i++) { _enemies[i].Update(gameTime); if (_enemies[i].HasViewRange && _enemies[i].ViewRangeCooldown <= 0f && _camera.Contains(_enemies[i].BoundingRectangle) != ContainmentType.Disjoint && _enemies[i].ViewRange.Intersects(_player.BoundingRectangle)) { _enemies[i].PlayerOnSight(_player.Position); } if (!_enemies[i].Dying && _enemies[i].ContactDamageEnabled && _enemies[i].BoundingRectangle.Intersects(_player.BoundingRectangle)) { var lastHearts = _player.HP; _player.ReceiveAttackWithRect(1, _enemies[i].BoundingRectangle); if (lastHearts - _player.HP > 0) { _heartsLost += lastHearts - _player.HP; } } for (var j = 0; j < _projectiles.Count; j++) { if (_projectiles[j].Subject == ProjectileSubject.FromPlayer) { if (!_enemies[i].Dying && !_enemies[i].IsImunity && _enemies[i].CanReceiveAttacks && _projectiles[j].BoundingBox.Intersects(_enemies[i].BoundingRectangle)) { if (_enemies[i].EnemyType == EnemyType.TurtleWheel && _enemies[i].InWheelMode) { _projectiles[j].Acceleration = new Vector2(_projectiles[j].Acceleration.X * -1.7f, _rand.Next(-4, 5)); CreateSparkParticle(_projectiles[j].Position); _projectiles[j].Subject = ProjectileSubject.FromEnemy; } else { _enemies[i].ReceiveAttack(_projectiles[j].Damage, _projectiles[j].LastPosition); _projectiles[j].Destroy(); } } } else if (_projectiles[j].BoundingBox.Intersects(_player.BoundingRectangle)) { var lastHearts = _player.HP; _player.ReceiveAttack(_projectiles[j].Damage, _projectiles[j].LastPosition); if (lastHearts - _player.HP > 0) { _heartsLost += lastHearts - _player.HP; } _projectiles[j].Destroy(); } if (_projectiles[j].RequestErase) { _projectiles.Remove(_projectiles[j]); } } if (_enemies[i].RequestErase) { _enemies.Remove(_enemies[i]); _enemiesDefeated++; } } for (var i = 0; i < GameMap.Instance.Spikes.Count; i++) { var spike = GameMap.Instance.Spikes[i]; if (_player.BoundingRectangle.Intersects(spike) && !_player.TouchedSpikes) { _heartsLost += _player.HP; _player.CharacterSprite.RemoveImmunity(); _player.ReceiveAttackWithRect(999, spike); _player.TouchedSpikes = true; } } for (var i = 0; i < _coins.Count; i++) { _coins[i].Update(gameTime); var sprite = _coins[i].CoinSprite; if (sprite.TextureRegion.Name.IndexOf("CoinSparkle") > 0 && sprite.Looped) { _coins.Remove(_coins[i]); } else if (sprite.TextureRegion.Name.IndexOf("CoinSparkle") < 0 && _player.BoundingRectangle.Intersects(sprite.BoundingBox)) { _coinsCollected += 1; PlayerManager.Instance.AddCoins(1); sprite.SetTexture(ImageManager.loadMisc("CoinSparkle"), false); sprite.SetDelay(80); SoundManager.PlaySafe(_coinsSe); } } for (var i = 0; i < _shops.Count; i++) { if (_shops[i].IsActive && !_player.BoundingRectangle.Intersects(_shops[i].BoundingRectangle)) { _shops[i].SetActive(false); } else if (!_shops[i].IsActive && _player.BoundingRectangle.Intersects(_shops[i].BoundingRectangle)) { _shops[i].SetActive(true); } if (_shops[i].IsActive && _player.IsAttacking && !_shops[i].IsDenied()) { _shops[i].SetArrowDenyState(); } else if (_shops[i].IsDenied() && !_player.IsAttacking) { _shops[i].SetArrowNormalState(); } _shops[i].Update(gameTime); } for (var i = 0; i < _checkpoints.Count; i++) { if (!_checkpoints[i].IsChecked && _player.BoundingRectangle.Intersects(_checkpoints[i].BoundingBox)) { if (_checkpoints[i].IsEndFlag) { FinishStage(false); } else { _checkpoints[i].OnPlayerCheck(); var checkpointData = new CheckpointData() { Activated = true, Checkpoint = _checkpoints[i], MapEnemies = new List <Enemy>(_enemies.Select(enemy => enemy.Clone <Enemy>())), MapCoins = new List <GameCoin>(_coins.Select(coin => coin.Clone())), Ammo = PlayerManager.Instance.Ammo, Coins = PlayerManager.Instance.Coins, CoinsCollected = _coinsCollected }; _lastCheckpoint = checkpointData; } } _checkpoints[i].Update(gameTime); } UpdateCamera(); }