3.19 Arrays
This commit is contained in:
100
dasher.cpp
100
dasher.cpp
@@ -12,11 +12,16 @@ struct AnimData
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
// Specified width and height
|
/* // Specified width and height
|
||||||
const int windowWidth{800};
|
const int windowWidth{800};
|
||||||
const int windowHeight{600};
|
const int windowHeight{600};
|
||||||
|
*/
|
||||||
|
|
||||||
|
// windows array dimensions
|
||||||
|
float windowArray[2] = { 800.0, 600.0 };
|
||||||
|
|
||||||
// Initialize the window
|
// Initialize the window
|
||||||
InitWindow(windowWidth, windowHeight, "ECS Dapper Dasher");
|
InitWindow(windowArray[0], windowArray[1], "ECS Dapper Dasher");
|
||||||
|
|
||||||
// Acceleration due to gravity (pixels per frame/frame)
|
// Acceleration due to gravity (pixels per frame/frame)
|
||||||
const int gravity{ 1'000 };
|
const int gravity{ 1'000 };
|
||||||
@@ -29,23 +34,12 @@ int main() {
|
|||||||
scarfyData.rec.x = 0;
|
scarfyData.rec.x = 0;
|
||||||
scarfyData.rec.y = 0;
|
scarfyData.rec.y = 0;
|
||||||
|
|
||||||
scarfyData.pos.x = windowWidth / 2 - scarfyData.rec.width / 2;
|
scarfyData.pos.x = windowArray[0] / 2 - scarfyData.rec.width / 2;
|
||||||
scarfyData.pos.y = windowHeight - scarfyData.rec.height;
|
scarfyData.pos.y = windowArray[1] - scarfyData.rec.height;
|
||||||
scarfyData.frame = 0;
|
scarfyData.frame = 0;
|
||||||
scarfyData.updateTime = 1.0 / 12.0;
|
scarfyData.updateTime = 1.0 / 12.0;
|
||||||
scarfyData.runningTime = 0.0;
|
scarfyData.runningTime = 0.0;
|
||||||
|
|
||||||
// Scarfy Rectangle
|
|
||||||
|
|
||||||
Rectangle scarfyRect;
|
|
||||||
scarfyRect.width = scarfy.width / 6;
|
|
||||||
scarfyRect.height = scarfy.height;
|
|
||||||
scarfyRect.x = 0;
|
|
||||||
scarfyRect.y = 0;
|
|
||||||
Vector2 scarfyPos;
|
|
||||||
scarfyPos.x = windowWidth / 2 - scarfyRect.width / 2;
|
|
||||||
scarfyPos.y = windowHeight - scarfyRect.height;
|
|
||||||
|
|
||||||
// Nebula setup
|
// Nebula setup
|
||||||
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture
|
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture
|
||||||
|
|
||||||
@@ -53,7 +47,7 @@ int main() {
|
|||||||
|
|
||||||
AnimData nebData{
|
AnimData nebData{
|
||||||
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
|
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
|
||||||
{windowWidth, windowHeight - nebula.height / 8}, // Vector2 pos
|
{windowArray[0], windowArray[1] - nebula.height / 8}, // Vector2 pos
|
||||||
0, // int frame
|
0, // int frame
|
||||||
1.0 / 12.0, // float updateTime
|
1.0 / 12.0, // float updateTime
|
||||||
0.0 // float runningTime
|
0.0 // float runningTime
|
||||||
@@ -61,25 +55,18 @@ int main() {
|
|||||||
|
|
||||||
AnimData neb2Data{
|
AnimData neb2Data{
|
||||||
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
|
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
|
||||||
{windowWidth + 300, windowHeight - nebula.height / 8}, // Vector2 pos
|
{windowArray[0] + 300, windowArray[1] - nebula.height / 8}, // Vector2 pos
|
||||||
0, // int frame
|
0, // int frame
|
||||||
1.0 / 16.0, // float updateTime
|
1.0 / 16.0, // float updateTime
|
||||||
0.0 // float runningTime
|
0.0 // float runningTime
|
||||||
};
|
};
|
||||||
|
|
||||||
Rectangle nebRect{0.0, 0.0, nebula.width / 8, nebula.height / 8};
|
// Nebulea
|
||||||
Vector2 nebulaPos{windowWidth, windowHeight - nebRect.height};
|
AnimData nebulae[2] =
|
||||||
|
{
|
||||||
Rectangle neb2Rect = nebRect;
|
nebData,
|
||||||
Vector2 neb2Pos{windowWidth + 300, windowHeight - neb2Rect.height};
|
neb2Data
|
||||||
|
};
|
||||||
int nebFrame{}; // Nebula animation frame
|
|
||||||
const float nebUpdateTime{1.0 / 12.0}; // Nebula running time
|
|
||||||
float nebRunningTime{}; // Nebula time since last frame change
|
|
||||||
|
|
||||||
int neb2Frame{}; // Nebula animation frame
|
|
||||||
const float neb2UpdateTime{1.0 / 16.0}; // Nebula running time
|
|
||||||
float neb2RunningTime{}; // Nebula time since last frame change
|
|
||||||
|
|
||||||
int nebVel{-200}; // Nebula x velocity (pixels per second)
|
int nebVel{-200}; // Nebula x velocity (pixels per second)
|
||||||
|
|
||||||
@@ -109,7 +96,7 @@ int main() {
|
|||||||
ClearBackground(WHITE);
|
ClearBackground(WHITE);
|
||||||
|
|
||||||
// perform ground check
|
// perform ground check
|
||||||
if (scarfyPos.y >= windowHeight - scarfyRect.height) {
|
if (scarfyData.pos.y >= windowArray[1] - scarfyData.rec.height) {
|
||||||
velocity = 0; // Reset velocity when on the ground
|
velocity = 0; // Reset velocity when on the ground
|
||||||
isInAir = false;
|
isInAir = false;
|
||||||
}
|
}
|
||||||
@@ -125,13 +112,13 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nebula movement
|
// Nebula movement
|
||||||
nebulaPos.x += nebVel * dT;
|
nebData.pos.x += nebVel * dT;
|
||||||
|
|
||||||
// Nebula 2 movement
|
// Nebula 2 movement
|
||||||
neb2Pos.x += nebVel * dT;
|
neb2Data.pos.x += nebVel * dT;
|
||||||
|
|
||||||
// Update position
|
// Update position
|
||||||
scarfyPos.y += velocity * dT;
|
scarfyData.pos.y += velocity * dT;
|
||||||
|
|
||||||
// Scarfy Animation Logic
|
// Scarfy Animation Logic
|
||||||
if (!isInAir)
|
if (!isInAir)
|
||||||
@@ -143,7 +130,7 @@ int main() {
|
|||||||
runningTime = 0.0;
|
runningTime = 0.0;
|
||||||
|
|
||||||
// Update animation frame
|
// Update animation frame
|
||||||
scarfyRect.x = frame * scarfyRect.width;
|
scarfyData.rec.x = frame * scarfyData.rec.width;
|
||||||
frame++;
|
frame++;
|
||||||
if (frame > 5)
|
if (frame > 5)
|
||||||
{
|
{
|
||||||
@@ -153,38 +140,47 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Nebula animation frame
|
// Nebula animation frame
|
||||||
nebRunningTime += dT;
|
nebulae[0].runningTime += dT;
|
||||||
if (nebRunningTime >= (nebUpdateTime))
|
if (nebulae[0].runningTime >= (nebData.updateTime))
|
||||||
{
|
{
|
||||||
nebRunningTime = 0.0;
|
nebulae[0].runningTime = 0.0;
|
||||||
nebRect.x = nebFrame * nebRect.width;
|
nebulae[0].rec.x = nebulae[0].frame * nebulae[0].rec.width;
|
||||||
nebFrame++;
|
nebulae[0].frame++;
|
||||||
if (nebFrame > 7)
|
if (nebulae[0].frame > 7)
|
||||||
{
|
{
|
||||||
nebFrame = 0;
|
nebulae[0].frame = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nebula animation frame
|
// Nebula animation frame
|
||||||
neb2RunningTime += dT;
|
nebulae[1].runningTime += dT;
|
||||||
if (neb2RunningTime >= (neb2UpdateTime))
|
if (nebulae[1].runningTime >= (nebulae[1].updateTime))
|
||||||
{
|
{
|
||||||
neb2RunningTime = 0.0;
|
nebulae[1].runningTime = 0.0;
|
||||||
neb2Rect.x = neb2Frame * neb2Rect.width;
|
nebulae[1].rec.x = nebulae[1].frame * nebulae[1].rec.width;
|
||||||
neb2Frame++;
|
nebulae[1].frame++;
|
||||||
if (neb2Frame > 7)
|
if (nebulae[1].frame > 7)
|
||||||
{
|
{
|
||||||
neb2Frame = 0;
|
nebulae[1].frame = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; i++)
|
||||||
|
{
|
||||||
|
// Respawn nebula if it goes off screen
|
||||||
|
if (nebulae[i].pos.x <= -nebulae[i].rec.width)
|
||||||
|
{
|
||||||
|
nebulae[i].pos.x = windowArray[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// draw Nebula
|
// draw Nebula
|
||||||
DrawTextureRec(nebula, nebRect, nebulaPos, WHITE); // Draw the texture
|
DrawTextureRec(nebula, nebulae[0].rec, nebulae[0].pos, WHITE); // Draw the texture
|
||||||
// draw second Nebula
|
// draw second Nebula
|
||||||
DrawTextureRec(nebula, neb2Rect, neb2Pos, RED); // Draw the texture
|
DrawTextureRec(nebula, nebulae[1].rec, nebulae[1].pos, RED); // Draw the texture
|
||||||
|
|
||||||
// draw Scarfy
|
// draw Scarfy
|
||||||
DrawTextureRec(scarfy, scarfyRect, scarfyPos, WHITE); // Draw the texture
|
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE); // Draw the texture
|
||||||
|
|
||||||
// End Game Logic
|
// End Game Logic
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
|||||||
Reference in New Issue
Block a user