aboutsummaryrefslogtreecommitdiff
path: root/SuperPolarityMac/ActorManager.cs
blob: f5587b9c9f772872a3915720451706b6fe3efcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SuperPolarity
{
    static class ActorManager
    {

        static SuperPolarity Game;
        static int OutlierBounds;
        static IList<Actor> Actors;

        static ActorManager()
        {
            OutlierBounds = 100;
            Actors = new List<Actor>();
        }

        static public void CheckIn(Actor actor)
        {
            Actors.Add(actor);
        }

        static public void CheckOut(Actor actor)
        {
            actor.CleanUp();
            Actors.Remove(actor);
            actor = null;
        }

        static public void Update(GameTime gameTime) 
        {
            CheckActors();
            CheckOutliers();
            foreach (Actor actor in Actors)
            {
                actor.Update(gameTime);
            }
        }

        static public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Actor actor in Actors)
            {
                actor.Draw(spriteBatch);
            }
        }

        static void CheckActors()
        {
            for (var i = Actors.Count - 1; i >= 0; i--)
            {
                if (i >= Actors.Count) {
                    i = Actors.Count - 1;
                }

                if (Actors.Count == 0)
                {
                    return;
                }

                Actor actor = Actors[i];
                for (var j = i - 1; j >= 0; j--)
                {
                    Actor other = Actors[j];

                    CheckCollision(actor, other);

                    if (actor.GetType().IsSubclassOf(typeof(Ship)) && other.GetType().IsSubclassOf(typeof(Ship)))
                    {
                        CheckMagnetism((Ship)actor, (Ship)other);
                    }
                }
            }
        }

        static void CheckCollision(Actor actor, Actor other)
        {
            var collision = Rectangle.Intersect(actor.Box, other.Box);
            var inverseCollision = Rectangle.Intersect(other.Box, actor.Box);
            if (!collision.IsEmpty && !actor.Dying && !other.Dying)
            {
                actor.Collide(other, collision);
                other.Collide(actor, inverseCollision);
            }

        }

        static void CheckMagnetism(Ship actor, Ship other)
        {
            if (actor.CurrentPolarity != Ship.Polarity.Neutral && other.CurrentPolarity != Ship.Polarity.Neutral)
            {
                var dy = other.Position.Y - actor.Position.Y;
                var dx = other.Position.X - actor.Position.X;
                var linearDistance = Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
                var angle = (float) Math.Atan2(dy, dx);
                var otherAngle = (float)Math.Atan2(-dy, -dx);

                if (linearDistance < actor.MagneticRadius || linearDistance < other.MagneticRadius)
                {
                    actor.Magnetize(other, (float)linearDistance, angle);
                    other.Magnetize(actor, (float)linearDistance, otherAngle);
                }
            }
        }

        static void CheckOutliers()
        {
            for (var i = Actors.Count; i > 0; i--)
            {
                var actor = Actors[i-1];
                if (actor.Position.X < -OutlierBounds || actor.Position.Y < -OutlierBounds ||
                    actor.Position.X > Game.GraphicsDevice.Viewport.Width + OutlierBounds ||
                    actor.Position.Y > Game.GraphicsDevice.Viewport.Height + OutlierBounds)
                {
                    CheckOut(actor);
                    if (actor.Parent != null)
                    {
                        actor.Parent.Children.Remove(actor);
                    }
                }
            }
        }

        static public void Empty()
        {
            foreach (Actor actor in Actors) {
                actor.CleanUp();
            }
            Actors.Clear();
        }

        internal static void SetGame(SuperPolarity game)
        {
            Game = game;
        }

        public static void Bomb()
        {
            for (var i = Actors.Count - 1; i >= 0; i--)
            {
                var actor = Actors[i];
                if (actor.GetType() == typeof(StandardShip))
                {
                    CheckOut(actor);
                    Renderer.CheckOut(actor);
                }
            }
        }

        public static int CountBaddies()
        {
            return Actors.Where(a => a.GetType() == typeof(StandardShip)).Count();
        }
    }
}