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

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 5.95 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 System.Collections;
using Brains.Framework.Map;
using Brains.Framework.Utility;
using AIDemos.GameClasses.Behaviors.Conditions;
using AIDemos.GameClasses.Behaviors.Decorators;
using Brains.Framework.Locomotion;
using Brains.Framework.Grouping;


namespace AIDemos.Screens.Demos
{
    public class Demo7 : DemoGameScreen
    {

        public Demo7():base("Demo 7 - Group Behaviors","")
        {
            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());
            ChangeDemoState(DemoState.Seperation);
        }

        private void ChangeDemoState(DemoState demoState)
        {
            _demoState = demoState;
            _engine = new AIEngine();
            _engine.CreateWorld(new DrawableWorld());
            
            Group _group = new Group();

            switch (_demoState)
            {
                case DemoState.Seperation:
                    _engine.World.SetupMap(400, 400, 64);
                    float _tmpY = 32;
                    for (int i = 0; i < 10; i++)
                    {
                        Agent boid = AddBoid(new Vector2(32,_tmpY));
                        _tmpY += 16;
                        ((LocomtionSteering)boid.Locomotion).TurnOnSeek(_engine.World.Map.ClusterGrid.Grids[0].GetCell(4, 4).Position);
                        ((LocomtionSteering)boid.Locomotion).TurnOnCohesion();
                        ((LocomtionSteering)boid.Locomotion).TurnOnAlignment();
                        ((LocomtionSteering)boid.Locomotion).TurnOnSeperation();
                        _engine.World.AddActor(boid);
                        _group.AddAgent(boid);
                    }
                    break;
            }
            _engine.World.AddGroup(_group);
            CenterCamOnMap();
        }

        private Agent AddBoid(Vector2 pos)
        {
            GameActor _actor = new GameActor(pos, 10);
            _actor.Locomotion = new Brains.Framework.Locomotion.LocomtionSteering();
            _actor.Locomotion.MaxRotation = MathHelper.TwoPi;
            _actor.Locomotion.MaxSpeed = 100;
            return _actor;
        }
        DemoState _demoState = DemoState.Seperation;
        enum DemoState
        {
            Seperation,
            Alignment,
            Cohesion
        }
        class CellComparer:IComparer
        {
            
            public int Compare(object x, object y)
            {
                GridCell p1;
                GridCell p2;

                if (x is GridCell)
                    p1 = x as GridCell;
                else
                    throw new ArgumentException("Object is not of type GridCell.");

                if (y is GridCell)
                    p2 = y as GridCell;
                else
                    throw new ArgumentException("Object is not of type GridCell.");

                return p1.Labels[AIConsts.COLORR].CompareTo(p2.Labels[AIConsts.COLORR]);
            }
        }
        
        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);


                Vector2 mousPos = new Vector2(_currentMouse.X, _currentMouse.Y);

                if (_currentMouse.LeftButton == ButtonState.Released &&
                    _lastMouse.LeftButton == ButtonState.Pressed)
                {
                    Vector2 mapMousePos = mousPos + primitiveBatch.CameraPosition;
                   
                }
            }
        }
        MouseState _currentMouse;
        MouseState _lastMouse;
        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];

           

            base.HandleInput(input);
        }

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

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

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

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