add fruit reaction

This commit is contained in:
2026-07-14 21:06:08 +01:00
parent ec7060668a
commit 0c3eb01521
10 changed files with 117 additions and 41 deletions
+3 -3
View File
@@ -1,16 +1,16 @@
#include <asset_packer/asset.hpp>
#include <asset/asset.hpp>
#include <asset/raylib_asset.hpp>
#include <raylib.h>
#include <asset_packer/asset.hpp>
#include <string>
#include <iostream>
#include <filesystem>
#include <variant>
namespace fs = std::filesystem;
// Helper to determine if the asset path implies streaming music
+31 -11
View File
@@ -1,30 +1,50 @@
// src\fruit.cpp
//
#include<raylib.h>
#include<entt/entt.hpp>
#include <fruit.hpp>
#include<components.hpp>
#include <components.hpp>
#include <cstdlib>
#include <math_util.hpp>
#include <engine_variant.hpp>
#include <raylib.h>
#include <entt/entt.hpp>
// #include <iostream>
entt::entity fruit;
// File Base Helper Function
Position get_player_position(entt::registry& registry){
auto player_view = registry.view<const Player>();
if (player_view.empty()) return {};
entt::entity player_entity = player_view.front();
return registry.get<Position>(player_entity);
}
void change_position(Position& pos){
pos.x = Math::RandomRange(30, GetScreenWidth()-30);
pos.y = Math::RandomRange(30, GetScreenHeight()-30);
};
void create_fruit(entt::registry& registry,Texture* texture){
const auto entity = registry.create();
registry.emplace<Position>(entity,100.0f,0.0f);
registry.emplace<Fruit>(entity);
registry.emplace<Sprite>(entity,texture);
registry.emplace<Sprite>(entity,texture,0.0f,0.0f,30.f,30.0f,false,false,0.0f);
}
void update_fruit_system(entt::registry& registry,float dt){
auto view = registry.view<const Fruit, const Position, Velocity>();
auto view = registry.view<const Fruit, Position>();
const Position player_pos = get_player_position(registry);
for (entt::entity entity : view){
const Position& pos = view.get<Position>(entity);
Velocity& vel = view.get<Velocity>(entity);
Position& pos = view.get<Position>(entity);
if (abs(pos.x - player_pos.x) < 35 && abs(pos.y - player_pos.y) < 35 ){
change_position(pos);
}
}
}
+7
View File
@@ -17,4 +17,11 @@ struct Fruit{};
struct Sprite{
Texture *texture;
float offset_x;
float offset_y;
float width;
float height;
bool flip_h;
bool flip_v;
float rotation;
};
+3
View File
@@ -0,0 +1,3 @@
#pragma once
class ServiceLocator{};
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <random>
namespace Math {
// Generate a random float between min and max
inline float RandomRange(float min, float max) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_real_distribution<float> dist(min, max);
return dist(gen);
}
// Generate a random int between min and max
inline int RandomRange(int min, int max) {
static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(min, max);
return dist(gen);
}
}
+3
View File
@@ -0,0 +1,3 @@
#include <locator.hpp>
// Will contain definition of service locator class
+2
View File
@@ -1,4 +1,6 @@
// src\movement.cpp
#include <movement.hpp>
#include <entt/entt.hpp>
#include <components.hpp>
+12 -19
View File
@@ -1,4 +1,6 @@
// src\player.cpp
#include <player.hpp>
#include<raylib.h>
#include<entt/entt.hpp>
@@ -6,28 +8,24 @@
entt::entity player;
const float SPEED = 300.0f;
void create_player(entt::registry& registry,Texture* texture){
const auto entity = registry.create();
registry.emplace<Position>(entity,100.0f,0.0f);
registry.emplace<Velocity>(entity,300.0f,0.0f);
registry.emplace<Velocity>(entity,SPEED,0.0f);
registry.emplace<Player>(entity);
registry.emplace<Sprite>(entity,texture);
registry.emplace<Sprite>(entity,texture,0.0f,0.0f,70.0f,70.0f,false,false,0.0f);
}
void update_player_system(entt::registry& registry,float dt){
auto view = registry.view<const Player, const Position, Velocity>();
// Constants for fine-tuning game feel
// const float GRAVITY = 900.0f; // Constant downward pull (pixels/sec^2)
// const float JUMP_FORCE = -350.0f; // Negative force to burst UPWARD
auto view = registry.view<const Player, const Position, Velocity,Sprite>();
for (entt::entity entity : view){
const Position& pos = view.get<Position>(entity);
Velocity& vel = view.get<Velocity>(entity);
const float SPEED = 350;
Sprite& sprite = view.get<Sprite>(entity);
if (IsKeyPressed(KEY_W)){
if (vel.dy > 0)
@@ -52,16 +50,11 @@ void update_player_system(entt::registry& registry,float dt){
vel.dy = 0;
}
// if (pos.x < -50 && vel.dx < 0){
// vel.dx *= -1;
// } else if (pos.x > (GetScreenWidth()-250) && vel.dx > 0){
// vel.dx *= -1;
// }
// if (IsKeyPressed(KEY_SPACE)){
// vel.dy = JUMP_FORCE;
// }
// vel.dy += GRAVITY * dt;
if (vel.dx != 0.0f || vel.dy != 0.0f) {
// atan2 returns radians. Convert to degrees if your renderer expects it!
float radians = std::atan2(vel.dy, vel.dx);
sprite.rotation = radians * (180.0f / 3.14159265f);
}
}
}
+34 -7
View File
@@ -8,11 +8,38 @@
void update_render_system(entt::registry &registry){
auto view = registry.view<const Sprite, const Position>();
view.each([] (const auto &sprite, const auto &position){
if (sprite.texture){
DrawTexture(*(sprite.texture),position.x,position.y,WHITE);
}else{
DrawRectangle(position.x,position.y,50, 70,BLACK);
}
});
for (entt::entity entity : view){
const Sprite &sprite = view.get<Sprite>(entity);
const Position &pos = view.get<Position>(entity);
if (sprite.texture){
DrawTexturePro(
*(sprite.texture),
Rectangle{
0,
0,
(float)sprite.texture->width,
(float)sprite.texture->height,
},
Rectangle {
sprite.offset_x + pos.x,
sprite.offset_y + pos.y,
sprite.width,
sprite.height
},
Vector2 {sprite.width * 0.5f, sprite.height*0.5f},
sprite.rotation,
WHITE);
// DrawTexture(
// *(sprite.texture),
// pos.x + sprite.offset_x,
// pos.y + sprite.offset_y,
// WHITE
// );
}else{
DrawRectangle(pos.x,pos.y,100, 100,PURPLE);
}
}
}
+1 -1
View File
@@ -54,6 +54,6 @@ void GamePlayScene::ui() {
}
GamePlayScene::~GamePlayScene(){
asset_loader.unload_asset("assets/bird.png");
asset_loader.unload_asset("assets/snake_head.png");
asset_loader.unload_asset("assets/fruit.png");
}