Skip to content

Commit

Permalink
Added grass island, trees, fixed component removal error
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Mar 4, 2021
1 parent 3454de9 commit 9c23e5e
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 35 deletions.
12 changes: 8 additions & 4 deletions data/assets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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
File renamed without changes.
2 changes: 1 addition & 1 deletion data/scene/entry.scn
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"Renderable" "Model" "model.sign"
58 changes: 58 additions & 0 deletions data/scene/exit.scn
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions data/scene/grass_1_16.scn
Original file line number Diff line number Diff line change
@@ -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"

9 changes: 5 additions & 4 deletions data/scene/main.scn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
"scene.entry" "scene.exit"
"scene.platform"
"scene.grass.1.16"
2 changes: 1 addition & 1 deletion data/scene/player.scn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/game/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vpg/memory/text_stream.hpp>
#include <vpg/memory/string_stream_buffer.hpp>
#include <vpg/ecs/transform.hpp>

ecs::Scene* Manager::scene = nullptr;

Expand All @@ -16,3 +17,16 @@ ecs::Entity Manager::instance(data::Handle<data::Text> 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<ecs::Transform>(entity);
auto c = transform->get_child();
while (c != ecs::NullEntity) {
auto transform = ecs::Coordinator::get_component<ecs::Transform>(entity);
auto e = c;
c = transform->get_next();
transform->set_parent(ecs::NullEntity);
ecs::Coordinator::destroy_entity(e);
}
ecs::Coordinator::destroy_entity(entity);
}
1 change: 1 addition & 0 deletions src/game/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Manager {
static bool load();

static ecs::Entity instance(data::Handle<data::Text> scene);
static void destroy_instance(ecs::Entity entity);

static ecs::Scene* scene;
};
61 changes: 55 additions & 6 deletions src/game/map_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -17,27 +19,39 @@ bool MapController::Info::deserialize(memory::Stream& stream) {
this->player = stream.read_ref();
this->kill_area = stream.read_ref();
this->entry = data::Manager::load<data::Text>(stream.read_string());
this->exit = data::Manager::load<data::Text>(stream.read_string());
this->platform = data::Manager::load<data::Text>(stream.read_string());
this->grass_16 = data::Manager::load<data::Text>(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<ecs::Behaviour>(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<physics::Collider>(info.kill_area);
collider->on_collision.add_listener(std::bind(
&MapController::on_kill_area_collision,
this,
std::placeholders::_1
));

collider = ecs::Coordinator::get_component<physics::Collider>(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<ecs::Behaviour>(info.player)->get();

this->level_num = 0;
this->gen_level();
}

MapController::~MapController() {
Expand All @@ -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);
}
}

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<ecs::Transform>(this->exit);

if (this->level_num == 0) {
auto e = Manager::instance(this->grass_16);
auto grass_16 = ecs::Coordinator::get_component<ecs::Transform>(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<ecs::Behaviour>(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);
}
13 changes: 11 additions & 2 deletions src/game/map_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
#include "player_instance.hpp"
#include "player_controller.hpp"

#include <vector>

using namespace vpg;

struct MapController : public ecs::IBehaviour {
static constexpr char TypeName[] = "MapController";

struct Info : public IBehaviour::Info {
ecs::Entity player, kill_area;
data::Handle<data::Text> entry;
data::Handle<data::Text> entry, exit;
data::Handle<data::Text> platform;
data::Handle<data::Text> grass_16;

virtual bool serialize(memory::Stream& stream) const override;
virtual bool deserialize(memory::Stream& stream) override;
Expand All @@ -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<data::Text> platform, grass_16;

ecs::Entity kill_area;
ecs::Entity entry, platform;
ecs::Entity entry, exit;
std::vector<ecs::Entity> level;
PlayerInstance* player;
int level_num;
};
15 changes: 7 additions & 8 deletions src/game/player_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/game/player_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 11 additions & 6 deletions src/vpg/ecs/component_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
Expand Down
Loading

0 comments on commit 9c23e5e

Please sign in to comment.