public void TankCreateOnMap(List <TankFragment> tank, MapBase map) { foreach (var t in tank) { map.GameMap[t.Y, t.X] = 2; } }
public void TankDeleteFromMap(List <TankFragment> tank, MapBase map) { foreach (var t in tank) { map.GameMap[t.Y, t.X] = 0; } }
public GameEngineControl(TankEngine tank, EnemyTankEngine enemyTank, BulletEngine bullet, ControlBase controlBase, MapBase map, MoveDirection direction, MoveDirection enemyDirection) { PlayerWin = false; GameOver = false; _tank = tank; _enemyTank = enemyTank; _bullet = bullet; _controlBase = controlBase; _map = map; _lastDirection = direction; _direction = direction; _lastEnemyDirection = enemyDirection; _enemyDirection = enemyDirection; _bulletsTimer = new Timer(10000); _bulletsTimer.Elapsed += BulletsTimerOnElapsed; _bulletsTimer.Interval = _bullesSpeed; _tanksTimer = new Timer(10000); _tanksTimer.Elapsed += TanksTimerOnElapsed; _tanksTimer.Interval = _tanksSpeed; switch (direction) { case MoveDirection.Up: _action = ControlActions.MoveUp; break; case MoveDirection.Down: _action = ControlActions.MoveDown; break; case MoveDirection.Right: _action = ControlActions.MoveRight; break; case MoveDirection.Left: _action = ControlActions.MoveLeft; break; } switch (enemyDirection) { case MoveDirection.Up: _enemyAction = ControlActions.MoveUp; break; case MoveDirection.Down: _enemyAction = ControlActions.MoveDown; break; case MoveDirection.Right: _enemyAction = ControlActions.MoveRight; break; case MoveDirection.Left: _enemyAction = ControlActions.MoveLeft; break; } }
public void TankMove(List <TankFragment> tank, MoveDirection direction, MapBase map) { OnTankErase(tank); TankDeleteFromMap(tank, map); if (!DirectionIsBlocked(tank, direction, map) && !TankNearBorder(tank, direction, map.Borders)) { switch (direction) { case MoveDirection.Up: TankMoveUp(tank); break; case MoveDirection.Down: TankMoveDown(tank); break; case MoveDirection.Left: TankMoveLeft(tank); break; case MoveDirection.Right: TankMoveRight(tank); break; } } TankCreateOnMap(tank, map); OnTankDraw(tank); }
public MoveDirection Control(out ControlActions enemyAction, List <TankFragment> enemyTank, List <TankFragment> myTank, EnemyTankEngine tank, ControlActions enemyTankLastAction, MoveDirection direction, BulletEngine bullet, MapBase map) { ControlActions nextEnemyAction; MoveDirection nextEnemyDirection = 0; EnemyTankChoseActions(out nextEnemyAction, enemyTank, myTank, enemyTankLastAction); if (nextEnemyAction == ControlActions.Shoot) { tank.Shoot(enemyTank[1].X, enemyTank[1].Y, direction); enemyAction = enemyTankLastAction; } else { enemyAction = nextEnemyAction; } switch (enemyAction) { case ControlActions.MoveUp: nextEnemyDirection = MoveDirection.Up; break; //дивитись коментар до GameEngineControl case ControlActions.MoveDown: nextEnemyDirection = MoveDirection.Down; break; case ControlActions.MoveLeft: nextEnemyDirection = MoveDirection.Left; break; case ControlActions.MoveRight: nextEnemyDirection = MoveDirection.Right; break; } return(nextEnemyDirection); }
public bool DirectionIsBlocked(List <TankFragment> tank, MoveDirection direction, MapBase map) { var directionIsBlocked = false; switch (direction) { case MoveDirection.Up: if (map.GameMap[tank[0].Y - 1, tank[0].X] == 1 || map.GameMap[tank[1].Y - 1, tank[1].X] == 1 || map.GameMap[tank[2].Y - 1, tank[2].X] == 1) { directionIsBlocked = true; } break; case MoveDirection.Down: if (map.GameMap[tank[0].Y + 1, tank[0].X] == 1 || map.GameMap[tank[1].Y + 1, tank[1].X] == 1 || map.GameMap[tank[2].Y + 1, tank[2].X] == 1) { directionIsBlocked = true; } break; case MoveDirection.Left: if (map.GameMap[tank[0].Y, tank[0].X - 1] == 1 || map.GameMap[tank[1].Y, tank[1].X - 1] == 1 || map.GameMap[tank[2].Y, tank[2].X - 1] == 1) { directionIsBlocked = true; } break; case MoveDirection.Right: if (map.GameMap[tank[0].Y, tank[0].X + 1] == 1 || map.GameMap[tank[1].Y, tank[1].X + 1] == 1 || map.GameMap[tank[2].Y, tank[2].X + 1] == 1) { directionIsBlocked = true; } break; } return(directionIsBlocked); }
public GameEngineControl(TankEngine tank, EnemyTankEngine enemyTank, BulletEngine bullet, ControlBase controlBase, MapBase map, MoveDirection direction, MoveDirection enemyDirection) { PlayerWin = false; GameOver = false; _tank = tank; _enemyTank = enemyTank; _bullet = bullet; _controlBase = controlBase; _map = map; _lastDirection = direction; _direction = direction; _lastEnemyDirection = enemyDirection; _enemyDirection = enemyDirection; _bulletsTimer = new Timer(10000); _bulletsTimer.Elapsed += BulletsTimerOnElapsed; _bulletsTimer.Interval = _bullesSpeed; _tanksTimer = new Timer(10000); _tanksTimer.Elapsed += TanksTimerOnElapsed; _tanksTimer.Interval = _tanksSpeed; //тут двічі, і ще в методі Control класу EnemyTankEngine, де ми отримуємо ControlAction є код, який повторюється // public static ControlAction GetControlAction(MoveDirection direction) // switch (direction) // { // case MoveDirection.Up: // return ControlActions.MoveUp; // break; // case MoveDirection.Down: // return = ControlActions.MoveDown; // break; // case MoveDirection.Right: // return = ControlActions.MoveRight; // break; // case MoveDirection.Left: // return = ControlActions.MoveLeft; // break; // } // return ControlActions.MoveUp; //_action = GetControlAction(direction) //_enemyAction = GetControlAction(enemyDirection) switch (direction) { case MoveDirection.Up: _action = ControlActions.MoveUp; break; case MoveDirection.Down: _action = ControlActions.MoveDown; break; case MoveDirection.Right: _action = ControlActions.MoveRight; break; case MoveDirection.Left: _action = ControlActions.MoveLeft; break; } switch (enemyDirection) { case MoveDirection.Up: _enemyAction = ControlActions.MoveUp; break; case MoveDirection.Down: _enemyAction = ControlActions.MoveDown; break; case MoveDirection.Right: _enemyAction = ControlActions.MoveRight; break; case MoveDirection.Left: _enemyAction = ControlActions.MoveLeft; break; } }
//private public void BulletAndTankColisionHandler(List <BulletEngine> bullets, bool enemy, MapBase map) { var tankId = enemy ? 3 : 2; foreach (var bul in bullets) { //4 рази дублювання коду. Можна оголосити // private void LoseOrWin(bool enemy) // { // GameOver = true; // if (enemy) // { // PlayerWin = true; // } // // І коли перевіряти // if (map.GameMap[bul.Y - 1, bul.X] == tankId // { // LoseOrWin(enemy) // } switch (bul.Direction) { case MoveDirection.Up: if (map.GameMap[bul.Y - 1, bul.X] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Down: if (map.GameMap[bul.Y + 1, bul.X] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Left: if (map.GameMap[bul.Y, bul.X - 1] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Right: if (map.GameMap[bul.Y, bul.X + 1] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; } } }
//private public void BulletsCollisionHandler(List <BulletEngine> bullets, out List <BulletEngine> enemyBullets, MapBase map, bool enemy) { var lEnemyBullets = enemy ? _tank.Bullets : _enemyTank.Bullets; for (var i = 0; i < bullets.Count; i++) { //В даному методі чотрири рази написаний код //if (bullet != null) //{ // _bullet.OnBulletErase(bullet); // _bullet.BulletDeleteFromMap(bullet, map); // lEnemyBullets.Remove(bullet); //} //_bullet.OnBulletErase(bullets[i]); //_bullet.BulletDeleteFromMap(bullets[i], map); //bullets.Remove(bullets[i]); // //Можливо, варто було з цього коду зробити приватний метод switch (bullets[i].Direction) { case MoveDirection.Up: if (map.GameMap[bullets[i].Y - 1, bullets[i].X] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X & bull.Y == bullets[i].Y - 1); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Down: if (map.GameMap[bullets[i].Y + 1, bullets[i].X] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X & bull.Y == bullets[i].Y + 1); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Left: if (map.GameMap[bullets[i].Y, bullets[i].X - 1] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X - 1 & bull.Y == bullets[i].Y); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Right: if (map.GameMap[bullets[i].Y, bullets[i].X + 1] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X + 1 & bull.Y == bullets[i].Y); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; } } enemyBullets = lEnemyBullets; }
public void BulletsMove(List <BulletEngine> bullets, MapBase map) { for (var i = 0; i < bullets.Count; i++) { if (BulletNearWall(bullets[i], map.GameMap)) #region BulletNearWall { switch (bullets[i].Direction) { case MoveDirection.Up: map.MapPointDelete(bullets[i].Y - 1, bullets[i].X - 1); map.MapPointDelete(bullets[i].Y - 1, bullets[i].X); map.MapPointDelete(bullets[i].Y - 1, bullets[i].X + 1); break; case MoveDirection.Down: map.MapPointDelete(bullets[i].Y + 1, bullets[i].X - 1); map.MapPointDelete(bullets[i].Y + 1, bullets[i].X); map.MapPointDelete(bullets[i].Y + 1, bullets[i].X + 1); break; case MoveDirection.Left: map.MapPointDelete(bullets[i].Y - 1, bullets[i].X - 1); map.MapPointDelete(bullets[i].Y, bullets[i].X - 1); map.MapPointDelete(bullets[i].Y + 1, bullets[i].X - 1); break; case MoveDirection.Right: map.MapPointDelete(bullets[i].Y - 1, bullets[i].X + 1); map.MapPointDelete(bullets[i].Y, bullets[i].X + 1); map.MapPointDelete(bullets[i].Y + 1, bullets[i].X + 1); break; } OnBulletErase(bullets[i]); BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); break; } #endregion if (!BulletNearBorder(bullets[i], map.Borders)) #region BulletNearBorder { OnBulletErase(bullets[i]); BulletDeleteFromMap(bullets[i], map); switch (bullets[i].Direction) { case MoveDirection.Up: BulletMoveUp(bullets[i]); break; case MoveDirection.Down: BulletMoveDown(bullets[i]); break; case MoveDirection.Left: BulletMoveLeft(bullets[i]); break; case MoveDirection.Right: BulletMoveRight(bullets[i]); break; } OnBulletDraw(bullets[i]); BulletCreateOnMap(bullets[i], map); } else { OnBulletErase(bullets[i]); BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } #endregion } }
public void BulletDeleteFromMap(BulletEngine bulletEngine, MapBase map) { map.GameMap[bulletEngine.Y, bulletEngine.X] = 0; }
public void BulletCreateOnMap(BulletEngine bulletEngine, MapBase map) { map.GameMap[bulletEngine.Y, bulletEngine.X] = 4; }
public void BulletAndTankColisionHandler(List <BulletEngine> bullets, bool enemy, MapBase map) { var tankId = enemy ? 3 : 2; foreach (var bul in bullets) { switch (bul.Direction) { case MoveDirection.Up: if (map.GameMap[bul.Y - 1, bul.X] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Down: if (map.GameMap[bul.Y + 1, bul.X] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Left: if (map.GameMap[bul.Y, bul.X - 1] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; case MoveDirection.Right: if (map.GameMap[bul.Y, bul.X + 1] == tankId) { GameOver = true; if (enemy) { PlayerWin = true; } } break; } } }
public void BulletsCollisionHandler(List <BulletEngine> bullets, out List <BulletEngine> enemyBullets, MapBase map, bool enemy) { var lEnemyBullets = enemy ? _tank.Bullets : _enemyTank.Bullets; for (var i = 0; i < bullets.Count; i++) { switch (bullets[i].Direction) { case MoveDirection.Up: if (map.GameMap[bullets[i].Y - 1, bullets[i].X] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X & bull.Y == bullets[i].Y - 1); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Down: if (map.GameMap[bullets[i].Y + 1, bullets[i].X] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X & bull.Y == bullets[i].Y + 1); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Left: if (map.GameMap[bullets[i].Y, bullets[i].X - 1] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X - 1 & bull.Y == bullets[i].Y); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; case MoveDirection.Right: if (map.GameMap[bullets[i].Y, bullets[i].X + 1] == 4) { var bullet = lEnemyBullets.SingleOrDefault(bull => bull.X == bullets[i].X + 1 & bull.Y == bullets[i].Y); if (bullet != null) { _bullet.OnBulletErase(bullet); _bullet.BulletDeleteFromMap(bullet, map); lEnemyBullets.Remove(bullet); } _bullet.OnBulletErase(bullets[i]); _bullet.BulletDeleteFromMap(bullets[i], map); bullets.Remove(bullets[i]); } break; } } enemyBullets = lEnemyBullets; }