From 9c23e5e28b4b9dba7bca975d67aca68f427b00e6 Mon Sep 17 00:00:00 2001 From: Ricardo Antunes Date: Thu, 4 Mar 2021 13:31:34 +0000 Subject: [PATCH] Added grass island, trees, fixed component removal error --- data/assets.cfg | 12 ++++--- data/model/{exit.qb => sign.qb} | Bin data/scene/entry.scn | 2 +- data/scene/exit.scn | 58 ++++++++++++++++++++++++++++++ data/scene/grass_1_16.scn | 27 ++++++++++++++ data/scene/main.scn | 9 ++--- data/scene/player.scn | 2 +- src/game/manager.cpp | 14 ++++++++ src/game/manager.hpp | 1 + src/game/map_controller.cpp | 61 ++++++++++++++++++++++++++++---- src/game/map_controller.hpp | 13 +++++-- src/game/player_controller.cpp | 15 ++++---- src/game/player_instance.cpp | 2 +- src/vpg/ecs/component_array.hpp | 17 +++++---- src/vpg/ecs/transform.hpp | 2 ++ vpg.cfg | 4 +-- 16 files changed, 204 insertions(+), 35 deletions(-) rename data/model/{exit.qb => sign.qb} (100%) create mode 100644 data/scene/exit.scn create mode 100644 data/scene/grass_1_16.scn diff --git a/data/assets.cfg b/data/assets.cfg index dfd59b2..7117e5d 100644 --- a/data/assets.cfg +++ b/data/assets.cfg @@ -11,15 +11,15 @@ font.inconsolata = static font font/Inconsolata.otf scene.main = static text scene/main.scn scene.player = static text scene/player.scn scene.entry = static text scene/entry.scn +scene.exit = static text scene/exit.scn scene.platform = static text scene/platform.scn +scene.grass.1.16 = static text scene/grass_1_16.scn shader.gbuffer = static shader shader/screen_quad.vert shader/gbuffer.frag shader.ssao = static shader shader/screen_quad.vert shader/ssao.frag shader.ssao_blur = static shader shader/screen_quad.vert shader/ssao_blur.frag shader.model = static shader shader/model.vert shader/model.frag -model.floor = dynamic model model/floor.qb - ; Player model model.player.torso = static model model/player/torso.qb model.player.lfoot = static model model/player/lfoot.qb @@ -28,6 +28,10 @@ model.player.lhand = static model model/player/lhand.qb model.player.rhand = static model model/player/rhand.qb ; Scenery -model.exit = static model model/exit.qb +model.sign = static model model/sign.qb model.base.8 = static model model/base_8.qb -model.base.16 = static model model/base_16.qb \ No newline at end of file +model.base.16 = static model model/base_16.qb +model.base.end = static model model/end_base.qb +model.grass.16 = static model model/grass_16.qb +model.grass.32 = static model model/grass_32.qb +model.tree.1 = static model model/tree_1.qb \ No newline at end of file diff --git a/data/model/exit.qb b/data/model/sign.qb similarity index 100% rename from data/model/exit.qb rename to data/model/sign.qb diff --git a/data/scene/entry.scn b/data/scene/entry.scn index 964c65c..37f5f94 100644 --- a/data/scene/entry.scn +++ b/data/scene/entry.scn @@ -20,4 +20,4 @@ $Base # Parent -30.0 10.0 0.0 # Position 0.5 0.5 0.5 # Scale 0.0 0.0 0.0 1.0 # Rotation -"Renderable" "Model" "model.exit" \ No newline at end of file +"Renderable" "Model" "model.sign" \ No newline at end of file diff --git a/data/scene/exit.scn b/data/scene/exit.scn new file mode 100644 index 0000000..6f2d31f --- /dev/null +++ b/data/scene/exit.scn @@ -0,0 +1,58 @@ +# Exit island +5 + +$Root +2 +"Transform" +$Null # Parent +0.0 0.0 0.0 # Position +1.0 1.0 1.0 # Scale +0.0 0.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-16.0 +4.0 -16.0 # Min ++16.0 +8.0 -8.0 # Max + +$Base +3 +"Transform" +$Root # Parent +0.0 13.0 0.0 # Position +2.0 2.0 2.0 # Scale +0.0 1.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-16.0 -17.0 -16.0 # Min ++16.0 -9.0 +16.0 # Max +"Renderable" "Model" "model.base.end" + +$ArchCollider1 +2 +"Transform" +$Root # Parent +0.0 0.0 0.0 # Position +1.0 1.0 1.0 # Scale +0.0 0.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-16.0 +4.0 -4.0 # Min +-14.0 +28.0 +4.0 # Max + +$ArchCollider2 +2 +"Transform" +$Root # Parent +0.0 0.0 0.0 # Position +1.0 1.0 1.0 # Scale +0.0 0.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" ++14.0 +4.0 -4.0 # Min ++16.0 +28.0 +4.0 # Max + +$ArchCollider3 +2 +"Transform" +$Root # Parent +0.0 0.0 0.0 # Position +1.0 1.0 1.0 # Scale +0.0 0.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-14.0 +28.0 -4.0 # Min ++14.0 +30.0 +4.0 # Max \ No newline at end of file diff --git a/data/scene/grass_1_16.scn b/data/scene/grass_1_16.scn new file mode 100644 index 0000000..2973dc3 --- /dev/null +++ b/data/scene/grass_1_16.scn @@ -0,0 +1,27 @@ +# Island with tree +2 + +$Root +3 +"Transform" +$Null # Parent +0.0 0.0 0.0 # Position +2.0 2.0 2.0 # Scale +0.0 0.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-16.0 -4.0 -16.0 # Min ++16.0 +4.0 +16.0 # Max +"Renderable" "Model" "model.grass.16" + +$Tree +3 +"Transform" +$Root # Parent +-5.0 8.5 -3.0 # Position +1.0 1.0 1.0 # Scale +0.0 1.0 0.0 1.0 # Rotation +"Collider" "Static" "AABB" +-3.0 -13.0 -3.0 # Min ++3.0 +13.0 +3.0 # Max +"Renderable" "Model" "model.tree.1" + diff --git a/data/scene/main.scn b/data/scene/main.scn index 28a3e35..de08927 100644 --- a/data/scene/main.scn +++ b/data/scene/main.scn @@ -10,7 +10,7 @@ $Null -0.44 -0.02 -0.59 0.67 # Rotation "Light" "Directional" 0.1 0.1 0.1 # Ambient RGB -0.7 0.7 0.7 # Diffuse RGB +0.9 0.9 0.9 # Diffuse RGB $Camera 2 # Number of components @@ -41,8 +41,9 @@ $Null # Parent +5000.0 -200.0 +5000.0 $Map -1 # Number of components +1 # Number of components "Behaviour" "MapController" $Player $KillArea -"scene.entry" -"scene.platform" \ No newline at end of file +"scene.entry" "scene.exit" +"scene.platform" +"scene.grass.1.16" \ No newline at end of file diff --git a/data/scene/player.scn b/data/scene/player.scn index 8f8c71e..2fa13dd 100644 --- a/data/scene/player.scn +++ b/data/scene/player.scn @@ -7,7 +7,7 @@ $Controller $Null # Parent 0.0 0.0 0.0 # Position 1.0 1.0 1.0 # Scale -0.0 0.0 0.0 1.0 # Rotation +0.0 -1.0 0.0 0.0 # Rotation "Collider" "Dynamic" "AABB" -2.5 2.75 -2.5 # Min +2.5 12.25 +2.5 # Max diff --git a/src/game/manager.cpp b/src/game/manager.cpp index cbaafcb..35e844b 100644 --- a/src/game/manager.cpp +++ b/src/game/manager.cpp @@ -2,6 +2,7 @@ #include #include +#include ecs::Scene* Manager::scene = nullptr; @@ -16,3 +17,16 @@ ecs::Entity Manager::instance(data::Handle scene) { auto stream = memory::TextStream(&stream_buf); return ecs::Scene::deserialize_tree(stream); } + +void Manager::destroy_instance(ecs::Entity entity) { + auto transform = ecs::Coordinator::get_component(entity); + auto c = transform->get_child(); + while (c != ecs::NullEntity) { + auto transform = ecs::Coordinator::get_component(entity); + auto e = c; + c = transform->get_next(); + transform->set_parent(ecs::NullEntity); + ecs::Coordinator::destroy_entity(e); + } + ecs::Coordinator::destroy_entity(entity); +} diff --git a/src/game/manager.hpp b/src/game/manager.hpp index b4ae418..f207bc7 100644 --- a/src/game/manager.hpp +++ b/src/game/manager.hpp @@ -12,6 +12,7 @@ class Manager { static bool load(); static ecs::Entity instance(data::Handle scene); + static void destroy_instance(ecs::Entity entity); static ecs::Scene* scene; }; diff --git a/src/game/map_controller.cpp b/src/game/map_controller.cpp index c51b007..1562435 100644 --- a/src/game/map_controller.cpp +++ b/src/game/map_controller.cpp @@ -8,7 +8,9 @@ bool MapController::Info::serialize(memory::Stream& stream) const { stream.write_ref(this->player); stream.write_ref(this->kill_area); stream.write_string(this->entry.get_asset()->get_id()); + stream.write_string(this->exit.get_asset()->get_id()); stream.write_string(this->platform.get_asset()->get_id()); + stream.write_string(this->grass_16.get_asset()->get_id()); return !stream.failed(); } @@ -17,19 +19,21 @@ bool MapController::Info::deserialize(memory::Stream& stream) { this->player = stream.read_ref(); this->kill_area = stream.read_ref(); this->entry = data::Manager::load(stream.read_string()); + this->exit = data::Manager::load(stream.read_string()); this->platform = data::Manager::load(stream.read_string()); + this->grass_16 = data::Manager::load(stream.read_string()); return !stream.failed(); } MapController::MapController(vpg::ecs::Entity entity, const Info& info) { + this->platform = info.platform; + this->grass_16 = info.grass_16; + this->kill_area = info.kill_area; this->entry = Manager::instance(info.entry); - this->platform = Manager::instance(info.platform); - - auto platform = (Platform*)ecs::Coordinator::get_component(this->platform)->get(); - platform->set_center(glm::vec3(0.0f, 0.0f, 30.0f)); - + this->exit = Manager::instance(info.exit); + auto collider = ecs::Coordinator::get_component(info.kill_area); collider->on_collision.add_listener(std::bind( &MapController::on_kill_area_collision, @@ -37,7 +41,17 @@ MapController::MapController(vpg::ecs::Entity entity, const Info& info) { std::placeholders::_1 )); + collider = ecs::Coordinator::get_component(this->exit); + collider->on_collision.add_listener(std::bind( + &MapController::on_exit_area_collision, + this, + std::placeholders::_1 + )); + this->player = (PlayerInstance*)ecs::Coordinator::get_component(info.player)->get(); + + this->level_num = 0; + this->gen_level(); } MapController::~MapController() { @@ -47,4 +61,39 @@ MapController::~MapController() { void MapController::on_kill_area_collision(const physics::Manifold& manifold) { // Respawn this->player->controller->respawn(this->player->spawn_position); -} \ No newline at end of file +} + +void MapController::on_exit_area_collision(const physics::Manifold& manifold) { + // Respawn + this->level_num += 1; + this->gen_level(); + this->player->controller->respawn(this->player->spawn_position); +} + +void MapController::gen_level() { + for (auto& e : this->level) { + Manager::destroy_instance(e); + } + this->level.clear(); + + auto exit = ecs::Coordinator::get_component(this->exit); + + if (this->level_num == 0) { + auto e = Manager::instance(this->grass_16); + auto grass_16 = ecs::Coordinator::get_component(e); + grass_16->set_position(glm::vec3(0.0f, 0.0f, -50.0f)); + this->level.push_back(e); + exit->set_position(glm::vec3(0.0f, 0.0f, -100.0f)); + } + else if (this->level_num == 1) { + ecs::Entity e = Manager::instance(this->platform); + auto platform = (Platform*)ecs::Coordinator::get_component(e)->get(); + platform->from = glm::vec3(-200.0f, 0.0f, 0.0f); + platform->to = glm::vec3(50.0f, 0.0f, 0.0f); + platform->set_center(glm::vec3(0.0f, 0.0f, -40.0f)); + exit->set_position(glm::vec3(-200.0f, 0.0f, -80.0f)); + this->level.push_back(e); + } + + this->player->controller->respawn(this->player->spawn_position); +} diff --git a/src/game/map_controller.hpp b/src/game/map_controller.hpp index 81c9c9c..3fee579 100644 --- a/src/game/map_controller.hpp +++ b/src/game/map_controller.hpp @@ -7,6 +7,8 @@ #include "player_instance.hpp" #include "player_controller.hpp" +#include + using namespace vpg; struct MapController : public ecs::IBehaviour { @@ -14,8 +16,9 @@ struct MapController : public ecs::IBehaviour { struct Info : public IBehaviour::Info { ecs::Entity player, kill_area; - data::Handle entry; + data::Handle entry, exit; data::Handle platform; + data::Handle grass_16; virtual bool serialize(memory::Stream& stream) const override; virtual bool deserialize(memory::Stream& stream) override; @@ -25,8 +28,14 @@ struct MapController : public ecs::IBehaviour { ~MapController(); void on_kill_area_collision(const physics::Manifold& manifold); + void on_exit_area_collision(const physics::Manifold& manifold); + void gen_level(); + + data::Handle platform, grass_16; ecs::Entity kill_area; - ecs::Entity entry, platform; + ecs::Entity entry, exit; + std::vector level; PlayerInstance* player; + int level_num; }; diff --git a/src/game/player_controller.cpp b/src/game/player_controller.cpp index c0ab916..21b89d2 100644 --- a/src/game/player_controller.cpp +++ b/src/game/player_controller.cpp @@ -94,13 +94,6 @@ PlayerController::~PlayerController() { Mouse::Move.remove_listener(this->mouse_move_listener); Mouse::Move.remove_listener(this->mouse_scroll_listener); Mouse::set_mode(Mouse::Mode::Normal); - - ecs::Coordinator::destroy_entity(this->torso); - ecs::Coordinator::destroy_entity(this->lfoot); - ecs::Coordinator::destroy_entity(this->rfoot); - ecs::Coordinator::destroy_entity(this->lhand); - ecs::Coordinator::destroy_entity(this->rhand); - ecs::Coordinator::destroy_entity(this->feet_collider); } void PlayerController::update(float dt) { @@ -129,6 +122,7 @@ void PlayerController::update(float dt) { glm::vec2 input = { 0.0f, 0.0f }; float speed = 30.0f; + const float jump_change_force = 30.0f; if (Keyboard::is_key_pressed(Key::LShift)) { speed = 50.0f; @@ -186,10 +180,15 @@ void PlayerController::update(float dt) { this->velocity = glm::mix(this->velocity, this->floor_velocity + desired_dir * speed, 25.0f * dt); } + else { + if (glm::dot(this->velocity, desired_dir) < 0.0f) { + this->velocity += desired_dir * jump_change_force * dt; + } + } } if (Keyboard::is_key_pressed(Key::Space) && this->on_floor) { - this->velocity.y = 70.0f; + this->velocity.y = 50.0f; } this->on_floor = false; diff --git a/src/game/player_instance.cpp b/src/game/player_instance.cpp index 2f875d1..706dc78 100644 --- a/src/game/player_instance.cpp +++ b/src/game/player_instance.cpp @@ -50,5 +50,5 @@ PlayerInstance::PlayerInstance(ecs::Entity entity, const Info& info) { } PlayerInstance::~PlayerInstance() { - ecs::Coordinator::destroy_entity(this->player); + Manager::destroy_instance(this->player); } diff --git a/src/vpg/ecs/component_array.hpp b/src/vpg/ecs/component_array.hpp index 4e9c37d..1807bab 100644 --- a/src/vpg/ecs/component_array.hpp +++ b/src/vpg/ecs/component_array.hpp @@ -77,12 +77,17 @@ namespace vpg::ecs { this->count -= 1; size_t last = this->count; this->components[index].~T(); - new (&this->components[index]) T(std::move(this->components[last])); - this->components[last].~T(); - auto last_entity = this->index_to_entity[last]; - this->index_to_entity.erase(last); - this->entity_to_index[last_entity] = index; - this->index_to_entity[index] = last_entity; + if (last != index) { + new (&this->components[index]) T(std::move(this->components[last])); + this->components[last].~T(); + auto last_entity = this->index_to_entity[last]; + this->index_to_entity.erase(last); + this->entity_to_index[last_entity] = index; + this->index_to_entity[index] = last_entity; + } + else { + this->index_to_entity.erase(index); + } } template diff --git a/src/vpg/ecs/transform.hpp b/src/vpg/ecs/transform.hpp index 1ec4f60..9869bfa 100644 --- a/src/vpg/ecs/transform.hpp +++ b/src/vpg/ecs/transform.hpp @@ -33,6 +33,8 @@ namespace vpg::ecs { void look_at(const glm::vec3& point, const glm::vec3& up); inline Entity get_parent() const { return this->parent; } + inline Entity get_child() const { return this->child; } + inline Entity get_next() const { return this->next; } inline const glm::vec3& get_position() const { return this->position; } inline const glm::quat& get_rotation() const { return this->rotation; } diff --git a/vpg.cfg b/vpg.cfg index 2a3e64d..ecf7a2e 100644 --- a/vpg.cfg +++ b/vpg.cfg @@ -1,6 +1,6 @@ ; Window settings -window.width = 1400 -window.height = 1000 +window.width = 800 +window.height = 600 window.fullscreen = false ; Number of update frames per second