Example #1
0
 private static bool HasWorkingPlayerGroup(Structure structure, List <UnitGroup> unitGroups)
 {
     return(unitGroups.Where(x => x.X == structure.X && x.Y == structure.Y).Sum(x => x.Units.Count()) > 0);
 }
Example #2
0
        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));
        }