mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-15 13:21:48 +01:00
73 lines
2.2 KiB
C++
73 lines
2.2 KiB
C++
#include "pch.h"
|
|
#include "Player.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include "colors.h"
|
|
#include "utils.h"
|
|
#include "WorldLevel.h"
|
|
|
|
Player::Player(const Point2f& Position) : m_Position(Position), m_Size(Point2f{40, 40})
|
|
{}
|
|
Collision::CollisionRect Player::GetCollisionRect() {
|
|
Collision::CollisionRect rect = {m_Position, m_Size, m_Vel};
|
|
return rect;
|
|
}
|
|
|
|
void Player::Draw() const {
|
|
utils::SetColor(Colors::RED);
|
|
utils::DrawRect(Rectf{m_Position.x, m_Position.y, m_Size.x, m_Size.y});
|
|
}
|
|
|
|
void Player::Update(float elapsedTime, WorldLevel& level) {
|
|
// m_Acc.y += m_Gravity.y;
|
|
// m_Vel.y = std::min(m_Vel.y, m_MaxSpeed);
|
|
//
|
|
// 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;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_S)) {
|
|
m_Vel.y = -100;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_A)) {
|
|
m_Vel.x = -100;
|
|
}
|
|
if(utils::isKeyDown(SDL_SCANCODE_D)) {
|
|
m_Vel.x = 100;
|
|
}
|
|
|
|
float t = 0, min_t = INFINITY;
|
|
Point2f intersectionPoint, normal;
|
|
|
|
std::vector<std::pair<int, float>> contactTimes{};
|
|
|
|
for (size_t x { 0 }; x < level.WORLD_WIDTH; ++x) {
|
|
for(size_t y { 0 }; y < level.WORLD_HEIGHT; ++y) {
|
|
if(level.GetTileAt(Point2f{(float)x,(float)y})->GetTileType() == GroundTileTypes::Dirt) {
|
|
if(Collision::DynamicRectVsRect(this->GetCollisionRect(), elapsedTime, level.GetTileAt(Point2f{(float)x, (float)y})->GetCollisionRect().getCollisionRect(), intersectionPoint, normal, t)) {
|
|
contactTimes.push_back(std::pair<int, float>{x + y * level.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) {
|
|
int x = contact_time.first % level.WORLD_WIDTH;
|
|
int y = contact_time.first / level.WORLD_WIDTH;
|
|
WorldTile* tile2 = level.GetTileAt(Point2f{(float) x, (float) y});
|
|
Collision::CollisionRect rect = tile2->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
|
|
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
|
|
}
|
|
|
|
m_Position = m_Position + m_Vel * elapsedTime;
|
|
|
|
} |