private void AddUnitButton_Clicked() { var unitStat = new UnitStat() { Hp = 100, Damage = 30, Team = "1" }; _gameState.SelectedUnitGroup.Units.Add(unitStat); _unitGroupLabel.Text = $"Fighters: {_gameState.SelectedUnitGroup.Units.Count}"; }
private void Button_Clicked() { var globeSize = 10; var globe = new Globe() { Terrain = new Terrain[globeSize, globeSize] }; for (var i = 0; i < globeSize; i++) { for (var j = 0; j < globeSize; j++) { globe.Terrain[i, j] = new Terrain { Type = (TerrainType)((i * j * 11) % 3) }; } } var unitGroup = new UnitGroup() { X = 5, Y = 5 }; for (var i = 0; i < 10; i++) { var unitStat = new UnitStat() { Hp = 100, Damage = 30, Defence = 1, Team = "1" }; unitGroup.Units.Add(unitStat); } _gameState.Globe = globe; _gameState.SelectedUnitGroup = unitGroup; _gameState.Globe.UnitGroups.Add(unitGroup); Application.RequestStop(); }
public Task <GameScreen> StartProcessingAsync(GameState gameState) { _gameState = gameState; Application.Init(); var top = Application.Top; var menu = new MenuBar(new MenuBarItem[] { new MenuBarItem("_Game", new MenuItem [] { new MenuItem("_Quit", "", () => { Application.RequestStop(); }) }), new MenuBarItem("_Current Group", new MenuItem [] { new MenuItem("_Build...", "", () => { var list = GetAvailableStructuresList(gameState); var buildingList = new ListView(list) { X = 0, Y = 0, Width = 50, Height = 15 }; var errorLabel = new Label(0, 16, string.Empty); var okButton = new Button("Ok", is_default: true); buildingList.SelectedItemChanged += (e) => { var structure = e.Value as StructureScheme; if (structure.Cost <= gameState.Resources[ResourceType.Money]) { errorLabel.Text = string.Empty; Application.Refresh(); } else { errorLabel.Text = "Has not enought Money."; Application.Refresh(); } }; okButton.Clicked += () => { var selectedBuilding = list[buildingList.SelectedItem]; if (selectedBuilding.Cost <= gameState.Resources[ResourceType.Money]) { var building = new Structure() { Scheme = selectedBuilding, X = gameState.SelectedUnitGroup.X, Y = gameState.SelectedUnitGroup.Y }; gameState.Globe.Structures.Add(building); gameState.Resources[ResourceType.Money] -= selectedBuilding.Cost; UpdateResourceLabel(); Application.RequestStop(); } else { errorLabel.Text = "Has not enought Money."; Application.Refresh(); } }; var cancelButton = new Button("Cancel"); cancelButton.Clicked += () => { Application.RequestStop(); }; var d = new Dialog( "Build something", 50, 20, okButton, cancelButton); d.Add(buildingList, errorLabel); Application.Run(d); }), new MenuItem("_Recruit...", "", () => { if (gameState.Resources[ResourceType.Money] >= 1000 && gameState.Resources[ResourceType.Food] >= 1000) { var unitStat = new UnitStat() { Hp = 100, Damage = 30, Team = "1" }; _gameState.SelectedUnitGroup.Units.Add(unitStat); _unitGroupLabel.Text = $"Fighters: {_gameState.SelectedUnitGroup.Units.Count}"; } }) }), }); var battleButton = new Button(1, 1, "Next day!"); _timeLabel = new Label(1, 2, "1 day of Spring, 1 year"); _unitGroupLabel = new Label(20, 1, $"Fighters: {_gameState.SelectedUnitGroup.Units.Count}"); _resourcesLabel = new Label(20, 2, "$1000 E1000 T1000 F1000"); var cellInfo = gameState.Globe.Terrain[gameState.SelectedUnitGroup.X, gameState.SelectedUnitGroup.Y].Type; _globeCellDecriptionLabel = new Label(20, 3, $"Location: {cellInfo}"); var globeViewer = new GlobeViewer(1, 5); globeViewer.SetFocus(); RedrawGlobe(gameState, globeViewer); top.Add(globeViewer, battleButton, _unitGroupLabel, _globeCellDecriptionLabel, menu, _resourcesLabel); battleButton.Clicked += () => { CalculateNextDay(gameState); UpdateResourceLabel(); }; globeViewer.KeyPress += (e) => { if (e.KeyEvent.Key == Key.CursorRight) { gameState.SelectedUnitGroup.X++; } else if (e.KeyEvent.Key == Key.CursorLeft) { gameState.SelectedUnitGroup.X--; } else if (e.KeyEvent.Key == Key.CursorUp) { gameState.SelectedUnitGroup.Y--; } else if (e.KeyEvent.Key == Key.CursorDown) { gameState.SelectedUnitGroup.Y++; } RedrawGlobe(gameState, globeViewer); Application.Refresh(); e.Handled = true; }; UpdateResourceLabel(); Application.Run(); return(Task.FromResult(GameScreen.Battle)); }
public Task <GameScreen> StartProcessingAsync(GameState gameState) { _gameState = gameState; Application.Init(); var top = Application.Top; var box = new BattlefieldViewer(0, 0); var quitButton = new Button(41, 1, "Ends"); quitButton.Clicked += QuitButton_Clicked; top.Add(box, quitButton); _units = new List <Unit>(); var matrixSize = 40; var unitMatrix = new Unit[matrixSize, matrixSize]; var playerUnits = gameState.SelectedUnitGroup.Units.OrderBy(x => Guid.NewGuid()).ToArray(); for (var i = 0; i < playerUnits.Length; i++) { var unitStat = playerUnits[i]; var unit = new Unit { CurrentHp = unitStat.Hp, Stat = unitStat, X = i % 10, Y = i / 10, }; unitMatrix[unit.X, unit.Y] = unit; _units.Add(unit); } for (var i = 0; i < 10; i++) { var unitStat = new UnitStat() { Hp = 70, Damage = 60, Team = "2" }; var unit = new Unit { CurrentHp = unitStat.Hp, Stat = unitStat, X = i, Y = matrixSize - 1 }; unitMatrix[i, matrixSize - 1] = unit; _units.Add(unit); } Task.Run(async() => { var counter = 0; while (true) { var delayTask = Task.Delay(1000); var calculationTask = Task.Run(() => { foreach (var unit in _units) { unit.WasBeAttacked = false; } var orderedUnits = _units.OrderBy(x => Guid.NewGuid()).ToArray(); foreach (var unit in orderedUnits) { if (unit.Stat.Hp <= 0) { continue; } if (unit.WasBeAttacked) { continue; } var moveQ = 0; if (unit.Stat.Team == "1") { moveQ = 1; } else { moveQ = -1; } var targetX = unit.X; var targetY = unit.Y + moveQ; if (targetY >= 0 && targetY <= matrixSize - 1) { var targetUnit = unitMatrix[targetX, targetY]; if (targetUnit is null) { unitMatrix[unit.X, unit.Y] = null; unitMatrix[targetX, targetY] = unit; unit.X = targetX; unit.Y = targetY; } else if (unit.Stat.Team != targetUnit.Stat.Team) { var damage = unit.Stat.Damage - targetUnit.Stat.Defence; damage = Math.Max(0, damage); targetUnit.CurrentHp -= damage; targetUnit.WasBeAttacked = true; if (targetUnit.CurrentHp <= 0) { _units.Remove(targetUnit); unitMatrix[unit.X, unit.Y] = null; unitMatrix[targetUnit.X, targetUnit.Y] = unit; unit.X = targetX; unit.Y = targetY; } } else { // Ally unit blocks the path. Just wait. } } } }); await Task.WhenAll(delayTask, calculationTask); var matrix = new Matrix() { Items = new int[matrixSize, matrixSize] }; for (var i = 0; i < matrixSize; i++) { for (var j = 0; j < matrixSize; j++) { var unit = unitMatrix[i, j]; if (unit is null) { continue; } if (unit.Stat.Team == "1") { matrix.Items[i, j] = 1; } else if (unit.Stat.Team == "2") { matrix.Items[i, j] = 2; } } } counter++; box.Matrix = matrix; Application.Refresh(); } }); Application.Run(); return(Task.FromResult(GameScreen.Globe)); }