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