System Controller

This is the main C# file that will be in charge of controlling all the other functions of the engine. It is, in other words, the core part of the core engine and changes made to other parts of the game engine will not affect it but changes made to it will have slight changes to other parts of the game engine.

#region File Description
//-----------------------------------------------------------------------------
// Game.cs
//
// Titanic Game Engine System Controller... LOL
// Made by 3rd Year Games Developers 2010
//-----------------------------------------------------------------------------
#endregion
 
#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion
 
namespace GameEngineCore
{
    public class GameEngineCore : Microsoft.Xna.Framework.Game
    {
        #region Fields
 
        GraphicsDeviceManager graphics;
 
        SpriteBatch spriteBatch;
 
        KeyboardState lastKeyboardState = new KeyboardState();
        GamePadState lastGamePadState = new GamePadState();
        KeyboardState currentKeyboardState = new KeyboardState();
        GamePadState currentGamePadState = new GamePadState();
/// <summary>
/// ///////////////////////////////////////////New////////////////////////////////
/// </summary>
        private float viewAspectRatio;
        private int viewPortWidth, viewPortHeight;
 
        Camera cameras = new Camera();
        ModelViewer modelViewer = new ModelViewer();
        ModelObject modelObject = new ModelObject();
 
        bool flyingChaseCam = false;
        /// ///////////////////////////////////////////New////////////////////////////////
 
        ChaseCamera camera;
        Character playerOne = new Character();
 
        Model groundModel;
 
        bool cameraSpringEnabled = true;
 
        #endregion
 
        #region Initialization
 
        public GameEngineCore()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
 
            graphics.PreferredBackBufferWidth = 853;
            graphics.PreferredBackBufferHeight = 480;
 
            // Create the chase camera
            camera = new ChaseCamera();
 
            // Set the camera offsets
            camera.DesiredPositionOffset = new Vector3(0.0f, 2000.0f, 3500.0f);
            camera.LookAtOffset = new Vector3(0.0f, 150.0f, 0.0f);
 
            // Set camera perspective
            camera.NearPlaneDistance = 10.0f;
            camera.FarPlaneDistance = 100000.0f;
 
            //TODO: Set any other camera invariants here such as field of view
        }
 
        /// <summary>
        /// Initalize the game
        /// </summary>
        protected override void Initialize()
        {
            if (flyingChaseCam == false)
            {
                /// ///////////////////////////////////////////New////////////////////////////////
                spriteBatch = new SpriteBatch(GraphicsDevice);
                viewPortHeight = graphics.GraphicsDevice.Viewport.Height;
                viewPortWidth = graphics.GraphicsDevice.Viewport.Width;
                viewAspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
                /// ///////////////////////////////////////////New////////////////////////////////
                base.Initialize();
            }
            else
            {
                base.Initialize();
 
                // Set the camera aspect ratio
                // This must be done after the class to base.Initalize() which will
                // initialize the graphics device.
                camera.AspectRatio = (float)graphics.GraphicsDevice.Viewport.Width /
                    graphics.GraphicsDevice.Viewport.Height;
 
                // Perform an inital reset on the camera so that it starts at the resting
                // position. If we don't do this, the camera will start at the origin and
                // race across the world to get behind the chased object.
                // This is performed here because the aspect ratio is needed by Reset.
                UpdateCameraChaseTarget();
                camera.Reset();
            }
        }
 
        /// <summary>
        /// Load graphics content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
 
