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);
53 static void CheckActors()
55 for (var i = Actors.Count - 1; i >= 0; i--)
57 if (i >= Actors.Count) {
61 if (Actors.Count == 0)
66 Actor actor = Actors[i];
67 for (var j = i - 1; j >= 0; j--)
69 Actor other = Actors[j];
71 if (actor.Collides && other.Collides) {
72 CheckCollision(actor, other);
75 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
77 CheckMagnetism((Ship)actor, (Ship)other);
83 static void CheckCollision(Actor actor, Actor other)
85 var collision = Rectangle.Intersect(actor.Box, other.Box);
86 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
87 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
89 actor.Collide(other, collision);
90 other.Collide(actor, inverseCollision);
95 static void CheckMagnetism(Ship actor, Ship other)
97 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
99 var dy = other.Position.Y - actor.Position.Y;
100 var dx = other.Position.X - actor.Position.X;
101 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
102 var angle = (float) Math.Atan2(dy, dx);
103 var otherAngle = (float)Math.Atan2(-dy, -dx);
105 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
107 actor.Magnetize(other, (float)linearDistance, angle);
108 other.Magnetize(actor, (float)linearDistance, otherAngle);
113 static void CheckOutliers()
115 for (var i = Actors.Count; i > 0; i--)
117 var actor = Actors[i-1];
118 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
119 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
120 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
123 if (actor.Parent != null)
125 actor.Parent.Children.Remove(actor);
131 static public void Empty()
133 foreach (Actor actor in Actors) {
139 internal static void SetGame(SuperPolarity game)
144 public static void Bomb()
146 for (var i = Actors.Count - 1; i >= 0; i--)
148 var actor = Actors[i];
149 if (actor.GetType() == typeof(StandardShip))
152 Renderer.CheckOut(actor);
157 public static int CountBaddies()
159 return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();