]> git.r.bdr.sh - rbdr/super-polarity/blob - SuperPolarity/ActorManager.cs
f5587b9c9f772872a3915720451706b6fe3efcd8
[rbdr/super-polarity] / SuperPolarity / ActorManager.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7
8 namespace SuperPolarity
9 {
10 static class ActorManager
11 {
12
13 static SuperPolarity Game;
14 static int OutlierBounds;
15 static IList<Actor> Actors;
16
17 static ActorManager()
18 {
19 OutlierBounds = 100;
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 {
30 actor.CleanUp();
31 Actors.Remove(actor);
32 actor = null;
33 }
34
35 static public void Update(GameTime gameTime)
36 {
37 CheckActors();
38 CheckOutliers();
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 }
52
53 static void CheckActors()
54 {
55 for (var i = Actors.Count - 1; i >= 0; i--)
56 {
57 if (i >= Actors.Count) {
58 i = Actors.Count - 1;
59 }
60
61 if (Actors.Count == 0)
62 {
63 return;
64 }
65
66 Actor actor = Actors[i];
67 for (var j = i - 1; j >= 0; j--)
68 {
69 Actor other = Actors[j];
70
71 CheckCollision(actor, other);
72
73 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
74 {
75 CheckMagnetism((Ship)actor, (Ship)other);
76 }
77 }
78 }
79 }
80
81 static void CheckCollision(Actor actor, Actor other)
82 {
83 var collision = Rectangle.Intersect(actor.Box, other.Box);
84 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
85 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
86 {
87 actor.Collide(other, collision);
88 other.Collide(actor, inverseCollision);
89 }
90
91 }
92
93 static void CheckMagnetism(Ship actor, Ship other)
94 {
95 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
96 {
97 var dy = other.Position.Y - actor.Position.Y;
98 var dx = other.Position.X - actor.Position.X;
99 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
100 var angle = (float) Math.Atan2(dy, dx);
101 var otherAngle = (float)Math.Atan2(-dy, -dx);
102
103 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
104 {
105 actor.Magnetize(other, (float)linearDistance, angle);
106 other.Magnetize(actor, (float)linearDistance, otherAngle);
107 }
108 }
109 }
110
111 static void CheckOutliers()
112 {
113 for (var i = Actors.Count; i > 0; i--)
114 {
115 var actor = Actors[i-1];
116 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
117 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
118 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
119 {
120 CheckOut(actor);
121 if (actor.Parent != null)
122 {
123 actor.Parent.Children.Remove(actor);
124 }
125 }
126 }
127 }
128
129 static public void Empty()
130 {
131 foreach (Actor actor in Actors) {
132 actor.CleanUp();
133 }
134 Actors.Clear();
135 }
136
137 internal static void SetGame(SuperPolarity game)
138 {
139 Game = game;
140 }
141
142 public static void Bomb()
143 {
144 for (var i = Actors.Count - 1; i >= 0; i--)
145 {
146 var actor = Actors[i];
147 if (actor.GetType() == typeof(StandardShip))
148 {
149 CheckOut(actor);
150 Renderer.CheckOut(actor);
151 }
152 }
153 }
154
155 public static int CountBaddies()
156 {
157 return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();
158 }
159 }
160 }