]> git.r.bdr.sh - rbdr/super-polarity/blame - SuperPolarity/Actors/Actor.cs
Update project to work on VS Code mac
[rbdr/super-polarity] / SuperPolarity / Actors / Actor.cs
CommitLineData
9ad526c0 1using System;
f8aec187
BB
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using Microsoft.Xna.Framework;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.Graphics;
8
9namespace SuperPolarity
10{
8bdfcb11 11 public class Actor
f8aec187 12 {
74c15570 13 protected SuperPolarity game;
2af83e98 14
38c7d3f9
BB
15 public List<Actor> Children;
16
05e4b948 17 // In-Game Properties
f8aec187 18 public bool Active;
05e4b948
BB
19 public Rectangle Box;
20 public Vector4 BoxDimensions;
21 protected Texture2D BoxTexture;
22 protected Color Color;
f8aec187
BB
23
24 // Physical Properties
2af83e98 25 public Vector2 Position;
8bdfcb11 26 public bool Collides;
f8aec187
BB
27 protected Vector2 Velocity;
28 protected Vector2 Acceleration;
38c7d3f9 29 public float Angle;
f8aec187
BB
30
31 // Constraints / Behavior
097781e6 32 public float MaxVelocity;
05e4b948 33 protected float AccelerationRate;
74c15570 34 public bool Dying;
74c15570
BB
35
36 public Actor Parent;
f8aec187 37
05e4b948 38 public virtual int Width
f8aec187 39 {
05e4b948 40 get { return 0; }
f8aec187
BB
41 }
42
05e4b948 43 public virtual int Height
f8aec187 44 {
05e4b948 45 get { return 0; }
f8aec187
BB
46 }
47
74c15570 48 public Actor(SuperPolarity newGame)
2af83e98
BB
49 {
50 game = newGame;
05e4b948
BB
51 BoxDimensions.X = 20;
52 BoxDimensions.Y = 20;
53 BoxDimensions.W = 15;
54 BoxDimensions.Z = 15;
2af83e98
BB
55 }
56
05e4b948 57 public virtual void Initialize(Vector2 position)
f8aec187 58 {
f8aec187
BB
59 Position = position;
60 Active = true;
61
05e4b948 62 Collides = false;
8bdfcb11 63
38c7d3f9
BB
64 Children = new List<Actor>();
65
f8aec187
BB
66 Velocity = new Vector2(0, 0);
67 Acceleration = new Vector2(0, 0);
68
69 MaxVelocity = 5;
70 AccelerationRate = 10;
74c15570 71
74c15570 72 Dying = false;
74c15570 73
05e4b948
BB
74 InitBox();
75 BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
76 BoxTexture.SetData(new Color[] { Color.White });
74c15570
BB
77
78 Color = Color.White;
79 }
80
05e4b948
BB
81 protected void InitBox()
82 {
83 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));
84 }
f8aec187
BB
85
86 public void AutoDeccelerate(GameTime gameTime)
87 {
88 if (Acceleration.X == 0 && Velocity.X > 0)
89 {
90 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
91 {
92 Velocity.X = 0;
93 Acceleration.X = 0;
94 }
95 else
96 {
97 Acceleration.X = -AccelerationRate;
98 }
99 }
100
101 if (Acceleration.X == 0 && Velocity.X < 0)
102 {
103 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
104 {
105 Velocity.X = 0;
106 Acceleration.X = 0;
107 }
108 else
109 {
110 Acceleration.X = AccelerationRate;
111 }
112 }
113
114 if (Acceleration.Y == 0 && Velocity.Y > 0)
115 {
116 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
117 {
118 Velocity.Y = 0;
119 Acceleration.Y = 0;
120 }
121 else
122 {
123 Acceleration.Y = -AccelerationRate;
124 }
125 }
126
127 if (Acceleration.Y == 0 && Velocity.Y < 0)
128 {
129 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
130 {
131 Velocity.Y = 0;
132 Acceleration.Y = 0;
133 }
134 else
135 {
136 Acceleration.Y = AccelerationRate;
137 }
138 }
139 }
140
141 public virtual void Update(GameTime gameTime)
142 {
143 Move(gameTime);
144 ChangeAngle();
38c7d3f9 145 CheckOutliers();
05e4b948 146 UpdateBox ();
74c15570
BB
147 }
148
05e4b948
BB
149 protected virtual void UpdateBox()
150 {
151 Box.X = (int)(Position.X - BoxDimensions.X);
152 Box.Y = (int)(Position.Y - BoxDimensions.Y);
153 }
f8aec187 154
38c7d3f9 155 public virtual void Move(GameTime gameTime)
f8aec187
BB
156 {
157 AutoDeccelerate(gameTime);
158
159 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
160 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
161
162 if (Velocity.X > MaxVelocity)
163 {
164 Velocity.X = MaxVelocity;
165 }
166
167 if (Velocity.X < -MaxVelocity)
168 {
169 Velocity.X = -MaxVelocity;
170 }
171
172 if (Velocity.Y > MaxVelocity)
173 {
174 Velocity.Y = MaxVelocity;
175 }
176
177 if (Velocity.Y < -MaxVelocity)
178 {
179 Velocity.Y = -MaxVelocity;
180 }
181
182 Position.X = Position.X + Velocity.X;
183 Position.Y = Position.Y + Velocity.Y;
184 }
185
186 public void ChangeAngle()
187 {
097781e6
BB
188 if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1)
189 {
190 return;
191 }
f8aec187
BB
192 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
193 }
194
195 public virtual void Draw(SpriteBatch spriteBatch)
196 {
b587e9d8
BB
197 Actor child = null;
198
199 // TODO: Check what's up with the null children.
200 if (Children == null)
38c7d3f9 201 {
b587e9d8
BB
202 return;
203 }
204 for (var i = Children.Count - 1; i >= 0; i--)
205 {
206 child = Children[i];
38c7d3f9
BB
207 child.Draw(spriteBatch);
208 }
209
05e4b948 210 //spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
f8aec187 211 }
38c7d3f9
BB
212
213 void CheckOutliers()
214 {
215 for (var i = Children.Count; i > 0; i--)
216 {
217 var actor = Children[i - 1];
218 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
219 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
220 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
221 {
222 Children.Remove(actor);
223 }
224 }
225 }
74c15570
BB
226
227 public virtual void Collide(Actor other, Rectangle collision)
228 {
229 }
230
74c15570
BB
231 protected virtual void Die()
232 {
233 Dying = true;
234 }
b587e9d8
BB
235
236 public virtual void CleanUp()
237 {
05e4b948
BB
238 BoxTexture = null;
239 Children = null;
b587e9d8 240 }
f8aec187
BB
241 }
242}