]> git.r.bdr.sh - rbdr/super-polarity/blob - Super Polarity/Actor.cs
f0da2440ff18b9cc88211ab7762a6ff1332d22ed
[rbdr/super-polarity] / Super Polarity / 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 // Graphics / In-Game
14 protected Texture2D Texture;
15 protected Vector2 Origin;
16 public bool Active;
17
18 // Physical Properties
19 protected Vector2 Position;
20 protected Vector2 Velocity;
21 protected Vector2 Acceleration;
22 protected float Angle;
23
24 // Constraints / Behavior
25 protected float MaxVelocity;
26 protected float AccelerationRate;
27
28 public int Width
29 {
30 get { return Texture.Width; }
31 }
32
33 public int Height
34 {
35 get { return Texture.Height; }
36 }
37
38 public virtual void Initialize(ContentManager Content, Texture2D texture, Vector2 position)
39 {
40 Texture = texture;
41 Position = position;
42 Active = true;
43
44 Origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
45 Velocity = new Vector2(0, 0);
46 Acceleration = new Vector2(0, 0);
47
48 MaxVelocity = 5;
49 AccelerationRate = 10;
50 }
51
52 public void AutoDeccelerate(GameTime gameTime)
53 {
54 if (Acceleration.X == 0 && Velocity.X > 0)
55 {
56 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X)
57 {
58 Velocity.X = 0;
59 Acceleration.X = 0;
60 }
61 else
62 {
63 Acceleration.X = -AccelerationRate;
64 }
65 }
66
67 if (Acceleration.X == 0 && Velocity.X < 0)
68 {
69 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X)
70 {
71 Velocity.X = 0;
72 Acceleration.X = 0;
73 }
74 else
75 {
76 Acceleration.X = AccelerationRate;
77 }
78 }
79
80 if (Acceleration.Y == 0 && Velocity.Y > 0)
81 {
82 if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y)
83 {
84 Velocity.Y = 0;
85 Acceleration.Y = 0;
86 }
87 else
88 {
89 Acceleration.Y = -AccelerationRate;
90 }
91 }
92
93 if (Acceleration.Y == 0 && Velocity.Y < 0)
94 {
95 if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y)
96 {
97 Velocity.Y = 0;
98 Acceleration.Y = 0;
99 }
100 else
101 {
102 Acceleration.Y = AccelerationRate;
103 }
104 }
105 }
106
107 public virtual void Update(GameTime gameTime)
108 {
109 Move(gameTime);
110 ChangeAngle();
111 }
112
113 public void Move(GameTime gameTime)
114 {
115 AutoDeccelerate(gameTime);
116
117 Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
118 Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;
119
120 if (Velocity.X > MaxVelocity)
121 {
122 Velocity.X = MaxVelocity;
123 }
124
125 if (Velocity.X < -MaxVelocity)
126 {
127 Velocity.X = -MaxVelocity;
128 }
129
130 if (Velocity.Y > MaxVelocity)
131 {
132 Velocity.Y = MaxVelocity;
133 }
134
135 if (Velocity.Y < -MaxVelocity)
136 {
137 Velocity.Y = -MaxVelocity;
138 }
139
140 Position.X = Position.X + Velocity.X;
141 Position.Y = Position.Y + Velocity.Y;
142 }
143
144 public void ChangeAngle()
145 {
146 Angle = (float)Math.Atan2(Velocity.Y, Velocity.X);
147 }
148
149 public virtual void Draw(SpriteBatch spriteBatch)
150 {
151 spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f);
152 }
153 }
154 }