Files
snake-raylib-prototype/src/player.cpp
T
shadesandgrays ec7060668a minor updates
add fruit class

update simple sprites

include variant type
2026-07-14 13:45:07 +01:00

69 lines
1.9 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);
const float SPEED = 350;
if (IsKeyPressed(KEY_W)){
if (vel.dy > 0)
continue;
vel.dx = 0;
vel.dy = -SPEED;
} else if (IsKeyPressed(KEY_S)){
if (vel.dy < 0)
continue;
vel.dx = 0;
vel.dy = SPEED;
} else if (IsKeyPressed(KEY_A)){
if (vel.dx > 0)
continue;
vel.dx = -SPEED;
vel.dy = 0;
} else if (IsKeyPressed(KEY_D)){
if (vel.dx < 0)
continue;
vel.dx = SPEED;
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;
}
}