root/src/AIDemos/Screens/Demos/DemoGameScreen.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 4.33 KB

(July 12, 2009 15:10 UTC) Almost 3 years ago


  

 
Show/hide line numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using AIRendering;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Brains.Framework;

namespace AIDemos.Screens.Demos
{
    public class DemoGameScreen:GameScreen
    {
        protected PrimitiveBatch primitiveBatch;
        protected ContentManager content;
       
        protected SpriteFont gameFont;
        protected SpriteFont titleFont;
        protected AIEngine _engine;
        public string Title { get; set; }
        public string Description{ get; set; }
        public DemoGameScreen(string title,string description)
        {
            Title = title;
            Description = description;
        }
        protected void CenterCamOnMap()
        {
            primitiveBatch.CameraPosition = new Vector2(
                -ScreenManager.GraphicsDevice.Viewport.Width / 2 +
                _engine.World.Map.ClusterGrid.Grids[0].Width / 2,
                -ScreenManager.GraphicsDevice.Viewport.Height / 2 +
                _engine.World.Map.ClusterGrid.Grids[0].Height / 2);

        }
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            gameFont = content.Load<SpriteFont>("gamefont");
            titleFont= content.Load<SpriteFont>("titlefont");
            primitiveBatch = new PrimitiveBatch(ScreenManager.GraphicsDevice);

            _engine = new AIEngine();
            
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            ScreenManager.SpriteBatch.DrawString(
               titleFont,
               Title,
               new Vector2(10, 10),
               Color.White,
               0,
               new Vector2(0),
               0.5f, SpriteEffects.None,
               0
               );

            ScreenManager.SpriteBatch.DrawString(
                gameFont,
                Description,
                new Vector2(10,50),
                Color.White,
                0,
                new Vector2(0),
                0.5f,SpriteEffects.None,
                0
                );
            base.Draw(gameTime);
        }
        GameTime lastGameTime;
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            lastGameTime = gameTime;
            Vector2 _moveCam = Vector2.Zero;
            KeyboardState _currentKB = Keyboard.GetState();
            if (_currentKB.IsKeyDown(Keys.Up))
                _moveCam.Y = -1;
            if (_currentKB.IsKeyDown(Keys.Down))
                _moveCam.Y = 1;
            if (_currentKB.IsKeyDown(Keys.Left))
                _moveCam.X = -1;
            if (_currentKB.IsKeyDown(Keys.Right))
                _moveCam.X = 1;
            if (_moveCam != Vector2.Zero)
            {
                primitiveBatch.CameraPosition += _moveCam * lastGameTime.GetElapsed() * 500;
            }

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
        public override void HandleInput(InputState input)
        {
            int playerIndex = (int)ControllingPlayer.Value;
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
            
           
            PlayerIndex index;
            if (input.IsNewKeyPress(Keys.PageDown, null, out index))
            {
                primitiveBatch.Zoom -= 0.1f;
            }
            if (input.IsNewKeyPress(Keys.PageUp,null,out index))
            {
                primitiveBatch.Zoom += 0.1f;
            }
            if (input.IsNewKeyPress(Keys.Space, null, out index))
            {
                primitiveBatch.Zoom = 1f;
            }

            bool gamePadDisconnected = !gamePadState.IsConnected &&
                                    input.GamePadWasConnected[playerIndex];

            if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
            {
                ScreenManager.AddScreen(new AIDemosPauseMenuScreen(), ControllingPlayer);
            }
            base.HandleInput(input);
        }
    }
}