Merge branch 'develop' into feature/handControl

This commit is contained in:
SemvdH
2021-06-18 15:28:30 +02:00
committed by GitHub
15 changed files with 386 additions and 101 deletions

View File

@@ -0,0 +1,217 @@
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <map>
#include "game_Over_Scene.h"
#include <iostream>
#include <opencv2/core/mat.hpp>
#include "../models/model.h"
#include "../renderEngine/loader.h"
#include "../renderEngine/obj_loader.h"
#include "../renderEngine/renderer.h"
#include "../shaders/entity_shader.h"
#include "../gui/gui_interactable.h"
#include "../toolbox/toolbox.h"
#include "../computervision/MenuTest.h"
#include "../computervision/ObjectDetection.h"
#include "../computervision/HandDetectRegion.h"
namespace scene
{
shaders::GuiShader* gui_shader_gameOver;
std::vector<gui::GuiTexture*> guis_gameOver;
computervision::ObjectDetection objDetect_gameOver;
std::vector<std::shared_ptr<gui::GuiTexture>> score_textures_gameOver;
float item_number_gameOver = 0;
bool hand_mode_gameOver = false;
Game_Over_Scene::Game_Over_Scene(int score)
{
shaders::EntityShader shader;
shader.Init();
render_engine::renderer::Init(shader);
shader.CleanUp();
gui_shader_gameOver = new shaders::GuiShader();
gui_shader_gameOver->Init();
for (int i = 0; i <= 9; i++)
{
std::shared_ptr<gui::GuiTexture> score_pointer;
std::string texture_path = "res/";
texture_path += std::to_string(i);
texture_path += ".png";
score_pointer = std::make_unique<gui::GuiTexture>(render_engine::loader::LoadTexture(texture_path), glm::vec2(0.0f, 0.2f), glm::vec2(0.07, 0.15));
score_textures_gameOver.push_back(score_pointer);
}
game_over_texture = std::make_unique<gui::GuiTexture>(render_engine::loader::LoadTexture("res/game_over.png"), glm::vec2(0.0f, 0.6f), glm::vec2(0.50f, 0.50f));
end_score = score;
}
gui::Button* ConvertGuiTextureToButtonGameOver(gui::GuiTexture* texture) {
gui::Button* button;
if (texture != NULL)
{
if (texture->GetType() == gui::GuiType::BUTTON) {
button = (gui::Button*)texture;
return button;
}
else {
button = nullptr;
return button;
}
}
else {
button = nullptr;
return button;
}
}
gui::GuiTexture* GetMenuItemGameOver(bool hand_state) {
if (hand_state)
item_number_gameOver += 0.20f;
int temp_item_number = item_number_gameOver;
//If temp_item_number is equal to the size of the array, set item_number bac to zero to loop through the array again
if (temp_item_number == guis_gameOver.size()) {
item_number_gameOver = 0;
temp_item_number = 0;
}
std::cout << guis_gameOver[temp_item_number]->texture << std::endl;
return guis_gameOver[temp_item_number];
}
scene::Scenes scene::Game_Over_Scene::start(GLFWwindow* window) {
gui::Button button_start_scene(render_engine::loader::LoadTexture("res/Birb1.jpg"), glm::vec2(0.0f, -0.5f), glm::vec2(0.25f, 0.25f));
button_start_scene.SetHoverTexture(render_engine::loader::LoadTexture("res/Birb2.jpg"));
button_start_scene.SetClickedTexture(render_engine::loader::LoadTexture("res/Birb3.jpg"));
button_start_scene.SetOnClickAction([]()
{
std::cout << "Back to start screen!!" << std::endl;
});
guis_gameOver.push_back(&button_start_scene);
computervision::ObjectDetection objDetect;
cv::Mat cameraFrame;
gui::GuiTexture* chosen_item_gameOver = NULL; //This is the selected menu_item
bool hand_closed = false; //Flag to prevent multiple button presses
while (return_value == scene::Scenes:: GAMEOVER)
{
render();
update(window);
if (hand_mode_gameOver)
{
cameraFrame = objDetect_gameOver.ReadCamera();
bool detect = false;
bool hand_detection = objDetect_gameOver.DetectHand(cameraFrame, detect);
if (hand_detection)
{
hand_closed = false;
std::cout << "hand is opened" << std::endl;
//Loop through menu items
chosen_item_gameOver = GetMenuItemGameOver(true);
gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver);
if (new_button != NULL) {
const float x_pos = (chosen_item_gameOver->position.x + 1.0) * WINDOW_WIDTH / 2;
const float y_pos = (1.0 - chosen_item_gameOver->position.y) * WINDOW_HEIGHT / 2;
//Set cursor to location of selected menu_item
glfwSetCursorPos(window, x_pos, y_pos);
}
}
else if (!hand_detection)
{
std::cout << "hand is closed" << std::endl;
//Gets selected menu_item
chosen_item_gameOver = GetMenuItemGameOver(false);
gui::Button* new_button = ConvertGuiTextureToButtonGameOver(chosen_item_gameOver);
if (new_button != NULL && !hand_closed) {
//Run function click
new_button->ForceClick(GLFW_MOUSE_BUTTON_LEFT);
hand_closed = true;
}
}
}
glfwSwapBuffers(window);
glfwPollEvents();
}
gui_shader_gameOver->CleanUp();
render_engine::loader::CleanUp();
return return_value;
}
/**
* renders the models in the start-up scene
*/
void scene::Game_Over_Scene::render()
{
render_engine::renderer::Prepare();
// Render GUI items
render_engine::renderer::Render(guis_gameOver, *gui_shader_gameOver);
}
/**
* updates the variables for the start-up scene
*/
void scene::Game_Over_Scene::update(GLFWwindow* window)
{
for (gui::GuiTexture* button : guis_gameOver) {
gui::Button* new_button = ConvertGuiTextureToButtonGameOver(button);
if (new_button != NULL)
new_button->Update(window);
}
bool hand_present;
objDetect_gameOver.DetectHand(objDetect_gameOver.ReadCamera(), hand_present);
render_engine::renderer::Render(game_over_texture, *gui_shader_gameOver);
DrawScore(end_score);
}
/**
* manages the key input in the start-up scene
*/
void scene::Game_Over_Scene::onKey(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
//return_value = scene::Scenes::STARTUP;
cv::destroyWindow("camera");
}
else if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
hand_mode_gameOver = !hand_mode_gameOver;
}
}
void Game_Over_Scene::DrawScore(int score)
{
std::vector<int> digits;
score_guis_gameOver.clear();
toolbox::GetDigitsFromNumber(score, digits);
for (int i = digits.size() - 1; i >= 0; i--)
{
score_textures_gameOver[digits[i]].get()->position.x = (0.15 * i - 0.05);
render_engine::renderer::Render(score_textures_gameOver[digits[i]], *gui_shader_gameOver);
}
}
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include "scene.h"
#include "../gui/gui_element.h"
namespace scene
{
extern GLFWwindow* window;
class Game_Over_Scene : public scene::Scene
{
private:
int end_score;
scene::Scenes return_value = scene::Scenes::GAMEOVER;
std::vector<std::shared_ptr<gui::GuiTexture>> score_guis_gameOver;
std::shared_ptr<gui::GuiTexture> game_over_texture;
public:
Game_Over_Scene(int score);
Scenes start(GLFWwindow* window) override;
void render() override;
void update(GLFWwindow* window) override;
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
/**
* @brief: This method renders the score points onto the game window
* @param score: Score to show
*/
void DrawScore(int score);
};
}

