diff options
| author | Ben Beltran <ben@nsovocal.com> | 2013-11-12 22:40:29 -0600 |
|---|---|---|
| committer | Ben Beltran <ben@nsovocal.com> | 2013-11-12 22:40:29 -0600 |
| commit | 38c7d3f9eb7d63937c6654ff5dd6046ce02dd59c (patch) | |
| tree | 9df6c6faa8cb250d66c022f2d54a0f769adb7cfd | |
| parent | 2af83e98005a14c439b360a5b9ac636f594d9f0c (diff) | |
I think bullets come out now.
21 files changed, 329 insertions, 28 deletions
diff --git a/Super Polarity Content/Super Polarity ContentContent/Graphics/square.png b/Super Polarity Content/Super Polarity ContentContent/Graphics/square.png Binary files differnew file mode 100644 index 0000000..960c50a --- /dev/null +++ b/Super Polarity Content/Super Polarity ContentContent/Graphics/square.png diff --git a/Super Polarity Content/Super Polarity ContentContent/Super Polarity ContentContent.contentproj b/Super Polarity Content/Super Polarity ContentContent/Super Polarity ContentContent.contentproj index f86c49e..1aedf53 100644 --- a/Super Polarity Content/Super Polarity ContentContent/Super Polarity ContentContent.contentproj +++ b/Super Polarity Content/Super Polarity ContentContent/Super Polarity ContentContent.contentproj @@ -110,6 +110,7 @@ </Compile> </ItemGroup> <ItemGroup> + <Folder Include="Fonts\" /> <Folder Include="Sound\" /> </ItemGroup> <ItemGroup> @@ -136,6 +137,13 @@ <Processor>TextureProcessor</Processor> </Compile> </ItemGroup> + <ItemGroup> + <Compile Include="Graphics\square.png"> + <Name>square</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. diff --git a/Super Polarity/ActorFactory.cs b/Super Polarity/ActorFactory.cs index 3e2f80b..4ebbb4c 100644 --- a/Super Polarity/ActorFactory.cs +++ b/Super Polarity/ActorFactory.cs @@ -52,5 +52,18 @@ namespace SuperPolarity { ActorFactory.Game = game; } + + internal static Bullet CreateBullet(Vector2 position, float angle) + { + Bullet bullet = new Bullet(Game); + + bullet.Initialize(Game.Content.Load<Texture2D>("Graphics\\square"), position); + + bullet.Angle = angle; + + ActorManager.CheckIn(bullet); + + return bullet; + } } } diff --git a/Super Polarity/ActorManager.cs b/Super Polarity/ActorManager.cs index 39ae336..1b4ef96 100644 --- a/Super Polarity/ActorManager.cs +++ b/Super Polarity/ActorManager.cs @@ -9,10 +9,14 @@ namespace SuperPolarity { static class ActorManager { + + static Game Game; + static int OutlierBounds; static List<Actor> Actors; static ActorManager() { + OutlierBounds = 100; Actors = new List<Actor>(); } @@ -29,6 +33,7 @@ namespace SuperPolarity static public void Update(GameTime gameTime) { CheckActors(); + CheckOutliers(); foreach (Actor actor in Actors) { actor.Update(gameTime); @@ -74,13 +79,33 @@ namespace SuperPolarity 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); + var otherAngle = (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); + other.Magnetize(actor, (float)linearDistance, otherAngle); } } } + + static void CheckOutliers() + { + for (var i = Actors.Count; i > 0; i--) + { + var actor = Actors[i-1]; + if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds || + actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds || + actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds) + { + CheckOut(actor); + } + } + } + + internal static void SetGame(Game game) + { + Game = game; + } } } diff --git a/Super Polarity/Actors/Actor.cs b/Super Polarity/Actors/Actor.cs index 4351bf2..588ff14 100644 --- a/Super Polarity/Actors/Actor.cs +++ b/Super Polarity/Actors/Actor.cs @@ -12,6 +12,8 @@ namespace SuperPolarity { protected Game game; + public List<Actor> Children; + // Graphics / In-Game protected Texture2D Texture; protected Vector2 Origin; @@ -21,7 +23,7 @@ namespace SuperPolarity public Vector2 Position; protected Vector2 Velocity; protected Vector2 Acceleration; - protected float Angle; + public float Angle; // Constraints / Behavior protected float MaxVelocity; @@ -48,6 +50,8 @@ namespace SuperPolarity Position = position; Active = true; + Children = new List<Actor>(); + Origin = new Vector2(Texture.Width / 2, Texture.Height / 2); Velocity = new Vector2(0, 0); Acceleration = new Vector2(0, 0); @@ -115,12 +119,15 @@ namespace SuperPolarity { Move(gameTime); ChangeAngle(); + CheckOutliers(); } - public void Move(GameTime gameTime) + public virtual void Move(GameTime gameTime) { AutoDeccelerate(gameTime); + var maxVelocity = MaxVelocity; + Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds; Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; @@ -155,7 +162,26 @@ namespace SuperPolarity public virtual void Draw(SpriteBatch spriteBatch) { + foreach (Actor child in Children) + { + child.Draw(spriteBatch); + } + spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f); } + + void CheckOutliers() + { + for (var i = Children.Count; i > 0; i--) + { + var actor = Children[i - 1]; + if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds || + actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds || + actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds) + { + Children.Remove(actor); + } + } + } } } diff --git a/Super Polarity/Actors/Bullet.cs b/Super Polarity/Actors/Bullet.cs new file mode 100644 index 0000000..6862e69 --- /dev/null +++ b/Super Polarity/Actors/Bullet.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace SuperPolarity +{ + class Bullet : Actor + { + protected ParticleEngine particleEngine; + + public Bullet(Game newGame) + : base(newGame) + { + } + + ~Bullet() + { + particleEngine = null; + } + + public override void Initialize(Texture2D texture, Vector2 position) + { + base.Initialize(texture, position); + particleEngine = ParticleEffectFactory.CreateBullet(position); + } + + public override void Update(GameTime gameTime) + { + Velocity.X = (float)(MaxVelocity * Math.Cos(Angle)); + Velocity.Y = (float)(MaxVelocity * Math.Sin(Angle)); + + Position += Velocity; + + particleEngine.Update(); + particleEngine.EmitterLocation = Position; + } + + public override void Draw(SpriteBatch spriteBatch) + { + base.Draw(spriteBatch); + particleEngine.Draw(spriteBatch); + } + } +} diff --git a/Super Polarity/Actors/MainShip.cs b/Super Polarity/Actors/MainShip.cs index 851c658..1f4f22a 100644 --- a/Super Polarity/Actors/MainShip.cs +++ b/Super Polarity/Actors/MainShip.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Threading; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; @@ -10,36 +11,40 @@ namespace SuperPolarity { class MainShip : Ship { - - uint Multiplier; - uint Lives; - uint Score; + + public static Color BlueColor; + public static Color RedColor; + ParticleEngine particleEngine; + protected bool Shooting; + protected int ShotCooldown; + public MainShip(Game newGame) : base(newGame) {} + ~MainShip() + { + particleEngine = null; + } + public override void Initialize(Texture2D texture, Vector2 position) { base.Initialize(texture, position); + MainShip.BlueColor = new Color(230, 244, 249); + MainShip.RedColor = new Color(255, 234, 241); + InitParticleEngine(); SetPolarity(Polarity.Positive); - Multiplier = 1; - Lives = 3; - Score = 0; + ShotCooldown = 50; 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); + particleEngine = ParticleEffectFactory.CreatePolarCircle(Position); } void BindInput() @@ -47,6 +52,20 @@ namespace SuperPolarity InputController.Bind("moveX", HandleHorizontalMovement); InputController.Bind("moveY", HandleVerticalMovement); InputController.Bind("changePolarity", HandleChangePolarity); + InputController.Bind("shoot", HandleShot); + } + + protected void HandleShot(float value) + { + Children.Add(ActorFactory.CreateBullet(Position, Angle)); + Shooting = true; + Timer t = new Timer(new TimerCallback(UnlockShot)); + t.Change(ShotCooldown, Timeout.Infinite); + } + + protected void UnlockShot(object state) + { + InputController.Unlock("shoot"); } protected void HandleChangePolarity(float value) @@ -90,11 +109,11 @@ namespace SuperPolarity { if (polarity == Polarity.Positive) { - particleEngine.Color = Color.Red; + particleEngine.Color = MainShip.RedColor; } else if (polarity == Polarity.Negative) { - particleEngine.Color = Color.Blue; + particleEngine.Color = MainShip.BlueColor; } else { @@ -108,6 +127,35 @@ namespace SuperPolarity particleEngine.EmitterLocation = Position; particleEngine.Update(); ConstrainToEdges(); + Shooting = false; + } + + public override void Move(GameTime gameTime) + { + base.Move(gameTime); + + if (Shooting) + { + if (Velocity.X > ActVelocity) + { + Velocity.X = ActVelocity; + } + + if (Velocity.X < -ActVelocity) + { + Velocity.X = -ActVelocity; + } + + if (Velocity.Y > ActVelocity) + { + Velocity.Y = ActVelocity; + } + + if (Velocity.Y < -ActVelocity) + { + Velocity.Y = -ActVelocity; + } + } } public override void Magnetize(Ship ship, float distance, float angle) diff --git a/Super Polarity/Actors/Ship.cs b/Super Polarity/Actors/Ship.cs index 502c22e..b4706ac 100644 --- a/Super Polarity/Actors/Ship.cs +++ b/Super Polarity/Actors/Ship.cs @@ -27,7 +27,7 @@ namespace SuperPolarity RepelRadius = 100; FleeVelocity = 5; - ActVelocity = 3; + ActVelocity = 2; ChargeVelocity = 1.5f; CurrentPolarity = Polarity.Neutral; Magnetizing = false; diff --git a/Super Polarity/Content/Graphics/square.xnb b/Super Polarity/Content/Graphics/square.xnb Binary files differnew file mode 100644 index 0000000..5a88d89 --- /dev/null +++ b/Super Polarity/Content/Graphics/square.xnb diff --git a/Super Polarity/InputController.cs b/Super Polarity/InputController.cs index ad07717..79bd039 100644 --- a/Super Polarity/InputController.cs +++ b/Super Polarity/InputController.cs @@ -88,8 +88,8 @@ namespace SuperPolarity { if (!BlockedKeys.Contains(entry.Key)) { - Dispatch(entry.Key, 1); BlockedKeys.Add(entry.Key); + Dispatch(entry.Key, 1); } keyFired = true; break; @@ -111,8 +111,8 @@ namespace SuperPolarity { if (!BlockedButtons.Contains(entry.Key)) { - Dispatch(entry.Key, 1); BlockedButtons.Add(entry.Key); + Dispatch(entry.Key, 1); } keyFired = true; break; @@ -192,5 +192,11 @@ namespace SuperPolarity method(value); } } + + public static void Unlock(string eventName) + { + BlockedButtons.Remove(eventName); + BlockedKeys.Remove(eventName); + } } } diff --git a/Super Polarity/ParticleEffectFactory.cs b/Super Polarity/ParticleEffectFactory.cs new file mode 100644 index 0000000..0cd9ed3 --- /dev/null +++ b/Super Polarity/ParticleEffectFactory.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace SuperPolarity +{ + static class ParticleEffectFactory + { + static Game Game; + static Random random; + + static ParticleEffectFactory() + { + random = new Random(); + } + + public static ParticleEngine CreatePolarCircle(Vector2 location) + { + 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\\square")); + + ParticleEngine particleEngine = new ParticleEngine(texturesList, location); + + particleEngine.Color = new Color(239, 203, 204); + particleEngine.TTL = 10; + particleEngine.TTLRandomFactor = 5; + particleEngine.ScatterFactor = 40; + particleEngine.ParticleCount = 50; + particleEngine.StretchFactor = 2.8f; + + + return particleEngine; + } + + public static ParticleEngine CreateBullet(Vector2 location) + { + 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\\square")); + + ParticleEngine particleEngine = new ParticleEngine(texturesList, location); + + particleEngine.Color = Color.Gray; + particleEngine.TTL = 5; + particleEngine.TTLRandomFactor = 5; + particleEngine.ScatterFactor = 5; + particleEngine.ParticleCount = 10; + particleEngine.StretchFactor = 1; + + return particleEngine; + } + + internal static void SetGame(Game game) + { + ParticleEffectFactory.Game = game; + } + } +} diff --git a/Super Polarity/ParticleEngine.cs b/Super Polarity/ParticleEngine.cs index 0fced25..51188ae 100644 --- a/Super Polarity/ParticleEngine.cs +++ b/Super Polarity/ParticleEngine.cs @@ -11,7 +11,12 @@ namespace SuperPolarity { private Random random; public Vector2 EmitterLocation { get; set; } - public Color Color; + public Color Color; //TODO: Color list for random colors. + public int TTL; + public int TTLRandomFactor; + public int ScatterFactor; + public int ParticleCount; + public float StretchFactor; private List<Particle> particles; private List<Texture2D> textures; @@ -22,6 +27,9 @@ namespace SuperPolarity this.particles = new List<Particle>(); random = new Random(); Color = Color.Red; + TTL = 20; + TTLRandomFactor = 40; + StretchFactor = 1; } private Particle GenerateNewParticle() @@ -31,12 +39,16 @@ namespace SuperPolarity Vector2 velocity = new Vector2( 1f * (float)(random.NextDouble() * 2 - 1), 1f * (float)(random.NextDouble() * 2 - 1)); + float angle = 0; float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1); Color color = Color; - float size = (float)random.NextDouble(); + float size = (float)random.NextDouble() * StretchFactor; + + position.X += random.Next(-ScatterFactor, ScatterFactor); + position.Y += random.Next(-ScatterFactor, ScatterFactor); - int ttl = 20 + random.Next(40); + int ttl = TTL + random.Next(TTLRandomFactor); return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl); } diff --git a/Super Polarity/Renderer.cs b/Super Polarity/Renderer.cs new file mode 100644 index 0000000..2f5c5aa --- /dev/null +++ b/Super Polarity/Renderer.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework.Graphics; + +namespace SuperPolarity +{ + class Renderer + { + + static List<Actor> Actors; + + static Renderer() + { + Actors = new List<Actor>(); + } + + static public void CheckIn(Actor actor) + { + Actors.Add(actor); + } + + static public void CheckOut(Actor actor) + { + Actors.Remove(actor); + } + + static public void Draw(SpriteBatch spriteBatch) + { + foreach (Actor actor in Actors) + { + actor.Draw(spriteBatch); + } + } + } +} diff --git a/Super Polarity/Super Polarity.csproj b/Super Polarity/Super Polarity.csproj index 4ed5c42..193edde 100644 --- a/Super Polarity/Super Polarity.csproj +++ b/Super Polarity/Super Polarity.csproj @@ -38,12 +38,15 @@ <Compile Include="ActorFactory.cs" /> <Compile Include="ActorManager.cs" /> <Compile Include="Actors\StandardShip.cs" /> + <Compile Include="Actors\Bullet.cs" /> <Compile Include="InputController.cs" /> <Compile Include="Actors\MainShip.cs" /> <Compile Include="Particle.cs" /> + <Compile Include="ParticleEffectFactory.cs" /> <Compile Include="ParticleEngine.cs" /> <Compile Include="Actors\Actor.cs" /> <Compile Include="Actors\Ship.cs" /> + <Compile Include="Renderer.cs" /> <Compile Include="SuperPolarity.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> @@ -129,6 +132,9 @@ <None Include="Content\Graphics\positive-supercruiser.xnb"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> + <None Include="Content\Graphics\square.xnb"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + </None> <None Include="Content\Graphics\star.xnb"> <CopyToOutputDirectory>Always</CopyToOutputDirectory> </None> diff --git a/Super Polarity/SuperPolarity.cs b/Super Polarity/SuperPolarity.cs index b590079..21d1ed4 100644 --- a/Super Polarity/SuperPolarity.cs +++ b/Super Polarity/SuperPolarity.cs @@ -20,6 +20,8 @@ namespace SuperPolarity public static GraphicsDeviceManager graphics; SpriteBatch spriteBatch; + public static int OutlierBounds; + public SuperPolarity() : base() { @@ -27,6 +29,8 @@ namespace SuperPolarity SuperPolarity.graphics.PreferMultiSampling = true; Content.RootDirectory = "Content"; ActorFactory.SetGame(this); + ParticleEffectFactory.SetGame(this); + ActorManager.SetGame(this); } /// <summary> @@ -39,6 +43,8 @@ namespace SuperPolarity { base.Initialize(); + OutlierBounds = 100; + InputController.RegisterEventForButton("changePolarity", Buttons.A); InputController.RegisterEventForKey("changePolarity", Keys.Z); @@ -57,9 +63,9 @@ namespace SuperPolarity 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); + Renderer.CheckIn(ActorFactory.CreateMainShip(playerPosition)); + Renderer.CheckIn(ActorFactory.CreateShip(Ship.Polarity.Positive, new Vector2(200, 200))); + Renderer.CheckIn(ActorFactory.CreateShip(Ship.Polarity.Negative, new Vector2(400, 200))); } /// <summary> @@ -99,7 +105,7 @@ namespace SuperPolarity spriteBatch.Begin(); - ActorManager.Draw(spriteBatch); + Renderer.Draw(spriteBatch); spriteBatch.End(); diff --git a/Super Polarity/bin/WindowsGL/Debug/Super Polarity.exe b/Super Polarity/bin/WindowsGL/Debug/Super Polarity.exe Binary files differindex 9279018..906274f 100644 --- a/Super Polarity/bin/WindowsGL/Debug/Super Polarity.exe +++ b/Super Polarity/bin/WindowsGL/Debug/Super Polarity.exe diff --git a/Super Polarity/bin/WindowsGL/Debug/Super Polarity.pdb b/Super Polarity/bin/WindowsGL/Debug/Super Polarity.pdb Binary files differindex d363b2b..837ce9b 100644 --- a/Super Polarity/bin/WindowsGL/Debug/Super Polarity.pdb +++ b/Super Polarity/bin/WindowsGL/Debug/Super Polarity.pdb diff --git a/Super Polarity/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/Super Polarity/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache Binary files differindex 907615f..c87641f 100644 --- a/Super Polarity/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache +++ b/Super Polarity/obj/x86/Debug/DesignTimeResolveAssemblyReferencesInput.cache diff --git a/Super Polarity/obj/x86/Debug/Super Polarity.csproj.FileListAbsolute.txt b/Super Polarity/obj/x86/Debug/Super Polarity.csproj.FileListAbsolute.txt index 99c9bb4..aa6ffb3 100644 --- a/Super Polarity/obj/x86/Debug/Super Polarity.csproj.FileListAbsolute.txt +++ b/Super Polarity/obj/x86/Debug/Super Polarity.csproj.FileListAbsolute.txt @@ -27,3 +27,4 @@ C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarit 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 +C:\Users\Miau\documents\visual studio 2010\Projects\Super Polarity\Super Polarity\bin\WindowsGL\Debug\Content\Graphics\square.xnb diff --git a/Super Polarity/obj/x86/Debug/Super Polarity.exe b/Super Polarity/obj/x86/Debug/Super Polarity.exe Binary files differindex 9279018..906274f 100644 --- a/Super Polarity/obj/x86/Debug/Super Polarity.exe +++ b/Super Polarity/obj/x86/Debug/Super Polarity.exe diff --git a/Super Polarity/obj/x86/Debug/Super Polarity.pdb b/Super Polarity/obj/x86/Debug/Super Polarity.pdb Binary files differindex d363b2b..837ce9b 100644 --- a/Super Polarity/obj/x86/Debug/Super Polarity.pdb +++ b/Super Polarity/obj/x86/Debug/Super Polarity.pdb |