]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actors/Actor.cs
588ff1421875dcf45c45055afeed61b3c0eb432f
[rbdr/super-polarity] / Super Polarity / Actors / Actor.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8
9 namespace SuperPolarity
10 {
11 class Actor
12 {
13 protected Game game;
14
15 public List<Actor> Children;
16
17 // Graphics / In-Game
18 protected Texture2D Texture;
19 protected Vector2 Origin;
20 public bool Active;
21
22 // Physical Properties
23 public Vector2 Position;
24 protected Vector2 Velocity;
25 protected Vector2 Acceleration;
26 public float Angle;
27
28 // Constraints / Behavior
29 protected float MaxVelocity;
30 protected float AccelerationRate;
31
32 public int Width
33 {
34 get { return Texture.Width; }
35 }
36
37 public int Height
38 {
39 get { return Texture.Height; }
40 }
41
42 public Actor(Game newGame)
43 {
44 game = newGame;
45 }
46
47 public virtual void Initialize(Texture2D texture, Vector2 position)
48 {
49 Texture = texture;
50 Position = position;
51 Active = true;
52
53 Children = new List<Actor>();
54
55 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
56 Velocity = new Vector2(0, 0);
57 Acceleration = new Vector2(0, 0);
58
59 MaxVelocity = 5;
60 AccelerationRate = 10;
61 }
62
63 public void AutoDeccelerate(GameTime gameTime)
64 {
65 if (Acceleration.X == 0 && Velocity.X > 0)
66 {
67 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
68 {
69 Velocity.X = 0;
70 Acceleration.X = 0;
71 }
72 else
73 {
74 Acceleration.X = -AccelerationRate;
75 }
76 }
77
78 if (Acceleration.X == 0 && Velocity.X < 0)
79 {
80 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
81 {
82 Velocity.X = 0;
83 Acceleration.X = 0;
84 }
85 else
86 {
87 Acceleration.X = AccelerationRate;
88 }
89 }
90
91 if (Acceleration.Y == 0 && Velocity.Y > 0)
92 {
93 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
94 {
95 Velocity.Y = 0;
96 Acceleration.Y = 0;
97 }
98 else
99 {
100 Acceleration.Y = -AccelerationRate;
101 }
102 }
103
104 if (Acceleration.Y == 0 && Velocity.Y < 0)
105 {
106 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
107 {
108 Velocity.Y = 0;
109 Acceleration.Y = 0;
110 }
111 else
112 {
113 Acceleration.Y = AccelerationRate;
114 }
115 }
116 }
117
118 public virtual void Update(GameTime gameTime)
119 {
120 Move(gameTime);
121 ChangeAngle();
122 CheckOutliers();
123 }
124
125 public virtual void Move(GameTime gameTime)
126 {
127 AutoDeccelerate(gameTime);
128
129 var maxVelocity = MaxVelocity;
130
131 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
132 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
133
134 if (Velocity.X > MaxVelocity)
135 {
136 Velocity.X = MaxVelocity;
137 }
138
139 if (Velocity.X < -MaxVelocity)
140 {
141 Velocity.X = -MaxVelocity;
142 }
143
144 if (Velocity.Y > MaxVelocity)
145 {
146 Velocity.Y = MaxVelocity;
147 }
148
149 if (Velocity.Y < -MaxVelocity)
150 {
151 Velocity.Y = -MaxVelocity;
152 }
153
154 Position.X = Position.X + Velocity.X;
155 Position.Y = Position.Y + Velocity.Y;
156 }
157
158 public void ChangeAngle()
159 {
160 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
161 }
162
163 public virtual void Draw(SpriteBatch spriteBatch)
164 {
165 foreach (Actor child in Children)
166 {
167 child.Draw(spriteBatch);
168 }
169
170 spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f);
171 }
172
173 void CheckOutliers()
174 {
175 for (var i = Children.Count; i > 0; i--)
176 {
177 var actor = Children[i - 1];
178 if (actor.Position.X < -SuperPolarity.OutlierBounds || actor.Position.Y < -SuperPolarity.OutlierBounds ||
179 actor.Position.X > game.GraphicsDevice.Viewport.Width + SuperPolarity.OutlierBounds ||
180 actor.Position.Y > game.GraphicsDevice.Viewport.Height + SuperPolarity.OutlierBounds)
181 {
182 Children.Remove(actor);
183 }
184 }
185 }
186 }
187 }