mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2026-02-04 09:19:19 +01:00
Lmao, git removed some things :>(
This commit is contained in:
@@ -765,6 +765,26 @@ bool utils::isKeyDown(int keycode) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
static bool s_KeyStates[256] = {false};
|
||||||
|
bool utils::isKeyPressed(int keycode) {
|
||||||
|
const Uint8* pStates = SDL_GetKeyboardState(nullptr);
|
||||||
|
if (pStates != nullptr) {
|
||||||
|
if(pStates[keycode] && !s_KeyStates[keycode]) {
|
||||||
|
s_KeyStates[keycode] = true;
|
||||||
|
return s_KeyStates[keycode];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool utils::isKeyUp(int keycode) {
|
||||||
|
const Uint8* pStates = SDL_GetKeyboardState(nullptr);
|
||||||
|
if(pStates[keycode] == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
bool utils::isMouseDown(int button) {
|
bool utils::isMouseDown(int button) {
|
||||||
const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr);
|
const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr);
|
||||||
if (pStates & SDL_BUTTON(button)) {
|
if (pStates & SDL_BUTTON(button)) {
|
||||||
|
|||||||
@@ -107,6 +107,8 @@ namespace utils
|
|||||||
#pragma endregion CollisionFunctionality
|
#pragma endregion CollisionFunctionality
|
||||||
|
|
||||||
bool isKeyDown(SDL_Keycode keycode);
|
bool isKeyDown(SDL_Keycode keycode);
|
||||||
|
bool isKeyPressed(int keycode);
|
||||||
bool isMouseDown(int button);
|
bool isMouseDown(int button);
|
||||||
|
bool isKeyUp(int keycode);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -25,3 +25,9 @@ Point2f Camera::TransformMouse(const Point2f& mousePos) const {
|
|||||||
worldPos.y = Viewport.height - worldPos.y + m_Position.y / m_Scale;
|
worldPos.y = Viewport.height - worldPos.y + m_Position.y / m_Scale;
|
||||||
return worldPos;
|
return worldPos;
|
||||||
}
|
}
|
||||||
|
Point2f Camera::TransformWorld(const Point2f& worldPos) const {
|
||||||
|
Point2f screenPos = worldPos;
|
||||||
|
screenPos.x = screenPos.x * m_Scale - m_Position.x;
|
||||||
|
screenPos.y = Viewport.height - screenPos.y * m_Scale - m_Position.y;
|
||||||
|
return screenPos;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
class Player;
|
||||||
|
|
||||||
class Camera
|
class Camera
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -16,11 +18,15 @@ public:
|
|||||||
void BeginRendering() const;
|
void BeginRendering() const;
|
||||||
void EndRendering() const;
|
void EndRendering() const;
|
||||||
|
|
||||||
|
void SetTrackingPlayer(Player* player) { m_FollowingPlayer = player; }
|
||||||
|
|
||||||
Point2f TransformMouse (const Point2f& mousePos) const;
|
Point2f TransformMouse (const Point2f& mousePos) const;
|
||||||
|
Point2f TransformWorld (const Point2f& worldPos) const;
|
||||||
Rectf Viewport = Rectf{ 0, 0, 846.f, 500.f };
|
Rectf Viewport = Rectf{ 0, 0, 846.f, 500.f };
|
||||||
//TODO: Remove this and make it some static
|
//TODO: Remove this and make it some static
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Point2f m_Position;
|
Point2f m_Position;
|
||||||
float m_Scale;
|
float m_Scale;
|
||||||
|
Player* m_FollowingPlayer{ nullptr };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "pch.h"
|
#include "imgui.h"
|
||||||
#include "Player.h"
|
#include "Player.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -22,6 +22,21 @@ void Player::Draw() const {
|
|||||||
utils::SetColor(Colors::PINK);
|
utils::SetColor(Colors::PINK);
|
||||||
utils::DrawRect(Rectf{m_Position.x, m_Position.y, m_Size.x, m_Size.y});
|
utils::DrawRect(Rectf{m_Position.x, m_Position.y, m_Size.x, m_Size.y});
|
||||||
}
|
}
|
||||||
|
void Player::ProcessImGui() {
|
||||||
|
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
|
||||||
|
ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false");
|
||||||
|
ImGui::Text("Did just dig right: %s", m_DidJustDigRight ? "true" : "false");
|
||||||
|
bool test = !utils::isKeyDown(SDL_SCANCODE_H);
|
||||||
|
ImGui::Text("Is Key Up H: %s", test ? "true" : "false");
|
||||||
|
|
||||||
|
//ContactMap
|
||||||
|
ImGui::Text("ContactMap:");
|
||||||
|
ImGui::Text("Top: %s", m_ContactMap[Collision::CollisionDirection::Top] != nullptr ? "true" : "false");
|
||||||
|
ImGui::Text("Bottom: %s", m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr ? "true" : "false");
|
||||||
|
ImGui::Text("Left: %s", m_ContactMap[Collision::CollisionDirection::Left] != nullptr ? "true" : "false");
|
||||||
|
ImGui::Text("Right: %s", m_ContactMap[Collision::CollisionDirection::Right] != nullptr ? "true" : "false");
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
void Player::Update(float elapsedTime, WorldLevel& level) {
|
void Player::Update(float elapsedTime, WorldLevel& level) {
|
||||||
// m_Acc.y += m_Gravity.y;
|
// m_Acc.y += m_Gravity.y;
|
||||||
@@ -29,13 +44,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
//
|
//
|
||||||
// Point2f nextPos = m_Position + m_Vel * elapsedTime;
|
// Point2f nextPos = m_Position + m_Vel * elapsedTime;
|
||||||
// //collision checking
|
// //collision checking
|
||||||
|
|
||||||
m_Vel = Point2f{0, -100};
|
m_Vel = Point2f{0, -100};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//check for keys
|
//check for keys
|
||||||
if(utils::isKeyDown(SDL_SCANCODE_W)) {
|
if(utils::isKeyDown(SDL_SCANCODE_W)) {
|
||||||
m_Vel.y = 100;
|
m_Vel.y = 100;
|
||||||
|
m_Grounded = false;
|
||||||
}
|
}
|
||||||
if(utils::isKeyDown(SDL_SCANCODE_S)) {
|
if(utils::isKeyPressed(SDL_SCANCODE_S)) {
|
||||||
m_Vel.y = -100;
|
m_Vel.y = -100;
|
||||||
if(m_Grounded) {
|
if(m_Grounded) {
|
||||||
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
if(m_ContactMap[Collision::CollisionDirection::Bottom] != nullptr) {
|
||||||
@@ -43,7 +62,9 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Bottom];
|
||||||
//center of tile
|
//center of tile
|
||||||
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||||
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2};
|
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5};
|
||||||
|
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +76,32 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
}
|
}
|
||||||
if(utils::isKeyDown(SDL_SCANCODE_D)) {
|
if(utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||||
m_Vel.x = 100;
|
m_Vel.x = 100;
|
||||||
|
if(m_Grounded && !m_DidJustDigRight) {
|
||||||
|
//Check if the player doesnt come from digging a tile
|
||||||
|
if(m_ContactMap[Collision::CollisionDirection::Right] != nullptr) {
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Right]->SetTileType(GroundTileTypes::Air);
|
||||||
|
WorldTile* tile = m_ContactMap[Collision::CollisionDirection::Right];
|
||||||
|
//center of tile
|
||||||
|
Point2f tileCenter = tile->GetCollisionRect().getCollisionRect().pos + tile->GetCollisionRect().getCollisionRect().size / 2;
|
||||||
|
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5};
|
||||||
|
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||||
|
m_DidJustDigRight = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(m_DidJustDigRight) {
|
||||||
|
if(!utils::isKeyDown(SDL_SCANCODE_D)) {
|
||||||
|
m_DidJustDigRight = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
|
||||||
|
m_Grounded = false;
|
||||||
|
|
||||||
|
|
||||||
float t = 0, min_t = INFINITY;
|
float t = 0, min_t = INFINITY;
|
||||||
Point2f intersectionPoint, normal;
|
Point2f intersectionPoint, normal;
|
||||||
@@ -64,8 +110,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
|
|
||||||
WorldGridManager& gridManager = level.GetGridManager();
|
WorldGridManager& gridManager = level.GetGridManager();
|
||||||
|
|
||||||
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||||
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
for(int y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||||
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
|
||||||
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
|
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
|
||||||
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||||
@@ -79,22 +125,32 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
return a.second < b.second;
|
return a.second < b.second;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//check if grounded
|
||||||
|
// if(contactTimes.size() == 0) {
|
||||||
|
// m_Grounded = false;
|
||||||
|
// }
|
||||||
|
|
||||||
for (std::pair<int, float> contact_time : contactTimes) {
|
for (std::pair<int, float> contact_time : contactTimes) {
|
||||||
int x = contact_time.first % WORLD_WIDTH;
|
int x = contact_time.first % WORLD_WIDTH;
|
||||||
int y = contact_time.first / WORLD_WIDTH;
|
int y = contact_time.first / WORLD_WIDTH;
|
||||||
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
|
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
|
||||||
world_tile->m_Hightlight = true;
|
|
||||||
|
|
||||||
bool isDiggingCandidate = true;
|
bool isDiggingCandidate = true;
|
||||||
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
|
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
|
||||||
//check if the tile is above the player
|
//check if the tile is above the player
|
||||||
if(tileRect.pos.y > m_Position.y + m_Size.y) {
|
if(tileRect.pos.y > m_Position.y + m_Size.y) {
|
||||||
|
|
||||||
isDiggingCandidate = false;
|
isDiggingCandidate = false;
|
||||||
}
|
}
|
||||||
//check if tile is left of player
|
//check if tile is left of player
|
||||||
if(tileRect.pos.x > m_Position.x + m_Size.x) {
|
if(tileRect.pos.x > m_Position.x + m_Size.x) {
|
||||||
|
if(tileRect.pos.y + tileRect.size.y > m_Position.y) {
|
||||||
|
world_tile->m_Hightlight = true;
|
||||||
|
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
|
||||||
isDiggingCandidate = false;
|
isDiggingCandidate = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
//check if tile is right of player
|
//check if tile is right of player
|
||||||
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
|
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
|
||||||
isDiggingCandidate = false;
|
isDiggingCandidate = false;
|
||||||
@@ -106,14 +162,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
|
|||||||
//check if the center of the player is between the centers of the tiles left and right
|
//check if the center of the player is between the centers of the tiles left and right
|
||||||
if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) {
|
if(m_Position.x + m_Size.x / 2 > tileRect.pos.x && m_Position.x + m_Size.x / 2 < tileRect.pos.x + tileRect.size.x) {
|
||||||
if(isDiggingCandidate) {
|
if(isDiggingCandidate) {
|
||||||
//Dig the tile
|
//check if the distance to the tile is less than the size of a tile
|
||||||
|
if(m_Position.y + tileRect.size.y > tileRect.pos.y) {
|
||||||
|
|
||||||
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||||
m_Grounded = true;
|
m_Grounded = true;
|
||||||
}
|
}
|
||||||
|
//Dig the tile
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
|
||||||
//m_Grounded = true;
|
//m_Grounded = true;
|
||||||
|
|
||||||
|
|||||||
@@ -34,4 +34,6 @@ private:
|
|||||||
Point2f m_Gravity{ 0, -9.81f };
|
Point2f m_Gravity{ 0, -9.81f };
|
||||||
|
|
||||||
bool m_Grounded{ false };
|
bool m_Grounded{ false };
|
||||||
|
|
||||||
|
bool m_DidJustDigRight{ false };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -23,9 +23,14 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
|
|||||||
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
|
||||||
int actualX = x - WORLD_WIDTH / 2;
|
int actualX = x - WORLD_WIDTH / 2;
|
||||||
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
|
Point2f pos = Point2f{ float(actualX * TILE_WIDTH), -float(y * TILE_HEIGHT) - TILE_HEIGHT};
|
||||||
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, GroundTileTypes::Dirt, TextureManager::GetInstance() });
|
GroundTileTypes type = rand() % 2 == 0 ? GroundTileTypes::Dirt : GroundTileTypes::Dirt;
|
||||||
|
m_gridManager.SetTileAtIndex(x,y, new WorldTile{ pos, type, TextureManager::GetInstance() });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//
|
||||||
|
// for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
|
||||||
|
// m_gridManager.GetTileAtIndex(x, 0)->SetTileType(GroundTileTypes::Dirt);
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
WorldLevel::~WorldLevel() {
|
WorldLevel::~WorldLevel() {
|
||||||
@@ -126,6 +131,7 @@ void WorldLevel::ProcessImGui() {
|
|||||||
}
|
}
|
||||||
ImGui::EndMenu();
|
ImGui::EndMenu();
|
||||||
}
|
}
|
||||||
|
if(ImGui::BeginMenu(std::to_string(utils::isKeyPressed(SDL_SCANCODE_S)).c_str())){}
|
||||||
ImGui::EndMainMenuBar();
|
ImGui::EndMainMenuBar();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,6 +166,6 @@ void WorldLevel::ProcessImGui() {
|
|||||||
// }
|
// }
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
|
m_player.ProcessImGui();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,5 +39,5 @@ private:
|
|||||||
// ImGui Vars
|
// ImGui Vars
|
||||||
bool m_ShowTextureManagerWindow{ false };
|
bool m_ShowTextureManagerWindow{ false };
|
||||||
bool m_ShowCameraWindow{ false };
|
bool m_ShowCameraWindow{ false };
|
||||||
bool m_ShowPlayerInfo{ false };
|
bool m_ShowPlayerInfo{ true };
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user