View File

@@ -7,7 +7,10 @@
namespace scene
{
std::shared_ptr<entities::MainCharacter>main_character;
std::vector<std::shared_ptr<entities::CollisionEntity>> collision_entities;
std::deque<std::shared_ptr<entities::CollisionEntity>> collision_entities;
//std::deque<std::shared_ptr<entities::CollisionEntity>> furniture_collision;
entities::HouseGenerator* house_generator;
std::deque<std::shared_ptr<entities::Entity>> house_models;
@@ -20,21 +23,19 @@ namespace scene
int furniture_count_old;
int score;
int* ptr;
<<<<<<< HEAD
std::vector<computervision::HandDetectRegion*> regions;
=======
float delta_time = 0;
std::vector<computervision::HandDetectRegion> regions;
>>>>>>> develop
computervision::HandDetectRegion reg_left("left", 0, 0, 150, 150), reg_right("right", 0, 0, 150, 150), reg_up("up", 0, 0, 150, 150);
/**
* sets up the first things when the objects has been made
*/
In_Game_Scene::In_Game_Scene()
In_Game_Scene::In_Game_Scene(int *score_ptr)
{
ptr = score_ptr;
camera = std::make_unique<entities::Camera>(glm::vec3(0, 0, 0), glm::vec3(0, 0, 0));
shader = new shaders::EntityShader;
@@ -54,11 +55,8 @@ namespace scene
texture_path += ".png";
score_pointer = std::make_unique<gui::GuiTexture>(render_engine::loader::LoadTexture(texture_path), glm::vec2(-0.9f, 0.8f), glm::vec2(0.07, 0.15));
score_textures.push_back(score_pointer);
}
}
/**
@@ -114,16 +112,19 @@ namespace scene
for (int i = 0; i < furniture_count; i++)
{
house_models.pop_front();
collision_entities.erase(collision_entities.begin() + 1);
}
}
int z_offset = model_pos * (house_generator->GetHouseDepth()); // how much "in the distance" we should load the model
std::deque<std::shared_ptr<entities::Entity>> furniture = house_generator->GenerateHouse(glm::vec3(0, -75, -50 - z_offset), 90);
std::deque<std::shared_ptr<entities::CollisionEntity>> furniture = house_generator->GenerateHouse(glm::vec3(0, -75, -50 - z_offset), 90);
furniture_count = furniture.size();
house_models.insert(house_models.end(), furniture.begin(), furniture.end());
collision_entities.insert(collision_entities.end(), furniture.begin(), furniture.end());
std::cout << "funriture_count in load chunk (house included): " << furniture_count << std::endl;
furniture_count_old = furniture_count - 1;
}
/**
@@ -139,8 +140,9 @@ namespace scene
raw_model_char = render_engine::LoadObjModel("res/beeTwo.obj");
models::TexturedModel model_char = { raw_model_char, texture };
collision::Box char_box = create_bounding_box(raw_model_char.model_size, glm::vec3(0, 0, 0), 1);
main_character = std::make_shared<entities::MainCharacter>(model_char, glm::vec3(0, -50, -100), glm::vec3(0, 90, 0), 5, char_box);
collision_entities.push_back(main_character);
main_character = std::make_shared<entities::MainCharacter>(model_char, glm::vec3(0, 50, -100), glm::vec3(0, 90, 0), 5, char_box);
//collision_entities.push_back(main_character);
house_generator = new entities::HouseGenerator();
SetupHandDetection();
@@ -261,14 +263,15 @@ namespace scene
{
UpdateDeltaTime();
//camera.Move(window);
<<<<<<< HEAD
update_hand_detection();
main_character->Move(regions);
=======
main_character->Move(window);
>>>>>>> develop
if (!main_character.get()->GetOnCollide())
{
*ptr = score;
std::cout << "Score: " << score << std::endl;
return_value = scene::Scenes::GAMEOVER;
}
//std::cout << "x get: " << movement.x << "\ny get: " << movement.y << "\nz get: " << movement.z << "\n";
camera->Follow(main_character->GetPosition());
// calculate where the next house model should be loaded
@@ -286,14 +289,12 @@ namespace scene
}
// remember the position at which the new model was added
last_model_pos = model_pos;
collision_entities.push_front(main_character);
collision::CheckCollisions(collision_entities);
<<<<<<< HEAD
=======
collision_entities.pop_front();
update_hand_detection();
>>>>>>> develop
}
//manages the key input in the game scene
@@ -350,11 +351,11 @@ namespace scene
toolbox::GetDigitsFromNumber(score, digits);
for (int i = digits.size() - 1; i >= 0; i--)
{
score_textures[digits[i]].get()->position.x = 0.15 * i - 0.9; // place the number at the top left. the numbers are just fine tuned to get the position just right
render_engine::renderer::Render(score_textures[digits[i]], *gui_shader);
}
}

View File

@@ -45,7 +45,6 @@ namespace scene
PAUSED
};
class In_Game_Scene : public scene::Scene
{
private:
@@ -117,7 +116,7 @@ namespace scene
void LoadChunk(int model_pos);
public:
In_Game_Scene();
In_Game_Scene(int *score_ptr);
~In_Game_Scene();
/**
@@ -152,7 +151,11 @@ namespace scene
*/
void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) override;
/**
* @brief: This method renders the score points onto the game window
* @param score: Score to show
*/
void DrawScore(int score);
};
}

View File

@@ -19,10 +19,10 @@ namespace scene {
};
class Scene
{
{
public:
virtual ~Scene() = 0;
/**
* @brief the method start is the start of a scene where a while loop runs, this runs the scene.
* @param window the main window of the application
@@ -54,7 +54,6 @@ namespace scene {
* @return void
*/
virtual void onKey(GLFWwindow* window, int key, int scancode, int action, int mods) {};
};
}