123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /*
- * 2D Game Engine
- * RenderGUI.h:
- * Based on pikuma.com 2D game engine in C++ and Lua course
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 28/02/2021.
- */
- #ifndef IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H
- #define IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H
- #include <math.h>
- #include <SDL.h>
- #include <glm/glm.hpp>
- #include <imgui.h>
- #include <imgui_sdl.h>
- #include <AssetStore.h>
- #include <ECS.h>
- #include <Logger.h>
- #include <Components/Transform.h>
- #include <Components/RigidBody.h>
- #include <Components/Sprite.h>
- #include <Components/BoxCollider.h>
- #include <Components/ProjectileEmitter.h>
- #include <Components/Heakth.h>
- class RenderGUISystem : public System
- {
- public:
- RenderGUISystem() = default;
- void update(const std::unique_ptr<Registry> ®istry, SDL_Rect &camera)
- {
- ImGui::NewFrame();
- ImGuiWindowFlags windowFlags;
- if (ImGui::Begin("Spawn enemies"))
- {
- /* Main properties */
- static int enemyXPos = 0, enemyYPos = 0, enemyHealth = 100;
- static int enemyXVelocity = 0, enemyYVelocity = 0;
- static float enemyScale = 1, enemyRotation = 0;
- const char* spriteList[] = { "truck-image", "tank-image" };
- static int spriteSelection = 0;
- /* Projectile */
- static float projectileAngle = 0, projectileDuration = 5, projectileFrequency = 3;
- static int projectileSpeed = 100;
- double projectileXVelocity, projectileYVelocity;
- // Sprite selection
- if (ImGui::CollapsingHeader("Sprite"))
- {
- ImGui::Combo("combo", &spriteSelection, spriteList, IM_ARRAYSIZE(spriteList));
- }
- ImGui::Spacing();
- if (ImGui::CollapsingHeader("Transform"))
- {
- ImGui::InputInt("X position", &enemyXPos);
- ImGui::InputInt("Y position", &enemyYPos);
- ImGui::SliderFloat("Scale", &enemyScale, 0.5, 10);
- ImGui::SliderAngle("Rotation", &enemyRotation, 0, 360);
- }
- ImGui::Spacing();
- if (ImGui::CollapsingHeader("Rigid Body"))
- {
- ImGui::InputInt("X Velocity", &enemyXVelocity);
- ImGui::InputInt("Y Velocity", &enemyYVelocity);
- }
- ImGui::Spacing();
- // default Health
- if (ImGui::CollapsingHeader("Health"))
- {
- ImGui::SliderInt("Starting health", &enemyHealth, 1, 100);
- }
- ImGui::Spacing();
- if (ImGui::CollapsingHeader("Projectile Emitter"))
- {
- // Angle/Speed of projectiles
- ImGui::SliderAngle("Angle", &projectileAngle, 0, 360);
- ImGui::SliderInt("Speed", &projectileSpeed, 10, 500);
- // Frequency / duration of projectives (seconds)
- ImGui::InputFloat("Frequency (sec)", &projectileFrequency);
- ImGui::InputFloat("Duration (sec)", &projectileDuration);
- }
- ImGui::Spacing();
- ImGui::Separator();
- ImGui::Spacing();
- if (ImGui::Button("Spawn"))
- {
- projectileXVelocity = cos(projectileAngle) * (double)projectileSpeed;
- projectileYVelocity = sin(projectileAngle) * (double)projectileSpeed;
- Logger::Warning("proj X Vel ==> %f", projectileXVelocity);
- Logger::Warning("proj Y Vel ==> %f", projectileYVelocity);
- Entity newEnemy = registry->createEntity();
- newEnemy.addComponent<TransformComponent>(glm::vec2(enemyXPos, enemyYPos),
- glm::vec2(enemyScale, enemyScale),
- glm::degrees(enemyRotation));
- newEnemy.addComponent<RigidBodyComponent>(glm::vec2(enemyXVelocity, enemyYVelocity));
- newEnemy.addComponent<SpriteComponent>(spriteList[spriteSelection], 2, 32, 32);
- newEnemy.addComponent<BoxColliderComponent>(25, 20, glm::vec2(5, 5));
- newEnemy.addComponent<ProjectileEmitterComponent>(glm::vec2(projectileXVelocity, projectileYVelocity),
- projectileFrequency * 1000,
- projectileDuration * 1000,
- 10,
- false);
- newEnemy.addComponent<HealthComponent>(enemyHealth);
- newEnemy.group("enemies");
- }
- }
- ImGui::End();
- /* Mouse overlay */
- windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
- ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_Always, ImVec2(0, 0));
- ImGui::SetNextWindowBgAlpha(0.9f);
- if (ImGui::Begin("Mouse coordinates", NULL, windowFlags))
- {
- ImGui::Text("Map Coordinates: x=%.1f, y=%.1f",
- ImGui::GetIO().MousePos.x + camera.x,
- ImGui::GetIO().MousePos.y + camera.y);
- }
- ImGui::End();
- ImGui::Render();
- ImGuiSDL::Render(ImGui::GetDrawData());
- }
- };
- #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H */
|