            if (flyingChaseCam == true)
            {
                groundModel = Content.Load<Model>("Groundx");
            }
            else
            {
                /// ///////////////////////////////////////////New////////////////////////////////
                /// 
                viewPortHeight = graphics.GraphicsDevice.Viewport.Height;
                viewPortWidth = graphics.GraphicsDevice.Viewport.Width;
                viewAspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
                cameras.LoadContent(this.Content, viewAspectRatio);
                modelViewer.LoadContent(this.Content);
                modelObject.LoadContent(this.Content);
 
                /// ///////////////////////////////////////////New////////////////////////////////
            }
        }
 
        #endregion
 
        #region Update and Draw
 
        /// <summary>
        /// Allows the game to run logic.
        /// </summary>
        protected override void Update(GameTime gameTime)
        {
            lastKeyboardState = currentKeyboardState;
            lastGamePadState = currentGamePadState;
 
            currentKeyboardState = Keyboard.GetState();
            currentGamePadState = GamePad.GetState(PlayerIndex.One);
 
            if (flyingChaseCam == true)
            {
                // Exit when the Escape key or Back button is pressed
                if (currentKeyboardState.IsKeyDown(Keys.Escape) ||
                    currentGamePadState.Buttons.Back == ButtonState.Pressed)
                {
                    Exit();
                }
 
                // Pressing the A button or key toggles the spring behavior on and off
                if (lastKeyboardState.IsKeyUp(Keys.A) &&
                    (currentKeyboardState.IsKeyDown(Keys.A)) ||
                    (lastGamePadState.Buttons.A == ButtonState.Released &&
                    currentGamePadState.Buttons.A == ButtonState.Pressed))
                {
                    cameraSpringEnabled = !cameraSpringEnabled;
                }
 
                // Reset the character on R key or right thumb stick clicked
                if (currentKeyboardState.IsKeyDown(Keys.R) ||
                    currentGamePadState.Buttons.RightStick == ButtonState.Pressed)
                {
                    playerOne.Reset();
                    camera.Reset();
                }
 
                // Update the character
                playerOne.Update(gameTime);
 
                // Update the camera to chase the new target
                UpdateCameraChaseTarget();
            }
            else
            {
                /// ///////////////////////////////////////////New////////////////////////////////
                viewPortHeight = graphics.GraphicsDevice.Viewport.Height;
                viewPortWidth = graphics.GraphicsDevice.Viewport.Width;
                viewAspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
 
                if (currentKeyboardState.IsKeyDown(Keys.R) || currentGamePadState.Buttons.Back == ButtonState.Pressed)
                {
                    cameras.ResetCamera();
                    modelViewer.ResetObject();
                }
 
                if (cameras.GetCameraState() == "Follow")
                {
 
                }
                cameras.Update(gameTime, modelViewer.GetPosition(), currentGamePadState, currentKeyboardState, modelViewer.GetDirection());
                modelViewer.Update(gameTime, currentGamePadState, currentKeyboardState);
 
                /// ///////////////////////////////////////////New////////////////////////////////
            }
 
            // The chase camera's update behavior is the springs, but we can
            // use the Reset method to have a locked, spring-less camera
            if (cameraSpringEnabled)
                camera.Update(gameTime);
            else
                camera.Reset();
 
            base.Update(gameTime);
        }
 
        /// <summary>
        /// Update the values to be chased by the camera
        /// </summary>
        private void UpdateCameraChaseTarget()
        {
            camera.ChasePosition = playerOne.Position;
            camera.ChaseDirection = playerOne.Direction;
            camera.Up = playerOne.Up;
        }
 
        /// <summary>
        /// Draws the character and ground.
        /// </summary>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice device = graphics.GraphicsDevice;
            device.Clear(Color.DarkBlue);
 
            if (flyingChaseCam == false)
            {
                /// ///////////////////////////////////////////New////////////////////////////////
 
                modelObject.Draw(cameras.GetCameraView(), cameras.GetCameraProjection());
                modelViewer.Draw(cameras.GetCameraView(), cameras.GetCameraProjection());
                cameras.Draw(spriteBatch, viewAspectRatio);
 
                Vector3 cameraPosition = cameras.GetCameraPosition();
                float cameraYaw = cameras.GetCameraYaw();
                float cameraPitch = cameras.GetCameraPitch();
 
                Vector3 modelPosition = modelViewer.GetPosition();
 
                /// ///////////////////////////////////////////New////////////////////////////////
            }
            else
            {
                DrawModel(groundModel, Matrix.Identity);
            }
 
            base.Draw(gameTime);
        }
 
        /// <summary>
        /// Simple model drawing method. The interesting part here is that
        /// the view and projection matrices are taken from the camera object.
        /// </summary>        
        private void DrawModel(Model model, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);
 
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                    effect.World = transforms[mesh.ParentBone.Index] * world;
 
                    // Use the matrices provided by the chase camera
                    effect.View = camera.View;
                    effect.Projection = camera.Projection;
                }
                mesh.Draw();
            }
        }
 
        #endregion
    }
 
    #region Entry Point
 
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static class Program
    {
        static void Main()
        {
            using (GameEngineCore game = new GameEngineCore())
            {
                game.Run();
            }
        }
    }
 
    #endregion
}

For any code classes to work with the System Controller and be successfully able to be merged with it, they need to have methods in them that'll enable them to be

  1. initialised
  2. updated
  3. drawn

Leaving such methods may make it tricky to incorporate classes to be used with the System Controller!!

People working with non-visual classes, or rather logic classes such as AI and Physics, do not worry of these methods except for initialisation and classes that will enable whatever is on screen to be updated. We'll work together on this soon…

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License