-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.cs
More file actions
166 lines (138 loc) · 5.11 KB
/
Car.cs
File metadata and controls
166 lines (138 loc) · 5.11 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
namespace test_game {
public class Car {
public Sprite sprite;
public int index;
public int health;
public int maxHealth;
public Car pulledBy;
public float acceleration;
public float speed;
public float topSpeed;
public float topSpeedReverse;
public bool isDestroyed;
public bool isPowered;
public bool isBoosted;
public bool isInvincible;
public bool isAttached;
public Weapon weapon;
public Vector2 weaponPos;
private Healthbar hpBar;
private Healthbar reloadBar;
public float defaultTopSpeed;
public float defaultTopSpeedRev;
public float remainingBoost;
public float remainingInv;
public Car(Sprite spr, int idx, Car pull, int hp = 500) {
sprite = spr;
index = idx;
pulledBy = pull;
health = hp;
maxHealth = hp;
isDestroyed = false;
isBoosted = false;
isInvincible = false;
isAttached = false;
hpBar = new Healthbar(new Vector2(100.0f, 8.0f), Color.SpringGreen, Color.DarkGray);
hpBar.health = health;
reloadBar = new Healthbar(new Vector2(80.0f, 8.0f), Color.Yellow, Color.DarkGray);
reloadBar.health = 0;
reloadBar.maxHealth = 100;
weaponPos = Vector2.Zero;
weapon = new Weapon_MachineGun();
weapon.sprite.anchor = weaponPos;
weapon.sprite.parent = sprite;
weapon.owner = this;
defaultTopSpeed = 300.0f;
defaultTopSpeedRev = 150.0f;
topSpeed = 300.0f;
topSpeedReverse = 150.0f;
acceleration = 0.0f;
speed = 0.0f;
remainingBoost = 0.0f;
remainingInv = 0.0f;
isPowered = false;
}
public void Equip(Weapon w) {
weapon = w;
weapon.sprite.anchor = weaponPos;
weapon.sprite.parent = sprite;
weapon.owner = this;
}
public void Update(GameTime gameTime) {
if (isDestroyed) {
return;
}
if (health <= 0) {
isDestroyed = true;
return;
}
float deltaT = (float)gameTime.ElapsedGameTime.TotalSeconds;
deltaT *= Game1.timeScale;
if (isBoosted) {
remainingBoost -= deltaT;
if (remainingBoost <= 0.0f) {
remainingBoost = 0.0f;
isBoosted = false;
topSpeed = defaultTopSpeed;
topSpeedReverse = defaultTopSpeedRev;
if (speed < 0.0f) {
if (speed < -topSpeedReverse) {
speed = -topSpeedReverse;
}
}
else {
if (speed > topSpeed) {
speed = topSpeed;
}
}
}
}
if (isInvincible) {
remainingInv -= deltaT;
if (remainingInv <= 0.0f) {
remainingInv = 0.0f;
isInvincible = false;
}
}
if (pulledBy == null || pulledBy.isDestroyed) {
if (acceleration == 0.0f || !isPowered) {
speed -= speed * 0.93f * deltaT;
if (Math.Abs(speed) < 5.0f) speed = 0.0f;
}
else {
speed += acceleration * deltaT;
if (speed < 0) {
if (speed < -topSpeedReverse) speed = -topSpeedReverse;
}
else {
if (speed > topSpeed) speed = topSpeed;
}
}
}
else {
acceleration = pulledBy.acceleration;
speed = pulledBy.speed;
}
sprite.position.X += speed * deltaT;
hpBar.position = sprite.position + new Vector2(0.0f, -50.0f);
hpBar.health += ((float)health - hpBar.health) * 8.0f * deltaT;
hpBar.maxHealth = maxHealth;
reloadBar.position = sprite.position + new Vector2(0.0f, 50.0f);
weapon.sprite.anchor = weaponPos;
weapon.Update(gameTime);
}
public void Draw(SpriteBatch batch) {
if (isDestroyed) return;
hpBar.Draw(batch);
if (weapon.isReloading) {
reloadBar.health = (weapon.reloadTime - weapon.remainingReloadT) / weapon.reloadTime * 100.0f;
reloadBar.Draw(batch);
}
sprite.Draw(batch);
weapon.sprite.Draw(batch);
}
}
}