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