]> git.r.bdr.sh - rbdr/super-polarity/blame - SuperPolarity/ActorManager.cs
Moves some files around!
[rbdr/super-polarity] / SuperPolarity / ActorManager.cs
CommitLineData
f8aec187
BB
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Graphics;
7
8namespace SuperPolarity
9{
10 static class ActorManager
11 {
38c7d3f9 12
74c15570 13 static SuperPolarity Game;
38c7d3f9 14 static int OutlierBounds;
74c15570 15 static IList<Actor> Actors;
f8aec187
BB
16
17 static ActorManager()
18 {
38c7d3f9 19 OutlierBounds = 100;
f8aec187
BB
20 Actors = new List<Actor>();
21 }
22
23 static public void CheckIn(Actor actor)
24 {
25 Actors.Add(actor);
26 }
27
28 static public void CheckOut(Actor actor)
29 {
b587e9d8 30 actor.CleanUp();
f8aec187 31 Actors.Remove(actor);
b587e9d8 32 actor = null;
f8aec187
BB
33 }
34
35 static public void Update(GameTime gameTime)
36 {
2af83e98 37 CheckActors();
38c7d3f9 38 CheckOutliers();
f8aec187
BB
39 foreach (Actor actor in Actors)
40 {
41 actor.Update(gameTime);
42 }
43 }
44
45 static public void Draw(SpriteBatch spriteBatch)
46 {
47 foreach (Actor actor in Actors)
48 {
49 actor.Draw(spriteBatch);
50 }
51 }
2af83e98
BB
52
53 static void CheckActors()
54 {
74c15570 55 for (var i = Actors.Count - 1; i >= 0; i--)
2af83e98 56 {
74c15570
BB
57 if (i >= Actors.Count) {
58 i = Actors.Count - 1;
59 }
b587e9d8
BB
60
61 if (Actors.Count == 0)
62 {
63 return;
64 }
65
74c15570
BB
66 Actor actor = Actors[i];
67 for (var j = i - 1; j >= 0; j--)
2af83e98 68 {
74c15570
BB
69 Actor other = Actors[j];
70
8bdfcb11
BB
71 if (actor.Collides && other.Collides) {
72 CheckCollision(actor, other);
73 }
2af83e98
BB
74
75 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
76 {
77 CheckMagnetism((Ship)actor, (Ship)other);
78 }
79 }
80 }
81 }
82
83 static void CheckCollision(Actor actor, Actor other)
84 {
74c15570
BB
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)
88 {
89 actor.Collide(other, collision);
90 other.Collide(actor, inverseCollision);
91 }
2af83e98
BB
92
93 }
94
95 static void CheckMagnetism(Ship actor, Ship other)
96 {
97 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
98 {
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);
38c7d3f9 103 var otherAngle = (float)Math.Atan2(-dy, -dx);
2af83e98
BB
104
105 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
106 {
107 actor.Magnetize(other, (float)linearDistance, angle);
38c7d3f9 108 other.Magnetize(actor, (float)linearDistance, otherAngle);
2af83e98
BB
109 }
110 }
111 }
38c7d3f9
BB
112
113 static void CheckOutliers()
114 {
115 for (var i = Actors.Count; i > 0; i--)
116 {
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)
121 {
122 CheckOut(actor);
b587e9d8
BB
123 if (actor.Parent != null)
124 {
125 actor.Parent.Children.Remove(actor);
126 }
38c7d3f9
BB
127 }
128 }
129 }
130
b587e9d8
BB
131 static public void Empty()
132 {
133 foreach (Actor actor in Actors) {
134 actor.CleanUp();
135 }
136 Actors.Clear();
137 }
138
74c15570 139 internal static void SetGame(SuperPolarity game)
38c7d3f9
BB
140 {
141 Game = game;
142 }
b587e9d8
BB
143
144 public static void Bomb()
145 {
146 for (var i = Actors.Count - 1; i >= 0; i--)
147 {
148 var actor = Actors[i];
149 if (actor.GetType() == typeof(StandardShip))
150 {
151 CheckOut(actor);
152 Renderer.CheckOut(actor);
153 }
154 }
155 }
156
157 public static int CountBaddies()
158 {
159 return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();
160 }
f8aec187
BB
161 }
162}