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

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 5.3 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;
using Microsoft.Xna.Framework.Graphics;
using AIDemos;
using Microsoft.Xna.Framework.Content;
using System.Threading;
using Microsoft.Xna.Framework.Input;
using Brains.Framework;
using AIRendering;
using AIDemos.GameClasses;
using Brains;
using Brains.Framework.Behaviors;
using AIDemos.GameClasses.Behaviors;
using Brains.Framework.Map;
using Brains.Framework.Utility;
using Brains.Framework.Locomotion;

namespace AIDemos.Screens.Demos
{
    public class Demo1 : DemoGameScreen
    {
        private GridCell _endNode;
        
        private MouseState _currentMouse;
        private MouseState _lastMouse;

        public Demo1():base("Demo 1 - Basic Pathfinding",
            "Click a valid grid cell to spawn a basic actor to pathfind to the destination\n" +
                "Red Cross   : Blocked Cell\n" +
                "Empty Tile  : Traversable Cell\n" +
                "Greed Circle: Destination Cell")
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void LoadContent()
        {
            base.LoadContent();
            
            LoadAI();

            CenterCamOnMap();
       
            ScreenManager.Game.ResetElapsedTime();
        }
        
        private void LoadAI()
        {
            _engine.CreateWorld(new DrawableWorld());
            _engine.World.LoadMapDataFromTexture(content.Load<Texture2D>("DemoTextures/MAPDemo1"),
                                                32,
                                                typeof(DrawableGrid));

            //Find the marked green node
            _endNode= _engine.World.Map.AllCells.First(
                                                    a => a.Labels[AIConsts.COLORG] == 255 &&
                                                    a.Labels[AIConsts.COLORR] == 0 &&
                                                    a.Labels[AIConsts.COLORB] == 0
                                                    );
            

        }
        
        public override void UnloadContent()
        {
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                     bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                _engine.Update(gameTime);
            }
        }
        
        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            _lastMouse = _currentMouse;
            _currentMouse = Mouse.GetState();


            int playerIndex = (int)ControllingPlayer.Value;
            
            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];

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

            if (!gamePadDisconnected)
            {
                Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y);

                if (_currentMouse.LeftButton == ButtonState.Released &&
                    _lastMouse.LeftButton == ButtonState.Pressed)
                {
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
                    GridCell cell = _engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(mapMousePos);
                    if (cell != null)
                    {
                        GameActor actor = new GameActor(cell.Position, 5);
                        actor.Locomotion = new LocomotionController();
                        actor.Locomotion.MaxSpeed = 50;
                        actor.Locomotion.MaxRotation = MathHelper.TwoPi;

                        _engine.World.AddActor(actor);
                        SequenceBehavior _sequence = new SequenceBehavior();
                        
                        DrawableBehaviorGoTo _goto = new DrawableBehaviorGoTo();
                        _goto.StartNode = cell;
                        _goto.EndNode=_endNode;
                        _sequence.SubBehaviors.Add(_goto);
                        _sequence.SubBehaviors.Add(new DieBehavior());
                        actor.RootBehavior = _sequence;
                    }
                }
            }

            base.HandleInput(input);
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.Black, 0, 0);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            spriteBatch.Begin();
            ((IRender)_engine.World).Render(primitiveBatch);
            base.Draw(gameTime);
            spriteBatch.End();

            
            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
        }
    }
}