public void Update(GameTime gt) { float dt = (float)gt.ElapsedGameTime.TotalSeconds; // Update the physics multiple times per frame (in this case, 10 times per frame) for (int i = 0; i < _physicsSubstep; i++) { PhysicsManager.Update(dt / _physicsSubstep); } AIManager.Update(this, dt); // Check if there are less enemies than the maximum and add more as necessary if (Enemies.Count < MaximumEnemies) { SpawnEnemy(); } // Update the spawn points foreach (SpawnPoint sp in SpawnPoints) { if (sp.TimeSinceLastSpawn > sp.RespawnDelay && !ActiveSpawnPoints.Contains(sp)) { ActiveSpawnPoints.Add(sp); } sp.TimeSinceLastSpawn += dt; } // Health shouldn't be below 0 if (PortalHealth < 0) { PortalHealth = 0; } // If you haven't lost yet, keep updating the score if (!GameOver) { Score += dt; } }
// Spawns an enemy public void SpawnEnemy() { if (Enemies.Count < MaximumEnemies && ActiveSpawnPoints.Count > 0) { SpawnPoint sp = ActiveSpawnPoints[RandomGen.Next(0, ActiveSpawnPoints.Count - 1)]; Character enemy = new Character(false); enemy.Behaviour = new AIBehaviour(enemy, AIManager.NodeManager); enemy.Position = sp.Position; RigidBody enemyBody = enemy.RigidBody; enemyBody.Position = enemy.Position; enemyBody.Width = 32; enemyBody.Height = 32; enemyBody.CollidableObject = new BoxCollider(); enemy.TexturePos = new Vector2(enemy.Position.X - enemyBody.Width / 2, enemy.Position.Y - enemyBody.Height / 2); PhysicsManager.DynamicObjects.Add(enemy); PhysicsManager.AllPhysObjects.Add(enemy); Enemies.Add(enemy); sp.TimeSinceLastSpawn = 0; ActiveSpawnPoints.Remove(sp); } }