diff --git a/README.md b/README.md index b67176d8..45f1c26e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ # Looper Looper is a game engine with an integrated editor and a 2D type game with a level editor, all written in modern C++20. It uses Vulkan for rendering and ImGui/glfw3 for UI and window/input handling. The project is compatible with Ubuntu and Windows. -![gif](https://raw.githubusercontent.com/wiki/JacobDomagala/Looper/Looper_github.gif) +![gif](https://raw.githubusercontent.com/wiki/JacobDomagala/Looper/gizmo.gif) +![gif](https://raw.githubusercontent.com/wiki/JacobDomagala/Looper/animation.gif) ## Requirements - C++20 compatible compiler (e.g. GCC, Clang, MSVC) diff --git a/editor/editor.cpp b/editor/editor.cpp index 1060f2d2..8d20910b 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -78,6 +78,10 @@ Editor::KeyCallback(KeyEvent& event) { ActionOnObject(action, currentSelectedEditorObject_); } + else if (currentSelectedGameObject_ != Object::INVALID_ID) + { + ActionOnObject(action, currentSelectedGameObject_); + } else { for (auto it = selectedObjects_.begin(); it < selectedObjects_.end();) @@ -199,6 +203,12 @@ Editor::MouseButtonCallback(MouseButtonEvent& event) gizmo_.Update((min + max) / 2.0f, selectedObjects_.size() == 1 ? firstObject.GetSprite().GetRotation() : 0.0f); + + // If we only selected single object, make it as main selected one + if (selectedObjects_.size() == 1) + { + SelectGameObject(selectedObjects_.front()); + } } selectStartPos_ = glm::vec2{}; selectRect_ = std::array< glm::vec2, 4 >{}; @@ -304,6 +314,8 @@ Editor::MoveLogic(const glm::vec2& axis) selectedGameObject.Move(camera_.ConvertToCameraVector(moveBy)); gui_.ObjectUpdated(currentSelectedGameObject_); + + UpdateAnimationData(currentSelectedGameObject_); } else { @@ -317,12 +329,7 @@ Editor::MoveLogic(const glm::vec2& axis) gui_.ObjectUpdated(object); - auto* animatable = dynamic_cast< Animatable* >(&baseObject); - if (animatable) - { - animatable->SetAnimationStartLocation(gameObject.GetPosition()); - UpdateAnimationData(object); - } + UpdateAnimationData(object); } } @@ -455,6 +462,11 @@ Editor::SetMouseOnObject() void Editor::HandleGameObjectClicked(Object::ID newSelectedGameObject, bool groupSelect, bool fromGUI) { + if (currentSelectedEditorObject_ != Object::INVALID_ID) + { + UnselectEditorObject(currentSelectedEditorObject_); + } + const auto objectAlreadySelected = stl::find(selectedObjects_, newSelectedGameObject) != selectedObjects_.end(); const auto mainSelectedObject = currentSelectedGameObject_ == newSelectedGameObject; @@ -562,7 +574,7 @@ Editor::SelectGameObject(Object::ID newSelectedGameObject) // Make sure to render animation points if needed auto& gameObject = currentLevel_->GetGameObjectRef(newSelectedGameObject); - gameObject.SetColor({0.4f, 0.0f, 0.0f, 0.8f}); + gameObject.SetColor({0.6f, 0.0f, 0.0f, 0.8f}); const auto* animatable = dynamic_cast< Animatable* >(&gameObject); if (animatable and animatable->GetRenderAnimationSteps()) @@ -629,6 +641,7 @@ void Editor::UnselectGameObject(Object::ID object, bool groupSelect) { movementOnGameObject_ = false; + animateGameObject_ = false; gui_.ObjectUnselected(object); @@ -643,6 +656,12 @@ Editor::UnselectGameObject(Object::ID object, bool groupSelect) { currentLevel_->GetGameObjectRef(object).SetColor({1.0f, 1.0f, 1.0f, 1.0f}); currentSelectedGameObject_ = Object::INVALID_ID; + + // If main selected object was the only one + if (selectedObjects_.size() == 1) + { + selectedObjects_.clear(); + } } else if (groupSelect) { @@ -680,9 +699,7 @@ Editor::UnselectAll() if (currentSelectedGameObject_ != Object::INVALID_ID) { - currentLevel_->GetGameObjectRef(currentSelectedGameObject_) - .SetColor({1.0f, 1.0f, 1.0f, 1.0f}); - currentSelectedGameObject_ = Object::INVALID_ID; + UnselectGameObject(currentSelectedGameObject_, false); } } @@ -700,6 +717,8 @@ Editor::HandleEditorObjectSelected(EditorObject& newSelectedEditorObject, bool f newSelectedEditorObject.SetObjectSelected(); + const auto& editorObj = GetEditorObjectRef(currentSelectedEditorObject_); + gizmo_.Update(editorObj.GetCenteredPosition(), editorObj.GetSprite().GetRotation()); gizmo_.Show(); gizmoActive_ = true; } @@ -720,6 +739,16 @@ Editor::GetEditorObjectRef(Object::ID object) return *animationPointIt; } +EditorObject& +Editor::GetEditorObjectRefByLinkedID(Object::ID linkedObjID) +{ + auto animationPointIt = stl::find_if(animationPoints_, [linkedObjID](const auto& editorObject) { + return editorObject.GetLinkedObjectID() == linkedObjID; + }); + + return *animationPointIt; +} + void Editor::UnselectEditorObject(Object::ID object) { @@ -728,6 +757,7 @@ Editor::UnselectEditorObject(Object::ID object) auto& editorObject = GetEditorObjectRef(object); editorObject.SetObjectUnselected(); + currentSelectedEditorObject_ = Object::INVALID_ID; gizmo_.Hide(); @@ -873,23 +903,32 @@ Editor::ActionOnObject(Editor::ACTION action, Object::ID object) } break; - case ACTION::REMOVE: - - if (Object::GetTypeFromID(object) == ObjectType::EDITOR_OBJECT) - { + case ACTION::REMOVE: { + auto removeEditorObject = [this](Object::ID object) { UnselectEditorObject(object); - auto& currentySelectedObj = GetEditorObjectRef(currentSelectedEditorObject_); + + auto& currentySelectedObj = GetEditorObjectRef(object); gui_.ObjectDeleted(currentySelectedObj.GetLinkedObjectID()); currentySelectedObj.DeleteLinkedObject(); if (Object::GetTypeFromID(currentySelectedObj.GetLinkedObjectID()) == ObjectType::ANIMATION_POINT) { - animationPoints_.erase( - stl::find_if(animationPoints_, [this](const auto& animationPoint) { - return animationPoint.GetID() == currentSelectedEditorObject_; - })); + auto animationIt = + stl::find_if(animationPoints_, [object](const auto& animationPoint) { + return animationPoint.GetID() == object; + }); + animationIt->GetSprite().ClearData(); + animationPoints_.erase(animationIt); } + }; + if (Object::GetTypeFromID(object) == ObjectType::EDITOR_OBJECT) + { + removeEditorObject(object); + } + else if (Object::GetTypeFromID(object) == ObjectType::ANIMATION_POINT) + { + removeEditorObject(GetEditorObjectRefByLinkedID(object).GetID()); } else { @@ -898,7 +937,8 @@ Editor::ActionOnObject(Editor::ACTION action, Object::ID object) currentLevel_->DeleteObject(object); } - break; + } + break; default: { } } @@ -939,11 +979,14 @@ Editor::Render(VkCommandBuffer cmdBuffer) vkCmdPushConstants(cmdBuffer, renderData.pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(renderer::QuadShader::PushConstants), &pushConstants); + const auto renderAllLayers = renderLayerToDraw_ == -1; for (int32_t layer = renderer::NUM_LAYERS - 1; layer >= 0; --layer) { const auto idx = static_cast< size_t >(layer); const auto& numObjects = renderData.numMeshes.at(idx); - const auto renderThisLayer = renderLayerToDraw_ == -1 ? true : renderLayerToDraw_ == layer; + + const auto renderThisLayer = + (renderAllLayers or layer == 0) ? true : renderLayerToDraw_ == layer; if (numObjects == 0 or !renderThisLayer) { @@ -1305,17 +1348,7 @@ Editor::AddObject(ObjectType objectType) void Editor::ToggleAnimateObject() { - if (animateGameObject_) - { - // TODO: This should be changed in future! - auto& enemy = dynamic_cast< Enemy& >(currentLevel_->GetObjectRef(currentSelectedGameObject_)); - enemy.SetPosition(enemy.GetInitialPosition()); - animateGameObject_ = false; - } - else - { - animateGameObject_ = true; - } + animateGameObject_ = not animateGameObject_; } bool @@ -1370,11 +1403,8 @@ Editor::SetRenderAnimationPoints(bool render) auto& animatable = dynamic_cast< Animatable& >(currentLevel_->GetObjectRef(currentSelectedGameObject_)); - if (animatable.GetRenderAnimationSteps() != render) - { - animatable.RenderAnimationSteps(render); - SetVisibleAnimationPoints(animatable, render); - } + animatable.RenderAnimationSteps(render); + SetVisibleAnimationPoints(animatable, render); } void @@ -1403,6 +1433,11 @@ Editor::Update() gameObject.Rotate(viewAngle); gameObject.Move(moveBy); + + if (currentSelectedEditorObject_ == Object::INVALID_ID) + { + gizmo_.Move(moveBy); + } } else if (animatable.AnimationFinished()) { @@ -1436,7 +1471,15 @@ Editor::Update() void Editor::UpdateAnimationData(Object::ID object) { - dynamic_cast< Animatable& >(currentLevel_->GetObjectRef(object)).UpdateAnimationData(); + auto& baseObject = currentLevel_->GetObjectRef(object); + auto& gameObject = dynamic_cast< GameObject& >(baseObject); + + auto* animatable = dynamic_cast< Animatable* >(&baseObject); + if (animatable) + { + animatable->SetAnimationStartLocation(gameObject.GetPosition()); + animatable->UpdateAnimationData(); + } } glm::vec2 diff --git a/editor/editor.hpp b/editor/editor.hpp index c56ef241..680aac30 100644 --- a/editor/editor.hpp +++ b/editor/editor.hpp @@ -189,6 +189,9 @@ class Editor : public Application EditorObject& GetEditorObjectRef(Object::ID object); + EditorObject& + GetEditorObjectRefByLinkedID(Object::ID linkedObjID); + private: // [[nodiscard]] std::shared_ptr< EditorObject > // GetEditorObjectByID(Object::ID ID); diff --git a/editor/editor_object.cpp b/editor/editor_object.cpp index 81358413..923c0110 100644 --- a/editor/editor_object.cpp +++ b/editor/editor_object.cpp @@ -117,13 +117,6 @@ EditorObject::GetSprite() return sprite_; } -void -EditorObject::CreateSprite(const glm::vec2& globalPosition, const glm::ivec2& size) -{ - sprite_.SetSprite(glm::vec3(globalPosition, 0.0f), size); - position_ = sprite_.GetPosition(); -} - void EditorObject::CreateSpriteTextured(const glm::vec2& /*position*/, const glm::ivec2& /*size*/, const std::string& /*fileName*/) diff --git a/editor/editor_object.hpp b/editor/editor_object.hpp index 8d1314d7..ff9ecb96 100644 --- a/editor/editor_object.hpp +++ b/editor/editor_object.hpp @@ -76,11 +76,6 @@ class EditorObject : public Object void DeleteLinkedObject(); - // Create sprite with default texture - void - CreateSprite(const glm::vec2& position = glm::vec2(0.0f, 0.0f), - const glm::ivec2& size = glm::ivec2(10, 10)); - // Create sprite with texture from 'fileName' void CreateSpriteTextured(const glm::vec2& position = glm::vec2(0.0f, 0.0f), diff --git a/editor/gizmo.cpp b/editor/gizmo.cpp index d5eb13df..4356aac5 100644 --- a/editor/gizmo.cpp +++ b/editor/gizmo.cpp @@ -6,17 +6,14 @@ namespace looper { void Gizmo::Initialize() { - gizmoCenter_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {16, 16}, - "centered_move.png", renderer::SpriteType::alwaysOnTop); + gizmoCenter_.SetSpriteTextured(glm::vec2{0.0f, 0.0f}, {16, 16}, "centered_move.png", 0); gizmoCenter_.SetColor({1.0f, 1.0f, 1.0f, 1.0f}); - gizmoUp_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {96, 32}, "arrow.png", - renderer::SpriteType::alwaysOnTop); + gizmoUp_.SetSpriteTextured(glm::vec2{0.0f, 0.0f}, {96, 32}, "arrow.png", 0); gizmoUp_.Rotate(90.0f, renderer::RotationType::degrees); gizmoUp_.SetColor({0.0f, 1.0f, 0.0f, 1.0f}); - gizmoSide_.SetSpriteTextured(glm::vec3{0.0f, 0.0f, renderer::LAYER_1}, {96, 32}, "arrow.png", - renderer::SpriteType::alwaysOnTop); + gizmoSide_.SetSpriteTextured(glm::vec2{0.0f, 0.0f}, {96, 32}, "arrow.png", 0); gizmoSide_.SetColor({1.0f, 0.0f, 0.0f, 1.0f}); centerInitialSize_ = {{16, 16}, {96, 96}}; @@ -105,13 +102,13 @@ Gizmo::AdjustSize() gizmoUp_.SetInitialPosition( gizmoCenter_.GetPosition() - + glm::vec3{0.0f, gizmoCenter_.GetSize().y / 2.0f + gizmoUp_.GetSize().x / 1.85f, 0.0f}); + + glm::vec2{0.0f, gizmoCenter_.GetSize().y / 2.0f + gizmoUp_.GetSize().x / 1.85f}); gizmoSide_.SetInitialPosition( gizmoCenter_.GetPosition() - + glm::vec3{glm::vec2(gizmoCenter_.GetSize().x / 2.0f + gizmoSide_.GetSize().x / 1.85f, 0.0f), - 0.0f}); + + glm::vec2{ + glm::vec2(gizmoCenter_.GetSize().x / 2.0f + gizmoSide_.GetSize().x / 1.85f, 0.0f)}); } void diff --git a/editor/gui/editor_gui_level.cpp b/editor/gui/editor_gui_level.cpp index c6366a02..4491efdf 100644 --- a/editor/gui/editor_gui_level.cpp +++ b/editor/gui/editor_gui_level.cpp @@ -214,7 +214,7 @@ EditorGUI::RenderLevelMenu() // NOLINT CreateActionRowLabel("RenderLayer", [this] { const auto items = std::to_array< std::string >( - {"All", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}); + {"All", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}); const auto layer = parent_.GetRenderLayerToDraw(); if (ImGui::BeginCombo( "##combo", diff --git a/engine/game/enemy.cpp b/engine/game/enemy.cpp index afa3d5a2..705b3642 100644 --- a/engine/game/enemy.cpp +++ b/engine/game/enemy.cpp @@ -9,12 +9,12 @@ namespace looper { -Enemy::Enemy(Application* context, const glm::vec3& pos, const glm::ivec2& size, +Enemy::Enemy(Application* context, const glm::vec2& pos, const glm::ivec2& size, const std::string& textureName, const std::vector< AnimationPoint >& keypoints, Animatable::ANIMATION_TYPE animationType) : GameObject(context, pos, size, textureName, ObjectType::ENEMY), Animatable(animationType), - initialPosition_(currentGameObjectState_.position_) + initialPosition_(sprite_.GetPosition()) { currentState_.currentHP_ = maxHP_; currentState_.visionRange_ = 1000.0f; @@ -28,23 +28,15 @@ Enemy::Enemy(Application* context, const glm::vec3& pos, const glm::ivec2& size, ResetAnimation(); } -Enemy::Enemy(Application* context, const glm::vec2& pos, const glm::ivec2& size, - const std::string& textureName, const std::vector< AnimationPoint >& keypoints, - Animatable::ANIMATION_TYPE animationType) - : Enemy(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName, keypoints, animationType) -{ -} - void Enemy::Setup(Application* context, const glm::vec2& pos, const glm::ivec2& size, const std::string& textureName, const std::vector< AnimationPoint >& keypoints, Animatable::ANIMATION_TYPE animationType) { - GameObject::Setup(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName, - ObjectType::ENEMY); + GameObject::Setup(context, pos, size, textureName, ObjectType::ENEMY); SetAnimationType(animationType); - initialPosition_ = currentGameObjectState_.position_; + initialPosition_ = sprite_.GetPosition(); currentState_.currentHP_ = maxHP_; currentState_.visionRange_ = 1000.0f; @@ -63,8 +55,8 @@ Enemy::DealWithPlayer() auto* gameHandle = ConvertToGameHandle(); const auto playerPosition = gameHandle->GetPlayer().GetCenteredPosition(); - const auto playerInVision = gameHandle->GetLevel().CheckCollisionAlongTheLine( - currentGameObjectState_.centeredPosition_, playerPosition); + const auto playerInVision = + gameHandle->GetLevel().CheckCollisionAlongTheLine(sprite_.GetPosition(), playerPosition); timer_.ToggleTimer(); @@ -160,8 +152,7 @@ Enemy::Shoot() { currentState_.timeSinceLastShot_ += timer_.GetFloatDeltaTime(); - if (glm::length(appHandle_->GetPlayer().GetCenteredPosition() - - currentGameObjectState_.centeredPosition_) + if (glm::length(appHandle_->GetPlayer().GetCenteredPosition() - glm::vec2(sprite_.GetPosition())) <= (300.0f)) { if (currentState_.timeSinceLastShot_ >= 0.3f) @@ -188,7 +179,7 @@ Enemy::EnemyMove(const glm::vec2& moveBy) prevPosition = GetPreviousPosition(); } - const auto direction = currentGameObjectState_.position_ - prevPosition; + const auto direction = sprite_.GetPosition() - prevPosition; if (glm::length(direction) > 0.0f) { @@ -212,7 +203,7 @@ Enemy::MoveToPosition(const glm::vec2& targetPosition, bool exactPosition) auto& pathFinder = gameHandle->GetLevel().GetPathfinder(); - const auto curPosition = glm::vec2(currentGameObjectState_.centeredPosition_); + const auto curPosition = sprite_.GetPosition(); const auto tiles = pathFinder.GetPath(curPosition, targetPosition); if (!tiles.empty()) @@ -227,8 +218,7 @@ Enemy::MoveToPosition(const glm::vec2& targetPosition, bool exactPosition) EnemyMove(moveVal); constexpr auto errorTreshold = 3.0f; - const auto distanceToDest = - targetPosition - glm::vec2(currentGameObjectState_.centeredPosition_); + const auto distanceToDest = targetPosition - sprite_.GetPosition(); // If Enemy is really close to target destination, just put it there if (glm::length(distanceToDest) < errorTreshold) diff --git a/engine/game/enemy.hpp b/engine/game/enemy.hpp index 3f5eb4be..fdff62da 100644 --- a/engine/game/enemy.hpp +++ b/engine/game/enemy.hpp @@ -15,10 +15,6 @@ class Application; class Enemy : public GameObject, public Animatable { public: - Enemy(Application* context, const glm::vec3& pos, const glm::ivec2& size, - const std::string& textureName, const std::vector< AnimationPoint >& keypoints = {}, - Animatable::ANIMATION_TYPE animationType = Animatable::ANIMATION_TYPE::REVERSABLE); - Enemy(Application* context, const glm::vec2& pos, const glm::ivec2& size, const std::string& textureName, const std::vector< AnimationPoint >& keypoints = {}, Animatable::ANIMATION_TYPE animationType = Animatable::ANIMATION_TYPE::REVERSABLE); diff --git a/engine/game/game_object.cpp b/engine/game/game_object.cpp index 8fb71508..c1079e53 100644 --- a/engine/game/game_object.cpp +++ b/engine/game/game_object.cpp @@ -6,70 +6,59 @@ namespace looper { -GameObject::GameObject(Application* application, const glm::vec3& position, const glm::vec2& size, +GameObject::GameObject(Application* application, const glm::vec2& position, const glm::vec2& size, const std::string& sprite, ObjectType type) : Object(type), appHandle_(application) { - auto newPosition = position; + uint32_t renderLayer = 2; switch (type) { - case looper::ObjectType::OBJECT: { - newPosition.z = renderer::LAYER_2; + case ObjectType::ENEMY: + case ObjectType::PLAYER: { + renderLayer = 1; } break; - - default: - break; + default: { + } } - sprite_.SetSpriteTextured(newPosition, size, sprite); - currentGameObjectState_.position_ = glm::vec2(position); + sprite_.SetSpriteTextured(position, size, sprite, renderLayer); currentGameObjectState_.visible_ = true; - currentGameObjectState_.centeredPosition_ = sprite_.GetPosition(); currentGameObjectState_.previousPosition_ = glm::vec2(position); - currentGameObjectState_.nodes_ = appHandle_->GetLevel().GetTilesFromBoundingBox(sprite_.GetTransformedRectangle()); appHandle_->GetLevel().OccupyNodes(id_, currentGameObjectState_.nodes_, hasCollision_); } -GameObject::GameObject(Application* application, const glm::vec2& position, const glm::vec2& size, - const std::string& sprite, ObjectType type) - : GameObject(application, glm::vec3{position, 0.0f}, size, sprite, type) -{ -} - GameObject::~GameObject() { appHandle_->GetLevel().FreeNodes(id_, currentGameObjectState_.nodes_, hasCollision_); } void -GameObject::Setup(Application* application, const glm::vec3& position, const glm::vec2& size, +GameObject::Setup(Application* application, const glm::vec2& position, const glm::vec2& size, const std::string& sprite, ObjectType type) { Object::Setup(type); appHandle_ = application; - auto newPosition = position; + uint32_t renderLayer = 2; switch (type) { - case looper::ObjectType::OBJECT: { - newPosition.z = renderer::LAYER_2; + case ObjectType::ENEMY: + case ObjectType::PLAYER: { + renderLayer = 1; } break; - - default: - break; + default: { + } } - sprite_.SetSpriteTextured(newPosition, size, sprite); - currentGameObjectState_.position_ = glm::vec2(position); + sprite_.SetSpriteTextured(position, size, sprite, renderLayer); currentGameObjectState_.visible_ = true; - currentGameObjectState_.centeredPosition_ = sprite_.GetPosition(); - currentGameObjectState_.previousPosition_ = glm::vec2(position); + currentGameObjectState_.previousPosition_ = position; currentGameObjectState_.nodes_ = @@ -89,7 +78,7 @@ GameObject::CheckIfCollidedScreenPosion(const glm::vec2& screenPosition) const glm::vec2 GameObject::GetScreenPositionPixels() const { - return appHandle_->GlobalToScreen(currentGameObjectState_.centeredPosition_); + return appHandle_->GlobalToScreen(sprite_.GetPosition()); } bool @@ -117,16 +106,10 @@ GameObject::GetSize() const return sprite_.GetSize(); } -void -GameObject::SetPosition(const glm::vec2& position) -{ - currentGameObjectState_.position_ = position; -} - glm::vec2 GameObject::GetPosition() const { - return currentGameObjectState_.position_; + return sprite_.GetPosition(); } glm::vec2 @@ -138,7 +121,7 @@ GameObject::GetPreviousPosition() const glm::vec2 GameObject::GetCenteredPosition() const { - return currentGameObjectState_.position_; + return sprite_.GetPosition(); // return currentGameObjectState_.centeredPosition_; } @@ -154,19 +137,11 @@ GameObject::GetSprite() return sprite_; } -void -GameObject::CreateSprite(const glm::vec3& position, const glm::ivec2& size) -{ - sprite_.SetSprite(position, size); - currentGameObjectState_.position_ = sprite_.GetPosition(); -} - void GameObject::CreateSpriteTextured(const glm::vec3& position, const glm::ivec2& size, const std::string& fileName) { sprite_.SetSpriteTextured(position, size, fileName); - currentGameObjectState_.position_ = sprite_.GetPosition(); } void @@ -227,10 +202,8 @@ GameObject::GetOccupiedNodes() const void GameObject::Move(const glm::vec2& moveBy) { - currentGameObjectState_.previousPosition_ = currentGameObjectState_.position_; + currentGameObjectState_.previousPosition_ = sprite_.GetPosition(); sprite_.Translate(glm::vec3(moveBy, 0.0f)); - currentGameObjectState_.position_ += moveBy; - currentGameObjectState_.centeredPosition_ += moveBy; updateCollision_ = true; } diff --git a/engine/game/game_object.hpp b/engine/game/game_object.hpp index 68b54ea0..c00fa7a9 100644 --- a/engine/game/game_object.hpp +++ b/engine/game/game_object.hpp @@ -16,16 +16,13 @@ class Game; class GameObject : public Object { public: - GameObject(Application* application, const glm::vec3& position, const glm::vec2& size, - const std::string& sprite, ObjectType type); - GameObject(Application* application, const glm::vec2& position, const glm::vec2& size, const std::string& sprite, ObjectType type); GameObject() = default; ~GameObject() override; void - Setup(Application* application, const glm::vec3& position, const glm::vec2& size, + Setup(Application* application, const glm::vec2& position, const glm::vec2& size, const std::string& sprite, ObjectType type); virtual void @@ -40,9 +37,6 @@ class GameObject : public Object virtual void SetColor(const glm::vec4& color); - virtual void - SetPosition(const glm::vec2& position); - virtual void SetName(const std::string& name); @@ -82,11 +76,6 @@ class GameObject : public Object [[nodiscard]] std::string GetName() const; - // Create sprite with default texture - virtual void - CreateSprite(const glm::vec3& position = glm::vec3{}, - const glm::ivec2& size = glm::ivec2(10, 10)); - // Create sprite with texture from 'fileName' virtual void CreateSpriteTextured(const glm::vec3& position = glm::vec3{}, @@ -136,12 +125,6 @@ class GameObject : public Object struct State { - // global position (in OpenGL coords) - glm::vec2 position_; - - // center of global's position (in OpenGL coords) - glm::vec2 centeredPosition_; - glm::vec2 previousPosition_ = {}; // should this object be visible diff --git a/engine/game/player.cpp b/engine/game/player.cpp index 34e31f2b..586ef8ce 100644 --- a/engine/game/player.cpp +++ b/engine/game/player.cpp @@ -51,7 +51,7 @@ bool Player::CheckCollision(const glm::vec2& bulletPosition, Enemy const* /* enemy*/, bool enemyShooting) { // if the bullet is inside collision zone then player got hit - if (glm::length(bulletPosition - glm::vec2(currentGameObjectState_.centeredPosition_)) + if (glm::length(bulletPosition - glm::vec2(sprite_.GetPosition())) < (static_cast< float >(sprite_.GetSize().x)) / 2.5f) { if (enemyShooting) @@ -68,8 +68,7 @@ glm::vec2 Player::GetScreenPosition() const { const glm::vec4 screenPosition = - appHandle_->GetProjection() - * glm::vec4(currentGameObjectState_.centeredPosition_, 0.0f, 1.0f); + appHandle_->GetProjection() * glm::vec4(sprite_.GetPosition(), 0.0f, 1.0f); return {screenPosition.x, screenPosition.y}; } @@ -86,7 +85,7 @@ Player::UpdateInternal(bool isReverse) { auto* const gameHandle = ConvertToGameHandle(); const auto cursorPos = gameHandle->ScreenToGlobal(gameHandle->GetCursor()); - const auto spritePosition = currentGameObjectState_.position_; + const auto spritePosition = sprite_.GetPosition(); currentState_.viewAngle_ = glm::atan(cursorPos.y - spritePosition.y, cursorPos.x - spritePosition.x); @@ -104,16 +103,10 @@ Player::Shoot() { auto* gameHandle = ConvertToGameHandle(); - const auto direction = gameHandle->GetCursor() - glm::vec2(currentGameObjectState_.position_); + const auto direction = gameHandle->GetCursor() - glm::vec2(sprite_.GetPosition()); currentWeapon_->Shoot(direction); } -void -Player::SetPosition(const glm::vec2& position) -{ - currentGameObjectState_.position_ = position; -} - float Player::GetReloadTime() const { diff --git a/engine/game/player.hpp b/engine/game/player.hpp index 35d54691..ebc1dc24 100644 --- a/engine/game/player.hpp +++ b/engine/game/player.hpp @@ -31,10 +31,6 @@ class Player : public GameObject bool CheckCollision(const glm::vec2& bulletPosition, Enemy const* enemy, bool enemyShooting = true); - // set position in OpenGL - void - SetPosition(const glm::vec2& position) override; - // get centered (center of player's sprite) position on screen // using projection matrix from OpenGL [[nodiscard]] glm::vec2 diff --git a/engine/renderer/renderer.cpp b/engine/renderer/renderer.cpp index d4d306c4..b855764c 100644 --- a/engine/renderer/renderer.cpp +++ b/engine/renderer/renderer.cpp @@ -858,16 +858,15 @@ CreatePerInstanceBuffer() for (size_t frame = 0; frame < MAX_FRAMES_IN_FLIGHT; frame++) { - auto& sbo = renderData.ssbo.at(frame); - if (sbo.buffer_ != VK_NULL_HANDLE) - { - sbo.Destroy(); - } + auto& sbo = renderData.ssbo.at(frame); + if (sbo.buffer_ != VK_NULL_HANDLE) + { + sbo.Destroy(); + } - sbo = Buffer::CreateBuffer(SSBObufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT - | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); - + sbo = Buffer::CreateBuffer(SSBObufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT + | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); } } @@ -1086,16 +1085,16 @@ void SubmitMeshData(const uint32_t idx, const TextureIDs& ids, const glm::mat4& modelMat, const glm::vec4& color) { - auto& object = Data::renderData_[boundApplication_].perInstance.at(idx); - object.model = modelMat; - object.color = color; - object.texSamples.x = static_cast< float >(ids.at(0)); - object.texSamples.y = static_cast< float >(ids.at(1)); - object.texSamples.z = static_cast< float >(ids.at(2)); - object.texSamples.w = static_cast< float >(ids.at(3)); - - updatePerInstanceBuffer_ = true; - updatedObjects_.push_back(idx); + auto& object = Data::renderData_[boundApplication_].perInstance.at(idx); + object.model = modelMat; + object.color = color; + object.texSamples.x = static_cast< float >(ids.at(0)); + object.texSamples.y = static_cast< float >(ids.at(1)); + object.texSamples.z = static_cast< float >(ids.at(2)); + object.texSamples.w = static_cast< float >(ids.at(3)); + + updatePerInstanceBuffer_ = true; + updatedObjects_.push_back(idx); } void diff --git a/engine/renderer/sprite.cpp b/engine/renderer/sprite.cpp index 9d0773f7..a339b3df 100644 --- a/engine/renderer/sprite.cpp +++ b/engine/renderer/sprite.cpp @@ -45,27 +45,10 @@ Sprite::ChangeRenderLayer(int32_t newLayer) } void -Sprite::SetSprite(const glm::vec3& position, const glm::vec2& size, SpriteType t) -{ - // m_texture = std::make_shared< Texture >(); - // m_texture->CreateColorTexture(size, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); - - type_ = t; - currentState_.currentPosition_ = position; - initialPosition_ = position; - currentState_.translateVal_ = position; - size_ = size; - currentState_.angle_ = 0.0f; - currentState_.scaleVal_ = glm::vec2(1.0f, 1.0f); - currentState_.color_ = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); -} - -void -Sprite::SetSpriteTextured(const glm::vec3& position, const glm::vec2& size, - const std::string& fileName, SpriteType t) +Sprite::SetSpriteTextured(const glm::vec2& position, const glm::vec2& size, + const std::string& fileName, uint32_t renderLayer) { changed_ = true; - type_ = t; initialPosition_ = position; currentState_.currentPosition_ = position; @@ -90,10 +73,10 @@ Sprite::SetSpriteTextured(const glm::vec3& position, const glm::vec2& size, */ vertices_ = std::vector< renderer::Vertex >{ - {glm::vec3{-0.5f, 0.5f, position.z}, glm::vec3{0.0f, 0.0f, 1.0f}}, - {glm::vec3{0.5f, 0.5f, position.z}, glm::vec3{1.0f, 0.0f, 1.0f}}, - {glm::vec3(0.5f, -0.5f, position.z), glm::vec3{1.0f, 1.0f, 1.0f}}, - {glm::vec3{-0.5f, -0.5f, position.z}, glm::vec3{0.0f, 1.0f, 1.0f}}}; + {glm::vec3{-0.5f, 0.5f, LAYERS.at(renderLayer)}, glm::vec3{0.0f, 0.0f, 1.0f}}, + {glm::vec3{0.5f, 0.5f, LAYERS.at(renderLayer)}, glm::vec3{1.0f, 0.0f, 1.0f}}, + {glm::vec3(0.5f, -0.5f, LAYERS.at(renderLayer)), glm::vec3{1.0f, 1.0f, 1.0f}}, + {glm::vec3{-0.5f, -0.5f, LAYERS.at(renderLayer)}, glm::vec3{0.0f, 1.0f, 1.0f}}}; const auto transformMat = ComputeModelMat(); ComputeBoundingBox(); @@ -107,13 +90,6 @@ Sprite::SetSpriteTextured(const glm::vec3& position, const glm::vec2& size, renderInfo_ = MeshLoaded(vertices_, textures_, transformMat, currentState_.color_); } -void -Sprite::SetSpriteTextured(const glm::vec2& position, const glm::vec2& size, - const std::string& fileName, SpriteType t) -{ - SetSpriteTextured(glm::vec3{position, 0.0f}, size, fileName, t); -} - void Sprite::Update(bool isReverse) { @@ -156,7 +132,7 @@ Sprite::Render() } } -glm::vec3 +glm::vec2 Sprite::GetPosition() const { return currentState_.currentPosition_; @@ -237,14 +213,14 @@ Sprite::SetTextureFromFile(const std::string& filePath) void Sprite::SetTranslateValue(const glm::vec2& translateBy) { - currentState_.currentPosition_ = initialPosition_ + glm::vec3{translateBy, 0.0f}; + currentState_.currentPosition_ = initialPosition_ + translateBy; currentState_.translateVal_ = currentState_.currentPosition_; changed_ = true; } void -Sprite::SetInitialPosition(const glm::vec3& globalPosition) +Sprite::SetInitialPosition(const glm::vec2& globalPosition) { initialPosition_ = globalPosition; currentState_.currentPosition_ = globalPosition; @@ -328,7 +304,7 @@ Sprite::ScaleCumulative(const glm::vec2& scaleValue) void Sprite::Translate(const glm::vec2& translateValue) { - currentState_.currentPosition_ += glm::vec3{translateValue, 0.0f}; + currentState_.currentPosition_ += translateValue; currentState_.translateVal_ += translateValue; changed_ = true; diff --git a/engine/renderer/sprite.hpp b/engine/renderer/sprite.hpp index d876a0e6..03cffd27 100644 --- a/engine/renderer/sprite.hpp +++ b/engine/renderer/sprite.hpp @@ -23,22 +23,11 @@ class Sprite void ClearData() const; - // Create sprite without texture - void - SetSprite(const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), - const glm::vec2& size = glm::vec2(128, 128), SpriteType = SpriteType::regular); - // Create sprite with texture void SetSpriteTextured(const glm::vec2& position = glm::vec2(0.0f, 0.0f), const glm::vec2& size = glm::vec2(128, 128), - const std::string& fileName = "Default128.png", - SpriteType = SpriteType::regular); - void - SetSpriteTextured(const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), - const glm::vec2& size = glm::vec2(128, 128), - const std::string& fileName = "Default128.png", - SpriteType = SpriteType::regular); + const std::string& fileName = "Default128.png", uint32_t renderLayer = 2); void SetColor(const glm::vec4& color); @@ -50,9 +39,9 @@ class Sprite SetTranslateValue(const glm::vec2& translateBy); void - SetInitialPosition(const glm::vec3& globalPosition); + SetInitialPosition(const glm::vec2& globalPosition); - [[nodiscard]] glm::vec3 + [[nodiscard]] glm::vec2 GetPosition() const; [[nodiscard]] glm::vec2 @@ -160,7 +149,7 @@ class Sprite glm::radians(360.0f)}; static constexpr std::pair< float, float > SCALE_RANGE = {16.0f, 1024.0f}; - glm::vec3 initialPosition_ = {}; + glm::vec2 initialPosition_ = {}; glm::vec2 initialSize_ = {}; private: @@ -173,7 +162,7 @@ class Sprite glm::vec4 color_ = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f); // sprite's position - glm::vec3 currentPosition_ = {0.0f, 0.0f, 0.0f}; + glm::vec2 currentPosition_ = {0.0f, 0.0f}; // transform values glm::vec2 translateVal_ = {0.0f, 0.0f}; @@ -189,7 +178,6 @@ class Sprite StateList< State > statesQueue_ = {}; State currentState_ = {}; - SpriteType type_ = SpriteType::regular; // sprite's texture TextureIDs textures_ = {};