Files
snake-raylib-prototype/src/player.cpp
T
2026-07-14 21:06:08 +01:00

62 lines
1.7 KiB
C++

// src\player.cpp
#include <player.hpp>
#include<raylib.h>
#include<entt/entt.hpp>
#include<components.hpp>
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,SPEED,0.0f);
registry.emplace<Player>(entity);
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,Sprite>();
for (entt::entity entity : view){
const Position& pos = view.get<Position>(entity);
Velocity& vel = view.get<Velocity>(entity);
Sprite& sprite = view.get<Sprite>(entity);
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 (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);
}
}
}