-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rocket.cs
146 lines (110 loc) · 4.44 KB
/
Rocket.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace gamedev_attempt_01 {
class Rocket : MovingEntity {
private readonly IUserInput userInput;
private readonly float forwardThrust = 10;
private readonly float angularThrust = 6;
private readonly float maxAngularVelocity = 3f;
private readonly float amunitionLossOnShoot = 1;
public override Tileset Tileset => tileset;
Tileset tileset;
public float KetchupAmount { get; set; }
public Rocket() {
userInput = new KeyboardInput();
Depth = 0.5f;
}
public override void Update(Context context) {
float dt = (float)context.Time.ElapsedGameTime.TotalSeconds;
userInput.Update(context);
HandleAcceleration(userInput.GoForwardAmount, dt);
HandleRotation(userInput.RotateClockwiseAmount, userInput.RotateCounterClockwiseAmount, dt);
if (userInput.Shoot) {
Shoot(context);
}
base.Update(context);
}
private void HandleAcceleration(float forwardAcc, float dt) {
Velocity += Forward * forwardAcc * forwardThrust * dt;
}
private void HandleRotation(float clockwiseAcc, float counterClockwiseAcc, float dt) {
AngularVelocity += (clockwiseAcc - counterClockwiseAcc) * angularThrust * dt;
AngularVelocity = Math.Clamp(AngularVelocity, -maxAngularVelocity, maxAngularVelocity);
if (clockwiseAcc == 0 && counterClockwiseAcc == 0) {
float angVelDecrease = Math.Min(Math.Abs(AngularVelocity), 1 * angularThrust * dt);
AngularVelocity -= angVelDecrease * MathF.Sign(AngularVelocity);
}
}
public void Shoot(Context ctx) {
KetchupAmount -= amunitionLossOnShoot;
var bullet = new Bullet(this);
ctx.World.AddEntity(bullet);
}
public override void Draw(Context context) {
tileset = context.Content.Raketa;
base.Draw(context);
}
}
class Bullet : MovingEntity {
public float Speed { get; set; } = 50;
public float Lifetime { get; private set; } = 99999;
public Bullet(Rocket rocket) {
Position = rocket.Position;
Orientation = rocket.Orientation;
//Velocity = rocket.Forward * Speed + rocket.Velocity;
Velocity = Vector2.Zero;
AngularVelocity = 0;
Depth = 0.6f;
}
public override void Update(Context ctx) {
Lifetime -= ctx.dt;
if (Lifetime <= 0) {
Destroyed = true;
return;
}
base.Update(ctx);
}
public override void Draw(Context ctx) {
var sb = ctx.SpriteBatch;
sb.Draw(sb.Pixel(), ctx.Camera.WorldToScreen(Position),
null, Color.Red, Orientation, new Vector2(.5f),
new Vector2(40, 6), SpriteEffects.None, 0f);
}
}
interface IUserInput : IUpdateable {
float GoForwardAmount { get; }
float RotateClockwiseAmount { get; }
float RotateCounterClockwiseAmount { get; }
bool Shoot { get; }
}
class KeyboardInput : IUserInput {
public float GoForwardAmount { get; private set; }
public float RotateClockwiseAmount { get; private set; }
public float RotateCounterClockwiseAmount { get; private set; }
public bool Shoot { get; private set; }
KeyboardState lastKbs;
public void Update(Context context) {
var kbs = context.KeyboardState;
if (kbs.IsKeyDown(Keys.W) || kbs.IsKeyDown(Keys.Up)) {
GoForwardAmount = 1f;
} else {
GoForwardAmount = 0f;
}
if (kbs.IsKeyDown(Keys.A) || kbs.IsKeyDown(Keys.Left)) {
RotateCounterClockwiseAmount = 1f;
} else {
RotateCounterClockwiseAmount = 0f;
}
if (kbs.IsKeyDown(Keys.D) || kbs.IsKeyDown(Keys.Right)) {
RotateClockwiseAmount = 1f;
} else {
RotateClockwiseAmount = 0f;
}
Shoot = kbs.IsKeyDown(Keys.Space) && lastKbs.IsKeyUp(Keys.Space);
lastKbs = context.KeyboardState;
}
}
}