mirror of
https://github.com/HowestDAE/dae16-VerhulstBram.git
synced 2025-12-16 12:21:48 +01:00
Added the Grid, Camera, Level system
Basic player Started (Barely) on the TextureManager And other fixes
This commit is contained in:
45
Game/Player.cpp
Normal file
45
Game/Player.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "pch.h"
|
||||
#include "Player.h"
|
||||
|
||||
#include "colors.h"
|
||||
#include "utils.h"
|
||||
#include "WorldLevel.h"
|
||||
|
||||
Player::Player(const Point2f& Position) : m_Position(Position), m_Size(Point2f{50, 20})
|
||||
{}
|
||||
|
||||
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, const WorldLevel& level) {
|
||||
Point2f acc{0, 0};
|
||||
acc.y += m_Gravity.y;
|
||||
|
||||
Point2f nextPos = m_Position + m_Vel * elapsedTime * acc * elapsedTime * elapsedTime;
|
||||
//collision checking
|
||||
auto tiles = level.GetAllTiles();
|
||||
for (int x{0}; x < WorldLevel::WORLD_WIDTH; ++x) {
|
||||
for (int y{0}; y < WorldLevel::WORLD_HEIGHT; ++y) {
|
||||
WorldTile* tile = tiles[x][y];
|
||||
if (tile->GetTileType() == GroundTileTypes::Dirt) {
|
||||
Rectf tileRect = Rectf{tile->GetPosition().x, tile->GetPosition().y, WorldLevel::TILE_WIDTH, WorldLevel::TILE_HEIGHT};
|
||||
if (utils::IsOverlapping(nextPos, m_Size, tileRect)) {
|
||||
//collision
|
||||
if (m_Vel.y < 0) {
|
||||
//collision from above
|
||||
m_Position.y = tileRect.bottom;
|
||||
m_Vel.y = 0;
|
||||
} else {
|
||||
m_Position.y = tileRect.bottom + m_Size.y;
|
||||
m_Vel.y = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_Vel = Point2f{m_Vel.x + float(acc.x * elapsedTime), m_Vel.y + float(acc.y * elapsedTime)};
|
||||
// m_Position += m_Vel * elapsedTime;
|
||||
m_Position = Point2f{m_Position.x + m_Vel.x * elapsedTime, m_Position.y + m_Vel.y * elapsedTime};
|
||||
}
|
||||
Reference in New Issue
Block a user