]> git.r.bdr.sh - rbdr/super-polarity/blame - SuperPolarity/ActorManager.cs
Update README
[rbdr/super-polarity] / SuperPolarity / ActorManager.cs
CommitLineData
9ad526c0 1using System;
f8aec187
BB
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);
9ad526c0
BB
50
51 if (actor.GetType ().IsAssignableFrom (typeof(MainShip))) {
52 var mainActor = (MainShip)actor;
53 mainActor.ResetEnemyModification ();
54 }
f8aec187
BB
55 }
56 }
2af83e98
BB
57
58 static void CheckActors()
59 {
74c15570 60 for (var i = Actors.Count - 1; i >= 0; i--)
2af83e98 61 {
74c15570
BB
62 if (i >= Actors.Count) {
63 i = Actors.Count - 1;
64 }
b587e9d8
BB
65
66 if (Actors.Count == 0)
67 {
68 return;
69 }
70
74c15570
BB
71 Actor actor = Actors[i];
72 for (var j = i - 1; j >= 0; j--)
2af83e98 73 {
74c15570
BB
74 Actor other = Actors[j];
75
8bdfcb11
BB
76 if (actor.Collides && other.Collides) {
77 CheckCollision(actor, other);
78 }
2af83e98
BB
79
80 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
81 {
82 CheckMagnetism((Ship)actor, (Ship)other);
83 }
84 }
85 }
86 }
87
88 static void CheckCollision(Actor actor, Actor other)
89 {
74c15570
BB
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)
93 {
94 actor.Collide(other, collision);
95 other.Collide(actor, inverseCollision);
96 }
2af83e98
BB
97
98 }
99
100 static void CheckMagnetism(Ship actor, Ship other)
101 {
102 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
103 {
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);
38c7d3f9 108 var otherAngle = (float)Math.Atan2(-dy, -dx);
2af83e98 109
9ad526c0
BB
110 if (actor.GetType ().IsAssignableFrom (typeof(MainShip))) {
111 var mainActor = (MainShip)actor;
112 mainActor.UpdateEnemyModification (other);
113 }
114
115 if (other.GetType ().IsAssignableFrom (typeof(MainShip))) {
116 var mainOther = (MainShip)other;
117 mainOther.UpdateEnemyModification (actor);
118 }
119
2af83e98
BB
120 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
121 {
122 actor.Magnetize(other, (float)linearDistance, angle);
38c7d3f9 123 other.Magnetize(actor, (float)linearDistance, otherAngle);
2af83e98
BB
124 }
125 }
126 }
38c7d3f9
BB
127
128 static void CheckOutliers()
129 {
130 for (var i = Actors.Count; i > 0; i--)
131 {
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)
136 {
137 CheckOut(actor);
b587e9d8
BB
138 if (actor.Parent != null)
139 {
140 actor.Parent.Children.Remove(actor);
141 }
38c7d3f9
BB
142 }
143 }
144 }
145
b587e9d8
BB
146 static public void Empty()
147 {
148 foreach (Actor actor in Actors) {
149 actor.CleanUp();
150 }
151 Actors.Clear();
152 }
153
74c15570 154 internal static void SetGame(SuperPolarity game)
38c7d3f9
BB
155 {
156 Game = game;
157 }
b587e9d8
BB
158
159 public static void Bomb()
160 {
161 for (var i = Actors.Count - 1; i >= 0; i--)
162 {
163 var actor = Actors[i];
164 if (actor.GetType() == typeof(StandardShip))
165 {
166 CheckOut(actor);
167 Renderer.CheckOut(actor);
168 }
169 }
170 }
171
172 public static int CountBaddies()
173 {
174 return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();
175 }
f8aec187
BB
176 }
177}