/// <summary> /// ищем новую позицию для монстра и рисуем монстра /// </summary> /// <param name="monster"></param> public void SetNewPositionMonster(Monster monster) { PointInMap point = new PointInMap(); _map.ArrayMap[monster.Position.X, monster.Position.Y] = new Space(); if (monster.TypeMonsters != 4) { monster.Position = SearchNextPositionToCurrentMonster(monster); } else { point = SearchPositionToCleverMonster(monster); if (point != null) { monster.Position = point; } } if (monster.TypeMonsters == 2) //не летающий { _map.ArrayMap[monster.Position.X, monster.Position.Y] = new MonsterNotFly(); } if (monster.TypeMonsters == 3) { _map.ArrayMap[monster.Position.X, monster.Position.Y] = new MonsterFly(); } if (monster.TypeMonsters == 4) { _map.ArrayMap[monster.Position.X, monster.Position.Y] = new MonsterClever(); } }
/// <summary> /// проверяет пустая ли указанная клетка /// </summary> /// <param name="point">точка на карте</param> /// <returns></returns> public bool CheckPosition(PointInMap point, Monster monster) { if ((point.X < 0) || (point.X >= _map.Height) || (point.Y < 0) || (point.Y >= _map.Width)) { Logging.WriteInfo("Попытка выхода за пределы карты!"); return(false); } if (_map.ArrayMap[point.X, point.Y] is Space) { return(true); } else { if ((monster.TypeMonsters == 3) && (_map.ArrayMap[point.X, point.Y] is Bomb)) { return(true); } else { Logging.WriteInfo("Не может шагнуть,упирается в припятствие"); return(false); } } }
public List <PointInMap> GetPointAroundBomb(PointInMap currentPoint, bool isDoubleBoom) // возвращаем список клеток ,которые заденут бомбу - шаг взрыва 1 { PointInMap point = new PointInMap(); List <PointInMap> returnList = new List <PointInMap>(); int startI = 1; int startJ = 1; if (isDoubleBoom) { startI = 2; startJ = 2; } for (var i = -(startI); i <= (startJ); i++) { for (var j = -(startJ); j <= startJ; j++) { if ((Math.Abs(i) != Math.Abs(j)) && (Math.Abs(i) + Math.Abs(j) != 3)) { point = new PointInMap(); point.X = currentPoint.X + i; point.Y = currentPoint.Y + j; if ((point.X >= 1) && (point.X < _map.Height) && (point.Y >= 1) && (point.Y < _map.Width) && (!(_map.ArrayMap[point.X, point.Y] is Bomb))) { returnList.Add(point); } } } } returnList.Add(currentPoint); return(returnList); }
public ConsoleMap(int width, int height, int bombTimer) { Width = width + 1; // +1 для нерушимых стенок Height = height + 1; BombTimer = bombTimer; FirstPositionBomber = new PointInMap(); ArrayMap = new Entity[height + 2, width + 2]; ArrayForCleverMonster = new int[height + 2, width + 2]; }
/// <summary> /// Размещает на карте апгрейды для бомбера /// </summary> /// <param name="itemCountFar">количество апгрейдов на дальность</param> /// <param name="itemCountDistance">количество агрейдов на дистанционное управление</param> public void SetUpgradeOnMap(int itemCountFar = 0, int itemCountDistance = 0, int itemCountMiltiBomb = 0) { int randomIndex = 0; bool isNewIndex = false; List <int> alrearyUseIndex = new List <int>(); List <PointInMap> listWithWall = new List <PointInMap>(); for (int i = 1; i < Height; i++) { for (int j = 1; j < Width; j++) { var tempPoint = new PointInMap(); var item = ArrayMap[i, j]; var tempWall = item as Wall; if (tempWall != null) { tempPoint.X = i; tempPoint.Y = j; listWithWall.Add(tempPoint); } } } for (int itemCnt = 0; itemCnt < itemCountFar + itemCountDistance + itemCountMiltiBomb; itemCnt++) { isNewIndex = false; while (!isNewIndex) { randomIndex = _rand.Next(listWithWall.Count); if (alrearyUseIndex.Where(n => n == randomIndex).Count() == 0) { isNewIndex = true; } } alrearyUseIndex.Add(randomIndex); if (itemCnt < itemCountFar) { ArrayMap[listWithWall[randomIndex].X, listWithWall[randomIndex].Y] = new WallWithUpgradeFar(); continue; } if ((itemCnt > itemCountDistance) && (itemCnt < itemCountDistance)) { ArrayMap[listWithWall[randomIndex].X, listWithWall[randomIndex].Y] = new WallWithUpgradeRemote(); continue; } if (itemCnt > (itemCountDistance + itemCountFar)) { ArrayMap[listWithWall[randomIndex].X, listWithWall[randomIndex].Y] = new WallWithUpgradeMultiBomb(); continue; } } }
/// <summary> /// ищем новую позицию для монстра и рисуем монстра /// </summary> /// <param name="monster"></param> /// <summary> /// выбор начальной позиции для существ /// </summary> /// <param name="typeMonster">тип монстра 2-не переходит через бомбу, 3-проъодит через бомбу</param> /// <param name="countMonster">количество монстров,которые надо расположить на карте</param> /// <returns>карту</returns> /// public Entity[,] SetPositionForPerson(byte typeMonster, byte countMonster) { var pointList = new List <PointInMap>(); PointInMap point; for (byte rowCnt = 1; rowCnt < _map.Height; rowCnt++) { for (byte colCnt = 1; colCnt < _map.Width; colCnt++) { if (_map.ArrayMap[rowCnt, colCnt] is Space) { point = new PointInMap(); point.X = rowCnt; point.Y = colCnt; pointList.Add(point); } } } for (int monsterCount = 0; monsterCount < countMonster; monsterCount++) { point = new PointInMap(); point = pointList[_rand.Next(pointList.Count)]; if (typeMonster == 2) { var currentMonster = new Monster(false, 2); _map.ArrayMap[point.X, point.Y] = new MonsterNotFly(); currentMonster.SetPosition(point.X, point.Y); ListWithMonster.Add(currentMonster); } if (typeMonster == 3) { var currentMonster = new Monster(true, 3); _map.ArrayMap[point.X, point.Y] = new MonsterFly(); currentMonster.SetPosition(point.X, point.Y); ListWithMonster.Add(currentMonster); } if (typeMonster == 4) { var currentMonster = new Monster(true, 4); _map.ArrayMap[point.X, point.Y] = new MonsterClever(); currentMonster.SetPosition(point.X, point.Y); ListWithMonster.Add(currentMonster); } pointList.Remove(point); } return(_map.ArrayMap); }
public int GetMonsterForPosition(PointInMap point) { int cnt = 0; foreach (var item in ListWithMonster) { if ((item.Position.X == point.X) && (item.Position.Y == point.Y)) { break; } cnt++; } return(cnt); }
/// <summary> /// перестраивает и перересовывает карту после нажатия одной из клавиш управления /// </summary> /// <param name="x">смещение по оси X</param> /// <param name="y">смещение по оси Y</param> public bool MoveBomber(int x = 0, int y = 0) { lock (_lock) { var newPosition = new PointInMap(); var beforePosition = this.GetCurrentPosition(_map); newPosition.X = beforePosition.X + x; newPosition.Y = beforePosition.Y + y; if ((newPosition.X >= 1) && (newPosition.Y >= 1) && (newPosition.X < _map.Height) && (newPosition.Y < _map.Width)) { if ((_map.ArrayMap[newPosition.X, newPosition.Y] is MonsterFly) || (_map.ArrayMap[newPosition.X, newPosition.Y] is MonsterNotFly) || (_map.ArrayMap[newPosition.X, newPosition.Y] is MonsterClever)) { _map.ArrayMap[beforePosition.X, beforePosition.Y] = new Space(); _bomber.Position = _map.FirstPositionBomber; _bomber.IsDead = true; return(false); } else { if (CheckPositionForBomber(newPosition)) { if (_bomber.IsOnBomb) { _map.ArrayMap[beforePosition.X, beforePosition.Y] = new Bomb(); } else { _map.ArrayMap[beforePosition.X, beforePosition.Y] = new Space(); } _map.ArrayMap[newPosition.X, newPosition.Y] = _bomber; _bomber.Position.X = newPosition.X; _bomber.Position.Y = newPosition.Y; isWantDrow = true; } } } _bomber.IsOnBomb = false; // _display.DrawMap(); return(true); } }
/// <summary> /// Возвращает позицию где стоит бомбермен /// </summary> /// <param name="map"></param> /// <returns></returns> public PointInMap GetCurrentPosition(IMap map) { var point = new PointInMap(); for (byte rowCnt = 1; rowCnt < _map.Height; rowCnt++) { for (byte colCnt = 1; colCnt < _map.Width; colCnt++) { if ((_map.ArrayMap[rowCnt, colCnt] is Bomber) || (_map.ArrayMap[rowCnt, colCnt] is BomberOnBomb)) { point.X = rowCnt; point.Y = colCnt; } } } return(point); }
public int GetDirectionForPoint(PointInMap prevPoint, PointInMap nextPoint) { if (nextPoint.X < prevPoint.X) { return(1); } if (nextPoint.X > prevPoint.X) { return(2); } if (nextPoint.Y < prevPoint.Y) { return(3); } if (nextPoint.Y > prevPoint.Y) { return(4); } return(1); }
public bool CheckPositionForBomber(PointInMap point) { if ((point.X < 0) || (point.X >= _map.Height) || (point.Y < 0) || (point.Y >= _map.Width)) { Logging.WriteInfo("Попытка выхода за пределы карты!"); return(false); } if (_map.ArrayMap[point.X, point.Y] is Space) { return(true); } if (_map.ArrayMap[point.X, point.Y] is UpgradeFar) { _bomber.IsHaveFarUpgrate = true; _display.SignalOnGetBonus(); return(true); } if (_map.ArrayMap[point.X, point.Y] is UpgradeRemote) { _bomber.IsHaveRemoteUpgrate = true; _display.SignalOnGetBonus(); return(true); } if (_map.ArrayMap[point.X, point.Y] is UpgradeMultiBomb) { _bomber.IsHaveMultiBombUpgrade = true; _display.SignalOnGetBonus(); return(true); } else { Logging.WriteInfo("Не может шагнуть,упирается в припятствие"); return(false); } }
public PointInMap SearchPositionToCleverMonster(Monster monster) { var target = new PointInMap(monster.Position.X - 1, monster.Position.Y - 1); int stepNumberToTargetPosition = 0; int stepNumber = 1; int emptyCell = 0; List <PointInMap> pointCollection; PointInMap resultPoint = new PointInMap(); List <PointInMap> currentPosition; int[,] mapInInt = ParseEntityToInt(_map.ArrayMap); while (true) { while (true) { if (_bomber.IsDead) //если бомбер мертв,следуюий умный монстр не сможет шагнуть,так как бомбера фактически нет на карте { break; } currentPosition = new List <PointInMap>(); emptyCell = 0; for (int colCnt = 0; colCnt <= _map.Height - 2; colCnt++) { for (int rowCnt = 0; rowCnt <= _map.Width - 2; rowCnt++) { if (mapInInt[colCnt, rowCnt] == stepNumber) { currentPosition.Add(new PointInMap(colCnt, rowCnt)); continue; } if (mapInInt[colCnt, rowCnt] != Const.SPACE_CLEVER) { emptyCell++; continue; } } } if (emptyCell == (_map.Height - 1) * (_map.Width - 1)) { break; } stepNumber++; foreach (var c in currentPosition) { pointCollection = new List <PointInMap>(); pointCollection.Add(new PointInMap(c.X + 1, c.Y)); pointCollection.Add(new PointInMap(c.X + -1, c.Y)); pointCollection.Add(new PointInMap(c.X, c.Y + 1)); pointCollection.Add(new PointInMap(c.X, c.Y - 1)); foreach (var p in pointCollection) { if ((p.X >= 0) && (p.X < _map.Height - 1) && (p.Y >= 0) && (p.Y < _map.Width - 1)) { if (mapInInt[p.X, p.Y] == Const.SPACE_CLEVER) { mapInInt[p.X, p.Y] = stepNumber; } } } } } if (_bomber.IsDead) { break; } stepNumberToTargetPosition = mapInInt[target.X, target.Y]; pointCollection = new List <PointInMap>(); pointCollection.Add(new PointInMap(target.X + 1, target.Y)); pointCollection.Add(new PointInMap(target.X + -1, target.Y)); pointCollection.Add(new PointInMap(target.X, target.Y + 1)); pointCollection.Add(new PointInMap(target.X, target.Y - 1)); foreach (var p in pointCollection) { if ((p.X >= 0) && (p.X < _map.Height - 1) && (p.Y >= 0) && (p.Y < _map.Width - 1)) { if (mapInInt[p.X, p.Y] == stepNumberToTargetPosition - 1) { resultPoint = new PointInMap(); resultPoint.X = p.X + 1; resultPoint.Y = p.Y + 1; break; } } } if (_map.ArrayMap[resultPoint.X, resultPoint.Y] is Bomber) { _bomber.IsDead = true; } return(resultPoint); // break; } return(null); }
public bool CheckPosition(PointInMap point, Monster monster, Bomber bomber) { bool isNotOutRange = ((point.X < 0) || (point.X >= _map.Height) || (point.Y < 0) || (point.Y >= _map.Width)); if ((!CheckPosition(point, monster)) && (!isNotOutRange)) { if (_map.ArrayMap[point.X, point.Y] is Bomber) { // _display.DrawMap(); bomber.IsDead = true; return(true); } if (_map.ArrayMap[point.X, point.Y] is Bomb) { return(false); } if ((_map.ArrayMap[point.X, point.Y] is Bomb) && (monster.TypeMonsters == 3)) { switch (monster.Direction) { case 1: nextPoint.X = monster.Position.X - 2; nextPoint.Y = monster.Position.Y; return(CheckPosition(nextPoint, monster)); case 2: nextPoint.X = monster.Position.X + 2; nextPoint.Y = monster.Position.Y; return(CheckPosition(nextPoint, monster)); case 3: nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y - 2; return(CheckPosition(nextPoint, monster)); case 4: nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y + 2; return(CheckPosition(nextPoint, monster)); } } if ((_map.ArrayMap[point.X, point.Y] is MonsterNotFly) || (_map.ArrayMap[point.X, point.Y] is MonsterFly)) { switch (monster.Direction) { case 1: nextPoint.X = monster.Position.X - 2; nextPoint.Y = monster.Position.Y; return(CheckPosition(nextPoint, monster)); case 2: nextPoint.X = monster.Position.X + 2; nextPoint.Y = monster.Position.Y; return(CheckPosition(nextPoint, monster)); case 3: nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y - 2; return(CheckPosition(nextPoint, monster)); case 4: nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y + 2; return(CheckPosition(nextPoint, monster)); } } return(false); } else { if (!isNotOutRange) { return(true); } else { return(false); } } }
// Запускает игру public void Start() { int now = DateTime.Now.Second; _gamePad.PressDown = () => MoveBomber(1, 0); _gamePad.PressLeft = () => MoveBomber(0, -1); _gamePad.PressRight = () => MoveBomber(0, 1); _gamePad.PressUp = () => MoveBomber(-1, 0); _gamePad.PressSpace = () => OnPressSpace(); _gamePad.PressR = () => RemoteBombBoom(); _gamePad.PressP = () => PressPause(isPaused); while (true) { if (ListWithMonster.Count == 0) { if (_mapManager.CheckAvailableMap()) { GetNextLevel(); } else { _display.ShowYouWin(); break; } } if (_bomber.IsDead) //если бомбера убили, то рисуем его на первочальной позиции { Logging.WriteDebbug("Пытаюсь помереть"); _display.SignalOnDead(); _bomber.IsHaveFarUpgrate = false; _bomber.IsHaveRemoteUpgrate = false; _bomber.IsHaveMultiBombUpgrade = false; _bomber.LiveCount -= 1; _display.ShowLive(_bomber.LiveCount); _bomber.IsDead = false; _map.ArrayMap[_map.FirstPositionBomber.X, _map.FirstPositionBomber.Y] = _bomber; isWantDrow = true; } if (_bomber.LiveCount <= 0) //если нет жизней ,то заканчиваем игру { Logging.WriteTrace("Конец игры"); _display.CleanMap(); isWantDrow = true; _display.ShowGameOver(); break; } if (_bomber.IsOnBomb) { var currentPosition = new PointInMap(); currentPosition = GetCurrentPosition(_map); if (!_bomber.NeedChangeImage) { _bomber.NeedChangeImage = true; _map.ArrayMap[currentPosition.X, currentPosition.Y] = new BomberOnBomb(); } else { _bomber.NeedChangeImage = false; _map.ArrayMap[currentPosition.X, currentPosition.Y] = _bomber; } isWantDrow = true; System.Threading.Thread.Sleep(50); } if (now != DateTime.Now.Second) { Logging.WriteInfo("прошла секунда, шагаю"); now = DateTime.Now.Second; UpgradeUpTime(); for (int i = 0; i < ListWithMonster.Count; i++) { SetNewPositionMonster(ListWithMonster[i]); } int bombCount = 0; while (bombCount != ListWithBomb.Count) { var bomb = ListWithBomb[bombCount]; bomb.CurrentTime += 1; if (bomb.CurrentTime == bomb.TimeToBoom) { if (!_bomber.IsHaveRemoteUpgrate) { OnBombBoom(bomb); ListWithBomb.Remove(bomb); bombCount--; } } bombCount++; } isSecondNow = true; } if (!isDrawNow) { if (((isWantDrow) || (isSecondNow)) || ((isSecondNow) && (isWantDrow))) { isWantDrow = false; isSecondNow = false; isDrawNow = true; lock (_lock) { System.Diagnostics.Stopwatch sw = new Stopwatch(); sw.Start(); _display.DrawMap(); sw.Stop(); Logging.WriteTrace("Время выполнения" + (sw.ElapsedMilliseconds / 100.0).ToString()); } isDrawNow = false; } } } }
/// <summary> /// Здесь ищем будущую позицию для монстра /// </summary> /// <param name="monster"></param> /// <returns></returns> public PointInMap SearchNextPositionToCurrentMonster(Monster monster) { var freePositionForMonster = new Dictionary <int, PointInMap>(); int nextDirection = 0; // long tryUseRandom; bool positionIsFind = false; while (!positionIsFind) { nextDirection = _rand.Next(1, 5); switch (monster.Direction) { case 1: nextPoint = new PointInMap(); nextPoint.X = monster.Position.X - 1; nextPoint.Y = monster.Position.Y; Logging.WriteInfo("Текущая точка для проверки " + nextPoint.X + " " + nextPoint.Y); Logging.WriteInfo("Текущая позиция монстра " + monster.Position.X + " " + monster.Position.Y); if (CheckPosition(nextPoint, monster, _bomber)) { positionIsFind = true; return(nextPoint); } else { Logging.WriteInfo("не смог шагнуть по вертикали " + nextPoint.X + " по горизонтали " + nextPoint.Y + " " + " следующая попытка в напрвалении" + nextDirection); monster.Direction = nextDirection; } break; case 2: nextPoint = new PointInMap(); nextPoint.X = monster.Position.X + 1; nextPoint.Y = monster.Position.Y; Logging.WriteInfo("Текущая точка для проверки " + nextPoint.X + " " + nextPoint.Y); Logging.WriteInfo("Текущая позиция монстра " + monster.Position.X + " " + monster.Position.Y); if (CheckPosition(nextPoint, monster, _bomber)) { positionIsFind = true; return(nextPoint); } else { Logging.WriteInfo("не смог шагнуть по вертикали " + nextPoint.X + " по горизонтали " + nextPoint.Y + " " + " следующая попытка в напрвалении" + nextDirection); monster.Direction = nextDirection; } break; case 3: nextPoint = new PointInMap(); nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y - 1; Logging.WriteInfo("Текущая точка для проверки " + nextPoint.X + " " + nextPoint.Y); Logging.WriteInfo("Текущая позиция монстра " + monster.Position.X + " " + monster.Position.Y); if (CheckPosition(nextPoint, monster, _bomber)) { positionIsFind = true; return(nextPoint); } else { Logging.WriteInfo("не смог шагнуть по вертикали " + nextPoint.X + " по горизонтали " + nextPoint.Y + " " + " следующая попытка в напрвалении" + nextDirection); monster.Direction = nextDirection; } break; case 4: nextPoint = new PointInMap(); nextPoint.X = monster.Position.X; nextPoint.Y = monster.Position.Y + 1; Logging.WriteInfo("Текущая точка для проверки " + nextPoint.X + " " + nextPoint.Y); Logging.WriteInfo("Текущая позиция монстра " + monster.Position.X + " " + monster.Position.Y); if (CheckPosition(nextPoint, monster, _bomber)) { positionIsFind = true; return(nextPoint); } else { Logging.WriteInfo("не смог шагнуть на " + nextPoint.X + " " + nextPoint.Y + " " + " следующая попытка " + nextDirection); monster.Direction = nextDirection; } break; } } return(nextPoint); }