body manager
This commit is contained in:
@@ -12,6 +12,9 @@ struct Velocity{
|
||||
};
|
||||
|
||||
struct Player{};
|
||||
struct PlayerBody{
|
||||
unsigned int part_idx;
|
||||
};
|
||||
|
||||
struct Fruit{};
|
||||
|
||||
|
||||
@@ -1,6 +1,24 @@
|
||||
#pragma once
|
||||
#include <components.hpp>
|
||||
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include<raylib.h>
|
||||
|
||||
void create_player(entt::registry& registry,Texture* texture);
|
||||
void create_player_body(entt::registry& registry,Texture* texture,unsigned int idx);
|
||||
void update_player_system(entt::registry& registry,float dt);
|
||||
|
||||
class BodyManager{
|
||||
private:
|
||||
std::vector<Position> parts_position_stack = {};
|
||||
public:
|
||||
Texture* body_texture;
|
||||
BodyManager(Texture* texture) : body_texture(texture){}
|
||||
|
||||
void add_body(entt::registry& registry);
|
||||
void pop_body();
|
||||
void update_body_positions(Position player_head_position);
|
||||
Position get_body_position(unsigned int part_idx);
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <functional>
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <memory>
|
||||
|
||||
enum class SceneType{
|
||||
MENU,
|
||||
@@ -51,7 +52,7 @@ class MenuScene : public Scene{
|
||||
|
||||
class SceneManager{
|
||||
private:
|
||||
Scene* current_scene = nullptr;
|
||||
std::unique_ptr<Scene> current_scene = nullptr;
|
||||
SceneType next_scene_type;
|
||||
bool pending_switch = false;
|
||||
bool should_quit = false;
|
||||
@@ -68,4 +69,5 @@ class SceneManager{
|
||||
bool should_game_close();
|
||||
void request_switch(SceneType type);
|
||||
void quit();
|
||||
Scene& get_current_scene();
|
||||
};
|
||||
|
||||
@@ -10,6 +10,8 @@ entt::entity player;
|
||||
|
||||
const float SPEED = 300.0f;
|
||||
|
||||
class BodyManager;
|
||||
|
||||
|
||||
void create_player(entt::registry& registry,Texture* texture){
|
||||
const auto entity = registry.create();
|
||||
@@ -17,6 +19,8 @@ void create_player(entt::registry& registry,Texture* texture){
|
||||
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){
|
||||
@@ -57,5 +61,44 @@ void update_player_system(entt::registry& registry,float dt){
|
||||
}
|
||||
}
|
||||
|
||||
auto body_view = registry.view<const PlayerBody, Position>();
|
||||
for (entt::entity entity : body_view){
|
||||
const PlayerBody& body = body_view.get<PlayerBody>(entity);
|
||||
Position& pos = body_view.get<Position>(entity);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void create_player_body(entt::registry& registry,Texture* texture,unsigned int idx){
|
||||
const auto entity = registry.create();
|
||||
registry.emplace<Position>(entity,100.0f,0.0f);
|
||||
registry.emplace<Velocity>(entity,SPEED,0.0f);
|
||||
registry.emplace<PlayerBody>(entity,idx);
|
||||
registry.emplace<Sprite>(entity,texture,0.0f,0.0f,70.0f,70.0f,false,false,0.0f);
|
||||
}
|
||||
|
||||
void BodyManager::add_body(entt::registry& registry)
|
||||
{
|
||||
create_player_body(registry,body_texture,parts_position_stack.size());
|
||||
parts_position_stack.push_back( Position{});
|
||||
}
|
||||
|
||||
void BodyManager::pop_body(){
|
||||
|
||||
parts_position_stack.pop_back();
|
||||
}
|
||||
|
||||
void BodyManager::update_body_positions(Position player_head_position){
|
||||
for ( auto i = parts_position_stack.size()-1; i > 0 ;i--){
|
||||
parts_position_stack[i] = parts_position_stack[i-1];
|
||||
}
|
||||
parts_position_stack[0] = player_head_position;
|
||||
}
|
||||
|
||||
Position BodyManager::get_body_position(unsigned int part_idx){
|
||||
return parts_position_stack[part_idx];
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
#include <scene.hpp>
|
||||
|
||||
SceneManager::SceneManager(){
|
||||
current_scene = new MenuScene();
|
||||
current_scene = std::make_unique<MenuScene>();
|
||||
}
|
||||
|
||||
SceneManager::~SceneManager(){
|
||||
delete current_scene;
|
||||
// delete current_scene;
|
||||
}
|
||||
|
||||
Scene& SceneManager::get_current_scene(){
|
||||
return *current_scene;
|
||||
}
|
||||
|
||||
void SceneManager::process_scene_switch(){
|
||||
@@ -14,15 +18,15 @@ void SceneManager::process_scene_switch(){
|
||||
if (!pending_switch) return;
|
||||
|
||||
if (current_scene){
|
||||
delete current_scene;
|
||||
current_scene.reset();
|
||||
}
|
||||
|
||||
switch(next_scene_type){
|
||||
case SceneType::MENU:
|
||||
current_scene = new MenuScene();
|
||||
current_scene = std::make_unique<MenuScene>();
|
||||
break;
|
||||
case SceneType::GAMEPLAY:
|
||||
current_scene = new GamePlayScene();
|
||||
current_scene = std::make_unique<GamePlayScene>();
|
||||
break;
|
||||
}
|
||||
pending_switch = false;
|
||||
|
||||
Reference in New Issue
Block a user