Lmao, git removed some things :>(

This commit is contained in:
Bram Verhulst
2024-03-25 09:30:59 +01:00
parent ad847355b5
commit 61d1d17795
8 changed files with 116 additions and 15 deletions

View File

@@ -765,6 +765,26 @@ bool utils::isKeyDown(int keycode) {
}
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) {
const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr);
if (pStates & SDL_BUTTON(button)) {

View File

@@ -107,6 +107,8 @@ namespace utils
#pragma endregion CollisionFunctionality
bool isKeyDown(SDL_Keycode keycode);
bool isKeyPressed(int keycode);
bool isMouseDown(int button);
bool isKeyUp(int keycode);
}

View File

@@ -25,3 +25,9 @@ Point2f Camera::TransformMouse(const Point2f& mousePos) const {
worldPos.y = Viewport.height - worldPos.y + m_Position.y / m_Scale;
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;
}

View File

@@ -1,5 +1,7 @@
#pragma once
class Player;
class Camera
{
public:
@@ -16,11 +18,15 @@ public:
void BeginRendering() const;
void EndRendering() const;
void SetTrackingPlayer(Player* player) { m_FollowingPlayer = player; }
Point2f TransformMouse (const Point2f& mousePos) const;
Point2f TransformWorld (const Point2f& worldPos) const;
Rectf Viewport = Rectf{ 0, 0, 846.f, 500.f };
//TODO: Remove this and make it some static
private:
Point2f m_Position;
float m_Scale;
Player* m_FollowingPlayer{ nullptr };
};

View File

@@ -1,4 +1,4 @@
#include "pch.h"
#include "imgui.h"
#include "Player.h"
#include <algorithm>
@@ -22,6 +22,21 @@ void Player::Draw() const {
utils::SetColor(Colors::PINK);
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) {
// m_Acc.y += m_Gravity.y;
@@ -29,13 +44,17 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
//
// Point2f nextPos = m_Position + m_Vel * elapsedTime;
// //collision checking
m_Vel = Point2f{0, -100};
//check for keys
if(utils::isKeyDown(SDL_SCANCODE_W)) {
m_Vel.y = 100;
m_Grounded = false;
}
if(utils::isKeyDown(SDL_SCANCODE_S)) {
if(utils::isKeyPressed(SDL_SCANCODE_S)) {
m_Vel.y = -100;
if(m_Grounded) {
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];
//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};
m_Position = Point2f{tileCenter.x - m_Size.x / 2, tileCenter.y - m_Size.y / 2 + 5};
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
}
@@ -55,8 +76,33 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
if(utils::isKeyDown(SDL_SCANCODE_D)) {
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;
Point2f intersectionPoint, normal;
@@ -64,8 +110,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
WorldGridManager& gridManager = level.GetGridManager();
for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
for(size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
for (int x { 0 }; x < WORLD_WIDTH; ++x) {
for(int y { 0 }; y < WORLD_HEIGHT; ++y) {
if(gridManager.GetTileAtIndex(x,y)->GetTileType() == GroundTileTypes::Dirt) {
gridManager.GetTileAtIndex(x,y)->m_Hightlight = false;
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, gridManager.GetTileAtIndex(x, y)->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
@@ -78,22 +124,32 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
return a.second < b.second;
});
//check if grounded
// if(contactTimes.size() == 0) {
// m_Grounded = false;
// }
for (std::pair<int, float> contact_time : contactTimes) {
int x = contact_time.first % WORLD_WIDTH;
int y = contact_time.first / WORLD_WIDTH;
WorldTile* world_tile = gridManager.GetTileAtIndex(x,y);
world_tile->m_Hightlight = true;
bool isDiggingCandidate = true;
Collision::CollisionRect tileRect = world_tile->GetCollisionRect().getCollisionRect();
//check if the tile is above the player
if(tileRect.pos.y > m_Position.y + m_Size.y) {
isDiggingCandidate = false;
}
//check if tile is left of player
if(tileRect.pos.x > m_Position.x + m_Size.x) {
isDiggingCandidate = false;
if(tileRect.pos.y + tileRect.size.y > m_Position.y) {
world_tile->m_Hightlight = true;
m_ContactMap[Collision::CollisionDirection::Right] = world_tile;
isDiggingCandidate = false;
}
}
//check if tile is right of player
if(tileRect.pos.x + tileRect.size.x < m_Position.x) {
@@ -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
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) {
//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_Grounded = true;
}
//Dig the tile
m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
m_Grounded = true;
}
}
}
//m_ContactMap[Collision::CollisionDirection::Bottom] = world_tile;
//m_Grounded = true;
@@ -123,4 +182,4 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_Position = m_Position + m_Vel * elapsedTime;
}
}

View File

@@ -34,4 +34,6 @@ private:
Point2f m_Gravity{ 0, -9.81f };
bool m_Grounded{ false };
bool m_DidJustDigRight{ false };
};

View File

@@ -23,9 +23,14 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport):
for (size_t y { 0 }; y < WORLD_HEIGHT; ++y) {
int actualX = x - WORLD_WIDTH / 2;
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() {
@@ -126,6 +131,7 @@ void WorldLevel::ProcessImGui() {
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu(std::to_string(utils::isKeyPressed(SDL_SCANCODE_S)).c_str())){}
ImGui::EndMainMenuBar();
}
@@ -160,6 +166,6 @@ void WorldLevel::ProcessImGui() {
// }
ImGui::End();
m_player.ProcessImGui();
}
}

View File

@@ -39,5 +39,5 @@ private:
// ImGui Vars
bool m_ShowTextureManagerWindow{ false };
bool m_ShowCameraWindow{ false };
bool m_ShowPlayerInfo{ false };
bool m_ShowPlayerInfo{ true };
};