mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 12:21:48 +01:00
Added basic player / collisions in player
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "pch.h"
|
||||
#include <imgui.h>
|
||||
#include "pch.h"
|
||||
#include "WorldLevel.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -88,48 +89,50 @@ void WorldLevel::Update(float elapsedSec) {
|
||||
Point2f intersectionPoint, normal;
|
||||
|
||||
std::vector<std::pair<int, float>> contactTimes{};
|
||||
|
||||
for (size_t i { 1 }; i < m_Rects.size(); ++i) {
|
||||
if(Collision::DynamicRectVsRect(m_Rects[0], elapsedSec, m_Rects[i], intersectionPoint, normal, t)) {
|
||||
contactTimes.push_back(std::pair<int, float>{i, t});
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
|
||||
return a.second < b.second;
|
||||
});
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
Collision::ResolveDynamicRectVsRect(m_Rects[0], elapsedSec, &m_Rects[contact_time.first]);
|
||||
}
|
||||
|
||||
contactTimes.clear();
|
||||
|
||||
//loop over the worldTiles
|
||||
for (int x {0} ; x < WORLD_WIDTH; ++x) {
|
||||
for (int y {0}; y < WORLD_HEIGHT; ++y) {
|
||||
if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) {
|
||||
if(Collision::DynamicRectVsRect(m_Rects[0], elapsedSec, m_worldTiles[x][y]->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
|
||||
return a.second < b.second;
|
||||
});
|
||||
m_player.Update(elapsedSec, *this);
|
||||
|
||||
for (std::pair<int, float> contact_time : contactTimes) {
|
||||
WorldTile* tile = m_worldTiles[contact_time.first % WORLD_WIDTH][contact_time.first / WORLD_WIDTH];
|
||||
Collision::CollisionRect rect = tile->GetCollisionRect().getCollisionRect();
|
||||
Collision::ResolveDynamicRectVsRect(m_Rects[0], elapsedSec, &rect);
|
||||
//delete tile;
|
||||
//WorldTile* tileRect = tile->GetCollisionRect().tile;
|
||||
//tileRect->SetTileType(GroundTileTypes::Air);
|
||||
}
|
||||
// for (size_t i { 1 }; i < m_Rects.size(); ++i) {
|
||||
// if(Collision::DynamicRectVsRect(m_Rects[0], elapsedSec, m_Rects[i], intersectionPoint, normal, t)) {
|
||||
// contactTimes.push_back(std::pair<int, float>{i, t});
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
|
||||
// return a.second < b.second;
|
||||
// });
|
||||
//
|
||||
// for (std::pair<int, float> contact_time : contactTimes) {
|
||||
// Collision::ResolveDynamicRectVsRect(m_Rects[0], elapsedSec, &m_Rects[contact_time.first]);
|
||||
// }
|
||||
|
||||
m_Rects[0].pos = m_Rects[0].pos + m_Rects[0].vel * elapsedSec;
|
||||
//contactTimes.clear();
|
||||
|
||||
// //loop over the worldTiles
|
||||
// for (int x {0} ; x < WORLD_WIDTH; ++x) {
|
||||
// for (int y {0}; y < WORLD_HEIGHT; ++y) {
|
||||
// if(m_worldTiles[x][y]->GetTileType() == GroundTileTypes::Dirt) {
|
||||
// if(Collision::DynamicRectVsRect(m_Rects[0], elapsedSec, m_worldTiles[x][y]->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
||||
// contactTimes.push_back(std::pair<int, float>{x + y * WORLD_WIDTH, t});
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// std::sort(contactTimes.begin(), contactTimes.end(), [](const std::pair<int, float>& a, const std::pair<int, float>& b) {
|
||||
// return a.second < b.second;
|
||||
// });
|
||||
//
|
||||
// for (std::pair<int, float> contact_time : contactTimes) {
|
||||
// WorldTile* tile = m_worldTiles[contact_time.first % WORLD_WIDTH][contact_time.first / WORLD_WIDTH];
|
||||
// Collision::CollisionRect rect = tile->GetCollisionRect().getCollisionRect();
|
||||
// Collision::ResolveDynamicRectVsRect(m_Rects[0], elapsedSec, &rect);
|
||||
// //delete tile;
|
||||
// //WorldTile* tileRect = tile->GetCollisionRect().tile;
|
||||
// //tileRect->SetTileType(GroundTileTypes::Air);
|
||||
// }
|
||||
//
|
||||
// m_Rects[0].pos = m_Rects[0].pos + m_Rects[0].vel * elapsedSec;
|
||||
|
||||
}
|
||||
void WorldLevel::Draw() const {
|
||||
@@ -139,11 +142,6 @@ void WorldLevel::Draw() const {
|
||||
utils::DrawRect(rect.pos, rect.size.x, rect.size.y);
|
||||
}
|
||||
|
||||
//Rectf expanded = Rectf{ m_Rects[1].bottomLeft.x - m_Rects[0].width / 2, m_Rects[1].bottomLeft.y - m_Rects[0].height / 2, m_Rects[1].width + m_Rects[0].width, m_Rects[1].height + m_Rects[0].height };
|
||||
//utils::SetColor(Colors::RED);
|
||||
//utils::DrawRect(expanded);
|
||||
|
||||
|
||||
Point2f RayPoint = Point2f{m_pCamera->Viewport.width, m_pCamera->Viewport.height};
|
||||
Point2f RayDir = Point2f{m_mousePos.x - RayPoint.x, m_mousePos.y - RayPoint.y};
|
||||
|
||||
@@ -152,18 +150,6 @@ void WorldLevel::Draw() const {
|
||||
|
||||
utils::FillEllipse(m_mousePos, 20, 20);
|
||||
|
||||
// Point2f intersectionPoint, normal;
|
||||
// float contactTime;
|
||||
// Rectf testRect = Rectf{m_Rects[1].bottomLeft.x, m_Rects[1].bottomLeft.y, m_Rects[1].width, m_Rects[1].height};
|
||||
// if(utils::RayVsRect(RayPoint, RayDir, testRect, intersectionPoint, normal, contactTime) && contactTime > 0.0f && contactTime < 1.0f) {
|
||||
// utils::SetColor(Colors::GREEN);
|
||||
// utils::FillEllipse(intersectionPoint, 5, 5);
|
||||
// utils::DrawLine(intersectionPoint, Point2f{intersectionPoint.x + normal.x * 50, intersectionPoint.y + normal.y * 50});
|
||||
// }
|
||||
//
|
||||
|
||||
// utils::SetColor(Colors::YELLOW);
|
||||
// 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_worldTiles[x][y]->Draw();
|
||||
@@ -199,16 +185,25 @@ void WorldLevel::Draw() const {
|
||||
utils::SetColor(Colors::MAGENTA);
|
||||
utils::FillEllipse(0, 0, 5, 5);
|
||||
|
||||
//m_player.Draw();
|
||||
m_player.Draw();
|
||||
m_pCamera->EndRendering();
|
||||
|
||||
//utils::SetColor(Colors::WHITE);
|
||||
//m_pTextTexture->Draw(Point2f{ 0, 0 });
|
||||
|
||||
}
|
||||
void WorldLevel::MouseMove(const Point2f& mousePos) {
|
||||
m_mousePos = mousePos;
|
||||
}
|
||||
void WorldLevel::ProcessImGui() {
|
||||
ImGui::Begin("Player Info", nullptr, 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);
|
||||
//PLAYER COLLISIONS
|
||||
ImGui::Text("Player Collisions:");
|
||||
// for (std::pair<Collision::CollisionDirection, Collision::CollisionRect*> contact : m_player.GetContactMap()) {
|
||||
// ImGui::Text("Direction: %d", contact.first);
|
||||
// ImGui::Text("Position: (%f, %f)", contact.second->pos.x, contact.second->pos.y);
|
||||
// ImGui::Text("Size: (%f, %f)", contact.second->size.x, contact.second->size.y);
|
||||
// }
|
||||
ImGui::End();
|
||||
}
|
||||
WorldTile* WorldLevel::GetTileAt(const Point2f& pos) const {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -218,3 +213,5 @@ std::array<std::array<WorldTile*, WorldLevel::WORLD_WIDTH>, WorldLevel::WORLD_HE
|
||||
return m_worldTiles;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user