Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Added rotation #73

Merged
merged 4 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Application/src/factory/EntityFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Engine/component/Color.hpp>
#include <Engine/component/Texture.hpp>
#include <Engine/component/Rotation.hpp>
#include <Engine/helpers/DrawableFactory.hpp>
#include <Engine/Core.hpp>

Expand All @@ -15,6 +16,7 @@ auto game::EntityFactory::create<game::EntityFactory::FLOOR_NORMAL>(

const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {1, 1, 1});
Expand All @@ -31,6 +33,7 @@ auto game::EntityFactory::create<game::EntityFactory::FLOOR_SPAWN>(

const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {0.5, 1, 0.5});
Expand All @@ -47,6 +50,7 @@ auto game::EntityFactory::create<game::EntityFactory::FLOOR_BOSS>(

const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {1, 1, 1});
Expand All @@ -63,6 +67,7 @@ auto game::EntityFactory::create<game::EntityFactory::FLOOR_CORRIDOR>(

const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {1, 1, 1});
Expand All @@ -77,6 +82,7 @@ auto game::EntityFactory::create<game::EntityFactory::EXIT_DOOR>(
{
const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {0.75, 0.25, 0.25});
Expand All @@ -94,6 +100,7 @@ auto game::EntityFactory::create<game::EntityFactory::WALL>(entt::registry &worl

const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {1, 1, 1});
Expand All @@ -110,6 +117,7 @@ auto game::EntityFactory::create<game::EntityFactory::DEBUG_TILE>(
{
const auto e = world.create();
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_TERRAIN>());
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, e, {1, 1, 0});
Expand All @@ -127,6 +135,7 @@ auto game::EntityFactory::create<game::EntityFactory::ENEMY>(entt::registry &wor
world.emplace<entt::tag<"enemy"_hs>>(e);
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_ENEMY>());
world.emplace<engine::d2::Velocity>(e, 0.02 * (std::rand() & 1), 0.02 * (std::rand() & 1));
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::d2::HitboxSolid>(e, 1.0, 1.0);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
Expand Down Expand Up @@ -154,6 +163,7 @@ auto game::EntityFactory::create<game::EntityFactory::BOSS>(entt::registry &worl
world.emplace<entt::tag<"boss"_hs>>(e);
world.emplace<engine::d3::Position>(e, pos.x, pos.y, get_z_layer<LAYER_ENEMY>());
world.emplace<engine::d2::Velocity>(e, (std::rand() & 1) ? -0.05 : 0.05, (std::rand() & 1) ? -0.05 : 0.05);
world.emplace<engine::d2::Rotation>(e, 0.f);
world.emplace<engine::d2::Scale>(e, size.x, size.y);
world.emplace<engine::d2::HitboxSolid>(e, 3.0, 3.0);
world.emplace<engine::Drawable>(e, engine::DrawableFactory::rectangle());
Expand Down Expand Up @@ -186,6 +196,7 @@ auto game::EntityFactory::create<game::EntityFactory::PLAYER>(
world.emplace<entt::tag<"player"_hs>>(player);
world.emplace<engine::d3::Position>(player, 0.0, 0.0, EntityFactory::get_z_layer<EntityFactory::LAYER_PLAYER>());
world.emplace<engine::d2::Velocity>(player, 0.0, 0.0);
world.emplace<engine::d2::Rotation>(player, 0.f);
world.emplace<engine::d2::Acceleration>(player, 0.0, 0.0);
world.emplace<engine::d2::Scale>(player, 1.0, 1.0);
world.emplace<engine::d2::HitboxSolid>(player, 1.0, 1.0);
Expand All @@ -211,8 +222,9 @@ auto game::EntityFactory::create<game::EntityFactory::KEY>(entt::registry &world
auto key = world.create();
world.emplace<entt::tag<"key"_hs>>(key);
world.emplace<engine::d2::HitboxFloat>(key);
world.emplace<engine::d2::Scale>(key, size.x, size.y);
world.emplace<engine::d3::Position>(key, pos.x, pos.y, get_z_layer<LAYER_PLAYER>());
world.emplace<engine::d2::Rotation>(key, 0.f);
world.emplace<engine::d2::Scale>(key, size.x, size.y);
world.emplace<engine::Drawable>(key, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, key, {1, 1, 0});
engine::DrawableFactory::fix_texture(world, key, holder.instance->settings().data_folder + "textures/key.png");
Expand Down
3 changes: 3 additions & 0 deletions src/Application/src/factory/SpellFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <Engine/component/Color.hpp>
#include <Engine/component/Texture.hpp>
#include <Engine/component/Rotation.hpp>
#include <Engine/helpers/DrawableFactory.hpp>
#include <Engine/Core.hpp>

Expand Down Expand Up @@ -31,6 +32,7 @@ auto game::SpellFactory::create<game::SpellFactory::STICK_ATTACK>(
world.emplace<engine::Drawable>(spell, engine::DrawableFactory::rectangle());
engine::DrawableFactory::fix_color(world, spell, std::move(color));
world.emplace<engine::d3::Position>(spell, caster_pos.x + direction.x, caster_pos.y + direction.y, -1.0);
world.emplace<engine::d2::Rotation>(spell, 0.f);
world.emplace<engine::d2::Scale>(spell, 0.7, 0.7);
world.emplace<engine::d2::HitboxFloat>(spell, 0.7, 0.7);
world.emplace<engine::Source>(spell, caster);
Expand Down Expand Up @@ -69,6 +71,7 @@ auto game::SpellFactory::create<game::SpellFactory::FIREBALL>(entt::registry &wo
engine::DrawableFactory::fix_color(world, spell, {0.6, 0.6, 1});
world.emplace<engine::d3::Position>(spell, caster_pos.x + direction.x, caster_pos.y + direction.y, -1.0); // note : why -1
world.emplace<engine::d2::Velocity>(spell, direction.x * speed , direction.y * speed);
world.emplace<engine::d2::Rotation>(spell, 0.f);
world.emplace<engine::d2::Scale>(spell, 0.7, 0.7);
world.emplace<engine::d2::HitboxFloat>(spell, 0.7, 0.7);
world.emplace<engine::Source>(spell, caster);
Expand Down
19 changes: 19 additions & 0 deletions src/Engine/include/Engine/component/Rotation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <concepts>

namespace engine {

namespace d2 {

template<std::floating_point T>
struct RotationT {
// Radian
T angle;
};

using Rotation = RotationT<double>;

} // namespace d2

} // namespace engine
11 changes: 7 additions & 4 deletions src/Engine/src/Engine/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Engine/helpers/overloaded.hpp"
#include "Engine/component/Drawable.hpp"
#include "Engine/component/Position.hpp"
#include "Engine/component/Rotation.hpp"
#include "Engine/component/Scale.hpp"
#include "Engine/component/Velocity.hpp"
#include "Engine/component/Acceleration.hpp"
Expand Down Expand Up @@ -434,10 +435,11 @@ auto engine::Core::tickOnce(const TimeElapsed &t) -> void

m_shader_colored->use();
m_shader_colored->setUniform<float>("time", static_cast<float>(tmp));
m_world.view<Drawable, Color, d3::Position, d2::Scale>(entt::exclude<Texture>)
.each([this](auto &drawable, [[maybe_unused]] auto &color, auto &pos, auto &scale) {
m_world.view<Drawable, Color, d3::Position, d2::Scale, d2::Rotation>(entt::exclude<Texture>)
.each([this](auto &drawable, [[maybe_unused]] auto &color, auto &pos, auto &scale, auto &rot) {
auto model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3{pos.x, pos.y, pos.z});
model = glm::rotate(model, static_cast<float>(rot.angle), glm::vec3(0.f, 0.f, 1.f));
model = glm::scale(model, glm::vec3{scale.x, scale.y, 1.0f});
m_shader_colored->setUniform("model", model);
::glBindVertexArray(drawable.VAO);
Expand All @@ -446,10 +448,11 @@ auto engine::Core::tickOnce(const TimeElapsed &t) -> void

m_shader_colored_textured->use();
m_shader_colored_textured->setUniform<float>("time", static_cast<float>(tmp));
m_world.view<Drawable, Color, Texture, d3::Position, d2::Scale>().each(
[this](auto &drawable, [[maybe_unused]] auto &color, auto &texture, auto &pos, auto &scale) {
m_world.view<Drawable, Color, Texture, d3::Position, d2::Scale, d2::Rotation>().each(
[this](auto &drawable, [[maybe_unused]] auto &color, auto &texture, auto &pos, auto &scale, auto &rot) {
auto model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3{pos.x, pos.y, pos.z});
model = glm::rotate(model, static_cast<float>(rot.angle), glm::vec3(0.f, 0.f, 1.f));
model = glm::scale(model, glm::vec3{scale.x, scale.y, 1.0f});
m_shader_colored_textured->setUniform("model", model);
::glBindTexture(GL_TEXTURE_2D, texture.texture);
Expand Down