/// <summary>
        /// Update the player. Takes input from the keyboard and updates the position of the player.
        /// </summary>
        /// <param name="gameTime">Game time</param>
        public void Update(GameTime gameTime)
        {
            Rectangle visibleArea = VirtualScreenSize.CalculateVisibleArea(_playerModel.Position);
            var       deltaTime   = gameTime.ElapsedGameTime.Milliseconds / 1000f;
            var       state       = Keyboard.GetState();
            var       pressedKeys = state.GetPressedKeys();

            if (_animation != null && !_animation.Done)
            {
                if (pressedKeys.Length == 0)
                {
                    _animation = null;
                    return;
                }
                _animation.Update(deltaTime);
                _playerModel.Position = _animation.Position + new Vector2(deltaTime, 0);
                _playerModel.Frame    = _animation.CurrentFrame;
                return;
            }

            if (_animation != null && _animation.Done)
            {
                _playerModel.Position = _animation.Position;
                _playerModel.Frame    = _animation.CurrentFrame;
                MoveFinished?.Invoke(this, EventArgs.Empty);
            }

            if (!Disabled)
            {
                _animation = null;
                if (pressedKeys.Contains(Keys.W))
                {
                    _animation = new PlayerAnimationController(new int[] { 8, 9, 10, 11, 12, 13, 14, 15 },
                                                               _playerModel.Position,
                                                               new Vector2(0, -_playerModel.Speed),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.A))
                {
                    _animation = new PlayerAnimationController(new int[] { 16, 17, 18, 19, 20, 21, 22, 23 },
                                                               _playerModel.Position,
                                                               new Vector2(-_playerModel.Speed, 0),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.S))
                {
                    _animation = new PlayerAnimationController(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 },
                                                               _playerModel.Position,
                                                               new Vector2(0, _playerModel.Speed),
                                                               MoveCooldownPeriod);
                }
                else if (pressedKeys.Contains(Keys.D))
                {
                    _animation = new PlayerAnimationController(new int[] { 24, 25, 26, 27, 28, 29, 30, 31 },
                                                               _playerModel.Position,
                                                               new Vector2(_playerModel.Speed, 0),
                                                               MoveCooldownPeriod);
                }
            }
        }
