Files
snake-raylib-prototype/src/player.cpp
T
2026-07-09 12:30:40 +01:00

51 lines
1.4 KiB
C++

// src\player.cpp
#include<raylib.h>
#include<entt/entt.hpp>
#include<components.hpp>
entt::entity player;
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<Player>(entity);
registry.emplace<Sprite>(entity,texture);
}
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
for (entt::entity entity : view){
const Position& pos = view.get<Position>(entity);
Velocity& vel = view.get<Velocity>(entity);
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;
}
view.each([dt,GRAVITY,JUMP_FORCE](entt::entity entity,const Position &position, auto &velocity){
if (position.x > 100 && position.x < 120){
velocity.dx += 20;
}
});
}