-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
167 lines (148 loc) · 4.72 KB
/
player.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
#include "zzxoto_player.h"
#include "zzxoto_constants.h"
Player::Player(Mat3 *bottomLeftToTopLeftTransform, const Model &model)
{
this->velocity = { 0.f, 0.f };
this->maxVelocity = PLAYER_MAX_VELOCITY;
this->angularVelocityPerSecond = PLAYER_ANGULAR_VELOCITY_PS;
this->bottomLeftToTopLeftTransform = bottomLeftToTopLeftTransform;
this->model = model;
this->scale = PLAYER_SCALE;
this->orientation = 0.f;
this->translate = { 100.f, 20.f };
this->collisionRadius = PLAYER_COLLISION_RADIUS;
this->bulletListSize = 20;
this->lastBulletFired = 0.f;
this->bullets = (Bullet *) platform_malloc(sizeof(Bullet) * this->bulletListSize);
for (int i = 0; i < bulletListSize; i++)
{
this->bullets[i].isKilled = true;
}
}
void Player::kill()
{
this->isKilled = true;
for (int i = 0; i < this->bulletListSize; i++)
{
Bullet *b = &this->bullets[i];
if (!b->isKilled)
{
b->kill();
}
}
platform_free(this->bullets);
}
void Player::render(platform_RenderBuffer *renderBuffer)
{
drawModel_outline(renderBuffer, &this->model);
for (int i = 0; i < this->bulletListSize; i++)
{
Bullet *bullet = this->bullets + i;
if (!bullet->isKilled)
{
bullet->render(renderBuffer);
}
}
}
void Player::update(platform_GameInput *gameInput)
{
float angularVelocity = this->angularVelocityPerSecond * gameInput->dtForFrame;
if (gameInput->leftIsDown)
{
angularVelocity *= 1;
}
else if (gameInput->rightIsDown)
{
angularVelocity *= -1;
}
else
{
angularVelocity = 0;
}
this->orientation += angularVelocity;
float accelaration = 0;
if (gameInput->upIsDown)
{
accelaration = this->maxVelocity * 4;
}
Mat3 rotationMatrix = rotate(this->orientation);
V3 accelarationDirection = { 0.f, 1.f, 1.f };
accelarationDirection = (rotationMatrix * accelarationDirection);
float accelarationX = accelarationDirection.x * accelaration;
float accelarationY = accelarationDirection.y * accelaration;
float deltaX = (.5 * square(gameInput->dtForFrame) * accelarationX) + (this->velocity.x * gameInput->dtForFrame);
float deltaY = (.5 * square(gameInput->dtForFrame) * accelarationY) + (this->velocity.y * gameInput->dtForFrame);
this->translate = { this->translate.x + deltaX, this->translate.y + deltaY };
this->translate.x = wrap<float>(this->translate.x, GAME_WIDTH - 1);
this->translate.y = wrap<float>(this->translate.y, GAME_HEIGHT - 1);
this->velocity.x += accelarationX * gameInput->dtForFrame;
this->velocity.y += accelarationY * gameInput->dtForFrame;
float velocityMagnitude = magnitude(this->velocity);
if (velocityMagnitude > this->maxVelocity)
{
float factor = velocityMagnitude / this->maxVelocity;
this->velocity = divide(this->velocity, factor);
}
this->model.transform = *this->bottomLeftToTopLeftTransform * scaleRotateTranslate(this->scale, this->orientation, this->translate);
if (gameInput->spaceIsDown)
{
if ((gameInput->secondsElapsed - this->lastBulletFired) >= BULLET_FIRE_FREQUENCY)
{
this->lastBulletFired = gameInput->secondsElapsed;
for (int i = 0; i < this->bulletListSize; i++)
{
Bullet *bullet = this->bullets + i;
if (bullet->isKilled)
{
V3 direction = { 0.f, 1.f, 1.f };
direction = (rotationMatrix * direction);
V2 bulletVelocity = { direction.x * BULLET_VELOCITY, direction.y * BULLET_VELOCITY };
*bullet = Bullet(this->bottomLeftToTopLeftTransform, bulletVelocity, this->translate);
break;
}
}
}
}
for (int i = 0; i < this->bulletListSize; i++)
{
Bullet *bullet = this->bullets + i;
if (!bullet->isKilled)
{
bullet->update(gameInput);
}
}
}
void Bullet::init(Mat3 *bottomLeftToTopLeftTransform, V2 velocity, V2 translate)
{
this->isKilled = false;
this->secondsInExistence = 0.f;
this->translate = translate;
this->velocity = velocity;
this->collisionRadius = BULLET_COLLISION_RADIUS;
this->bottomLeftToTopLeftTransform = bottomLeftToTopLeftTransform;
}
void Bullet::update(platform_GameInput *gameInput)
{
if (this->secondsInExistence > BULLET_LIFE_SECONDS_MAX)
{
this->kill();
}
else
{
float tx = this->translate.x + (this->velocity.x * gameInput->dtForFrame);
float ty = this->translate.y + (this->velocity.y * gameInput->dtForFrame);
tx = wrap<float>(tx, GAME_WIDTH - 1);
ty = wrap<float>(ty, GAME_HEIGHT - 1);
this->translate = { tx, ty };
this->secondsInExistence += gameInput->dtForFrame;
}
}
void Bullet::render(platform_RenderBuffer *renderBuffer)
{
V2 position = *this->bottomLeftToTopLeftTransform * this->translate;
drawDot(renderBuffer, position, BULLET_THICKNESS, COLOR_BULLET);
}
void Bullet::kill()
{
this->isKilled = true;
}