2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
9 namespace SuperPolarity
13 protected SuperPolarity game;
15 public List<Actor> Children;
18 protected Texture2D Texture;
19 protected Vector2 Origin;
22 public Vector4 BoxDimensions;
23 protected Texture2D BoxTexture;
25 // Physical Properties
26 public Vector2 Position;
28 protected Vector2 Velocity;
29 protected Vector2 Acceleration;
32 // Constraints / Behavior
33 public float MaxVelocity;
34 protected float AccelerationRate;
36 protected bool Immortal;
39 protected Color Color;
45 get { return Texture.Width; }
50 get { return Texture.Height; }
53 public Actor(SuperPolarity newGame)
62 public virtual void Initialize(Texture2D texture, Vector2 position)
70 Children = new List<Actor>();
72 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
73 Velocity = new Vector2(0, 0);
74 Acceleration = new Vector2(0, 0);
77 AccelerationRate = 10;
86 BoxTexture = new Texture2D(game.GraphicsDevice, 1, 1);
87 BoxTexture.SetData(new Color[] { Color.White });
92 protected void InitBox()
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));
97 public void AutoDeccelerate(GameTime gameTime)
99 if (Acceleration.X == 0 && Velocity.X > 0)
101 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
108 Acceleration.X = -AccelerationRate;
112 if (Acceleration.X == 0 && Velocity.X < 0)
114 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
121 Acceleration.X = AccelerationRate;
125 if (Acceleration.Y == 0 && Velocity.Y > 0)
127 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
134 Acceleration.Y = -AccelerationRate;
138 if (Acceleration.Y == 0 && Velocity.Y < 0)
140 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
147 Acceleration.Y = AccelerationRate;
152 public virtual void Update(GameTime gameTime)
160 protected virtual void UpdateBox()
162 Box.X = (int)(Position.X - BoxDimensions.X);
163 Box.Y = (int)(Position.Y - BoxDimensions.Y);
166 public virtual void Move(GameTime gameTime)
168 AutoDeccelerate(gameTime);
170 var maxVelocity = MaxVelocity;
172 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
173 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
175 if (Velocity.X > MaxVelocity)
177 Velocity.X = MaxVelocity;
180 if (Velocity.X < -MaxVelocity)
182 Velocity.X = -MaxVelocity;
185 if (Velocity.Y > MaxVelocity)
187 Velocity.Y = MaxVelocity;
190 if (Velocity.Y < -MaxVelocity)
192 Velocity.Y = -MaxVelocity;
195 Position.X = Position.X + Velocity.X;
196 Position.Y = Position.Y + Velocity.Y;
199 public void ChangeAngle()
201 if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1)
205 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
208 public virtual void Draw(SpriteBatch spriteBatch)
212 // TODO: Check what's up with the null children.
213 if (Children == null)
217 for (var i = Children.Count - 1; i >= 0; i--)
220 child.Draw(spriteBatch);
223 spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f);
224 //spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25));
229 for (var i = Children.Count; i > 0; i--)
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)
236 Children.Remove(actor);
241 public virtual void Collide(Actor other, Rectangle collision)
245 public void TakeDamage(int amount)
257 protected virtual void Die()
262 public virtual void CleanUp()