Example #2
0
        public override void Draw()
        {
            var visibleArea = VirtualScreenSize.CalculateVisibleArea(_playerModel.Position);
            var position    = new Vector2(visibleArea.X, visibleArea.Y + 20);

            int i = 0;

            while (i <= visibleArea.Width)
            {
                _bar.Draw(new Vector2(position.X + i, visibleArea.Y + 5), 0, Color.White, new Vector2(0.25f, 0.25f));
                i += 64;
            }

            // Water
            _water.Draw(position + new Vector2(15, 0), 0, Color.White, new Vector2(0.125f, 0.125f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Water.ToString(), position + new Vector2(55, 0), _playerResourcesModel.Water >= 0 ? Color.White : Color.Red);

            // Food
            _food.Draw(position + new Vector2(160, 0), 0, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Food.ToString(), position + new Vector2(200, 0), _playerResourcesModel.Food >= 0 ? Color.White : Color.Red);

            // Sand
            _terrain.Draw(position + new Vector2(320, 0), 0, Color.White, new Vector2(0.125f, 0.125f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Sand.ToString(), position + new Vector2(360, 0), _playerResourcesModel.Sand >= 0 ? Color.White : Color.Red);

            // Rock
            _terrain.Draw(position + new Vector2(480, 0), 2, Color.White, new Vector2(0.125f, 0.125f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Rock.ToString(), position + new Vector2(520, 0), _playerResourcesModel.Rock >= 0 ? Color.White : Color.Red);

            // Wood
            _tree.Draw(position + new Vector2(640, 0), 7, Color.White, new Vector2(0.25f, 0.25f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Wood.ToString(), position + new Vector2(680, 0), _playerResourcesModel.Wood >= 0 ? Color.White : Color.Red);

            // Coal
            _minerals.Draw(position + new Vector2(800, 0), 0, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Coal.ToString(), position + new Vector2(840, 0), _playerResourcesModel.Coal >= 0 ? Color.White : Color.Red);

            // Copper
            _minerals.Draw(position + new Vector2(960, 0), 1, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Copper.ToString(), position + new Vector2(1000, 0), _playerResourcesModel.Copper >= 0 ? Color.White : Color.Red);

            // Silver
            _minerals.Draw(position + new Vector2(1120, 0), 6, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Silver.ToString(), position + new Vector2(1160, 0), _playerResourcesModel.Silver >= 0 ? Color.White : Color.Red);

            // Gold
            _minerals.Draw(position + new Vector2(1280, 0), 3, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Gold.ToString(), position + new Vector2(1320, 0), _playerResourcesModel.Gold >= 0 ? Color.White : Color.Red);

            // Diamond
            _minerals.Draw(position + new Vector2(1440, 0), 2, Color.White, new Vector2(0.5f, 0.5f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Diamond.ToString(), position + new Vector2(1480, 0), _playerResourcesModel.Diamond >= 0 ? Color.White : Color.Red);

            // Population
            _population.Draw(position + new Vector2(1600, 0), 5, Color.White, new Vector2(0.5f, 0.7f));
            _spriteBatch.DrawString(_gameFont, _playerResourcesModel.Population.ToString() + "/" + _playerResourcesModel.PopulationLimit.ToString(), position + new Vector2(1640, 0), Color.White);
        }
        public override void Draw()
        {
            var visibleArea = VirtualScreenSize.CalculateVisibleArea(_playerModel.Position);

            visibleArea.Width  += 20;
            visibleArea.Height += 20;

            // Draw map and resources
            for (var x = 0; x < _tileList.Tiles.GetLength(0); x++)
            {
                for (var y = 0; y < _tileList.Tiles.GetLength(1); y++)
                {
                    var tile = _tileList.Tiles[x, y];
                    if (tile.Area.Intersects(visibleArea))
                    {
                        if (tile.Type == BiomeType.Water)
                        {
                            _water.Draw(tile.Position, tile.Frame, Color.White, tile.Scale);
                        }
                        else
                        {
                            _terrain.Draw(tile.Position, tile.Frame, Color.White, tile.Scale);
                        }

                        var resource = _tileList.Resources[x, y];
                        if (resource.Amount <= 0)
                        {
                            continue;
                        }

                        if (resource.Type == ResourceType.Rock || resource.Type == ResourceType.Sand || resource.Type == ResourceType.Water)
                        {
                            if (resource.AmountVisible)
                            {
                                DrawResourceAmount(resource);
                            }
                            continue;
                        }

                        if (resource.Type == ResourceType.Wood)
                        {
                            _tree.Draw(resource.Position, resource.Frame, Color.White);
                        }
                        else
                        {
                            _minerals.Draw(resource.Position, resource.Frame, resource.Amount > 0 ? Color.White : Color.Red);
                        }

                        if (resource.AmountVisible)
                        {
                            DrawResourceAmount(resource);
                        }
                    }
                }
            }
        }
        public override void Draw()
        {
            var visibleArea   = VirtualScreenSize.CalculateVisibleArea(_playerModel.Position);
            var mouseState    = Mouse.GetState();
            var mousePoint    = mouseState.Position;
            var mousePosition = VirtualScreenSize.ScreenToWorld(_playerModel.Position, mousePoint.X, mousePoint.Y);

            var position = new Vector2(visibleArea.X, visibleArea.Bottom);

            int i = 0;

            while (i <= visibleArea.Width)
            {
                _bar.Draw(new Vector2(position.X + i, visibleArea.Bottom - 47), 0, Color.White, new Vector2(0.25f, 0.25f));
                i += 64;
            }

            var j = 15;

            foreach (var building in _buildingListModel.AvailableBuildings)
            {
                building.Position = new Vector2(position.X + j, visibleArea.Bottom - 47);
                building.Area     = new Rectangle((int)building.Position.X, (int)building.Position.Y, 64, 64);
                _buildings.Draw(building.Position, building.Frame, _playerResourcesModel.HasEnoughResources(building.Cost) ? Color.White : Color.Red, new Vector2(0.5f, 0.5f));
                j += 68;

                if (building.ShowTooltip)
                {
                    ShowTooltip(building);
                }

                if (building.IsSelected)
                {
                    _buildings.Draw(mousePosition, building.Frame, building.CanBuild ? Color.White : Color.Red);
                }
            }

            foreach (var b in _buildingListModel.BuiltBuildings)
            {
                _buildings.Draw(b.Position, b.Frame, Color.White);
            }
        }