]>
Commit | Line | Data |
---|---|---|
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 | public class Actor | |
12 | { | |
13 | protected SuperPolarity 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 | public Rectangle Box; | |
22 | public Vector4 BoxDimensions; | |
23 | protected Texture2D BoxTexture; | |
24 | ||
25 | // Physical Properties | |
26 | public Vector2 Position; | |
27 | public bool Collides; | |
28 | protected Vector2 Velocity; | |
29 | protected Vector2 Acceleration; | |
30 | public float Angle; | |
31 | ||
32 | // Constraints / Behavior | |
33 | public float MaxVelocity; | |
34 | protected float AccelerationRate; | |
35 | public int HP; | |
36 | protected bool Immortal; | |
37 | public bool Dying; | |
38 | public int Value; | |
39 | protected Color Color; | |
40 | ||
41 | public Actor Parent; | |
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 | ||
53 | public Actor(SuperPolarity newGame) | |
54 | { | |
55 | game = newGame; | |
56 | BoxDimensions.X = 20; | |
57 | BoxDimensions.Y = 20; | |
58 | BoxDimensions.W = 15; | |
59 | BoxDimensions.Z = 15; | |
60 | } | |
61 | ||
62 | public virtual void Initialize(Texture2D texture, Vector2 position) | |
63 | { | |
64 | Texture = texture; | |
65 | Position = position; | |
66 | Active = true; | |
67 | ||
68 | Collides = true; | |
69 | ||
70 | Children = new List<Actor>(); | |
71 | ||
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; | |
78 | ||
79 | HP = 1; | |
80 | Immortal = false; | |
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 | { | |
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)); | |
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(); | |
156 | CheckOutliers(); | |
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); | |
164 | } | |
165 | ||
166 | public virtual void Move(GameTime gameTime) | |
167 | { | |
168 | AutoDeccelerate(gameTime); | |
169 | ||
170 | var maxVelocity = MaxVelocity; | |
171 | ||
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 | { | |
201 | if (Math.Abs(Velocity.Y) <= 0.1 && Math.Abs(Velocity.X) <= 0.1) | |
202 | { | |
203 | return; | |
204 | } | |
205 | Angle = (float)Math.Atan2(Velocity.Y, Velocity.X); | |
206 | } | |
207 | ||
208 | public virtual void Draw(SpriteBatch spriteBatch) | |
209 | { | |
210 | Actor child = null; | |
211 | ||
212 | // TODO: Check what's up with the null children. | |
213 | if (Children == null) | |
214 | { | |
215 | return; | |
216 | } | |
217 | for (var i = Children.Count - 1; i >= 0; i--) | |
218 | { | |
219 | child = Children[i]; | |
220 | child.Draw(spriteBatch); | |
221 | } | |
222 | ||
223 | spriteBatch.Draw(Texture, Position, null, Color, Angle, Origin, 1f, SpriteEffects.None, 0f); | |
224 | //spriteBatch.Draw(BoxTexture, Box, new Color(255, 0, 255, 25)); | |
225 | } | |
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 | } | |
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 | } | |
261 | ||
262 | public virtual void CleanUp() | |
263 | { | |
264 | Texture = null; | |
265 | BoxTexture = null; | |
266 | Children = null; | |
267 | Texture = null; | |
268 | } | |
269 | } | |
270 | } |