1. Carolyn Van Slyck
  2. omgponies

Commits

Carolyn Van Slyck  committed aca8082

* Made Quit a controller action
* Poll the first active controller instead of relying on Player1
* Added SpriteManager
* Fixed namespace to be OMG, not OMG.Windows
* Added GameScreen

  • Participants
  • Parent commits db419d0
  • Branches default

Comments (0)

Files changed (16)

File OMG/GameControllerAction.cs

View file
 using System;
 
-namespace OMT
+namespace OMG
 {
     [Flags]
     public enum GameControllerAction
     {
         None = 0,
-        Up = 1,
-        Down = 2,
-        Left = 4,
-        Right = 8,
-        Select = 16
+        Quit = 1,
+        Up = 2,
+        Down = 4,
+        Left = 8,
+        Right = 16,
+        Select = 32   
     }
 }

File OMG/GameControllerState.cs

View file
 using System.Linq;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Input;
-using OMG.Windows;
 
-namespace OMT
+namespace OMG
 {
     public class GameControllerState
     {
             {
                 foreach (var keyPressed in keysPressed)
                 {
+                    if (!_keysMapping.ContainsKey(keyPressed))
+                        continue;
                     state.Actions |= _keysMapping[keyPressed];
                 }
             }
 
         private static void ProcessGamePad(GameControllerState state)
         {
-            var gamePadState = GamePad.GetState(PlayerIndex.One);
+            GamePadState? activeGamePad = GetActiveGamePad();
 
-            if (!gamePadState.IsConnected)
+            if (activeGamePad == null)
                 return;
 
+            var gamePadState = activeGamePad.Value;
+
+            if(gamePadState.Buttons.Back == ButtonState.Pressed)
+            {
+                state.Actions |= GameControllerAction.Quit;
+            }
+
             if (gamePadState.DPad.Up == ButtonState.Pressed)
             {
                 state.Actions |= GameControllerAction.Up;
                 _lastRightThumStickLocation = rightThumbStickLocation;
             }
         }
+
+        private static GamePadState? GetActiveGamePad()
+        {
+            var gamePadState = GamePad.GetState(PlayerIndex.One);
+            if (gamePadState.IsConnected)
+                return gamePadState;
+
+            gamePadState = GamePad.GetState(PlayerIndex.Two);
+            if (gamePadState.IsConnected)
+                return gamePadState;
+
+            gamePadState = GamePad.GetState(PlayerIndex.Three);
+            if (gamePadState.IsConnected)
+                return gamePadState;
+
+            gamePadState = GamePad.GetState(PlayerIndex.Four);
+            if (gamePadState.IsConnected)
+                return gamePadState;
+
+            return null;
+        }
     }
 }

File OMG/GameCursor.cs

View file
 using Microsoft.Xna.Framework;
 
-namespace OMG.Windows
+namespace OMG
 {
     public static class GameCursor
     {

File OMG/GameScreen.cs

View file
+using Microsoft.Xna.Framework;
+
+namespace OMG
+{
+    public static class GameScreen
+    {
+        public static Rectangle SafeArea { get; set; }
+    }
+}

File OMG/MiniGame/AgilityCourse/AgilityCourseGame.cs

View file
-using OMT.Sprite;
+using OMG.Sprite;
 
-namespace OMT.MiniGame.AgilityCourse
+namespace OMG.MiniGame.AgilityCourse
 {
     public class AgilityCourseGame : MiniGameBase
     {
             Sprites.Add(_monkey);
             Sprites.Add(_scrollingBackground);
             Sprites.Add(_cursor);
-        }
+        } 
     }
 }

File OMG/MiniGame/MiniGameBase.cs

View file
-using System.Collections.Generic;
-using System.Linq;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Graphics;
-using Microsoft.Xna.Framework.Input;
-using OMT.Sprite;
-using OMG.Windows;
+using OMG.Sprite;
 
