root/src/ZuneDemo/Game1.cs

User picture

Author: conkerjo

Revision: 30 («Previous)


File Size: 5.66 KB

(November 01, 2009 14:48 UTC) Over 2 years ago

Added Zune HD support and simple sample

 
Show/hide line numbers
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ZuneDemo.Renderer;
using Brains.Framework.Map;
using Brains.Framework.Utility;
using Brains.Framework.Locomotion;
using Brains.Framework.Behaviors;
using ZuneDemo.Behaviors;

namespace ZuneDemo
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        RenderTarget2D zuneTarget;
        private GridCell endNode;
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private Brains.Framework.AIEngine engine;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Zune.
            TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
        }

        protected override void Initialize()
        {
    
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            zuneTarget = new RenderTarget2D(GraphicsDevice, 480, 272, 1, SurfaceFormat.Color);
            
            LoadAI();

            //CenterCamOnMap();

        }

        private void LoadAI()
        {
            this.engine = new Brains.Framework.AIEngine();
            this.engine.CreateWorld(new DrawableWorld(this.Content));
            this.engine.World.LoadMapDataFromTexture(Content.Load<Texture2D>("Textures/MAPDemo1"),
                                                16,
                                                typeof(DrawableGrid));

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


        }

        protected override void UnloadContent()
        {
        }

        float timer = 0;
        
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            this.engine.Update(gameTime);

            timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (timer > 3)
            {
                timer = 0;
                GridCell cell = this.engine.World.Map.ClusterGrid.Grids[0].GetCellAtPosition(new Vector2(20, 20));
                if (cell != null)
                {
                    DrawableAgent actor = new DrawableAgent(this.Content, cell.Position, 5);
                    actor.Locomotion = new LocomotionController();
                    actor.Locomotion.MaxSpeed = 50;
                    actor.Locomotion.MaxRotation = MathHelper.TwoPi;

                    this.engine.World.AddActor(actor);
                    SequenceBehavior _sequence = new SequenceBehavior();

                    GoToBehavior _goto = new GoToBehavior();
                    _goto.StartNode = cell;
                    _goto.EndNode = endNode;
                    _sequence.SubBehaviors.Add(_goto);
                    _sequence.SubBehaviors.Add(new DieBehavior());
                    actor.RootBehavior = _sequence;
                }
            }
            base.Update(gameTime);
        }
        
        protected override bool BeginDraw()
        {
            bool val = base.BeginDraw();
            if (val)
            {
                GraphicsDevice.SetRenderTarget(0, zuneTarget);
                GraphicsDevice.Viewport = new Viewport
                {
                    X = 0,
                    Y = 0,
                    Width = 480,
                    Height = 272,
                    MinDepth = GraphicsDevice.Viewport.MinDepth,
                    MaxDepth = GraphicsDevice.Viewport.MaxDepth
                };
            }
            return val;
            
        }

        protected override void EndDraw()
        {
            GraphicsDevice.SetRenderTarget(0, null);
            GraphicsDevice.Viewport = new Viewport
            {
                X = 0,
                Y = 0,
                Width = 272,
                Height = 480,
                MinDepth = GraphicsDevice.Viewport.MinDepth,
                MaxDepth = GraphicsDevice.Viewport.MaxDepth
            };

            GraphicsDevice.Clear(Color.Yellow);


            spriteBatch.Begin();
            spriteBatch.Draw(
                zuneTarget.GetTexture(),
                new Vector2(136f, 240f),
                null,
                Color.White,
                MathHelper.PiOver2,
                new Vector2(240f, 136f),
                1f,
                SpriteEffects.None,
                0);
            spriteBatch.End();
            base.EndDraw();
            
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            this.spriteBatch.Begin();
            ((DrawableWorld)this.engine.World).Render(this.spriteBatch);

            this.spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}