RenderGUI.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * 2D Game Engine
  3. * RenderGUI.h:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 28/02/2021.
  8. */
  9. #ifndef IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H
  10. #define IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H
  11. #include <math.h>
  12. #include <SDL.h>
  13. #include <glm/glm.hpp>
  14. #include <imgui.h>
  15. #include <imgui_sdl.h>
  16. #include <AssetStore.h>
  17. #include <ECS.h>
  18. #include <Logger.h>
  19. #include <Components/Transform.h>
  20. #include <Components/RigidBody.h>
  21. #include <Components/Sprite.h>
  22. #include <Components/BoxCollider.h>
  23. #include <Components/ProjectileEmitter.h>
  24. #include <Components/Heakth.h>
  25. class RenderGUISystem : public System
  26. {
  27. public:
  28. RenderGUISystem() = default;
  29. void update(const std::unique_ptr<Registry> &registry, SDL_Rect &camera)
  30. {
  31. ImGui::NewFrame();
  32. ImGuiWindowFlags windowFlags;
  33. if (ImGui::Begin("Spawn enemies"))
  34. {
  35. /* Main properties */
  36. static int enemyXPos = 0, enemyYPos = 0, enemyHealth = 100;
  37. static int enemyXVelocity = 0, enemyYVelocity = 0;
  38. static float enemyScale = 1, enemyRotation = 0;
  39. const char* spriteList[] = { "truck-image", "tank-image" };
  40. static int spriteSelection = 0;
  41. /* Projectile */
  42. static float projectileAngle = 0, projectileDuration = 5, projectileFrequency = 3;
  43. static int projectileSpeed = 100;
  44. double projectileXVelocity, projectileYVelocity;
  45. // Sprite selection
  46. if (ImGui::CollapsingHeader("Sprite"))
  47. {
  48. ImGui::Combo("combo", &spriteSelection, spriteList, IM_ARRAYSIZE(spriteList));
  49. }
  50. ImGui::Spacing();
  51. if (ImGui::CollapsingHeader("Transform"))
  52. {
  53. ImGui::InputInt("X position", &enemyXPos);
  54. ImGui::InputInt("Y position", &enemyYPos);
  55. ImGui::SliderFloat("Scale", &enemyScale, 0.5, 10);
  56. ImGui::SliderAngle("Rotation", &enemyRotation, 0, 360);
  57. }
  58. ImGui::Spacing();
  59. if (ImGui::CollapsingHeader("Rigid Body"))
  60. {
  61. ImGui::InputInt("X Velocity", &enemyXVelocity);
  62. ImGui::InputInt("Y Velocity", &enemyYVelocity);
  63. }
  64. ImGui::Spacing();
  65. // default Health
  66. if (ImGui::CollapsingHeader("Health"))
  67. {
  68. ImGui::SliderInt("Starting health", &enemyHealth, 1, 100);
  69. }
  70. ImGui::Spacing();
  71. if (ImGui::CollapsingHeader("Projectile Emitter"))
  72. {
  73. // Angle/Speed of projectiles
  74. ImGui::SliderAngle("Angle", &projectileAngle, 0, 360);
  75. ImGui::SliderInt("Speed", &projectileSpeed, 10, 500);
  76. // Frequency / duration of projectives (seconds)
  77. ImGui::InputFloat("Frequency (sec)", &projectileFrequency);
  78. ImGui::InputFloat("Duration (sec)", &projectileDuration);
  79. }
  80. ImGui::Spacing();
  81. ImGui::Separator();
  82. ImGui::Spacing();
  83. if (ImGui::Button("Spawn"))
  84. {
  85. projectileXVelocity = cos(projectileAngle) * (double)projectileSpeed;
  86. projectileYVelocity = sin(projectileAngle) * (double)projectileSpeed;
  87. Logger::Warning("proj X Vel ==> %f", projectileXVelocity);
  88. Logger::Warning("proj Y Vel ==> %f", projectileYVelocity);
  89. Entity newEnemy = registry->createEntity();
  90. newEnemy.addComponent<TransformComponent>(glm::vec2(enemyXPos, enemyYPos),
  91. glm::vec2(enemyScale, enemyScale),
  92. glm::degrees(enemyRotation));
  93. newEnemy.addComponent<RigidBodyComponent>(glm::vec2(enemyXVelocity, enemyYVelocity));
  94. newEnemy.addComponent<SpriteComponent>(spriteList[spriteSelection], 2, 32, 32);
  95. newEnemy.addComponent<BoxColliderComponent>(25, 20, glm::vec2(5, 5));
  96. newEnemy.addComponent<ProjectileEmitterComponent>(glm::vec2(projectileXVelocity, projectileYVelocity),
  97. projectileFrequency * 1000,
  98. projectileDuration * 1000,
  99. 10,
  100. false);
  101. newEnemy.addComponent<HealthComponent>(enemyHealth);
  102. newEnemy.group("enemies");
  103. }
  104. }
  105. ImGui::End();
  106. /* Mouse overlay */
  107. windowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
  108. ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_Always, ImVec2(0, 0));
  109. ImGui::SetNextWindowBgAlpha(0.9f);
  110. if (ImGui::Begin("Mouse coordinates", NULL, windowFlags))
  111. {
  112. ImGui::Text("Map Coordinates: x=%.1f, y=%.1f",
  113. ImGui::GetIO().MousePos.x + camera.x,
  114. ImGui::GetIO().MousePos.y + camera.y);
  115. }
  116. ImGui::End();
  117. ImGui::Render();
  118. ImGuiSDL::Render(ImGui::GetDrawData());
  119. }
  120. };
  121. #endif /* IMGUI_SOURCE_INCLUDE_SYSTEMS_RENDERGUI_H */