second commit
This commit is contained in:
+50
-34
@@ -1,34 +1,50 @@
|
||||
// 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,0.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, 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
|
||||
|
||||
view.each([dt,GRAVITY,JUMP_FORCE](const auto &player, auto &velocity){
|
||||
if (IsKeyPressed(KEY_SPACE)){
|
||||
velocity.dy = JUMP_FORCE;
|
||||
}
|
||||
velocity.dy += GRAVITY * dt;
|
||||
});
|
||||
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user