Files
cpp-dapper-dasher/dasher.cpp
2026-02-02 05:31:38 -05:00

240 lines
6.5 KiB
C++

#include <raylib.h>
struct AnimData
{
Rectangle rec;
Vector2 pos;
int frame;
float updateTime;
float runningTime;
};
int main() {
/* // Specified width and height
const int windowWidth{800};
const int windowHeight{600};
*/
// windows array dimensions
float windowArray[2] = { 800.0, 600.0 };
// Initialize the window
InitWindow(windowArray[0], windowArray[1], "ECS Dapper Dasher");
// Acceleration due to gravity (pixels per frame/frame)
const int gravity{ 1'000 };
// Scarfy setup
Texture2D scarfy = LoadTexture("textures/scarfy.png"); // Load the texture
AnimData scarfyData;
scarfyData.rec.width = scarfy.width / 6;
scarfyData.rec.height = scarfy.height;
scarfyData.rec.x = 0;
scarfyData.rec.y = 0;
scarfyData.pos.x = windowArray[0] / 2 - scarfyData.rec.width / 2;
scarfyData.pos.y = windowArray[1] - scarfyData.rec.height;
scarfyData.frame = 0;
scarfyData.updateTime = 1.0 / 12.0;
scarfyData.runningTime = 0.0;
// Nebula setup
Texture2D nebula = LoadTexture("textures/12_nebula_spritesheet.png"); // Load the texture
// Nebula anim
/*AnimData nebData{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowArray[0], windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 12.0, // float updateTime
0.0 // float runningTime
};*/
/*AnimData neb2Data{
{0.0, 0.0, nebula.width / 8, nebula.height / 8}, // Rectangle rec
{windowArray[0] + 300, windowArray[1] - nebula.height / 8}, // Vector2 pos
0, // int frame
1.0 / 16.0, // float updateTime
0.0 // float runningTime
};*/
const int sizeofNebulae{6};
// Nebulea
AnimData nebulae[sizeofNebulae]{};
for (int i = 0; i < sizeofNebulae; i++)
{
nebulae[i].rec.x = 0.0;
nebulae[i].rec.y = 0.0;
nebulae[i].rec.width = nebula.width / 8;
nebulae[i].rec.height = nebula.height / 8;
nebulae[i].pos.y = windowArray[1] - nebulae[i].rec.height;
nebulae[i].frame = 0;
nebulae[i].runningTime = 0.0;
nebulae[i].updateTime = 1.0 / 16.0;
nebulae[i].pos.x = windowArray[0] + i * 300;
}
/* Neblua Positions
nebulae[0].pos.x = windowArray[0];
nebulae[1].pos.x = windowArray[0] + 300;
nebulae[2].pos.x = windowArray[0] + 600;
nebulae[3].pos.x = windowArray[0] + 900;
nebulae[4].pos.x = windowArray[0] + 1200;
nebulae[5].pos.x = windowArray[0] + 1500;
*/
int nebVel{-200}; // Nebula x velocity (pixels per second)
// Animation Frame
int frame{};
const float updateTime{1.0 / 12.0}; // Time between frames
float runningTime{}; // Time since last frame change
// Is the rectangle in the air
bool isInAir{};
int velocity{}; // Current velocity
// Jump velocity (pixels per second)
const int jumpVelocity{-600};
// Main game loop
SetTargetFPS(60);
while (!WindowShouldClose())
{
// Delta time
const float dT = GetFrameTime(); // Delta time (time since last frame)
// Start Game Logic
BeginDrawing();
ClearBackground(WHITE);
// perform ground check
if (scarfyData.pos.y >= windowArray[1] - scarfyData.rec.height) {
velocity = 0; // Reset velocity when on the ground
isInAir = false;
}
else
{
velocity += gravity * dT; // Apply gravity when in the air
isInAir = true;
}
// Jump logic / jump check
if (IsKeyPressed(KEY_SPACE) && !isInAir) {
velocity += jumpVelocity; // Jump velocity
}
/*// Nebula movement
nebulae[0].pos.x += nebVel * dT;
// Nebula 2 movement
nebulae[1].pos.x += nebVel * dT;
*/
// Nebula movement loop
for (int i = 0; i < sizeofNebulae; i++)
{
// Nebula update position
nebulae[i].pos.x += nebVel * dT;
}
// Update position
scarfyData.pos.y += velocity * dT;
// Scarfy Animation Logic
if (!isInAir)
{
// Update running time
runningTime += dT;
if (runningTime >= updateTime)
{
runningTime = 0.0;
// Update animation frame
scarfyData.rec.x = frame * scarfyData.rec.width;
frame++;
if (frame > 5)
{
frame = 0;
}
}
}
for (int i = 0; i < sizeofNebulae; i++)
{
// Update running time
nebulae[i].runningTime += dT;
if (nebulae[i].runningTime >= (nebulae[i].updateTime))
{
nebulae[i].runningTime = 0.0;
nebulae[i].rec.x = nebulae[i].frame * nebulae[i].rec.width;
nebulae[i].frame++;
if (nebulae[i].frame > 7)
{
nebulae[i].frame = 0;
}
}
}
/* // Nebula animation frame
nebulae[0].runningTime += dT;
if (nebulae[0].runningTime >= (nebulae[0].updateTime))
{
nebulae[0].runningTime = 0.0;
nebulae[0].rec.x = nebulae[0].frame * nebulae[0].rec.width;
nebulae[0].frame++;
if (nebulae[0].frame > 7)
{
nebulae[0].frame = 0;
}
}
// Nebula animation frame
nebulae[1].runningTime += dT;
if (nebulae[1].runningTime >= (nebulae[1].updateTime))
{
nebulae[1].runningTime = 0.0;
nebulae[1].rec.x = nebulae[1].frame * nebulae[1].rec.width;
nebulae[1].frame++;
if (nebulae[1].frame > 7)
{
nebulae[1].frame = 0;
}
}
*/
// Nebula Animation Frame and Respawn Logic
for (int i = 0; i < sizeofNebulae; i++)
{
nebulae[i].pos.x += nebVel * dT;
}
for (int i = 0; i < sizeofNebulae; i++)
{
// draw nebula
DrawTextureRec(nebula, nebulae[i].rec, nebulae[i].pos, WHITE);
}
// draw Scarfy
DrawTextureRec(scarfy, scarfyData.rec, scarfyData.pos, WHITE); // Draw the texture
// End Game Logic
EndDrawing();
}
// Unload texture
UnloadTexture(scarfy);
UnloadTexture(nebula);
// Close the window properly
CloseWindow();
}