minor updates
add fruit class update simple sprites include variant type
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
// src\fruit.cpp
|
||||
//
|
||||
#include<raylib.h>
|
||||
#include<entt/entt.hpp>
|
||||
|
||||
#include<components.hpp>
|
||||
|
||||
entt::entity fruit;
|
||||
|
||||
|
||||
void create_fruit(entt::registry& registry,Texture* texture){
|
||||
const auto entity = registry.create();
|
||||
registry.emplace<Position>(entity,100.0f,0.0f);
|
||||
registry.emplace<Fruit>(entity);
|
||||
registry.emplace<Sprite>(entity,texture);
|
||||
}
|
||||
|
||||
void update_fruit_system(entt::registry& registry,float dt){
|
||||
|
||||
auto view = registry.view<const Fruit, const Position, Velocity>();
|
||||
|
||||
for (entt::entity entity : view){
|
||||
|
||||
const Position& pos = view.get<Position>(entity);
|
||||
Velocity& vel = view.get<Velocity>(entity);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
[Window][Debug##Default]
|
||||
Pos=60,60
|
||||
Size=400,400
|
||||
|
||||
[Window][VBoxContainer]
|
||||
Pos=540,275
|
||||
Size=200,250
|
||||
|
||||
@@ -41,7 +41,6 @@ class AssetLoader{
|
||||
std::unordered_map<std::string,Asset> assets;
|
||||
virtual void load_all_assets(std::string data_path) = 0;
|
||||
virtual void unload_all_assets() = 0;
|
||||
AssetReader get_reader();
|
||||
|
||||
public:
|
||||
AssetLoader(std::string asset_data);
|
||||
@@ -49,6 +48,7 @@ class AssetLoader{
|
||||
virtual void load_asset(std::string path) = 0;
|
||||
virtual void unload_asset(std::string path) = 0;
|
||||
virtual Asset get_asset(std::string path) = 0;;
|
||||
AssetReader get_reader();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <asset/asset.hpp>
|
||||
#include <raylib.h>
|
||||
|
||||
class RaylibAssetLoader : AssetLoader{
|
||||
class RaylibAssetLoader : public AssetLoader{
|
||||
protected:
|
||||
void load_all_assets(std::string asset_list) override {};
|
||||
void unload_all_assets() override;
|
||||
|
||||
@@ -13,6 +13,8 @@ struct Velocity{
|
||||
|
||||
struct Player{};
|
||||
|
||||
struct Fruit{};
|
||||
|
||||
struct Sprite{
|
||||
Texture *texture;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include<raylib.h>
|
||||
#include<entt/entt.hpp>
|
||||
|
||||
void create_fruit(entt::registry& registry,Texture* texture);
|
||||
|
||||
void update_fruit_system(entt::registry& registry,float dt);
|
||||
|
||||
+1
-1
@@ -79,7 +79,7 @@ int main(){
|
||||
SetTargetFPS(60);
|
||||
// SetTraceLogLevel(TraceLogLevel::LOG_NONE);
|
||||
|
||||
InitWindow(screen_width, screen_height, "Bullet bird");
|
||||
InitWindow(screen_width, screen_height, "Snake");
|
||||
SetWindowState(FLAG_WINDOW_RESIZABLE);
|
||||
|
||||
|
||||
|
||||
+34
-16
@@ -21,30 +21,48 @@ 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
|
||||
// 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 (pos.x < -50 && vel.dx < 0){
|
||||
vel.dx *= -1;
|
||||
} else if (pos.x > (GetScreenWidth()-250) && vel.dx > 0){
|
||||
vel.dx *= -1;
|
||||
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 (IsKeyPressed(KEY_SPACE)){
|
||||
vel.dy = JUMP_FORCE;
|
||||
}
|
||||
vel.dy += GRAVITY * dt;
|
||||
// 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;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,29 @@
|
||||
#include <imgui.h>
|
||||
#include <player.hpp>
|
||||
#include <movement.hpp>
|
||||
#include <scene.hpp>
|
||||
#include <render.hpp>
|
||||
#include <asset/raylib_asset.hpp>
|
||||
#include <fruit.hpp>
|
||||
|
||||
#include <asset/raylib_asset.hpp>
|
||||
#include <imgui.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <format>
|
||||
|
||||
GamePlayScene::GamePlayScene() {
|
||||
registry = entt::registry();
|
||||
|
||||
asset_loader.load_asset("assets/bird.png");
|
||||
create_player(registry,asset_loader.get_texture("assets/bird.png"));
|
||||
asset_loader.load_asset("assets/snake_head.png");
|
||||
asset_loader.load_asset("assets/fruit.png");
|
||||
create_player(registry,asset_loader.get_texture("assets/snake_head.png"));
|
||||
create_fruit(registry,asset_loader.get_texture("assets/fruit.png"));
|
||||
|
||||
}
|
||||
|
||||
void GamePlayScene::update(float dt) {
|
||||
update_movement_system(registry, dt);
|
||||
update_player_system(registry, dt);
|
||||
update_fruit_system(registry, dt);
|
||||
}
|
||||
|
||||
|
||||
@@ -49,5 +55,5 @@ void GamePlayScene::ui() {
|
||||
|
||||
GamePlayScene::~GamePlayScene(){
|
||||
asset_loader.unload_asset("assets/bird.png");
|
||||
|
||||
asset_loader.unload_asset("assets/snake_head.png");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user