1. Michael Gough
  2. HI-2DRTSSGMDDPP1

Commits

Michael Gough  committed f8ac76b

Added some basic camera controls. One can now pan around and zoom in and out on
the star system.
I've also added a few more planets and greatly improved the randomisation of
the orbit angular speeds for entities.

  • Participants
  • Parent commits d2d051b
  • Branches default

Comments (0)

Files changed (11)

File GameProgram/GameProgram/Game1.cs

View file
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
 using Microsoft.Xna.Framework.Media;
+using Innovation;
 
 namespace GameProgram
 {
+  
   /// <summary>
   /// This is the main type for your game
   /// </summary>
   public class Game1 : Microsoft.Xna.Framework.Game
   {
     GraphicsDeviceManager graphics;
+    MouseDevice mouse;
     SpriteBatch spriteBatch;
+
+    public static readonly Random randomNumber = new Random();
     
     private World world;
     private GraphicsScene graphicsScene;
 
     public Game1()
     {
-      graphics = new GraphicsDeviceManager( this );
+      graphics = new GraphicsDeviceManager( this );      
       Content.RootDirectory = "Content";
 
       graphics.PreferredBackBufferWidth = 1024;
       //graphics.IsFullScreen = true;
 
       this.Window.AllowUserResizing = true;
+      this.IsMouseVisible           = true;
+
+      mouse = new MouseDevice( graphics );
     }
 
     /// <summary>
 
       world.update( gameTime );
 
+      mouse.Update();
+
+      graphicsScene.setZoomFactor( ( mouse.ScrollPosition / 10000.0F ) + 0.5F );
+
+      if( mouse.WasButtonHeld( MouseButtons.Left ) )
+      {
+        graphicsScene.setCameraOffset( mouse.Delta );
+      }
+
+      //graphicsScene.setZoomFactor( ( Mouse.GetState().ScrollWheelValue / 10000.0F) + 0.5F );
+
       //// TODO: Add your update logic here
       //// Move the sprite by speed, scaled by elapsed time.
       //spritePosition +=
 
       // TODO: Add your drawing code here
       spriteBatch.Begin( SpriteSortMode.BackToFront, BlendState.AlphaBlend );
+      
       graphicsScene.draw( spriteBatch );
       //spriteBatch.Draw( myTexture, spritePosition, Color.White );
       spriteBatch.End();

File GameProgram/GameProgram/GameProgram.csproj

View file
     <Compile Include="GraphicsItemFactory.cs" />
     <Compile Include="GraphicsSceneFactory.cs" />
     <Compile Include="GroundStructure.cs" />
+    <Compile Include="InputDevice.cs" />
     <Compile Include="Loading.cs" />
+    <Compile Include="MouseDevice.cs" />
     <Compile Include="VisibleGameEntity.cs" />
     <Compile Include="Moon.cs" />
     <Compile Include="MoonFactory.cs" />

File GameProgram/GameProgram/Geoid.cs

View file
     protected Vector2 velocityWrtPlanetarySystemOrigin = Vector2.Zero;
     private float parentOrbitRadius = 0.0F;
     private float longitude = 0.0F;
-    protected float angularSpeed = ( ( 2F* 2F * (float)Math.PI ) / 60.0F );
+    protected float orbitAngularSpeed = ( ( 2F* 2F * (float)Math.PI ) / 60.0F );
 
     public Geoid( PlanetarySystem planetarySystem, float parentOrbitRadius, float initialLongitude )      
     {      
       //                          * (float)gameTime.ElapsedGameTime.TotalSeconds
       //                          * parentOrbitRadius;
 
-      longitude += angularSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
+      longitude += orbitAngularSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
 
       //coordinatesWrtStarSystemOrigin += velocityWrtStarSystemOrigin * (float)gameTime.ElapsedGameTime.TotalSeconds;
 

File GameProgram/GameProgram/GraphicsItem.cs

View file
   {
     Texture2D texture;
     VisableGameEntity visableGameEntity;
-    Vector2 cameraOffset = new Vector2( 800.0F, 600.0F);
+    Vector2 cameraOffset = new Vector2( -800.0F, -600.0F );
+
+    public Vector2 CameraOffset
+    {
+      get { return cameraOffset; }
+      set { cameraOffset -= value; }
+    }
     Vector2 spriteOffset = Vector2.Zero;
-    float scaleFactor = 0.3F;
+    float worldScaleFactor = 0.5F;
+    float zoomFactor = 6000.0F;
+
+    public float ZoomFactor
+    {
+      get { return zoomFactor; }
+      set { zoomFactor = value; }
+    }
 
     public VisableGameEntity VisableGameEntity
     {
 
     public void draw( SpriteBatch spriteBatch )
     {
-      spriteBatch.Draw( texture, 
-                        visableGameEntity.getAbsolutePosition() + cameraOffset + spriteOffset, 
+      spriteBatch.Draw( texture,
+                        (( visableGameEntity.getAbsolutePosition() + spriteOffset ) * zoomFactor) - cameraOffset, 
                         null, 
                         Color.White, 
                         0f, 
-                        Vector2.Zero, 
-                        visableGameEntity.RelativeRadius * scaleFactor, 
+                        Vector2.Zero,
+                        visableGameEntity.RelativeRadius * worldScaleFactor * zoomFactor, 
                         SpriteEffects.None, 
                         0f );
     }
     public void loadContent( Game1 game1 )
     {
       texture = game1.Content.Load<Texture2D>( "WhiteCircle" );
-      spriteOffset.X = -(texture.Width/2) * visableGameEntity.RelativeRadius * scaleFactor;
-      spriteOffset.Y = -(texture.Height/2) * visableGameEntity.RelativeRadius * scaleFactor;
+      spriteOffset.X = -(texture.Width/2) * visableGameEntity.RelativeRadius * worldScaleFactor;
+      spriteOffset.Y = -(texture.Height/2) * visableGameEntity.RelativeRadius * worldScaleFactor;
     }
 
     public GraphicsItem( VisableGameEntity visableGameEntity )

File GameProgram/GameProgram/GraphicsScene.cs

View file
 using System.Linq;
 using System.Text;
 using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework;
 
 namespace GameProgram
 {
     {
       graphicsItems.Add( newGraphicsItem );
     }
+
+    public void setZoomFactor( float newZoomFactor )
+    {
+      if (newZoomFactor < 0.0F)
+      {
+        newZoomFactor = 0.0F;
+      }
+
+      foreach (GraphicsItem graphicsItem in graphicsItems)
+      {
+        graphicsItem.ZoomFactor = -newZoomFactor;
+      }
+
+    }
+
+    public void setCameraOffset( Vector2 cameraOffset )
+    {
+      foreach (GraphicsItem graphicsItem in graphicsItems)
+      {
+        graphicsItem.CameraOffset = cameraOffset;
+      }
+
+    }
+
   }
 }

File GameProgram/GameProgram/InputDevice.cs

View file
+using System;
+
+/// <summary> http://www.innovativegames.net/blog/blog/2008/11/16/engine-tutorial-6/
+/// </summary>
+namespace Innovation
+{
+    // The base input device class. Other input device types will
+    // inherit from it. The <T> generic allows us to specify what
+    // type of input device state it manages. (MouseState, etc.)
+    public abstract class InputDevice<T> //: Component
+    {
+        // The State object of type T specified above
+        public abstract T State { get; }
+    }
+
+    // An input device event argument class that can handle events
+    // for several types of input device. "O" specified what type
+    // of object the event was triggered by (Key, Button,
+    // MouseButton, etc). "S" specifies the type of state the
+    // event provides (MouseState, KeyboardState, etc.)
+    public class InputDeviceEventArgs<O, S> : EventArgs
+    {
+        // The object of type O that triggered the event
+        public O Object;
+
+        // The input device that manages the state of type S that
+        // owns the triggered object
+        public InputDevice<S> Device;
+
+        // The state of the input device of type S that was triggered
+        public S State;
+
+        // Constructor takes the triggered object and input device
+        public InputDeviceEventArgs(O Object, InputDevice<S> Device)
+        {
+            this.Object = Object;
+            this.Device = Device;
+            this.State = ((InputDevice<S>)Device).State;
+        }
+    }
+
+    // An input device event handler delegate. This defines what type
+    // of method may handle an event. In this case, it is a void that
+    // accepts the specified input device arguments
+    public delegate void InputEventHandler<O, S>(object sender,
+        InputDeviceEventArgs<O, S> e);
+}

File GameProgram/GameProgram/Moon.cs

View file
   {
     public Moon( PlanetarySystem planetarySystem, float parentOrbitRadius, float initialLongitude ) :
       base( planetarySystem, parentOrbitRadius, initialLongitude )
-    {
-      Random randomNumber = new Random( System.DateTime.Now.Millisecond);
+    {     
 
       RelativeRadius = 0.1F;
-      angularSpeed = (randomNumber.Next(-100,100) % 40) * ( 2F * (float)Math.PI ) / 60.0F;
+      orbitAngularSpeed = ( Game1.randomNumber.Next( -100, 100 ) % 40 ) * ( 2F * (float)Math.PI ) / 60.0F;
      
     }
 

File GameProgram/GameProgram/MouseDevice.cs

View file
+using Microsoft.Xna.Framework.Input;
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using Microsoft.Xna.Framework;
+
+
+
+namespace Innovation
+{
+    // An enum of buttons on the mouse, since XNA doesn't provide one
+    public enum MouseButtons { Left, Right, Middle, X1, X2 };
+
+    // An input device that keeps track of the Mouse and MouseState
+    public class MouseDevice : InputDevice<MouseState>
+    {
+        // The last and current MouseStates
+        MouseState last;
+        MouseState current;
+
+        // The MouseButtons that are currently down
+        MouseButtons[] currentButtons;
+
+        GraphicsDeviceManager graphics;
+
+        // Public properties for the above members
+        public override MouseState State { get { return current; } }
+        public MouseButtons[] PressedButtons { get { return currentButtons; } }
+
+        // The position in (X,Y) coordinates of the mouse. Setting the mouse
+        // position will tell XNA to move the mouse
+        Vector2 position;
+        public Vector2 Position
+        {
+            get { return position; }
+            set 
+            { 
+                position = value; 
+                Mouse.SetPosition((int)value.X, (int)value.Y); 
+            }
+        }
+
+        // The change in position of the mouse between the last two frames
+        public Vector2 Delta = Vector2.Zero;
+
+        // Whether to reset the mouse to the middle of the screen on each
+        // update to simulate a screen with no borders
+        public bool ResetMouseAfterUpdate = false;
+
+        // The current position of the scroll wheel
+        public float ScrollPosition { get { return current.ScrollWheelValue; } }
+
+        // The change in position of the scroll wheel between the last two
+        // frames
+        public float ScrollDelta = 0;
+
+        // Events for when a button is pressed, released, or held
+        public event InputEventHandler<MouseButtons, MouseState> ButtonPressed;
+        public event InputEventHandler<MouseButtons, MouseState> ButtonReleased;
+        public event InputEventHandler<MouseButtons, MouseState> ButtonHeld;
+
+        // Constructor gets the initial MouseState, moves the mouse to the
+        // center of the screen, and does the first update
+        public MouseDevice( GraphicsDeviceManager graphics)
+        {
+          this.graphics = graphics;
+
+            if (ResetMouseAfterUpdate)
+                Position = new Vector2(
+                    graphics.GraphicsDevice.Viewport.Width / 2,
+                    graphics.GraphicsDevice.Viewport.Height / 2 );
+
+            current = Mouse.GetState();
+            Update();
+        }
+
+        // List of pressed buttons used to setup currentButtons
+        List<MouseButtons> pressed = new List<MouseButtons>();
+
+        // public override void Update()
+        public void Update()
+        {
+            // Update the last state
+            last = current;
+
+            // Extract the scroll wheel position from the last state
+            float lastScrollWheel = last.ScrollWheelValue;
+
+            // Update the current state and clear the list of pressed
+            // buttons
+            current = Mouse.GetState();
+            pressed.Clear();
+
+            // Calculcate the scroll delta with the current position
+            // and the copy of the last position we made earlier
+            ScrollDelta = ScrollPosition - lastScrollWheel;
+
+            // If we are supposed to reset the mouse after each update...
+            if (ResetMouseAfterUpdate)
+            {
+                // Find the center of the screen
+                Vector2 center = new Vector2(
+                    graphics.GraphicsDevice.Viewport.Width / 2,
+                    graphics.GraphicsDevice.Viewport.Height / 2);
+
+                // Find the mouse's distance from the center of the screen to
+                // find the change in position
+                Delta = new Vector2(current.X - center.X, current.Y - center.Y);
+
+                // Move the mouse back to the center so we never run out of
+                // screen space
+                Position = center;
+            }
+            else
+                // Otherwise just find the difference in position between the
+                // last two frames
+                Delta = new Vector2(current.X - last.X, current.Y - last.Y);
+
+            // For each mouse button...
+            foreach (MouseButtons button in Util.GetEnumValues<MouseButtons>())
+            {
+                // If it is down, add it to the list
+                if (IsButtonDown(button))
+                    pressed.Add(button);
+
+                // If it was just pressed, fire the event
+                if (WasButtonPressed(button))
+                    if (ButtonPressed != null)
+                        ButtonPressed(this, new InputDeviceEventArgs
+                            <MouseButtons, MouseState>(button, this));
+
+                // If it was just released, fire the event
+                if (WasButtonReleased(button))
+                    if (ButtonReleased != null)
+                        ButtonReleased(this, new InputDeviceEventArgs
+                            <MouseButtons, MouseState>(button, this));
+
+                // If it was held, fire the event
+                if (WasButtonHeld(button))
+                    if (ButtonHeld != null)
+                        ButtonHeld(this, new InputDeviceEventArgs
+                            <MouseButtons, MouseState>(button, this));
+            }
+
+            // Update the currentButtons array from the list of buttons
+            // that are down
+            currentButtons = pressed.ToArray();
+        }
+
+        // Whether the specified button is currently down
+        public bool IsButtonDown(MouseButtons Button)
+        {
+            return IsButtonDown(Button, current);
+        }
+
+        // An internal version of IsButtonDown that also allows us
+        // to specify which state the check against
+        bool IsButtonDown(MouseButtons Button, MouseState State)
+        {
+            return GetButtonState(Button, State) == 
+                ButtonState.Pressed ? true : false;
+        }
+
+        // Whether the specified button is currently up
+        public bool IsButtonUp(MouseButtons Button)
+        {
+            return IsButtonUp(Button, current);
+        }
+
+        // An internal version of IsButtonUp that also allows us
+        // to specify which state the check against
+        bool IsButtonUp(MouseButtons Button, MouseState State)
+        {
+            return GetButtonState(Button, State) == 
+                ButtonState.Released ? true : false;
+        }
+
+        // Whether the specified button is down for the time this frame
+        public bool WasButtonPressed(MouseButtons Button)
+        {
+            if (IsButtonUp(Button, last) && IsButtonDown(Button, current))
+                return true;
+
+            return false;
+        }
+
+        // Whether the specified button is up for the first this frame
+        public bool WasButtonReleased(MouseButtons Button)
+        {
+            if (IsButtonDown(Button, last) && IsButtonUp(Button, current))
+                return true;
+
+            return false;
+        }
+
+        // Whether the specified button has been down for more than one frame
+        public bool WasButtonHeld(MouseButtons Button)
+        {
+            if (IsButtonDown(Button, last) && IsButtonDown(Button, current))
+                return true;
+
+            return false;
+        }
+
+        // Retrieves the ButtonState of the specified button from the 
+        // specified state.
+        ButtonState GetButtonState(MouseButtons Button, MouseState State)
+        {
+            if (Button == MouseButtons.Left)
+                return State.LeftButton;
+            else if (Button == MouseButtons.Middle)
+                return State.MiddleButton;
+            else if (Button == MouseButtons.Right)
+                return State.RightButton;
+            else if (Button == MouseButtons.X1)
+                return State.XButton1;
+            else if (Button == MouseButtons.X2)
+                return State.XButton2;
+
+            return ButtonState.Released;
+        }
+    }
+
+  public static class Util
+  {
+    public static List<T> GetEnumValues<T>()
+    {
+      Type currentEnum = typeof( T );
+      List<T> resultSet = new List<T>();
+
+      if (currentEnum.IsEnum)
+      {
+        FieldInfo[] fields = currentEnum.GetFields( BindingFlags.Static | BindingFlags.Public );
+        foreach (FieldInfo field in fields)
+          resultSet.Add( (T)field.GetValue( null ) );
+      }
+      else
+        throw new ArgumentException( "The argument must of type Enum or of a type derived from Enum", "T" );
+
+      return resultSet;
+    }
+  }
+}

File GameProgram/GameProgram/PlanetarySystem.cs

View file
 
       coordinatesWrtStarSystemOrigin.X = parentOrbitRadius * (float)Math.Cos( longitude );
       coordinatesWrtStarSystemOrigin.Y = parentOrbitRadius * (float)Math.Sin( longitude );
-
-      Random randomNumber = new Random( System.DateTime.Now.Millisecond );
-
-      angularSpeed = ( randomNumber.Next( -100, 100 ) % 4 )+1 * ( ( 2F * (float)Math.PI ) / 60.0F );
+      
+      angularSpeed = ( Game1.randomNumber.Next( -100, 100 ) / 1000.0F )+1 * ( ( 2F * (float)Math.PI ) / 60.0F );
     }
 
 

File GameProgram/GameProgram/PlanetarySystemFactory.cs

View file
     }
 
     internal PlanetarySystem createPlanetarySystem( StarSystem newStarSystem, float parentOrbitalRadius, float initialLongitude )
-    {
-      Random randomNumber = new Random(System.DateTime.Now.Millisecond);
+    {     
 
-      PlanetarySystem newPlanetarySystem = new PlanetarySystem( newStarSystem, parentOrbitalRadius, randomNumber.Next( 2 * (int)Math.PI ) );
+      PlanetarySystem newPlanetarySystem = new PlanetarySystem( newStarSystem, parentOrbitalRadius, Game1.randomNumber.Next( 2 * (int)Math.PI ) );
 
       Planet newPlanet = planetFactory.createPlanet( newPlanetarySystem, 0.0F, 0.0F );
 
-      Moon newMoon = moonFactory.createMoon( newPlanetarySystem, randomNumber.Next( 50, 75 ), randomNumber.Next( 2 * (int)Math.PI ) );
+      Moon newMoon = moonFactory.createMoon( newPlanetarySystem, Game1.randomNumber.Next( 150, 175 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
 
       newPlanetarySystem.Planet = newPlanet;
       newPlanetarySystem.addMoon( newMoon );
 
-      newMoon = moonFactory.createMoon( newPlanetarySystem, randomNumber.Next( 100, 125 ), randomNumber.Next( 2 * (int)Math.PI ) );
+      newMoon = moonFactory.createMoon( newPlanetarySystem, Game1.randomNumber.Next( 300, 325 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
 
       newPlanetarySystem.addMoon( newMoon );
 

File GameProgram/GameProgram/StarSystemFactory.cs

View file
 
     internal StarSystem createStarSystem()
     {
-      Random randomNumber = new Random( System.DateTime.Now.Millisecond );
 
       StarSystem newStarSystem = new StarSystem();
 
       Star newStar = starFactory.createStar( newStarSystem );
-      PlanetarySystem newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, randomNumber.Next( 150, 225 ), randomNumber.Next( 2 * (int)Math.PI ) );
+      PlanetarySystem newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 500, 1000 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      
+      newStarSystem.Star = newStar;      
       newStarSystem.addPlanetarySystem( newPlanetarySystem );
-      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, randomNumber.Next( 350, 400), randomNumber.Next( 2 * (int)Math.PI ) );
 
-      newStarSystem.Star = newStar;
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 2500, 3500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
       newStarSystem.addPlanetarySystem( newPlanetarySystem );
 
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 4500, 5500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 5500, 6500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 6500, 7500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 7500, 8500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 7500, 8500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 10500, 12500 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      newPlanetarySystem = planetarySystemFactory.createPlanetarySystem( newStarSystem, Game1.randomNumber.Next( 12000, 15000 ), Game1.randomNumber.Next( 2 * (int)Math.PI ) );
+      newStarSystem.addPlanetarySystem( newPlanetarySystem );
+
+      
+      
+
       return newStarSystem;
 
     }