From 8feb32ae75ecf114dc0d18551ba218d9f66f406e Mon Sep 17 00:00:00 2001 From: ShadesAndGrays Date: Mon, 20 Jul 2026 20:28:00 +0100 Subject: [PATCH] final cpommit --- .clang-format | 4 ++ .gitignore | 2 + src/game/ecs/components.hpp | 2 +- src/game/main.cpp | 4 ++ src/game/scenes/game-over-scene/game-over.cpp | 61 +++++++++++++++++++ src/game/scenes/game-over-scene/game-over.hpp | 13 ++++ src/game/scenes/main-scene/body.cpp | 49 +++++++++++---- src/game/scenes/main-scene/body.hpp | 5 +- src/game/scenes/main-scene/main-scene.cpp | 5 ++ src/game/scenes/main-scene/player.cpp | 18 +++++- 10 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 .clang-format create mode 100644 src/game/scenes/game-over-scene/game-over.cpp create mode 100644 src/game/scenes/game-over-scene/game-over.hpp diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..960bc8c --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Never diff --git a/.gitignore b/.gitignore index c67454b..85a7660 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ assets/ bin/ +web/ +web* debug/ external/ compile_commands.json diff --git a/src/game/ecs/components.hpp b/src/game/ecs/components.hpp index 995b830..cf02e64 100644 --- a/src/game/ecs/components.hpp +++ b/src/game/ecs/components.hpp @@ -1,7 +1,7 @@ #pragma once struct Player{}; -struct PlayerBody{ unsigned int idx;}; +struct PlayerBody{}; struct Fruit{}; diff --git a/src/game/main.cpp b/src/game/main.cpp index a23bad1..15d4729 100644 --- a/src/game/main.cpp +++ b/src/game/main.cpp @@ -3,6 +3,7 @@ #include "engine/core/service-locator.hpp" #include "engine/scene/scene.hpp" +#include "game/scenes/game-over-scene/game-over.hpp" #include "game/scenes/main-scene/main-scene.hpp" #include "game/scenes/menu-scene/menu.hpp" @@ -19,6 +20,9 @@ class Game : public varicle::Application{ scene_manager.register_scene("menu", []() { return std::make_unique(); }); + scene_manager.register_scene("game-over", []() { return std::make_unique(); }); + + change_scene("menu"); } diff --git a/src/game/scenes/game-over-scene/game-over.cpp b/src/game/scenes/game-over-scene/game-over.cpp new file mode 100644 index 0000000..fec3924 --- /dev/null +++ b/src/game/scenes/game-over-scene/game-over.cpp @@ -0,0 +1,61 @@ +#include "game-over.hpp" + +#include "engine/core/service-locator.hpp" +#include "raylib.h" + +#include + +void GameOverScene::init() {} + +void GameOverScene::update(float dt) { +} + +void GameOverScene::render() { + ClearBackground(BLACK); + DrawText("GAME OVER",400,0,48,RED); +} + +void GameOverScene::ui() { + + // Get the current screen/viewport size + ImVec2 screen_size = ImGui::GetIO().DisplaySize; + + // size of button container + ImVec2 vbox_size = ImVec2(200.0f, 250.0f); + + // set next container to be center of the screen + ImVec2 screen_center = ImVec2(screen_size.x * 0.5f, screen_size.y * 0.5f); + ImGui::SetNextWindowPos(screen_center, ImGuiCond_Always,ImVec2(0.5f, 0.5f)); + ImGui::SetNextWindowSize(vbox_size); + + // remove sub window decoration + ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoBackground; + + ImGui::Begin("VBoxContainer", nullptr, flags); + + // set button width to be size fill container + ImVec2 button_size = ImVec2(ImGui::GetContentRegionAvail().x, 50.0f); + + // ImGui::SetCursorPos(centeredPos); + if (ImGui::Button("Play Again",button_size)){ + v::ServiceLocator::get().switch_to_scene("main"); + } + + ImGui::Dummy(ImVec2(0, 10)); // Gap + + if (ImGui::Button("Menu",button_size)){ + v::ServiceLocator::get().switch_to_scene("menu"); + } + + ImGui::Dummy(ImVec2(0, 10)); // Gap + if (ImGui::Button("Quit",button_size)){ + v::ServiceLocator::get().quit(); + } + + ImGui::End(); + + // ImGui::Begin("Debug"); + // ImGui::Button("Hello",button_size); + // ImGui::TextUnformatted(std::format("window size: {} {} ", screen_size.x,screen_size.y).c_str()); + // ImGui::End(); +} diff --git a/src/game/scenes/game-over-scene/game-over.hpp b/src/game/scenes/game-over-scene/game-over.hpp new file mode 100644 index 0000000..5167d8e --- /dev/null +++ b/src/game/scenes/game-over-scene/game-over.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "engine/scene/scene.hpp" + +namespace v = varicle; + +class GameOverScene : public v::Scene{ + public: + void init() override; + void update(float dt) override; + void render() override; + void ui() override ; +}; diff --git a/src/game/scenes/main-scene/body.cpp b/src/game/scenes/main-scene/body.cpp index 75b9f6a..be3a1bc 100644 --- a/src/game/scenes/main-scene/body.cpp +++ b/src/game/scenes/main-scene/body.cpp @@ -12,18 +12,22 @@ namespace v = varicle; -void create_body(entt::registry& registry,unsigned int idx){ +entt::entity create_body(entt::registry& registry,unsigned int idx){ auto& asset_loader = v::ServiceLocator::get(); + auto& body_manager = v::ServiceLocator::get(); auto body_texture = asset_loader.get_texture("assets/snake_body.png"); const auto entity = registry.create(); registry.emplace(entity,-10000.0f,-10000.0f); - registry.emplace(entity,idx); - registry.emplace(entity,body_texture,0.0f,0.0f,70.0f,70.0f,false,false,0.0f); + // registry.emplace(entity,100.0f,100.0f); + registry.emplace(entity); + registry.emplace_or_replace(entity,body_texture,0.0f,0.0f,70.0f,70.0f,false,false,0.0f); + return entity; } void BodyManager::add_body(entt::registry& registry) { - create_body(registry,parts_position_stack.size()); + auto body_entity =create_body(registry,parts_position_stack.size()); + body_parts.push_back(body_entity); parts_position_stack.push_back( v::Position{}); } @@ -46,22 +50,41 @@ void BodyManager::update_body_positions(v::Position player_head_position){ v::Position BodyManager::get_body_position(unsigned int part_idx){ return parts_position_stack[part_idx]; } +bool BodyManager::dectect_collision(entt::registry& registry) { + auto player_view = registry.view(); + float radius = 50; + auto player_entity = player_view.front(); + if (player_entity == entt::null) return false; + auto player_head_position = player_view.get(player_entity); + for (auto body_part_position : parts_position_stack) { + float x = abs(player_head_position.x - body_part_position.x); + float y = abs(player_head_position.y - body_part_position.y); + float distance = x*x + y*y; + if (distance < (radius * radius)) { + return true; + } + } + return false; +} + +void BodyManager::update_body_positions(entt::registry& registry) { + for (int i = 0; i < body_parts.size() ;i++) { + auto body_part = body_parts[i]; + auto& body_pos = registry.get(body_part); + auto new_pos = parts_position_stack[i]; + body_pos.x = new_pos.x; + body_pos.y = new_pos.y; + } + +} void update_body_system(entt::registry& registry){ auto& body_manager = v::ServiceLocator::get(); auto player_view = registry.view(); auto player_entity = player_view.front(); if (player_entity == entt::null) return; - body_manager.update_body_positions(player_view.get(player_entity)); - - auto body_view = registry.view(); - - body_view.each([&body_manager](const auto entity, const PlayerBody& pb, v::Position& pos){ - auto new_pos = body_manager.get_body_position(pb.idx); - pos.x = new_pos.x; - pos.y = new_pos.y; - }); + body_manager.update_body_positions(registry); } diff --git a/src/game/scenes/main-scene/body.hpp b/src/game/scenes/main-scene/body.hpp index 8299e26..8af8276 100644 --- a/src/game/scenes/main-scene/body.hpp +++ b/src/game/scenes/main-scene/body.hpp @@ -11,6 +11,7 @@ namespace v = varicle; class BodyManager{ private: std::vector parts_position_stack = {}; + std::vector body_parts = {}; public: Texture* body_texture; BodyManager() = default; @@ -19,8 +20,10 @@ class BodyManager{ void add_body(entt::registry& registry); void pop_body(); void update_body_positions(v::Position player_head_position); + void update_body_positions(entt::registry& registry); + bool dectect_collision(entt::registry& registry); v::Position get_body_position(unsigned int part_idx); }; -void create_body(entt::registry& registry,unsigned int idx); +entt::entity create_body(entt::registry& registry,unsigned int idx); void update_body_system(entt::registry& registry); diff --git a/src/game/scenes/main-scene/main-scene.cpp b/src/game/scenes/main-scene/main-scene.cpp index 88cc6a1..3973bf3 100644 --- a/src/game/scenes/main-scene/main-scene.cpp +++ b/src/game/scenes/main-scene/main-scene.cpp @@ -43,6 +43,11 @@ void MainScene::update(float dt) { tick_timer -= TICK_INTERVAL; update_body_system(registry); update_movement_system(registry,dt); + auto bm = v::ServiceLocator::get(); + if (bm.dectect_collision(registry)) { + v::ServiceLocator::get().switch_to_scene("game-over"); + } + } } diff --git a/src/game/scenes/main-scene/player.cpp b/src/game/scenes/main-scene/player.cpp index 6e50a3c..46438d5 100644 --- a/src/game/scenes/main-scene/player.cpp +++ b/src/game/scenes/main-scene/player.cpp @@ -3,6 +3,7 @@ #include "engine/core/service-locator.hpp" #include "engine/asset/raylib-asset.hpp" #include "game/ecs/components.hpp" +#include "raylib.h" #include @@ -27,10 +28,10 @@ void create_player(entt::registry ®istry){ void update_player_system(entt::registry& registry,float dt){ using namespace varicle; - auto view = registry.view(); + auto view = registry.view(); // for (entt::entity entity : view){ - view.each([](const auto entity, const Position& pos, Velocity& vel, Sprite& sprite){ + view.each([](const auto entity, Position& pos, Velocity& vel, Sprite& sprite){ if (IsKeyPressed(KEY_W) && vel.dy <= 0.0f){ @@ -52,6 +53,19 @@ void update_player_system(entt::registry& registry,float dt){ float radians = std::atan2(vel.dy, vel.dx); sprite.rotation = radians * (180.0f / 3.14159265f); } + + if (pos.x > GetScreenWidth()) { + pos.x = 45; + } else if (pos.x < 0) { + pos.x = GetScreenWidth() - 45; + } + + if (pos.y > GetScreenHeight()) { + pos.y = 45; + } else if (pos.y < 0) { + pos.y = GetScreenHeight() - 45; + } + }); }