]> git.r.bdr.sh - rbdr/super-polarity/blame_incremental - SuperPolarity/Actors/Actor.cs
Moves some files around!
[rbdr/super-polarity] / SuperPolarity / Actors / Actor.cs
... / ...
CommitLineData
1using System;
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{
11 public class Actor
12 {
13 protected SuperPolarity game;
14
15 public List<Actor> Children;
16
17 // In-Game Properties
18 public bool Active;
19 public Rectangle Box;
20 public Vector4 BoxDimensions;
21 protected Texture2D BoxTexture;
22 protected Color Color;
23
24 // Physical Properties
25 public Vector2 Position;
26 public bool Collides;
27 protected Vector2 Velocity;
28 protected Vector2 Acceleration;
29 public float Angle;
30
31 // Constraints / Behavior
32 public float MaxVelocity;
33 protected float AccelerationRate;
34 public bool Dying;
35
36 public Actor Parent;
37
38 public virtual int Width
39 {
40 get { return 0; }
41 }
42
43 public virtual int Height
44 {
45 get { return 0; }
46 }
47
48 public Actor(SuperPolarity newGame)
49 {
50 game = newGame;
51 BoxDimensions.X = 20;
52 BoxDimensions.Y = 20;
53 BoxDimensions.W = 15;
54 BoxDimensions.Z = 15;
55 }
56
57 public virtual void Initialize(Vector2 position)
58 {
59 Position = position;
60 Active = true;
61
62 Collides = false;
63
64 Children = new List<Actor>();
65
66 Velocity = new Vector2(0, 0);
67 Acceleration = new Vector2(0, 0);
68
69 MaxVelocity = 5;
70 AccelerationRate = 10;
71
72 Dying = false;
73
74 InitBox();
75 BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
76 BoxTexture.SetData(new Color[] { Color.White });
77
78 Color = Color.White;
79 }
80
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 }
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();
145 CheckOutliers();
146 UpdateBox ();
147 }
148
149 protected virtual void UpdateBox()
150 {
151 Box.X = (int)(Position.X - BoxDimensions.X);
152 Box.Y = (int)(Position.Y - BoxDimensions.Y);
153 }
154
155 public virtual void Move(GameTime gameTime)
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 {
188 if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1)
189 {
190 return;
191 }
192 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
193 }
194
195 public virtual void Draw(SpriteBatch spriteBatch)
196 {
197 Actor child = null;
198
199 // TODO: Check what's up with the null children.
200 if (Children == null)
201 {
202 return;
203 }
204 for (var i = Children.Count - 1; i >= 0; i--)
205 {
206 child = Children[i];
207 child.Draw(spriteBatch);
208 }
209
210 //spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
211 }
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 }
226
227 public virtual void Collide(Actor other, Rectangle collision)
228 {
229 }
230
231 protected virtual void Die()
232 {
233 Dying = true;
234 }
235
236 public virtual void CleanUp()
237 {
238 BoxTexture = null;
239 Children = null;
240 }
241 }
242}