2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
8 namespace SuperPolarity
10 static class ActorManager
13 static SuperPolarity Game;
14 static int OutlierBounds;
15 static IList<Actor> Actors;
20 Actors = new List<Actor>();
23 static public void CheckIn(Actor actor)
28 static public void CheckOut(Actor actor)
35 static public void Update(GameTime gameTime)
39 foreach (Actor actor in Actors)
41 actor.Update(gameTime);
45 static public void Draw(SpriteBatch spriteBatch)
47 foreach (Actor actor in Actors)
49 actor.Draw(spriteBatch);
51 if (actor.GetType ().IsAssignableFrom (typeof(MainShip))) {
52 var mainActor = (MainShip)actor;
53 mainActor.ResetEnemyModification ();
58 static void CheckActors()
60 for (var i = Actors.Count - 1; i >= 0; i--)
62 if (i >= Actors.Count) {
66 if (Actors.Count == 0)
71 Actor actor = Actors[i];
72 for (var j = i - 1; j >= 0; j--)
74 Actor other = Actors[j];
76 if (actor.Collides && other.Collides) {
77 CheckCollision(actor, other);
80 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
82 CheckMagnetism((Ship)actor, (Ship)other);
88 static void CheckCollision(Actor actor, Actor other)
90 var collision = Rectangle.Intersect(actor.Box, other.Box);
91 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
92 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
94 actor.Collide(other, collision);
95 other.Collide(actor, inverseCollision);
100 static void CheckMagnetism(Ship actor, Ship other)
102 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
104 var dy = other.Position.Y - actor.Position.Y;
105 var dx = other.Position.X - actor.Position.X;
106 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
107 var angle = (float) Math.Atan2(dy, dx);
108 var otherAngle = (float)Math.Atan2(-dy, -dx);
110 if (actor.GetType ().IsAssignableFrom (typeof(MainShip))) {
111 var mainActor = (MainShip)actor;
112 mainActor.UpdateEnemyModification (other);
115 if (other.GetType ().IsAssignableFrom (typeof(MainShip))) {
116 var mainOther = (MainShip)other;
117 mainOther.UpdateEnemyModification (actor);
120 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
122 actor.Magnetize(other, (float)linearDistance, angle);
123 other.Magnetize(actor, (float)linearDistance, otherAngle);
128 static void CheckOutliers()
130 for (var i = Actors.Count; i > 0; i--)
132 var actor = Actors[i-1];
133 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
134 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
135 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
138 if (actor.Parent != null)
140 actor.Parent.Children.Remove(actor);
146 static public void Empty()
148 foreach (Actor actor in Actors) {
154 internal static void SetGame(SuperPolarity game)
159 public static void Bomb()
161 for (var i = Actors.Count - 1; i >= 0; i--)
163 var actor = Actors[i];
164 if (actor.GetType() == typeof(StandardShip))
167 Renderer.CheckOut(actor);
172 public static int CountBaddies()
174 return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();