]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/ActorManager.cs
1b4ef96c6e1c506a690a0b6c9899e782879d0287
[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 Game Game;
14 static int OutlierBounds;
15 static List<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 var i = 0;
54 foreach (Actor actor in Actors)
55 {
56 i++;
57 foreach (Actor other in Actors.Skip(i))
58 {
59 CheckCollision(actor, other);
60
61 if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
62 {
63 CheckMagnetism((Ship)actor, (Ship)other);
64 }
65 }
66 }
67 }
68
69 static void CheckCollision(Actor actor, Actor other)
70 {
71
72 }
73
74 static void CheckMagnetism(Ship actor, Ship other)
75 {
76 if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
77 {
78 var dy = other.Position.Y - actor.Position.Y;
79 var dx = other.Position.X - actor.Position.X;
80 var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
81 var angle = (float) Math.Atan2(dy, dx);
82 var otherAngle = (float)Math.Atan2(-dy, -dx);
83
84 if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
85 {
86 actor.Magnetize(other, (float)linearDistance, angle);
87 other.Magnetize(actor, (float)linearDistance, otherAngle);
88 }
89 }
90 }
91
92 static void CheckOutliers()
93 {
94 for (var i = Actors.Count; i > 0; i--)
95 {
96 var actor = Actors[i-1];
97 if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
98 actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
99 actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
100 {
101 CheckOut(actor);
102 }
103 }
104 }
105
106 internal static void SetGame(Game game)
107 {
108 Game = game;
109 }
110 }
111 }