-namespace OMT.MiniGame
+namespace OMG.MiniGame
 {
     public abstract class MiniGameBase : Game
     {
         private GraphicsDeviceManager _graphics;
         private SpriteBatch _spriteBatch;
 
-        protected List<ISprite> Sprites { get; set; }
+        protected SpriteManager Sprites { get; set; }
 
         protected MiniGameBase()
         {
             _graphics = new GraphicsDeviceManager(this);
             Content.RootDirectory = "Content";
-            Sprites = new List<ISprite>();
-            GameCursor.Location = new Vector2(Window.ClientBounds.X / 2, Window.ClientBounds.Y / 2);
+            Sprites = new SpriteManager();
         }
 
         /// <summary>
             // Create a new SpriteBatch, which can be used to draw textures.
             _spriteBatch = new SpriteBatch(GraphicsDevice);
 
-            foreach (var sprite in Sprites)
-            {
-                sprite.LoadContent(Content, Window);
-            }
+            GameScreen.SafeArea = _graphics.GraphicsDevice.Viewport.TitleSafeArea;
+            GameCursor.Location = new Vector2(GameScreen.SafeArea.X / 2, GameScreen.SafeArea.Y / 2);
+
+            Sprites.LoadContent(Content);
         }
 
         /// <summary>
         /// <param name="gameTime">Provides a snapshot of timing values.</param>
         protected override void Update(GameTime gameTime)
         {
-            // Allows the game to exit
-            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
-                this.Exit();
+            var gameControllerState = GameControllerState.GetState();
+            if (gameControllerState.HasAction(GameControllerAction.Quit))
+                Exit();
 
-            foreach (var sprite in Sprites)
-            {
-                sprite.Update(gameTime);
-            }
+            Sprites.Update(gameTime, gameControllerState);
 
             base.Update(gameTime);
         }
 
             _spriteBatch.Begin();
 
-            foreach (var sprite in Sprites.OrderBy(sprite => sprite.DrawOrder))
-            {
-                sprite.Draw(_spriteBatch);
-            }
+            Sprites.Draw(_spriteBatch);
 
             _spriteBatch.End();
 

File OMG/OMG.Windows.csproj

View file
     <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
     <OutputType>WinExe</OutputType>
     <AppDesignerFolder>Properties</AppDesignerFolder>
-    <RootNamespace>OMG.Windows</RootNamespace>
+    <RootNamespace>OMG</RootNamespace>
     <AssemblyName>OMG.Windows</AssemblyName>
     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
     <TargetFrameworkProfile>Client</TargetFrameworkProfile>
     <Compile Include="GameControllerAction.cs" />
     <Compile Include="GameControllerState.cs" />
     <Compile Include="GameCursor.cs" />
+    <Compile Include="GameScreen.cs" />
     <Compile Include="MiniGame\AgilityCourse\AgilityCourseGame.cs" />
     <Compile Include="MiniGame\MiniGameBase.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Sprite\Monkey.cs" />
     <Compile Include="Sprite\ScrollingBackground.cs" />
     <Compile Include="Sprite\Sprite.cs" />
+    <Compile Include="Sprite\SpriteManager.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="Game.ico" />

File OMG/OMG.Xbox.csproj

View file
     <Compile Include="GameControllerAction.cs" />
     <Compile Include="GameControllerState.cs" />
     <Compile Include="GameCursor.cs" />
+    <Compile Include="GameScreen.cs" />
     <Compile Include="MiniGame\AgilityCourse\AgilityCourseGame.cs" />
     <Compile Include="MiniGame\MiniGameBase.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Sprite\Monkey.cs" />
     <Compile Include="Sprite\ScrollingBackground.cs" />
     <Compile Include="Sprite\Sprite.cs" />
+    <Compile Include="Sprite\SpriteManager.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="Game.ico" />

File OMG/Program.cs

View file
-using System;
-using OMT.MiniGame.AgilityCourse;
+using OMG.MiniGame.AgilityCourse;
 
-namespace OMG.Windows
+namespace OMG
 {
 #if WINDOWS || XBOX
     static class Program

File OMG/Properties/AssemblyInfo.cs

View file
 using System.Reflection;
-using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 
 // General Information about an assembly is controlled through the following 
 // set of attributes. Change these attribute values to modify the information
 // associated with an assembly.
-[assembly: AssemblyTitle("OMG.Windows")]
-[assembly: AssemblyProduct("OMG.Windows")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyCompany("Microsoft")]
-[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
+[assembly: AssemblyTitle("OMG Ponies, Puppies and Kitties")]
+[assembly: AssemblyProduct("OMG Ponies, Puppies and Kitties")]
+[assembly: AssemblyDescription("Now with 100% more cute!")]
+[assembly: AssemblyCompany("SweetGeek")]
+[assembly: AssemblyCopyright("Copyright © SweetGeek 2011")]
 [assembly: AssemblyTrademark("")]
 [assembly: AssemblyCulture("")]
 

File OMG/Sprite/Cursor.cs

View file
-using Microsoft.Xna.Framework;
+using System.Collections.Generic;
+using Microsoft.Xna.Framework;
 
-namespace OMT.Sprite
+namespace OMG.Sprite
 {
     public class Cursor : Sprite
     {
         public const string AssetName = "cursor";
 
+        protected List<ISprite> CollidedSprites = new List<ISprite>();
+
         public Cursor() : base(AssetName)
         {
             
         }
 
-        public override void Update(GameTime gameTime)
+        protected bool Selected;
+
+        private Color? _lastTint;
+        public override void Update(GameTime gameTime, GameControllerState gameControllerState)
         {
-            var controllerState = GameControllerState.GetState();
-            Position = controllerState.GameCursorLocation;
+            Position = gameControllerState.GameCursorLocation;
 
-            if(controllerState.HasAction(GameControllerAction.Select))
+            if (!Selected && gameControllerState.HasAction(GameControllerAction.Select))
             {
+                Selected = true;
+                _lastTint = Tint;
                 Tint = Color.Red;
             }
             else
             {
-                Tint = NoTint;
+                Selected = false;
+                if (_lastTint.HasValue)
+                {
+                    Tint = _lastTint.Value;
+                    _lastTint = null;
+                }
             }
+
+            //else
+            //{
+            //    if(!CollidedSprites.Any())
+            //    {
+            //        Tint = NoTint;
+            //    }
+            //}
+        }
+
+        protected override Rectangle TargetSize
+        {
+            get
+            {
+                return new Rectangle(0,0, 1,1);
+            }
+        }
+
+        public override void Collide(ISprite sprite)
+        {
+            //if(!CollidedSprites.Contains(sprite))
+            //{
+            //    CollidedSprites.Add(sprite);
+            //}
+            Tint = Color.Green;
+        }
+
+        public override void Free(ISprite sprite)
+        {
+            //CollidedSprites.Remove(sprite);
+            Tint = NoTint;
         }
     }
 }

File OMG/Sprite/ISprite.cs

View file
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 
-namespace OMT.Sprite
+namespace OMG.Sprite
 {
+    public delegate void SpritePositionChanging(ISprite sprite, SpritePositionChangingEventArguments e);
+
+    public class SpritePositionChangingEventArguments
+    {
+        public SpritePositionChangingEventArguments(Rectangle newPosition)
+        {
+            NewPosition = newPosition;
+        }
+
+        public Rectangle NewPosition;
+        public bool ChangePositionAllowed = true;
+    }
+
     public interface ISprite
     {
         Vector2 Position { get; set; }
+        event SpritePositionChanging PositionChanging;
+        bool AllowCollide(ISprite sprite);
+        void Collide(ISprite sprite);
         Rectangle Size { get; }
+        Rectangle Border { get; }
         float Scale { get; set; }
         int DrawOrder { get; set; }
-        void LoadContent(ContentManager contentManager, GameWindow window);
+        void LoadContent(ContentManager contentManager);
         void Draw(SpriteBatch spriteBatch);
-        void Update(GameTime gameTime);
+        void Update(GameTime gameTime, GameControllerState gameControllerState);
+        void Free(ISprite sprite);
     }
 }

File OMG/Sprite/Monkey.cs

View file
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Content;
 
-namespace OMT.Sprite
+namespace OMG.Sprite
 {
     public class Monkey : Sprite
     {
         {
         }
 
-        public override void LoadContent(ContentManager contentManager, GameWindow gameWindow)
+        public override void LoadContent(ContentManager contentManager)
         {
-            base.LoadContent(contentManager, gameWindow);
-            Position = new Vector2(0, gameWindow.ClientBounds.Height - Size.Height);
+            base.LoadContent(contentManager);
+            Position = new Vector2(GameScreen.SafeArea.Width / 2, GameScreen.SafeArea.Height / 2);
         }
 
-        public override void Update(GameTime gameTime)
+        public override void Update(GameTime gameTime, GameControllerState gameControllerState)
         {
-            var controllerState = GameControllerState.GetState();
-
-            UpdateMovement(controllerState);
+            UpdateMovement(gameControllerState);
             Move(gameTime, _currentSpeed, _currentDirection);
         }
 

File OMG/Sprite/ScrollingBackground.cs

View file
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 
-namespace OMT.Sprite
+namespace OMG.Sprite
 {
     public class ScrollingBackground : ISprite
     {
         }
 
         public Vector2 Position { get; set; }
+        public event SpritePositionChanging PositionChanging;
+
+        public bool AllowCollide(ISprite sprite)
+        {
+            return true;
+        }
+
+        public void Collide(ISprite sprite)
+        {
+            
+        }
 
         public Rectangle Size { get; set; }
 
+        public Rectangle Border
+        {
+            get { return new Rectangle((int)Position.X, (int)Position.Y, Size.Width, Size.Height); }
+        }
+
         public float Scale { get; set; }
 
         public int DrawOrder { get; set; }
 
-        public void LoadContent(ContentManager contentManager, GameWindow window)
+        public void LoadContent(ContentManager contentManager)
         {
             for (int i = 0; i < NumberOfBackgrounds; i++)
             {
                 var background = _backgrounds[i];
                 background.Scale = 2.0f;
-                background.LoadContent(contentManager, window);
+                background.LoadContent(contentManager);
 
                 if (i > 0)
                 {
             }
         }
 
-        public void Update(GameTime gameTime)
+        public void Update(GameTime gameTime, GameControllerState gameControllerState)
         {
             for (int i = 0; i < NumberOfBackgrounds; i++)
             {
             }
         }
 
+        public void Free(ISprite sprite)
+        {
+            
+        }
+
         private void ScrollBackground(ISprite background, GameTime gameTime)
         {
             var direction = new Vector2(-1, 0);

File OMG/Sprite/Sprite.cs

View file
-using Microsoft.Xna.Framework;
+using System;
+using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Content;
 using Microsoft.Xna.Framework.Graphics;
 
-namespace OMT.Sprite
+namespace OMG.Sprite
 {
     public class Sprite : ISprite
     {
         public Sprite(string assetName)
         {
             _assetName = assetName;
-            Position = new Vector2(0,0);
+            Position = new Vector2(0, 0);
             Scale = 1.0f;
             Tint = NoTint;
         }
         protected static readonly Color NoTint = Color.White;
 
         protected Color Tint { get; set; }
-        protected GameWindow GameWindow;
-        public Vector2 Position { get; set; }
+
+        private Vector2 _position;
+        public Vector2 Position
+        {
+            get { return _position; }
+            set
+            {
+                if (_position == value)
+                    return;
+
+                if (PositionChanging != null)
+                {
+                    var e = new SpritePositionChangingEventArguments(GetBorder(value));
+                    PositionChanging(this, e);
+                    if (!e.ChangePositionAllowed)
+                        return;
+                }
+
+                _position = value;
+            }
+        }
+
+        public event SpritePositionChanging PositionChanging;
+
+        public virtual bool AllowCollide(ISprite sprite)
+        {
+            return true;
+        }
+
+        public virtual void Collide(ISprite sprite)
+        {
+            
+        }
+
         public Rectangle Size { get; private set; }
 
+        public Rectangle Border
+        {
+            get { return GetBorder(Position); }
+        }
+
+        private Rectangle GetBorder(Vector2 position)
+        {
+            return new Rectangle((int)position.X, (int)position.Y, TargetSize.Width, TargetSize.Height);
+        }
+
+        protected virtual Rectangle TargetSize
+        {
+            get { return Size; }
+        }
+
         private float _scale;
         public float Scale
         {
 
         private Texture2D _spriteTexture;
 
-        public virtual void LoadContent(ContentManager contentManager, GameWindow gameWindow)
+        public virtual void LoadContent(ContentManager contentManager)
         {
-            GameWindow = gameWindow;
             _spriteTexture = contentManager.Load<Texture2D>(_assetName);
             UpdateSize();
         }
 
         public virtual void Draw(SpriteBatch spriteBatch)
         {
-            spriteBatch.Draw(_spriteTexture, Position, new Rectangle(0,0,_spriteTexture.Width, _spriteTexture.Height), Tint, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
+            spriteBatch.Draw(_spriteTexture, Position, null, Tint, 0.0f, Vector2.Zero, Scale, SpriteEffects.None, 0);
         }
 
-        public virtual void Update(GameTime gameTime)
+        public virtual void Update(GameTime gameTime, GameControllerState gameControllerState)
+        {
+            
+        }
+
+        public virtual void Free(ISprite sprite)
         {
             
         }

File OMG/Sprite/SpriteManager.cs

View file
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace OMG.Sprite
+{
+    public class SpriteManager : List<ISprite>
+    {
+        public new void Add(ISprite sprite)
+        {
+            base.Add(sprite);
+            sprite.PositionChanging += SpritePositionChanging;
+            CollidedSprites.Add(sprite, new List<ISprite>());
+        }
+
+        private bool _collisionDetectionOn = false;
+        protected Dictionary<ISprite, IEnumerable<ISprite>> CollidedSprites = new Dictionary<ISprite, IEnumerable<ISprite>>();
+
+        private void SpritePositionChanging(ISprite movingSprite, SpritePositionChangingEventArguments e)
+        {
+            if (!_collisionDetectionOn)
+                return;
+
+            if(IsSpriteMovingOffOfScreen(movingSprite, e.NewPosition))
+            {
+                e.ChangePositionAllowed = false;
+                return;
+            }
+
+            var collidedSprites = DetectCollidedSprites(movingSprite, e.NewPosition);
+
+            var allowCollision = AllowSpriteCollision(collidedSprites, movingSprite);
+            if(!allowCollision)
+            {
+                e.ChangePositionAllowed = false;
+                return;
+            }
+
+            FreeSprites(collidedSprites, movingSprite);
+            CollidedSprites[movingSprite] = collidedSprites;
+            CollideSprites(movingSprite);
+        }
+
+        private void FreeSprites(IEnumerable<ISprite> collidedSprites, ISprite movingSprite)
+        {
+            var freeSprites = CollidedSprites[movingSprite].Except(collidedSprites);
+
+            foreach (var sprite in freeSprites)
+            {
+                sprite.Free(movingSprite);
+                movingSprite.Free(sprite);
+            }
+        }
+
+        private bool IsSpriteMovingOffOfScreen(ISprite movingSprite, Rectangle newPosition)
+        {
+            return !GameScreen.SafeArea.Contains(newPosition);
+        }
+
+        private IEnumerable<ISprite> DetectCollidedSprites(ISprite movingSprite, Rectangle newPosition)
+        {
+            return this.Where(sprite => sprite != movingSprite && sprite.Border.Intersects(newPosition));
+        }
+
+        private bool AllowSpriteCollision(IEnumerable<ISprite> collidedSprites, ISprite movingSprite)
+        {
+            return !collidedSprites.Any(sprite => !sprite.AllowCollide(movingSprite));
+        }
+
+        public void CollideSprites(ISprite movingSprite)
+        {
+            foreach (var collidedSprite in CollidedSprites[movingSprite])
+            {
+                movingSprite.Collide(collidedSprite);
+                collidedSprite.Collide(movingSprite);
+            }
+        }
+
+        public void LoadContent(ContentManager contentManager)
+        {
+            foreach (var sprite in this)
+            {
+                sprite.LoadContent(contentManager);
+            }
+
+            _collisionDetectionOn = true;
+        }
+
+        public void Draw(SpriteBatch spriteBatch)
+        {
+            foreach (var sprite in this.OrderBy(sprite => sprite.DrawOrder))
+            {
+                sprite.Draw(spriteBatch);
+            }
+        }
+
+        public void Update(GameTime gameTime, GameControllerState gameControllerState)
+        {
+            foreach (var sprite in this)
+            {
+                sprite.Update(gameTime, gameControllerState);
+            }
+        }
+    }
+}