public CGameState(int num_enemies) { if (num_enemies > 0) { Enemies = new CEnemy[num_enemies]; } for (int i = 0; i < num_enemies; i++) { Enemies[i] = new CEnemy(); } }
//****************************************************************// // This function updates the position of each enemy in the game. // // If it would be in collision with a wall the position is not // // changed. // //****************************************************************// private void UpdatePosition(CSprite sprite, float time_delta) { ////////////////////////////////////////////////////////////////// // Update a sprite to be in a new position using it's velocity. // If it is in a collision, we leave it in current location. // however, if it is an enemy we move it to it's target position // ready to find a new direction to move into. CPoint2f newPos = new CPoint2f(); newPos.X = sprite.Position.X + time_delta * sprite.Velocity.X; newPos.Y = sprite.Position.Y + time_delta * sprite.Velocity.Y; bool collisionFire = false; if (sprite is CEnemy) { collisionFire = IsInCollisionWithFire(newPos); } if (!IsInCollisionWithWall(newPos) && !collisionFire) { sprite.Position.X = newPos.X; sprite.Position.Y = newPos.Y; } else { /////////////////////////////////////////////////////////////// // See if sprite is an enemy, if so update it's position to be // the target position. NOTE: this could make odd behaviour if // enemy walks too far! if (sprite is CEnemy) { CEnemy enemy = (CEnemy)sprite; enemy.Position.X = enemy.TargetPosition.X; enemy.Position.Y = enemy.TargetPosition.Y; } } }