<Processor>TextureProcessor</Processor>
</Compile>
</ItemGroup>
+ <ItemGroup>
+ <Compile Include="Graphics\negative-ship.png">
+ <Name>negative-ship</Name>
+ <Importer>TextureImporter</Importer>
+ <Processor>TextureProcessor</Processor>
+ </Compile>
+ </ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
<!--
To modify your build process, add your task inside one of the targets below and uncomment it.
{
static class ActorFactory
{
- static internal ContentManager Content;
+ static internal Game Game;
static public MainShip CreateMainShip(Vector2 position)
{
- MainShip mainShip = new MainShip();
- mainShip.Initialize(Content, Content.Load<Texture2D>("Graphics\\main-ship"), position);
+ MainShip mainShip = new MainShip(Game);
+ mainShip.Initialize(Game.Content.Load<Texture2D>("Graphics\\main-ship"), position);
ActorManager.CheckIn(mainShip);
return mainShip;
}
- internal static void SetContentManager(ContentManager content)
+ static public StandardShip CreateShip(Ship.Polarity polarity, Vector2 position)
{
- Content = content;
+ StandardShip ship = new StandardShip(Game);
+ Texture2D texture;
+
+ if (polarity == Ship.Polarity.Positive)
+ {
+ texture = Game.Content.Load<Texture2D>("Graphics\\positive-ship");
+ }
+ else if (polarity == Ship.Polarity.Negative)
+ {
+ texture = Game.Content.Load<Texture2D>("Graphics\\negative-ship");
+ }
+ else
+ {
+ texture = Game.Content.Load<Texture2D>("Graphics\\neutral-ship");
+ }
+
+ ship.Initialize(texture, position);
+ ship.SetPolarity(polarity);
+
+ ActorManager.CheckIn(ship);
+
+ return ship;
+ }
+
+ internal static void SetGame(Game game)
+ {
+ ActorFactory.Game = game;
}
}
}
static public void Update(GameTime gameTime)
{
+ CheckActors();
foreach (Actor actor in Actors)
{
actor.Update(gameTime);
actor.Draw(spriteBatch);
}
}
+
+ static void CheckActors()
+ {
+ var i = 0;
+ foreach (Actor actor in Actors)
+ {
+ i++;
+ foreach (Actor other in Actors.Skip(i))
+ {
+ CheckCollision(actor, other);
+
+ if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
+ {
+ CheckMagnetism((Ship)actor, (Ship)other);
+ }
+ }
+ }
+ }
+
+ static void CheckCollision(Actor actor, Actor other)
+ {
+
+ }
+
+ static void CheckMagnetism(Ship actor, Ship other)
+ {
+ if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
+ {
+ var dy = other.Position.Y - actor.Position.Y;
+ var dx = other.Position.X - actor.Position.X;
+ var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
+ var angle = (float) Math.Atan2(dy, dx);
+
+ if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
+ {
+ actor.Magnetize(other, (float)linearDistance, angle);
+ other.Magnetize(actor, (float)linearDistance, 90 - angle);
+ }
+ }
+ }
}
}
{
class Actor
{
+ protected Game game;
+
// Graphics / In-Game
protected Texture2D Texture;
protected Vector2 Origin;
public bool Active;
// Physical Properties
- protected Vector2 Position;
+ public Vector2 Position;
protected Vector2 Velocity;
protected Vector2 Acceleration;
protected float Angle;
get { return Texture.Height; }
}
- public virtual void Initialize(ContentManager Content, Texture2D texture, Vector2 position)
+ public Actor(Game newGame)
+ {
+ game = newGame;
+ }
+
+ public virtual void Initialize(Texture2D texture, Vector2 position)
{
Texture = texture;
Position = position;
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Content;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace SuperPolarity
+{
+ class MainShip : Ship
+ {
+
+ uint Multiplier;
+ uint Lives;
+ uint Score;
+ ParticleEngine particleEngine;
+
+ public MainShip(Game newGame) : base(newGame) {}
+
+ public override void Initialize(Texture2D texture, Vector2 position)
+ {
+ base.Initialize(texture, position);
+
+ InitParticleEngine();
+ SetPolarity(Polarity.Positive);
+
+ Multiplier = 1;
+ Lives = 3;
+ Score = 0;
+
+ BindInput();
+ }
+
+ void InitParticleEngine()
+ {
+ List<Texture2D> texturesList = new List<Texture2D>();
+ texturesList.Add(game.Content.Load<Texture2D>("Graphics\\circle"));
+ texturesList.Add(game.Content.Load<Texture2D>("Graphics\\diamond"));
+ texturesList.Add(game.Content.Load<Texture2D>("Graphics\\star"));
+
+ particleEngine = new ParticleEngine(texturesList, Position);
+ }
+
+ void BindInput()
+ {
+ InputController.Bind("moveX", HandleHorizontalMovement);
+ InputController.Bind("moveY", HandleVerticalMovement);
+ InputController.Bind("changePolarity", HandleChangePolarity);
+ }
+
+ protected void HandleChangePolarity(float value)
+ {
+ SwitchPolarity();
+ }
+
+ public void HandleHorizontalMovement(float value)
+ {
+ Acceleration.X = value * AccelerationRate;
+
+ if (value > 0.1 && Velocity.X < 0 || value < 0.1 && Velocity.X > 0)
+ {
+ Acceleration.X *= 2;
+ }
+
+ if (value > 0.1 && Velocity.Y < 0 || value < 0.1 && Velocity.Y > 0)
+ {
+ Acceleration.Y *= 2;
+ }
+ }
+
+ public void HandleVerticalMovement(float value)
+ {
+ Acceleration.Y = value * AccelerationRate;
+ }
+
+ public override void SwitchPolarity()
+ {
+ base.SwitchPolarity();
+ SwitchParticleEngine(CurrentPolarity);
+ }
+
+ public override void SetPolarity(Polarity newPolarity)
+ {
+ base.SetPolarity(newPolarity);
+ SwitchParticleEngine(newPolarity);
+ }
+
+ protected void SwitchParticleEngine(Polarity polarity)
+ {
+ if (polarity == Polarity.Positive)
+ {
+ particleEngine.Color = Color.Red;
+ }
+ else if (polarity == Polarity.Negative)
+ {
+ particleEngine.Color = Color.Blue;
+ }
+ else
+ {
+ particleEngine.Color = Color.Gray;
+ }
+ }
+
+ public override void Update(GameTime gameTime)
+ {
+ base.Update(gameTime);
+ particleEngine.EmitterLocation = Position;
+ particleEngine.Update();
+ ConstrainToEdges();
+ }
+
+ public override void Magnetize(Ship ship, float distance, float angle)
+ {
+ }
+
+ protected void ConstrainToEdges()
+ {
+ if (Position.X < 0)
+ {
+ Position.X = 0;
+
+ if (Velocity.X < 0)
+ {
+ Velocity.X = 0;
+ }
+ }
+ if (Position.X > game.GraphicsDevice.Viewport.Width)
+ {
+ Position.X = game.GraphicsDevice.Viewport.Width;
+
+ if (Velocity.X > 0)
+ {
+ Velocity.X = 0;
+ }
+ }
+ if (Position.Y < 0)
+ {
+ Position.Y = 0;
+
+ if (Velocity.Y < 0)
+ {
+ Velocity.Y = 0;
+ }
+ }
+ if (Position.Y > game.GraphicsDevice.Viewport.Height)
+ {
+ Position.Y = game.GraphicsDevice.Viewport.Height;
+
+ if (Velocity.Y < 0)
+ {
+ Velocity.Y = 0;
+ }
+ }
+ }
+
+ public override void Draw(SpriteBatch spriteBatch)
+ {
+ particleEngine.Draw(spriteBatch);
+ base.Draw(spriteBatch);
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+
+namespace SuperPolarity
+{
+ class Ship : Actor
+ {
+ public enum Polarity : byte { Negative, Positive, Neutral };
+
+ protected uint HP;
+ public Polarity CurrentPolarity;
+ public uint MagneticRadius;
+
+ protected float FleeVelocity;
+ protected float ActVelocity;
+ protected float ChargeVelocity;
+ protected int RepelRadius;
+
+ protected bool Magnetizing;
+
+ public Ship(Game newGame) : base(newGame) {
+ MagneticRadius = 250;
+ RepelRadius = 100;
+
+ FleeVelocity = 5;
+ ActVelocity = 3;
+ ChargeVelocity = 1.5f;
+ CurrentPolarity = Polarity.Neutral;
+ Magnetizing = false;
+ }
+
+ public virtual void SwitchPolarity()
+ {
+ if (CurrentPolarity == Polarity.Positive)
+ {
+ CurrentPolarity = Polarity.Negative;
+ }
+ else
+ {
+ CurrentPolarity = Polarity.Positive;
+ }
+ }
+
+ public virtual void SetPolarity(Polarity newPolarity)
+ {
+ CurrentPolarity = newPolarity;
+ }
+
+ public virtual void Shoot()
+ {
+ }
+
+ public override void Update(GameTime gameTime)
+ {
+ base.Update(gameTime);
+ Magnetizing = false;
+ }
+
+ public virtual void Magnetize(Ship ship, float distance, float angle)
+ {
+ Magnetizing = true;
+ Polarity polarity = ship.CurrentPolarity;
+
+ if (polarity != CurrentPolarity)
+ {
+ Attract(angle);
+ }
+ else
+ {
+ Repel(distance, angle);
+ }
+ }
+
+ protected void Attract(float angle)
+ {
+ Velocity.X = (float) (ChargeVelocity * Math.Cos(angle));
+ Velocity.Y = (float) (ChargeVelocity * Math.Sin(angle));
+ }
+
+ protected void Repel(float distance, float angle)
+ {
+ if (distance > RepelRadius) { Magnetizing = false; return; }
+ Velocity.X = -(float)(FleeVelocity * Math.Cos(angle));
+ Velocity.Y = -(float)(FleeVelocity * Math.Sin(angle));
+ }
+ }
+}
--- /dev/null
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using Microsoft.Xna.Framework.Content;
+using System.Security.Cryptography;
+
+namespace SuperPolarity
+{
+ class StandardShip : Ship
+ {
+
+ protected int ChangeRate;
+ protected int CurrentTime;
+ protected int AngleChangeProbability;
+ protected int BouncePadding;
+ protected float RotationFactor;
+ protected Random Random;
+ protected bool AddingAngle;
+
+ public StandardShip(Game newGame) : base(newGame) {}
+
+ public override void Initialize(Texture2D texture, Vector2 position)
+ {
+ base.Initialize(texture, position);
+
+ var cryptoResult = new byte[4];
+ new RNGCryptoServiceProvider().GetBytes(cryptoResult);
+
+ ChangeRate = 50;
+ AngleChangeProbability = 50;
+ BouncePadding = 0;
+ MaxVelocity = 1;
+ CurrentTime = 0;
+ RotationFactor = (float) (3 * Math.PI / 180);
+ Random = new Random(BitConverter.ToInt32(cryptoResult, 0));
+ AddingAngle = true;
+ }
+
+ public override void Magnetize(Ship ship, float distance, float angle)
+ {
+ if (ship.GetType() == typeof(MainShip)) {
+ base.Magnetize(ship, distance, angle);
+ }
+ }
+
+ public override void Update(GameTime gameTime)
+ {
+ CurrentTime += gameTime.ElapsedGameTime.Milliseconds;
+ if (!Magnetizing)
+ {
+ AutoMove();
+ BounceBack();
+ }
+ ChangeAngle();
+ Position += Velocity;
+ Magnetizing = false;
+ }
+
+ protected void AutoMove()
+ {
+ float newAngle = Angle;
+
+ if (CurrentTime < ChangeRate)
+ {
+ return;
+ }
+
+ if (Random.Next(AngleChangeProbability) == 2)
+ {
+ AddingAngle = !AddingAngle;
+ }
+
+ CurrentTime = 0;
+
+ if (AddingAngle)
+ {
+ newAngle += (float) ( Random.NextDouble() * RotationFactor);
+ }
+ else
+ {
+ newAngle -= (float) (Random.NextDouble() * RotationFactor);
+ }
+
+ Velocity.X = (float) (MaxVelocity * Math.Cos(newAngle));
+ Velocity.Y = (float) (MaxVelocity * Math.Sin(newAngle));
+ }
+
+ protected void BounceBack()
+ {
+ if (Position.X + Width < -BouncePadding && Velocity.X < 0)
+ {
+ Velocity.X = -Velocity.X;
+ }
+
+ if (Position.Y + Height < -BouncePadding && Velocity.Y < 0)
+ {
+ Velocity.Y = -Velocity.Y;
+ }
+
+ if (Position.X > game.GraphicsDevice.Viewport.Width + BouncePadding && Velocity.X > 0)
+ {
+ Velocity.X = -Velocity.X;
+ }
+
+ if (Position.Y > game.GraphicsDevice.Viewport.Height + BouncePadding && Velocity.Y > 0)
+ {
+ Velocity.Y = -Velocity.Y;
+ }
+ }
+ }
+}
static Dictionary<string, List<Action<float>>> Listeners;
static Dictionary<string, List<Keys>> RegisteredKeys;
static Dictionary<string, List<Buttons>> RegisteredButtons;
+ static List<string> BlockedKeys;
+ static List<string> BlockedButtons;
static GamePadState InputGamePadState;
static KeyboardState InputKeyboardState;
static InputController()
{
Listeners = new Dictionary<string,List<Action<float>>>();
+ RegisteredButtons = new Dictionary<string, List<Buttons>>();
+ RegisteredKeys = new Dictionary<string, List<Keys>>();
+ BlockedKeys = new List<string>();
+ BlockedButtons = new List<string>();
InputKeyboardState = new KeyboardState();
InputGamePadState = new GamePadState();
}
InputKeyboardState = Keyboard.GetState();
}
+ public static void RegisterEventForKey(string eventName, Keys key)
+ {
+ List<Keys> newKeyList;
+ if (!RegisteredKeys.ContainsKey(eventName))
+ {
+ newKeyList = new List<Keys>();
+ RegisteredKeys.Add(eventName, newKeyList);
+ }
+
+ RegisteredKeys.TryGetValue(eventName, out newKeyList);
+
+ newKeyList.Add(key);
+ }
+
+ public static void RegisterEventForButton(string eventName, Buttons button)
+ {
+ List<Buttons> newButtonList;
+ if (!RegisteredButtons.ContainsKey(eventName))
+ {
+ newButtonList = new List<Buttons>();
+ RegisteredButtons.Add(eventName, newButtonList);
+ }
+
+ RegisteredButtons.TryGetValue(eventName, out newButtonList);
+
+ newButtonList.Add(button);
+ }
+
private static void DispatchRegisteredEvents()
{
+ var keyFired = false;
+
+ foreach (KeyValuePair<string,List<Keys>> entry in RegisteredKeys) {
+ keyFired = false;
+ foreach (Keys key in entry.Value)
+ {
+ if (InputKeyboardState.IsKeyDown(key))
+ {
+ if (!BlockedKeys.Contains(entry.Key))
+ {
+ Dispatch(entry.Key, 1);
+ BlockedKeys.Add(entry.Key);
+ }
+ keyFired = true;
+ break;
+ }
+ }
+
+ if (!keyFired)
+ {
+ BlockedKeys.Remove(entry.Key);
+ }
+ }
+
+ foreach (KeyValuePair<string, List<Buttons>> entry in RegisteredButtons)
+ {
+ keyFired = false;
+ foreach (Buttons button in entry.Value)
+ {
+ if (InputGamePadState.IsButtonDown(button))
+ {
+ if (!BlockedButtons.Contains(entry.Key))
+ {
+ Dispatch(entry.Key, 1);
+ BlockedButtons.Add(entry.Key);
+ }
+ keyFired = true;
+ break;
+ };
+ }
+
+ if (!keyFired)
+ {
+ BlockedButtons.Remove(entry.Key);
+ }
+ }
}
private static void DispatchMoveEvents()
xMovement = InputGamePadState.ThumbSticks.Left.X;
yMovement = -InputGamePadState.ThumbSticks.Left.Y;
- Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left));
-
if (InputKeyboardState.IsKeyDown(Keys.Left))
{
xMovement = -1.0f;
+++ /dev/null
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Microsoft.Xna.Framework;
-using Microsoft.Xna.Framework.Content;
-using Microsoft.Xna.Framework.Graphics;
-
-namespace SuperPolarity
-{
- class MainShip : Ship
- {
-
- uint Multiplier;
- uint Lives;
- uint Score;
- ParticleEngine particleEngine;
-
- public override void Initialize(ContentManager Content, Texture2D texture, Vector2 position)
- {
- base.Initialize(Content, texture, position);
-
- Multiplier = 1;
- Lives = 3;
- Score = 0;
-
- List<Texture2D> texturesList = new List<Texture2D>();
- texturesList.Add(Content.Load<Texture2D>("Graphics\\circle"));
- texturesList.Add(Content.Load<Texture2D>("Graphics\\diamond"));
- texturesList.Add(Content.Load<Texture2D>("Graphics\\star"));
-
- particleEngine = new ParticleEngine(texturesList, Position);
-
- BindInput();
- }
-
- void BindInput()
- {
- InputController.Bind("moveX", HandleHorizontalMovement);
- InputController.Bind("moveY", HandleVerticalMovement);
- }
-
- public void HandleHorizontalMovement(float value)
- {
- Acceleration.X = value * AccelerationRate;
- }
-
- public void HandleVerticalMovement(float value)
- {
- Acceleration.Y = value * AccelerationRate;
- }
-
- public override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- particleEngine.EmitterLocation = Position;
- particleEngine.Update();
- }
-
- public override void Draw(SpriteBatch spriteBatch)
- {
- particleEngine.Draw(spriteBatch);
- base.Draw(spriteBatch);
- }
- }
-}
{
private Random random;
public Vector2 EmitterLocation { get; set; }
+ public Color Color;
private List<Particle> particles;
private List<Texture2D> textures;
this.textures = textures;
this.particles = new List<Particle>();
random = new Random();
+ Color = Color.Red;
}
private Particle GenerateNewParticle()
1f * (float)(random.NextDouble() * 2 - 1));
float angle = 0;
float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
- Color color = new Color(
- (float)random.NextDouble(),
- (float)random.NextDouble(),
- (float)random.NextDouble());
+ Color color = Color;
float size = (float)random.NextDouble();
int ttl = 20 + random.Next(40);
+++ /dev/null
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using Microsoft.Xna.Framework;
-using Microsoft.Xna.Framework.Graphics;
-
-namespace SuperPolarity
-{
- class Ship : Actor
- {
- public enum Polarity : byte { Negative, Positive, Neutral };
-
- protected uint HP;
- protected Polarity CurrentPolarity;
-
- public void SwitchPolarity()
- {
- if (CurrentPolarity == Polarity.Positive)
- {
- CurrentPolarity = Polarity.Negative;
- }
- else
- {
- CurrentPolarity = Polarity.Positive;
- }
- }
-
- public void SetPolarity(Polarity newPolarity)
- {
- CurrentPolarity = newPolarity;
- }
-
- public virtual void Shoot()
- {
- }
- }
-}
<ItemGroup>
<Compile Include="ActorFactory.cs" />
<Compile Include="ActorManager.cs" />
+ <Compile Include="Actors\StandardShip.cs" />
<Compile Include="InputController.cs" />
- <Compile Include="MainShip.cs" />
+ <Compile Include="Actors\MainShip.cs" />
<Compile Include="Particle.cs" />
<Compile Include="ParticleEngine.cs" />
- <Compile Include="Actor.cs" />
- <Compile Include="Ship.cs" />
+ <Compile Include="Actors\Actor.cs" />
+ <Compile Include="Actors\Ship.cs" />
<Compile Include="SuperPolarity.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<None Include="Content\Graphics\negative-scout.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
+ <None Include="Content\Graphics\negative-ship.xnb">
+ <CopyToOutputDirectory>Always</CopyToOutputDirectory>
+ </None>
<None Include="Content\Graphics\negative-supercruiser.xnb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
SuperPolarity.graphics = new GraphicsDeviceManager(this);
SuperPolarity.graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
- ActorFactory.SetContentManager(Content);
+ ActorFactory.SetGame(this);
}
/// <summary>
protected override void Initialize()
{
base.Initialize();
+
+ InputController.RegisterEventForButton("changePolarity", Buttons.A);
+ InputController.RegisterEventForKey("changePolarity", Keys.Z);
+
+ InputController.RegisterEventForButton("shoot", Buttons.X);
+ InputController.RegisterEventForKey("shoot", Keys.X);
}
/// <summary>
Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
+ ActorFactory.CreateShip(Ship.Polarity.Positive, new Vector2(200, 200));
+ ActorFactory.CreateShip(Ship.Polarity.Negative, new Vector2(400, 200));
ActorFactory.CreateMainShip(playerPosition);
}
C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarity\bin\WindowsGL\Debug\Content\Graphics\circle.xnb
C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarity\bin\WindowsGL\Debug\Content\Graphics\diamond.xnb
C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarity\bin\WindowsGL\Debug\Content\Graphics\star.xnb
+C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarity\bin\WindowsGL\Debug\Content\Graphics\negative-ship.xnb