]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/ActorManager.cs
8cd265a432a06d10867bd6fdd3e86836a638eb94
[rbdr/super-polarity] / Super Polarity / 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 Actors.Remove(actor);
31 }
32
33 static public void Update(GameTime gameTime)
34 {
35 CheckActors();
36 CheckOutliers();
37 foreach (Actor actor in Actors)
38 {
39 actor.Update(gameTime);
40 }
41 }
42
43 static public void Draw(SpriteBatch spriteBatch)
44 {
45 foreach (Actor actor in Actors)
46 {
47 actor.Draw(spriteBatch);
48 }
49 }
50
51 static void CheckActors()
52 {
53 for (var i = Actors.Count - 1; i >= 0; i--)
54 {
55 if (i >= Actors.Count) {
56 i = Actors.Count - 1;
57 }
58 Actor actor = Actors[i];
59 for (var j = i - 1; j >= 0; j--)
60 {
61 Actor other = Actors[j];
62
63 CheckCollision(actor, other);
64
65 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
66 {
67 CheckMagnetism((Ship)actor, (Ship)other);
68 }
69 }
70 }
71 }
72
73 static void CheckCollision(Actor actor, Actor other)
74 {
75 var collision = Rectangle.Intersect(actor.Box, other.Box);
76 var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
77 if (!collision.IsEmpty && !actor.Dying && !other.Dying)
78 {
79 actor.Collide(other, collision);
80 other.Collide(actor, inverseCollision);
81 }
82
83 }
84
85 static void CheckMagnetism(Ship actor, Ship other)
86 {
87 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
88 {
89 var dy = other.Position.Y - actor.Position.Y;
90 var dx = other.Position.X - actor.Position.X;
91 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
92 var angle = (float) Math.Atan2(dy, dx);
93 var otherAngle = (float)Math.Atan2(-dy, -dx);
94
95 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
96 {
97 actor.Magnetize(other, (float)linearDistance, angle);
98 other.Magnetize(actor, (float)linearDistance, otherAngle);
99 }
100 }
101 }
102
103 static void CheckOutliers()
104 {
105 for (var i = Actors.Count; i > 0; i--)
106 {
107 var actor = Actors[i-1];
108 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
109 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
110 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
111 {
112 CheckOut(actor);
113 }
114 }
115 }
116
117 internal static void SetGame(SuperPolarity game)
118 {
119 Game = game;
120 }
121 }
122 }