aboutsummaryrefslogtreecommitdiff
path: root/SuperPolarity/Actors/Actor.cs
blob: b25de42acb309b6e727fc77e89105cde0e5104af (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SuperPolarity
{
    public class Actor
    {
        protected SuperPolarity game;

        public List<Actor> Children;

        // In-Game Properties
        public bool Active;
		public Rectangle Box;
		public Vector4 BoxDimensions;
		protected Texture2D BoxTexture;
		protected Color Color;

        // Physical Properties
        public Vector2 Position;
		public bool Collides;
        protected Vector2 Velocity;
        protected Vector2 Acceleration;
        public float Angle;

        // Constraints / Behavior
        public float MaxVelocity;
		protected float AccelerationRate;
        public bool Dying;

        public Actor Parent;

        public virtual int Width
        {
            get { return 0; }
        }

        public virtual int Height
        {
            get { return 0; }
        }

        public Actor(SuperPolarity newGame)
        {
            game = newGame;
			BoxDimensions.X = 20;
			BoxDimensions.Y = 20;
			BoxDimensions.W = 15;
			BoxDimensions.Z = 15;
        }

        public virtual void Initialize(Vector2 position)
        {
            Position = position;
            Active = true;

			Collides = false;

            Children = new List<Actor>();

            Velocity = new Vector2(0, 0);
            Acceleration = new Vector2(0, 0);

            MaxVelocity = 5;
            AccelerationRate = 10;

            Dying = false;

			InitBox();
			BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
			BoxTexture.SetData(new Color[] { Color.White });

            Color = Color.White;
        }

		protected void InitBox()
		{
			Box = new Rectangle((int)(Position.X - BoxDimensions.X), (int)(Position.Y - BoxDimensions.X), (int)(BoxDimensions.X + BoxDimensions.X + BoxDimensions.W), (int)(BoxDimensions.Y + BoxDimensions.Y + BoxDimensions.Z));
		}

        public void AutoDeccelerate(GameTime gameTime)
        {
            if (Acceleration.X == 0 && Velocity.X > 0)
            {
                if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
                {
                    Velocity.X = 0;
                    Acceleration.X = 0;
                }
                else
                {
                    Acceleration.X = -AccelerationRate;
                }
            }

            if (Acceleration.X == 0 && Velocity.X < 0)
            {
                if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
                {
                    Velocity.X = 0;
                    Acceleration.X = 0;
                }
                else
                {
                    Acceleration.X = AccelerationRate;
                }
            }

            if (Acceleration.Y == 0 && Velocity.Y > 0)
            {
                if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
                {
                    Velocity.Y = 0;
                    Acceleration.Y = 0;
                }
                else
                {
                    Acceleration.Y = -AccelerationRate;
                }
            }

            if (Acceleration.Y == 0 && Velocity.Y < 0)
            {
                if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
                {
                    Velocity.Y = 0;
                    Acceleration.Y = 0;
                }
                else
                {
                    Acceleration.Y = AccelerationRate;
                }
            }
        }

        public virtual void Update(GameTime gameTime)
        {
            Move(gameTime);
            ChangeAngle();
            CheckOutliers();
			UpdateBox ();
        }

		protected virtual void UpdateBox()
		{
			Box.X = (int)(Position.X - BoxDimensions.X);
			Box.Y = (int)(Position.Y - BoxDimensions.Y);
		}

        public virtual void Move(GameTime gameTime)
        {
            AutoDeccelerate(gameTime);

            Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
            Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (Velocity.X > MaxVelocity)
            {
                Velocity.X = MaxVelocity;
            }

            if (Velocity.X < -MaxVelocity)
            {
                Velocity.X = -MaxVelocity;
            }

            if (Velocity.Y > MaxVelocity)
            {
                Velocity.Y = MaxVelocity;
            }

            if (Velocity.Y < -MaxVelocity)
            {
                Velocity.Y = -MaxVelocity;
            }

            Position.X = Position.X + Velocity.X;
            Position.Y = Position.Y + Velocity.Y;
        }

        public void ChangeAngle()
        {
            if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1)
            {
                return;
            }
            Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            Actor child = null;
            
            //  TODO: Check what's up with the null children.
            if (Children == null)
            {
                return;
            }
            for (var i = Children.Count - 1; i >= 0; i--)
            {
                child = Children[i];
                child.Draw(spriteBatch);
            }

			//spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
        }

        void CheckOutliers()
        {
            for (var i = Children.Count; i > 0; i--)
            {
                var actor = Children[i - 1];
                if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
                    actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
                    actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
                {
                    Children.Remove(actor);
                }
            }
        }

        public virtual void Collide(Actor other, Rectangle collision)
        {
        }

        protected virtual void Die()
        {
            Dying = true;
        }

        public virtual void CleanUp()
        {
			BoxTexture = null;
			Children = null;
        }
    }
}