#include #include "player.hpp" #include void Player::findFacingBlock(World* world){ Vector3f dir(sin(yaw)*cos(pitch), -sin(pitch), cos(yaw)*cos(pitch)); #define INCREASE 0.1 for(float c =0;c<20;c+=INCREASE){ Vector3f candidate = c*dir+pos; if(world->GetBlock(candidate)!=AIR){ facingBlock =Vector3i( candidate ); //A new block should be placed in the //block between the player and the facingBlock //that is next adjacent to the facingBlock newBlock =Vector3i((c-INCREASE)*dir+pos); isFacingBlock=true; return; } } isFacingBlock=false; } bool Player::isValidPos(Vector3f pos, World& world){ for(int i=0;i<8;i++){ if(world.GetBlock(Vector3i(hitBox[i]+pos))!=AIR) return false; } return true; } bool Player::move(Vector3f movement,World& world, bool alwaysMove){ Vector3f newPos =pos+movement; Vector3f newPosx =Vector3f(pos.x+movement.x, pos.y, pos.z); Vector3f newPosz =Vector3f(pos.x, pos.y, pos.z+movement.z); if(alwaysMove || isValidPos(newPos,world)){ pos=newPos; return true; }else if(isValidPos(newPosx, world)){ pos=newPosx; }else if(isValidPos(newPosz, world)){ pos=newPosz; } return false; } bool Player::isAllowedToPlaceBlock(Vector3i blockPos){ for(int i=0;i<8;i++){ if(Vector3i(pos+hitBox[i])==blockPos) return(false); } return true; }