Skip to content

Commit

Permalink
Merge branch 'pre' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
mcpeapsUnterstrichHD committed Nov 28, 2024
2 parents 98ca6b5 + 40ab479 commit 90ffaf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 140 deletions.
12 changes: 5 additions & 7 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,16 @@ all:

# Project target defined by PROJECT_NAME
$(PROJECT_NAME): $(OBJS)
@echo "Building raylib..."
$(MAKE) -C $(RAYLIB_LIB_PATH) RAYLIB_LIBTYPE=SHARED RAYLIB_RELEASE_PATH=$(PROJECT_BUILD_PATH) PLATFORM=$(PLATFORM)
@echo "Building $(PROJECT_NAME)..."
$(CC) -o $(PROJECT_BUILD_PATH)/$(PROJECT_NAME)$(EXT) $(OBJS) $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
ifeq ($(PLATFORM_OS),OSX)
@echo "Setting up runtime path for macOS..."
install_name_tool -add_rpath "$(PROJECT_BUILD_PATH)" $(PROJECT_BUILD_PATH)/$(PROJECT_NAME)
endif

# Compile source files
# NOTE: This pattern will compile every module defined on $(OBJS)
%.o: %.c
$(CC) -c $< -o $@ $(CFLAGS) $(INCLUDE_PATHS) -D$(PLATFORM)
ifeq ($(PLATFORM_OS),OSX)
@echo "Setting up runtime path for macOS..."
install_name_tool -add_rpath "@executable_path/$(RAYLIB_LIB_PATH)" $(PROJECT_BUILD_PATH)/$(PROJECT_NAME)
endif

# Clean everything
clean:
Expand Down
149 changes: 16 additions & 133 deletions src/jumping-runner.cpp
Original file line number Diff line number Diff line change
@@ -1,141 +1,24 @@
#include "raylib.h"
#include <raylib.h>
#include <raymath.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include "nord-color.hpp"
#include <vector>

#define SCREEN_WIDTH 1920
#define SCREEN_HEIGHT 1080
#define DINO_WIDTH_PERCENT 0.05f
#define OBSTACLE_WIDTH_PERCENT 0.03f
#define GROUND_HEIGHT_PERCENT 0.09f
#define SREEN_WIDTH 1920
#define SREEN_HEIGHT 1080
#define TARGET_FPS 60

struct Dino {
Rectangle rect;
float velocity;
bool isJumping;
};

struct Obstacle {
Rectangle rect;
bool active;
};

struct GameState {
float scrollingSpeed;
float obstacleSpawnTimer;
int score;
bool gameOver;
std::vector<Obstacle> obstacles;
Dino dino;
};

int main(void) {
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Jumping Runner");
SetWindowState(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI |
FLAG_MSAA_4X_HINT | FLAG_INTERLACED_HINT);

Font gameFont = LoadFont("../assets/fonts/CaskaydiaCoveNerdFontPropo-Regular.ttf");
SetTargetFPS(60);

GameState game;

auto resetDinoPosition = [&]() {
float groundHeight = GetScreenHeight() * GROUND_HEIGHT_PERCENT;
game.dino.rect.y = GetScreenHeight() - groundHeight - game.dino.rect.height;
};

auto resetGame = [&]() {
game.scrollingSpeed = GetScreenWidth() * 0.4f;
game.obstacleSpawnTimer = 0;
game.score = 0;
game.gameOver = false;
game.obstacles.clear();

float dinoWidth = GetScreenWidth() * DINO_WIDTH_PERCENT;
float dinoHeight = dinoWidth;

game.dino = {
{dinoWidth, 0, dinoWidth, dinoHeight},
0,
false
};
resetDinoPosition();
};

resetGame();

while (!WindowShouldClose()) {
if (IsWindowResized() && !game.dino.isJumping) {
float dinoWidth = GetScreenWidth() * DINO_WIDTH_PERCENT;
game.dino.rect.width = dinoWidth;
game.dino.rect.height = dinoWidth;
resetDinoPosition();
game.scrollingSpeed = GetScreenWidth() * 0.4f;
}

if (game.gameOver && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
resetGame();
}

if (!game.gameOver) {
float jumpVelocity = -GetScreenHeight() * 1.1f;
float gravity = GetScreenHeight() * 2.2f;

if ((IsKeyPressed(KEY_SPACE) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) &&
!game.dino.isJumping) {
game.dino.velocity = jumpVelocity;
game.dino.isJumping = true;
}

if (game.dino.isJumping) {
game.dino.velocity += gravity * GetFrameTime();
game.dino.rect.y += game.dino.velocity * GetFrameTime();

float groundHeight = GetScreenHeight() * GROUND_HEIGHT_PERCENT;
float groundLevel = GetScreenHeight() - groundHeight - game.dino.rect.height;

if (game.dino.rect.y > groundLevel) {
game.dino.rect.y = groundLevel;
game.dino.velocity = 0;
game.dino.isJumping = false;
}
}

game.obstacleSpawnTimer += GetFrameTime();
if (game.obstacleSpawnTimer > 2.0f) {
float obstacleWidth = GetScreenWidth() * OBSTACLE_WIDTH_PERCENT;
float obstacleHeight = game.dino.rect.height;
float groundHeight = GetScreenHeight() * GROUND_HEIGHT_PERCENT;

game.obstacles.push_back({
{
static_cast<float>(GetScreenWidth()),
GetScreenHeight() - groundHeight - obstacleHeight,
obstacleWidth,
obstacleHeight
},
true
});
game.obstacleSpawnTimer = 0;
}

for (auto& obs : game.obstacles) {
if (obs.active) {
obs.rect.x -= game.scrollingSpeed * GetFrameTime();
if (CheckCollisionRecs(game.dino.rect, obs.rect)) {
game.gameOver = true;
}
}
}

game.obstacles.erase(
std::remove_if(game.obstacles.begin(), game.obstacles.end(),
[](const Obstacle& obs) { return obs.rect.x < -obs.rect.width; }),
game.obstacles.end()
);

game.score++;
}
int main(void)
{
InitWindow(SREEN_WIDTH, SREEN_HEIGHT, "Jumping Runner");
SetTargetFPS(TARGET_FPS);

while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(GetColor(NORD_BACKGROUND));

Expand Down

0 comments on commit 90ffaf6

Please sign in to comment.