mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 22:41:48 +01:00
Fix precompiled headers
Added edge detection (if it works :/)
This commit is contained in:
228
Game/Levels/World/WorldLevel.cpp
Normal file
228
Game/Levels/World/WorldLevel.cpp
Normal file
@@ -0,0 +1,228 @@
|
||||
#include <imgui.h>
|
||||
#include "pch.h"
|
||||
#include "WorldLevel.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
#include "Collision.h"
|
||||
#include "colors.h"
|
||||
#include "utils.h"
|
||||
#include "GridSystem/WorldTile.h"
|
||||
#include "Gui/Screens/ScreenManager.h"
|
||||
|
||||
|
||||
class GroundTileType;
|
||||
WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
|
||||
m_gridManager(WorldGridManager()),
|
||||
m_player(Player { Point2f { 0, 100 }, TextureManager::GetInstance() }),
|
||||
m_mousePos { 0, 0 },
|
||||
m_viewport(viewport),
|
||||
m_screenManager(ScreenManager::GetInstance()) {
|
||||
// The grid is 34 x 50 big, the top center is 0,0 in world coords
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
int actualX = x - WORLD_WIDTH / 2;
|
||||
auto pos = Point2f { float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT };
|
||||
GroundTileType* type = Tiles::AIR;
|
||||
switch (utils::randRange(0, 2)) {
|
||||
case 0:
|
||||
type = Tiles::DIRT;
|
||||
break;
|
||||
case 1:
|
||||
type = Tiles::IRON;
|
||||
break;
|
||||
case 2:
|
||||
//AIR
|
||||
break;
|
||||
default:
|
||||
std::cout << "??" << '\n';
|
||||
}
|
||||
|
||||
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, Tiles::DIRT, TextureManager::GetInstance(), &m_gridManager});
|
||||
}
|
||||
}
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR);
|
||||
}
|
||||
|
||||
}
|
||||
WorldLevel::~WorldLevel() {
|
||||
//delete m_pTextTexture;
|
||||
|
||||
}
|
||||
void WorldLevel::Update(float elapsedSec) {
|
||||
int mouseX, mouseY;
|
||||
SDL_GetMouseState(&mouseX, &mouseY);
|
||||
m_mousePos = Point2f { float(mouseX), float(mouseY) };
|
||||
m_mousePos = m_pCamera->TransformMouse(m_mousePos);
|
||||
//m_player.Update(elapsedSec, *this);
|
||||
|
||||
|
||||
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
m_gridManager.GetTileAtIndex(x, y)->m_Hightlight = false;
|
||||
}
|
||||
}
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
m_gridManager.GetTileAtIndex(x, y)->Update(m_pCamera);
|
||||
if (m_gridManager.GetTileAtIndex(x, y)->GetCollisionRect().Contains(m_mousePos)) {
|
||||
m_pSelectedTile = m_gridManager.GetTileAtIndex(x, y);
|
||||
//selectedTile->m_Hightlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (m_pSelectedTile != nullptr) {
|
||||
if (utils::isMouseDown(SDL_BUTTON_LEFT)) {
|
||||
m_pSelectedTile->SetTileType(Tiles::AIR);
|
||||
}
|
||||
}
|
||||
|
||||
//Get the diogonal tiles of the selected tile
|
||||
if(m_pSelectedTile != nullptr) {
|
||||
surroundingTiles surroundingTiles = m_gridManager.GetSurroundingTiles(m_pSelectedTile);
|
||||
TileDirection direction = TileDirection::TopMiddle;
|
||||
std::array<TileDirection, 8> directions = {TileDirection::TopLeft, TileDirection::TopMiddle, TileDirection::TopRight, TileDirection::MiddleLeft, TileDirection::MiddleRight, TileDirection::BottomLeft, TileDirection::BottomMiddle, TileDirection::BottomRight};
|
||||
|
||||
for(int i = 0; i < 8; i++) {
|
||||
direction = directions[i];
|
||||
if(surroundingTiles.GetTile(direction) != nullptr) {
|
||||
if(surroundingTiles.GetTile(direction)->GetTileType() != Tiles::AIR) {
|
||||
//surroundingTiles.GetTile(direction)->m_Hightlight = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//m_player.Update(elapsedSec, *this);
|
||||
|
||||
Screen* screen = m_screenManager->GetCurrentScreen();
|
||||
if (screen != nullptr) {
|
||||
screen->Update(elapsedSec);
|
||||
}
|
||||
|
||||
//Point2f playerPos = m_player.GetPosition();
|
||||
//Point2f newCameraPos = playerPos;
|
||||
//m_pCamera->SetPosition(newCameraPos);
|
||||
|
||||
//place the player in the center of the camera
|
||||
//m_pCamera->SetPosition(Point2f{playerPos.x - m_viewport.width / 2, playerPos.y - m_viewport.height / 2});
|
||||
|
||||
}
|
||||
void WorldLevel::Draw() const {
|
||||
m_pCamera->BeginRendering();
|
||||
|
||||
for (Collision::CollisionRect rect : m_Rects) {
|
||||
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
||||
}
|
||||
|
||||
utils::SetColor(Colors::WHITE);
|
||||
utils::FillEllipse(m_mousePos, 2, 2);
|
||||
|
||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
m_gridManager.GetTileAtIndex(x, y)->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
//loop over worldtiles
|
||||
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||
for (int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||
if (*m_gridManager.GetTileAtIndex(x, y)->GetTileType() != Tiles::AIR) {
|
||||
Collision::CollisionRect rect = m_gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect();
|
||||
utils::SetColor(Colors::GREEN);
|
||||
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
utils::SetColor(Colors::MAGENTA);
|
||||
utils::FillEllipse(0, 0, 5, 5);
|
||||
|
||||
m_player.Draw();
|
||||
if(m_pSelectedTile != nullptr) {
|
||||
m_pSelectedTile->Draw();
|
||||
}
|
||||
|
||||
m_pCamera->EndRendering();
|
||||
|
||||
utils::FillRect(utils::GetMousePos(), 10, 10);
|
||||
const Screen* screen = m_screenManager->GetCurrentScreen();
|
||||
if (screen != nullptr) {
|
||||
screen->Draw();
|
||||
}
|
||||
}
|
||||
void WorldLevel::MouseMove(const Point2f& mousePos) {
|
||||
m_mousePos = mousePos;
|
||||
}
|
||||
|
||||
void WorldLevel::ProcessImGui() {
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMenu("Properties")) {
|
||||
if (ImGui::MenuItem("TextureManager Info")) {
|
||||
m_ShowTextureManagerWindow = !m_ShowTextureManagerWindow;
|
||||
}
|
||||
if (ImGui::MenuItem("Camera Info")) {
|
||||
m_ShowCameraWindow = !m_ShowCameraWindow;
|
||||
}
|
||||
if (ImGui::MenuItem("Player Info")) {
|
||||
m_ShowPlayerInfo = !m_ShowPlayerInfo;
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
if(ImGui::BeginMenu("Screens")) {
|
||||
if(ImGui::MenuItem("Open Fuel screen")) {
|
||||
ScreenManager::GetInstance()->OpenScreen(ScreenManager::Fuel);
|
||||
}
|
||||
|
||||
|
||||
if(ImGui::MenuItem("Open Sell screen")) {
|
||||
ScreenManager::GetInstance()->OpenScreen(ScreenManager::SellScreen);
|
||||
}
|
||||
|
||||
if(ImGui::MenuItem("Close Screen")) {
|
||||
ScreenManager::GetInstance()->CloseScreen();
|
||||
}
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
Point2f screenPos = utils::GetMousePos();
|
||||
std::string mousePos = "Mouse Pos: (" + std::to_string(screenPos.x) + ", " + std::to_string(screenPos.y) + ")";
|
||||
if(ImGui::BeginMenu(mousePos.c_str())) {
|
||||
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
||||
if (m_ShowTextureManagerWindow) {
|
||||
ImGui::Begin("Texture Manager", &m_ShowTextureManagerWindow, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::Text("Texture loading Count:");
|
||||
ImGui::SameLine();
|
||||
ImGui::TextColored(ImVec4(1.0f, 0.0f, 1.0f, 1.0f), std::to_string(Texture::m_TextureCounter).c_str());
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (m_ShowCameraWindow) {
|
||||
ImGui::Begin("Camera", &m_ShowCameraWindow, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::Text("Camera Position: (%f, %f)", m_pCamera->GetPosition().x, m_pCamera->GetPosition().y);
|
||||
ImGui::Text("Is Right Mouse Down: %s", utils::isMouseDown(0) ? "true" : "false");
|
||||
if (ImGui::Button("Reset Camera")) {
|
||||
m_pCamera->SetPosition(Point2f { -m_viewport.width / 2, -m_viewport.height / 2 });
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
if (m_ShowPlayerInfo) {
|
||||
ImGui::Begin("Player Info", &m_ShowPlayerInfo, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
ImGui::Text("Player Position: (%f, %f)", m_player.GetPosition().x, m_player.GetPosition().y);
|
||||
ImGui::Text("Player Velocity: (%f, %f)", m_player.GetVelocity().x, m_player.GetVelocity().y);
|
||||
ImGui::End();
|
||||
m_player.ProcessImGui();
|
||||
}
|
||||
}
|
||||
45
Game/Levels/World/WorldLevel.h
Normal file
45
Game/Levels/World/WorldLevel.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include "Collision.h"
|
||||
#include "Levels/Level.h"
|
||||
#include "Player.h"
|
||||
#include "GridSystem/WorldGridManager.h"
|
||||
#include "Gui/Screens/ScreenManager.h"
|
||||
#include "Camera.h"
|
||||
|
||||
class WorldLevel : public Level
|
||||
{
|
||||
public:
|
||||
WorldLevel(Camera* camera, Rectf viewport);
|
||||
~WorldLevel() override;
|
||||
|
||||
WorldLevel(const WorldLevel& other) = default;
|
||||
WorldLevel(WorldLevel&& other) = default;
|
||||
|
||||
void Update(float elapsedSec) override;
|
||||
void Draw() const override;
|
||||
|
||||
void MouseMove(const Point2f& mousePos) override;
|
||||
void ProcessImGui() override;
|
||||
|
||||
WorldGridManager& GetGridManager() {
|
||||
return m_gridManager;
|
||||
}
|
||||
|
||||
std::vector<Collision::CollisionRect> m_Rects;
|
||||
|
||||
private:
|
||||
WorldGridManager m_gridManager {};
|
||||
Player m_player;
|
||||
Point2f m_mousePos {};
|
||||
|
||||
Rectf m_viewport;
|
||||
|
||||
ScreenManager* m_screenManager;
|
||||
|
||||
WorldTile* m_pSelectedTile { nullptr };
|
||||
|
||||
// ImGui Vars
|
||||
bool m_ShowTextureManagerWindow { true };
|
||||
bool m_ShowCameraWindow { false };
|
||||
bool m_ShowPlayerInfo { false };
|
||||
};
|
||||
Reference in New Issue
Block a user