@@ -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 ( in t x { 0 } ; x < WORLD_WIDTH ; + + x ) {
for ( in t 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 ;
}
}