private void ResolvePlayerProjectileCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.Projectiles.Count; i++) { if (graphicalObjects.Projectiles[i].Type == ProjectileType.Enemy) { byte projectileX = (byte)graphicalObjects.Projectiles[i].Xposition; byte projectileY = (byte)graphicalObjects.Projectiles[i].Yposition; byte projectileFutureX = graphicalObjects.Projectiles[i].XfuturePosition; byte projectileFutureY = graphicalObjects.Projectiles[i].YfuturePosition; if ((projectileX == (byte)graphicalObjects.SpaceShipPlayerOne.Xposition && projectileY == (byte)graphicalObjects.SpaceShipPlayerOne.Yposition) || (projectileFutureX == (byte)graphicalObjects.SpaceShipPlayerOne.Xposition && projectileFutureY == (byte)graphicalObjects.SpaceShipPlayerOne.Yposition)) { if (graphicalObjects.SpaceShipPlayerOne.ShieldTimeAvailable == 0) { graphicalObjects.SpaceShipPlayerOne.ChangeHealth(Projectile.Damage); this.soundEffects.PlayHit(); } else { this.soundEffects.PlayDullHit(); } graphicalObjects.Projectiles.Remove(graphicalObjects.Projectiles[i]); } } } }
private static void FindEmptySpace(GraphicalObjectContainer graphicalObjects, out byte potentialXposition, out byte potentialYposition) { Random randomGenerator = new Random(); bool generationOnAnEmptySpaceSuccessful = false; potentialXposition = 0; potentialYposition = 0; while (!generationOnAnEmptySpaceSuccessful) { generationOnAnEmptySpaceSuccessful = true; potentialXposition = (byte)randomGenerator.Next(1, Console.BufferWidth - 2); potentialYposition = (byte)randomGenerator.Next(1, Console.BufferHeight - 2); List <GraphicalObject> currentGraphicalObjects = graphicalObjects.GetAll(); for (int i = 0; i < currentGraphicalObjects.Count; i++) { if (currentGraphicalObjects[i].Xposition == potentialXposition && currentGraphicalObjects[i].Yposition == potentialYposition) { generationOnAnEmptySpaceSuccessful = false; break; } } } }
private void ResolvePlayerWallCollision(GraphicalObjectContainer graphicalObjects) { byte spaceShipX = (byte)graphicalObjects.SpaceShipPlayerOne.Xposition; byte spaceShipY = (byte)graphicalObjects.SpaceShipPlayerOne.Yposition; if (spaceShipX < 1) { graphicalObjects.SpaceShipPlayerOne.Xposition = 1; } if (spaceShipX > Console.BufferWidth - 2) { graphicalObjects.SpaceShipPlayerOne.Xposition = Console.BufferWidth - 2; } if (spaceShipY < 1) { graphicalObjects.SpaceShipPlayerOne.Yposition = 1; } if (spaceShipY > Console.BufferHeight - 2) { graphicalObjects.SpaceShipPlayerOne.Yposition = Console.BufferHeight - 2; } }
private void ResolveEnemyDotCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.TronDotsContainers.Count; i++) { for (int j = 0; j < graphicalObjects.TronDotsContainers[i].Dots.Count; j++) { byte dotX = (byte)graphicalObjects.TronDotsContainers[i].Dots[j].Xposition; byte dotY = (byte)graphicalObjects.TronDotsContainers[i].Dots[j].Yposition; for (int k = 0; k < graphicalObjects.Enemies.Count; k++) { // Skipping stationary enemies because they cannot collide with dots if (graphicalObjects.Enemies[k].GetType() == typeof(StationaryEnemy)) { continue; } byte enemyX = (byte)graphicalObjects.Enemies[k].Xposition; byte enemyY = (byte)graphicalObjects.Enemies[k].Yposition; if (dotX == enemyX && dotY == enemyY) { this.soundEffects.PlayExplosion(); graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[k]); this.scoreContainer.Score++; } } } } }
public void GenerateBonus(GraphicalObjectContainer graphicalObjects) { if (this.BonusGeneratorTimer.ElapsedMilliseconds > BonusGenerationInterval) { Random randomGenerator = new Random(); bool generationOnAnEmptySpaceSuccessful = false; byte potentialXposition = 0; byte potentialYposition = 0; while (!generationOnAnEmptySpaceSuccessful) { generationOnAnEmptySpaceSuccessful = true; potentialXposition = (byte)randomGenerator.Next(1, Console.BufferWidth - 2); potentialYposition = (byte)randomGenerator.Next(1, Console.BufferHeight - 2); List <GraphicalObject> currentGraphicalObjects = graphicalObjects.GetAll(); for (int i = 0; i < currentGraphicalObjects.Count; i++) { if (currentGraphicalObjects[i].Xposition == potentialXposition && currentGraphicalObjects[i].Yposition == potentialYposition) { generationOnAnEmptySpaceSuccessful = false; break; } } } switch (this.currentBonusToGenerate) { case Bonus.Heart: graphicalObjects.Bonuses.Add(new HealthBonus(potentialXposition, potentialYposition, ConsoleColor.Red, 20)); this.currentBonusToGenerate = Bonus.Shield; break; case Bonus.Shield: graphicalObjects.Bonuses.Add(new ShieldBonus(potentialXposition, potentialYposition, ConsoleColor.Yellow, 10)); this.currentBonusToGenerate = Bonus.Ammo; break; case Bonus.Ammo: graphicalObjects.Bonuses.Add(new AmmoBonus(potentialXposition, potentialYposition, ConsoleColor.White, 20)); this.currentBonusToGenerate = Bonus.Tron; break; case Bonus.Tron: graphicalObjects.Bonuses.Add(new TronBonus(potentialXposition, potentialYposition, ConsoleColor.Cyan)); this.currentBonusToGenerate = Bonus.Heart; break; default: break; } this.BonusGeneratorTimer.Restart(); } }
public void Resolve(GraphicalObjectContainer graphicalObjects) { this.ResolvePlayerWallCollision(graphicalObjects); if (graphicalObjects.Bonuses.Count > 0) { this.ResolvePlayerBonusCollision(graphicalObjects); } if (graphicalObjects.Projectiles.Count > 0) { this.ResolveProjectileWallCollisions(graphicalObjects); this.ResolvePlayerProjectileCollisions(graphicalObjects); } if (graphicalObjects.TronDotsContainers.Count > 0) { this.ResolveEnemyDotCollisions(graphicalObjects); this.ResolvePlayerDotCollisions(graphicalObjects); } if (graphicalObjects.Enemies.Count > 0) { this.ResolveEnemyProjectileCollisions(graphicalObjects); this.ResolveEnemyEnemyCollisions(graphicalObjects); this.ResolvePlayerEnemyCollision(graphicalObjects); } }
private void ReadAndProcessCommands(GraphicalObjectContainer graphicalObjects) { if (Console.KeyAvailable) { ConsoleKeyInfo pressedKey = Console.ReadKey(); // Direction control if (pressedKey.Key == ConsoleKey.RightArrow) { this.graphicalObjects.SpaceShipPlayerOne.ChangeDirection(Direction.Right, this.graphicalObjects); } if (pressedKey.Key == ConsoleKey.LeftArrow) { this.graphicalObjects.SpaceShipPlayerOne.ChangeDirection(Direction.Left, this.graphicalObjects); } if (pressedKey.Key == ConsoleKey.UpArrow) { this.graphicalObjects.SpaceShipPlayerOne.ChangeDirection(Direction.Up, this.graphicalObjects); } if (pressedKey.Key == ConsoleKey.DownArrow) { this.graphicalObjects.SpaceShipPlayerOne.ChangeDirection(Direction.Down, this.graphicalObjects); } // Firing weapons if (pressedKey.Key == ConsoleKey.Spacebar) { this.graphicalObjects.SpaceShipPlayerOne.FireCurrentWeapon(this.graphicalObjects, this.soundEffects); } } }
public DifficultyController( GraphicalObjectContainer graphicalObjects, ObjectGenerator objectGenerator, ScoreContainer scoreContainer) { this.graphicalObjects = graphicalObjects; this.objectGenerator = objectGenerator; this.scoreContainer = scoreContainer; }
public void ChangeDirection(Direction direction, GraphicalObjectContainer graphicalObjects) { bool changeSuccessful = false; switch (direction) { // Changing the graphical direction of the ship upon changing the direction property // Making sure the ship cannot move in the opposite direction when the tron bonus is on case Direction.Right: if (this.direction != Direction.Left || graphicalObjects.TronDotsContainers.Count == 0) { this.Sprite = ShipCharRight; changeSuccessful = true; } break; case Direction.Left: if (this.direction != Direction.Right || graphicalObjects.TronDotsContainers.Count == 0) { this.Sprite = ShipCharLeft; changeSuccessful = true; } break; case Direction.Up: if (this.direction != Direction.Down || graphicalObjects.TronDotsContainers.Count == 0) { this.Sprite = ShipCharUp; changeSuccessful = true; } break; case Direction.Down: if (this.direction != Direction.Up || graphicalObjects.TronDotsContainers.Count == 0) { this.Sprite = ShipCharDown; changeSuccessful = true; } break; default: break; } if (changeSuccessful) { this.direction = direction; } }
public void GenerateEnemy(GraphicalObjectContainer graphicalObjects) { int numberOfMovingEnemies = 0; int numberOfStationaryEnemies = 0; for (int i = 0; i < graphicalObjects.Enemies.Count; i++) { if (graphicalObjects.Enemies[i].GetType() == typeof(MovingEnemy)) { numberOfMovingEnemies++; } else if (graphicalObjects.Enemies[i].GetType() == typeof(StationaryEnemy)) { numberOfStationaryEnemies++; } } if (numberOfMovingEnemies <= this.movingEnemyNumberLimit - this.numberOfEnemiesToGenerate) { if (numberOfMovingEnemies != this.oldNumberOfMovingEnemies) { for (int i = 0; i < this.numberOfEnemiesToGenerate; i++) { byte potentialXposition; byte potentialYposition; FindEmptySpace(graphicalObjects, out potentialXposition, out potentialYposition); graphicalObjects.Enemies.Add(new MovingEnemy(potentialXposition, potentialYposition, ConsoleColor.Gray, graphicalObjects.SpaceShipPlayerOne)); } this.oldNumberOfMovingEnemies = numberOfMovingEnemies + this.numberOfEnemiesToGenerate; } } if (numberOfStationaryEnemies <= this.stationaryEnemyNumberLimit - this.numberOfEnemiesToGenerate) { if (numberOfStationaryEnemies != this.oldNumberOfStationaryEnemies) { for (int i = 0; i < this.numberOfEnemiesToGenerate; i++) { byte potentialXposition; byte potentialYposition; FindEmptySpace(graphicalObjects, out potentialXposition, out potentialYposition); graphicalObjects.Enemies.Add(new StationaryEnemy(potentialXposition, potentialYposition, ConsoleColor.Magenta)); } this.oldNumberOfStationaryEnemies = numberOfStationaryEnemies + this.numberOfEnemiesToGenerate; } } }
private void ResolveProjectileWallCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.Projectiles.Count; i++) { byte projectileX = (byte)graphicalObjects.Projectiles[i].Xposition; byte projectileY = (byte)graphicalObjects.Projectiles[i].Yposition; if (projectileX > Console.BufferWidth - 1 || projectileX < 1 || projectileY > Console.BufferHeight - 1 || projectileY < 1) { graphicalObjects.Projectiles.Remove(graphicalObjects.Projectiles[i]); } } }
private void ResolvePlayerDotCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.TronDotsContainers.Count; i++) { for (int j = 0; j < graphicalObjects.TronDotsContainers[i].Dots.Count; j++) { byte dotX = (byte)graphicalObjects.TronDotsContainers[i].Dots[j].Xposition; byte dotY = (byte)graphicalObjects.TronDotsContainers[i].Dots[j].Yposition; if (dotX == (byte)graphicalObjects.SpaceShipPlayerOne.Xposition && dotY == (byte)graphicalObjects.SpaceShipPlayerOne.Yposition) { graphicalObjects.SpaceShipPlayerOne.HealthPoints = 0; } } } }
private void ResolvePlayerBonusCollision(GraphicalObjectContainer graphicalObjects) { byte spaceShipX = (byte)graphicalObjects.SpaceShipPlayerOne.Xposition; byte spaceShipY = (byte)graphicalObjects.SpaceShipPlayerOne.Yposition; for (int i = 0; i < graphicalObjects.Bonuses.Count; i++) { byte bonusX = (byte)graphicalObjects.Bonuses[i].Xposition; byte bonusY = (byte)graphicalObjects.Bonuses[i].Yposition; if (spaceShipX == bonusX && spaceShipY == bonusY) { if (graphicalObjects.Bonuses[i].GetType() == typeof(AmmoBonus)) { graphicalObjects.SpaceShipPlayerOne.IncreaseShotsAvailable(((AmmoBonus)graphicalObjects.Bonuses[i]).BonusPoints); this.soundEffects.PlayAmmoLoad(); graphicalObjects.SpaceShipPlayerOne.PrintStatus(); } else if (graphicalObjects.Bonuses[i].GetType() == typeof(HealthBonus)) { graphicalObjects.SpaceShipPlayerOne.ChangeHealth(((HealthBonus)graphicalObjects.Bonuses[i]).BonusPoints); this.soundEffects.PlayPowerUp(); } else if (graphicalObjects.Bonuses[i].GetType() == typeof(ShieldBonus)) { graphicalObjects.SpaceShipPlayerOne.IncreaseShieldTimeAvailable(((ShieldBonus)graphicalObjects.Bonuses[i]).TimeInvincibleInSeconds); this.soundEffects.PlayShieldPowerUp(); graphicalObjects.SpaceShipPlayerOne.PrintStatus(); } else if (graphicalObjects.Bonuses[i].GetType() == typeof(TronBonus)) { /* Making all other containers reach their capacity so that dots from the newest containver * do not overlap the dots from the previous container */ for (int j = 0; j < graphicalObjects.TronDotsContainers.Count; j++) { graphicalObjects.TronDotsContainers[j].ReachCapacity(); } graphicalObjects.TronDotsContainers.Add(new TronDotsContainer(graphicalObjects.SpaceShipPlayerOne)); this.soundEffects.PlayTronBonus(); } graphicalObjects.Bonuses.Remove(graphicalObjects.Bonuses[i]); } } }
private void ResolvePlayerEnemyCollision(GraphicalObjectContainer graphicalObjects) { byte spaceShipX = (byte)graphicalObjects.SpaceShipPlayerOne.Xposition; byte spaceShipY = (byte)graphicalObjects.SpaceShipPlayerOne.Yposition; for (int i = 0; i < graphicalObjects.Enemies.Count; i++) { byte enemyX = (byte)graphicalObjects.Enemies[i].Xposition; byte enemyY = (byte)graphicalObjects.Enemies[i].Yposition; if (spaceShipX == enemyX && spaceShipY == enemyY) { if (graphicalObjects.SpaceShipPlayerOne.ShieldTimeAvailable > 0) { if (graphicalObjects.Enemies[i].GetType() == typeof(MovingEnemy)) { this.scoreContainer.Score++; } else if (graphicalObjects.Enemies[i].GetType() == typeof(StationaryEnemy)) { this.scoreContainer.Score += this.stationaryEnemyDestructionScore; } this.soundEffects.PlayExplosion(); graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[i]); } else { if (graphicalObjects.Enemies[i].GetType() == typeof(MovingEnemy)) { this.soundEffects.PlayExplosion(); graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[i]); this.scoreContainer.Score += this.movingEnemyDestructionScore; graphicalObjects.SpaceShipPlayerOne.ChangeHealth(Projectile.Damage * 2); } else if (graphicalObjects.Enemies[i].GetType() == typeof(StationaryEnemy)) { graphicalObjects.SpaceShipPlayerOne.HealthPoints = 0; } } } } }
public void FireCurrentWeapon(GraphicalObjectContainer graphicalObjects, SoundEffectContainer soundEffects) { if (this.shotDelayStopwatch.ElapsedMilliseconds > this.shotDelayTime && this.ShotsAvailable > 0) { graphicalObjects.Projectiles.Add( new Projectile( this.Xposition, this.Yposition, ConsoleColor.Cyan, this.Direction, ProjectileType.Player, '*')); soundEffects.PlayShot(); this.DecreaseShotsAvailable(); this.shotDelayStopwatch.Restart(); } this.PrintStatus(); }
public void FireWeapon(GraphicalObjectContainer graphicalObjects, SoundEffectContainer soundEffects) { if (this.shotDelayStopwatch.ElapsedMilliseconds > this.shotDelayTime) { graphicalObjects.Projectiles.Add( new Projectile( this.Xposition, this.Yposition, ConsoleColor.Red, Direction.Up, ProjectileType.Enemy, '▲')); graphicalObjects.Projectiles.Add( new Projectile( this.Xposition, this.Yposition, ConsoleColor.Red, Direction.Down, ProjectileType.Enemy, '▼')); graphicalObjects.Projectiles.Add( new Projectile( this.Xposition, this.Yposition, ConsoleColor.Red, Direction.Left, ProjectileType.Enemy, '◄')); graphicalObjects.Projectiles.Add( new Projectile( this.Xposition, this.Yposition, ConsoleColor.Red, Direction.Right, ProjectileType.Enemy, '►')); soundEffects.PlayEnemyShot(); this.shotDelayStopwatch.Restart(); } }
private void ResolveEnemyEnemyCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.Enemies.Count; i++) { // Collisions with static enemies is to do nothing if (graphicalObjects.Enemies[i].GetType() == typeof(StationaryEnemy)) { continue; } byte currentEnemyX = (byte)graphicalObjects.Enemies[i].Xposition; byte currentEnemyY = (byte)graphicalObjects.Enemies[i].Yposition; for (int j = 0; j < graphicalObjects.Enemies.Count; j++) { // Collisions with static enemies is to do nothing if (graphicalObjects.Enemies[j].GetType() == typeof(StationaryEnemy)) { continue; } if (graphicalObjects.Enemies[i] != graphicalObjects.Enemies[j]) { byte otherEnemyX = (byte)graphicalObjects.Enemies[j].Xposition; byte otherEnemyY = (byte)graphicalObjects.Enemies[j].Yposition; if (currentEnemyX == otherEnemyX && currentEnemyY == otherEnemyY) { graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[j]); graphicalObjects.Enemies[i].Color = ConsoleColor.Red; this.soundEffects.PlayExplosion(); ((MovingEnemy)graphicalObjects.Enemies[i]).HealthPoints += 100; } } } } }
private void ResolveEnemyProjectileCollisions(GraphicalObjectContainer graphicalObjects) { for (int i = 0; i < graphicalObjects.Projectiles.Count; i++) { if (graphicalObjects.Projectiles[i].Type == ProjectileType.Player) { byte projectileX = (byte)graphicalObjects.Projectiles[i].Xposition; byte projectileY = (byte)graphicalObjects.Projectiles[i].Yposition; byte projectileFutureX = graphicalObjects.Projectiles[i].XfuturePosition; byte projectileFutureY = graphicalObjects.Projectiles[i].YfuturePosition; for (int j = 0; j < graphicalObjects.Enemies.Count; j++) { byte enemyX = (byte)graphicalObjects.Enemies[j].Xposition; byte enemyY = (byte)graphicalObjects.Enemies[j].Yposition; if ((projectileX == enemyX && projectileY == enemyY) || (projectileFutureX == enemyX && projectileFutureY == enemyY)) { this.soundEffects.PlayHit(); try { graphicalObjects.Projectiles.RemoveAt(i); } catch (ArgumentOutOfRangeException) { Console.Clear(); string exceptionMessage = string.Format( "Argument exception: \nValue of i: {0} \n graphicalObjects.Projectiles.Count: {1}", i, graphicalObjects.Projectiles.Count); throw new ArgumentOutOfRangeException( exceptionMessage); } if (graphicalObjects.Enemies[j].GetType() == typeof(MovingEnemy)) { ((MovingEnemy)graphicalObjects.Enemies[j]).ReduceHealth(Projectile.Damage); if (((MovingEnemy)graphicalObjects.Enemies[j]).HealthPoints == 0) { graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[j]); this.scoreContainer.Score += this.movingEnemyDestructionScore; } } else if (graphicalObjects.Enemies[j].GetType() == typeof(StationaryEnemy)) { ((StationaryEnemy)graphicalObjects.Enemies[j]).ReduceHealth(Projectile.Damage); if (((StationaryEnemy)graphicalObjects.Enemies[j]).HealthPoints == 0) { graphicalObjects.Enemies.Remove(graphicalObjects.Enemies[j]); this.scoreContainer.Score += this.stationaryEnemyDestructionScore; } } break; } } } } }