-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.cpp
239 lines (208 loc) · 5.63 KB
/
car.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "car.h"
#include "game.h"
#include <cmath>
#pragma region Car
Car::Car(SDL_Surface* sprite, const int x, const int y, const double speed, CarType type) : Sprite(sprite, x, y) {
this->speed = speed;
this->type = type;
}
Car::Car() : Sprite() {
this->speed = 0;
this->type = CarType::civil;
}
CarType Car::GetType()
{
return type;
}
void Car::SetType(CarType type)
{
this->type = type;
}
void Car::Crash(SDL_Surface* crashedSprite, CarType type)
{
this->type = type;
SetSurface(crashedSprite);
SetSpeed(0);
}
void Car::SetSpeed(double speed)
{
this->speed = speed;
}
double Car::GetSpeed()
{
return this->speed;
}
void Car::Update(const double delta, const double playerSpeed)
{
this->MoveY((this->GetSpeed() - playerSpeed) * -delta);
}
MapTile Car::CheckForCollisionWithMap(const int screenWidth, const int screenHeight, Map* map)
{
const int mapWidth = map->GetWidth(), mapHeight = map->GetHeight();
const double blockWidth = screenWidth / (double)mapWidth, blockHeight = screenHeight / (double)mapHeight;
const int carX = this->GetX(), carY = this->GetY();
const int width = this->GetWidth(), height = this->GetHeight();
const int rightX = carX + width / 2, leftX = carX - width / 2, bottomY = carY + height / 2, topY = carY - height / 2;
const int rightBlock = rightX / blockWidth, leftBlock = leftX / blockWidth, bottomBlock = bottomY / blockHeight, topBlock = topY / blockHeight;
MapTile resultTile = MapTile::road;
for (int y = bottomBlock; y >= topBlock; y--)
for (int x = leftBlock; x <= rightBlock; x++) {
MapTile tile = map->GetMapTile(x, y);
if (tile == MapTile::grass) {
return tile; // return the first grass tile
}
else if (tile != MapTile::road && tile != MapTile::stripes) {
resultTile = tile; // return the last tile that is not road or stripes
}
}
return resultTile;
}
void Car::SaveToFile(FILE* file) {
fwrite(&x, sizeof(x), 1, file);
fwrite(&y, sizeof(y), 1, file);
fwrite(&speed, sizeof(speed), 1, file);
fwrite(&type, sizeof(type), 1, file);
}
void Car::LoadFromFile(FILE* file) {
fread(&x, sizeof(x), 1, file);
fread(&y, sizeof(y), 1, file);
fread(&speed, sizeof(speed), 1, file);
fread(&type, sizeof(type), 1, file);
}
#pragma endregion
#pragma region Player
Player::Player(SDL_Surface* sprite, const int x, const int y, const double speed) : Car(sprite, x, y, speed)
{
}
double Player::GetShootCooldown() {
switch (ammoType) {
case AmmoType::multiMissile:
return 1;
case AmmoType::bomb:
return 3;
case AmmoType::laser:
return 0.001;
default:
return 0.5;
}
}
AmmoType Player::Shoot()
{
if (ammo > 0)
{
ammo--;
AmmoType result = ammoType;
if (ammo == 0) {
ammoType = AmmoType::missile;
}
return result;
}
ammoType = AmmoType::missile;
return ammoType;
}
void Player::Accelerate()
{
if ((this->GetSpeed() + speedBuffer) < MAX_SPEED)
this->speedBuffer += AccelerationSpeed();
if (this->GetSpeed() > MAX_SPEED) {
this->SetSpeed(MAX_SPEED);
if(speedBuffer > 0)
speedBuffer = 0;
}
}
void Player::Brake()
{
if ((this->GetSpeed() + speedBuffer) > 0)
this->speedBuffer -= AccelerationSpeed();
if (this->GetSpeed() < 0) {
this->SetSpeed(0);
if (speedBuffer < 0)
speedBuffer = 0;
}
}
void Player::Right()
{
this->moveBuffer += SteeringSpeed();
}
void Player::Left()
{
this->moveBuffer-= SteeringSpeed();
}
double Player::SteeringSpeed()
{
double speed = this->GetSpeed();
return pow(speed, 0.5) * STEERING_FORCE * delta;
}
double Player::AccelerationSpeed() {
return ACCELERATION * delta;
}
void Player::Crash(SDL_Surface* crashedSprite)
{
this->type = CarType::crashedPlayer;
SetSurface(crashedSprite);
SetSpeed(0);
}
void Player::Update(const double delta)
{
this->delta = delta;
if (this->type == CarType::crashedPlayer)
return;
const double steeringSpeed = SteeringSpeed(), accelerationSpeed = AccelerationSpeed();
if (this->moveBuffer > 0) {
this->moveBuffer-= steeringSpeed;
this->MoveX(steeringSpeed);
}
else if (this->moveBuffer < 0) {
this->moveBuffer+= steeringSpeed;
this->MoveX(-steeringSpeed);
}
if (this->speedBuffer > 0) {
this->speedBuffer-= accelerationSpeed;
this->SetSpeed(this->GetSpeed() + accelerationSpeed);
}
else if (this->speedBuffer < 0) {
this->speedBuffer+= accelerationSpeed;
this->SetSpeed(this->GetSpeed() - accelerationSpeed);
}
if (this->moveBuffer > -steeringSpeed && this->moveBuffer < steeringSpeed)
this->moveBuffer = 0;
if (this->speedBuffer > -accelerationSpeed && this->speedBuffer < accelerationSpeed)
this->speedBuffer = 0;
}
int Player::GetAmmo()
{
return ammo;
}
void Player::SetAmmo(int ammo)
{
this->ammo = ammo;
}
AmmoType Player::GetAmmoType()
{
return ammoType;
}
void Player::SetAmmoType(AmmoType ammoType)
{
this->ammoType = ammoType;
}
void Player::SaveToFile(FILE* file) {
fwrite(&x, sizeof(x), 1, file);
fwrite(&y, sizeof(y), 1, file);
fwrite(&speed, sizeof(speed), 1, file);
fwrite(&type, sizeof(type), 1, file);
fwrite(&moveBuffer, sizeof(moveBuffer), 1, file);
fwrite(&speedBuffer, sizeof(speedBuffer), 1, file);
fwrite(&ammo, sizeof(ammo), 1, file);
fwrite(&ammoType, sizeof(ammoType), 1, file);
}
void Player::LoadFromFile(FILE* file) {
fread(&x, sizeof(x), 1, file);
fread(&y, sizeof(y), 1, file);
fread(&speed, sizeof(speed), 1, file);
fread(&type, sizeof(type), 1, file);
fread(&moveBuffer, sizeof(moveBuffer), 1, file);
fread(&speedBuffer, sizeof(speedBuffer), 1, file);
fread(&ammo, sizeof(ammo), 1, file);
fread(&ammoType, sizeof(ammoType), 1, file);
}
#pragma endregion