-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
222 lines (171 loc) · 7.24 KB
/
main.cpp
File metadata and controls
222 lines (171 loc) · 7.24 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
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
#include <vector>
#include "raylib.h"
#include "headers/types.h"
std::vector<AnimationData> setupNebulaData(const int totalNebula, int windowWidth, const int windowHeight, const Texture &nebulaTexture) {
std::vector<AnimationData> nebulas;
// Start off screen
float nebulaPos { static_cast<float>(windowWidth + 200) };
for (int idx = 0; idx < totalNebula; idx++) {
nebulaPos = nebulaPos + GetRandomValue(400, 800);
nebulas.push_back({
{ 0.0, 0.0, nebulaTexture.width / 8.0f, nebulaTexture.height / 8.0f },
{ nebulaPos, windowHeight - (nebulaTexture.height / 8.0f) },
0,
1.0 / 10.0,
0.0
});
}
return nebulas;
}
void updateAnimationData(AnimationData& animData, const float deltaTime, const int maxFrames) {
// Only update the current frame when we want to animate, otherwise the frame will exceed the sprite sheet
// length and cause no animation to occur.
animData.runningTime += deltaTime;
if (animData.runningTime >= animData.updateTime) {
animData.runningTime = 0.0;
animData.frame++;
animData.rect.x = animData.frame * animData.rect.width;
if (animData.frame > maxFrames) {
animData.frame = 0;
}
}
}
bool isPlayerColliding(const std::vector <AnimationData>& nebulaAnimData, const AnimationData& player) {
// The nebula sprites are not the size of the sprite rectangles. We need to handling padding
// to get the real position of the nebula and compute the raw width/height.
constexpr float nebulaPadding { 50.0 };
for (const AnimationData& nebulaData : nebulaAnimData) {
Rectangle nebulaPos {
nebulaData.position.x + nebulaPadding,
nebulaData.position.y + nebulaPadding,
nebulaData.rect.width - (nebulaPadding * 2.0f),
nebulaData.rect.height - (nebulaPadding * 2.0f)
};
Rectangle playerPos {
player.position.x,
player.position.y,
player.rect.width,
player.rect.height
};
if (CheckCollisionRecs(nebulaPos, playerPos)) {
return true;
}
}
return false;
}
void unloadTextures(const std::vector<Texture2D>& textures) {
for (const Texture2D& texture : textures) {
UnloadTexture(texture);
}
}
void scrollTexture(BackgroundData& backgroundData, const float& deltaTime, const float& fScale) {
backgroundData.x -= backgroundData.speed * deltaTime;
// If we've completely scrolled the texture, move it off screen
if (backgroundData.x < -backgroundData.texture.width * fScale) {
backgroundData.x = 0.0;
}
// We have two textures, ensure they're each scrolled in tandem to avoid popping
const Vector2 firstTextPos { backgroundData.x, 0.0 };
const Vector2 secondTextPos { backgroundData.x + backgroundData.texture.width * fScale, 0.0 };
DrawTextureEx(backgroundData.texture, firstTextPos, 0.0, fScale, WHITE);
DrawTextureEx(backgroundData.texture, secondTextPos, 0.0, fScale, WHITE);
}
int main() {
// Window dimensions
constexpr int iWindowHeight { 500 };
constexpr int iWindowWidth { 750 };
// The max length of the sprite sheets (textures).
constexpr int iDapSpriteSheetLen { 5 };
constexpr int iNebSpriteSheetLen { 7 };
// Pixels per second velocity to keep frame independent.
int iDapVelocity { 0 };
int iNebulaVelocity { -400 };
// Acceleration due to velocity over time (pixels per second per second)
constexpr int iGravity { 800 };
constexpr int iJumpVelocity { -500 };
bool bCharIsInAir { false };
// Must be called before LoadTexture or else memory is unavailable on the GPU and
// segfaults.
InitWindow(iWindowWidth, iWindowHeight, "Dap Dash");
SetTargetFPS(60);
// Load textures.
Texture2D dapTexture = LoadTexture("assets/textures/dap_spritesheet.png");
Texture2D nebulaTexture = LoadTexture("assets/textures/nebula_spritesheet.png");
Texture2D backgroundTexture = LoadTexture("assets/textures/background.png");
Texture2D midgroundTexture = LoadTexture("assets/textures/midground.png");
Texture2D foregroundTexture = LoadTexture("assets/textures/foreground.png");
std::vector textures { dapTexture, nebulaTexture, backgroundTexture, midgroundTexture, foregroundTexture };
BackgroundData backgroundData {
backgroundTexture,
0.0,
40
};
BackgroundData midgroundData {
midgroundTexture,
0.0,
60
};
BackgroundData foregroundData {
foregroundTexture,
0.0,
80
};
constexpr float fScale = 3.0;
// 6x1 sprite sheet
// We use the rectangle value for position so we reference the size of a single sprite in the sheet.
AnimationData dapAnimData {
{ 0.0, 0.0, dapTexture.width / 6.0f, static_cast<float>(dapTexture.height) },
{ (iWindowWidth * 0.25f) - ((dapTexture.width / 6.0f) / 2.0f), static_cast<float>(iWindowHeight - dapTexture.height) },
0,
1.0 / 10.0,
0.0
};
std::vector<AnimationData> nebulaAnimData = setupNebulaData(10, iWindowWidth, iWindowHeight, nebulaTexture);
float fFinishLineX { nebulaAnimData.back().position.x };
bool bIsGameOver { false };
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(WHITE);
// Capture the time from last frame to keep the jumping consistent across FPS changes.
float fDeltaTime { GetFrameTime() };
scrollTexture(backgroundData, fDeltaTime, fScale);
scrollTexture(midgroundData, fDeltaTime, fScale);
scrollTexture(foregroundData, fDeltaTime, fScale);
// += because velocity is negative (moving to the left)
fFinishLineX += iNebulaVelocity * fDeltaTime;
// Only apply gravity when in the air.
if (dapAnimData.position.y + dapAnimData.rect.height >= iWindowHeight) {
iDapVelocity = 0;
bCharIsInAir = false;
} else {
iDapVelocity += iGravity * fDeltaTime;
bCharIsInAir = true;
}
if ((IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_UP)) && !bCharIsInAir) {
iDapVelocity += iJumpVelocity;
}
dapAnimData.position.y += iDapVelocity * fDeltaTime;
for (AnimationData &animData : nebulaAnimData) {
animData.position.x += iNebulaVelocity * fDeltaTime;
updateAnimationData(animData, fDeltaTime, iNebSpriteSheetLen);
}
if (!bCharIsInAir) {
updateAnimationData(dapAnimData, fDeltaTime, iDapSpriteSheetLen);
}
if (bIsGameOver) {
DrawText("Game Over!", iWindowWidth / 2, iWindowHeight / 2, 25, RED);
} else if (dapAnimData.position.x >= fFinishLineX) {
DrawText("You Win!", iWindowWidth / 2, iWindowHeight / 2, 25, GREEN);
} else {
bIsGameOver = isPlayerColliding(nebulaAnimData, dapAnimData);
DrawTextureRec(dapTexture, dapAnimData.rect, dapAnimData.position, WHITE);
for (AnimationData &animData : nebulaAnimData) {
DrawTextureRec(nebulaTexture, animData.rect, animData.position, WHITE);
}
}
EndDrawing();
}
unloadTextures(textures);
CloseWindow();
return 0;
}