This commit is contained in:
Bram Verhulst
2024-06-09 22:03:29 +02:00
parent d7389411f5
commit 5f1dcd5788
76 changed files with 1532 additions and 385 deletions

View File

@@ -18,43 +18,72 @@ Player::Player(const Vector2f& Position, TextureManager* manager) : m_Position(P
m_ContactMap[Collision::CollisionDirection::Left] = nullptr;
m_ContactMap[Collision::CollisionDirection::Right] = nullptr;
m_walkAnimation = new Animation(
m_WalkAnimation = new Animation(
manager->GetTexture("animations/player/player_walk.png"),
8, 0.1f, Rectf { 0, 0, 70, 70 }, true);
m_turnAnimation = new Animation(
m_TurnAnimation = new Animation(
manager->GetTexture("animations/player/player_turn.png"),
5, 0.07f, Rectf { 0, 0, 70, 70 }, false);
m_digStartAnimation = new Animation(
m_DigStartAnimation = new Animation(
manager->GetTexture("animations/player/player_dig_start.png"),
7, 0.07f, Rectf { 0, 0, 70, 70 }, false);
m_digAnimation = new Animation(
m_DigAnimation = new Animation(
manager->GetTexture("animations/player/player_dig.png"),
7, 0.05f, Rectf { 0, 0, 70, 70 }, true);
m_flyStartAnimation = new Animation(
m_FlyStartAnimation = new Animation(
manager->GetTexture("animations/player/player_fly_start.png"),
12, 0.05f, Rectf { 0, 0, 70, 70 }, false);
m_flyAnimation = new Animation(
m_FlyAnimation = new Animation(
manager->GetTexture("animations/player/player_fly.png"),
5, 0.01f, Rectf { 0, 0, 70, 70 }, true);
m_flyTurnAnimation = new Animation(
m_FlyTurnAnimation = new Animation(
manager->GetTexture("animations/player/player_fly_turn.png"),
3, 0.05f, Rectf { 0, 0, 70, 70 }, false);
m_currentAnimation = m_walkAnimation;
m_DieStartAnimation = new Animation(
manager->GetTexture("animations/player/player_die_start.png"),
11, 0.05f, Rectf { 0, 0, 100, 100 }, false);
m_DieLoopAnimation = new Animation(
manager->GetTexture("animations/player/player_die_loop.png"),
4, 0.05f, Rectf { 0, 0, 100, 100 }, true);
m_CurrentAnimation = m_WalkAnimation;
m_IdleSound = new SoundEffect("sound/idle.wav");
m_FlySound = new SoundEffect("sound/fly.wav");
m_DigSound = new SoundEffect("sound/drill.wav");
m_DigSound->SetVolume(64);
m_WalkSound = new SoundEffect("sound/walk.wav");
m_CurrentSound = m_IdleSound;
}
Player::~Player() {
delete m_walkAnimation;
delete m_turnAnimation;
delete m_digAnimation;
delete m_digStartAnimation;
delete m_WalkAnimation;
delete m_TurnAnimation;
delete m_DigAnimation;
delete m_DigStartAnimation;
delete m_flyStartAnimation;
delete m_flyAnimation;
delete m_flyTurnAnimation;
delete m_IdleSound;
delete m_FlySound;
delete m_DigSound;
delete m_WalkSound;
delete m_FlyStartAnimation;
delete m_FlyAnimation;
delete m_FlyTurnAnimation;
delete m_DieStartAnimation;
delete m_DieLoopAnimation;
for (Particle* particle : m_DigParticles) {
delete particle;
}
}
Collision::CollisionRect Player::GetCollisionRect() const {
@@ -81,13 +110,30 @@ void Player::Draw() const {
glRotatef(m_Direction == PlayerDirection::Left ? rotateOffset : -rotateOffset, 0, 0, 1);
}
{
m_currentAnimation->Draw(Vector2f { 0, 0 }, Rectf { 0, 0, frameWidth, frameWidth });
utils::SetColor(Colors::WHITE);
m_CurrentAnimation->Draw(Vector2f { 0, 0 }, Rectf { 0, 0, frameWidth, frameWidth });
utils::SetColor(Colors::GREEN);
}
glPopMatrix();
utils::FillEllipse(m_OutletPos + m_Position, 5, 5);
for (Particle* particle : m_DigParticles) {
utils::SetColor(Colors::WHITE);
particle->Draw();
}
for (Particle* particle : m_SmokeParticles) {
utils::SetColor(Colors::WHITE);
particle->Draw();
}
}
void Player::Die() {
m_IsDead = true;
m_CurrentAnimation = m_DieStartAnimation;
m_HasPlayedDeathAnimation = false;
}
void Player::ProcessImGui() {
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
@@ -152,6 +198,7 @@ void Player::Dig(Collision::CollisionDirection dir, WorldLevel& level) {
//Set the digging location in the center of the destination tile;
const WorldTile* tile = m_ContactMap[dir];
m_ToAddPoints = tile->GetTileType()->GetValue();
m_ToAddTile = tile->GetTileType();
//Add case for bottom because otherwise i clip through the floor
m_DigDestination = tile->GetPosition();
if (dir == Collision::Bottom) {
@@ -171,7 +218,10 @@ bool Player::CanDig(Collision::CollisionDirection dir, WorldLevel& level) {
return false;
}
GroundTileType type = *tile->GetTileType();
//TODO: Add a list of non diggable tiles
if (type == GroundTileTypeManager::GetInstance()->STONE) {
return false;
}
if (type == GroundTileTypeManager::GetInstance()->HARD_LEFT || type == GroundTileTypeManager::GetInstance()->HARD_MIDDLE || type == GroundTileTypeManager::GetInstance()->
HARD_RIGHT) {
@@ -180,14 +230,32 @@ bool Player::CanDig(Collision::CollisionDirection dir, WorldLevel& level) {
return true;
}
void Player::ChangeSound(SoundEffect* sound) {
m_PrevSound = m_CurrentSound;
// m_CurrentSound->Stop();
m_CurrentSound = sound;
}
void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_IsDead) {
m_CurrentAnimation->Update(elapsedTime);
if (m_CurrentAnimation == m_DieStartAnimation) {
if (m_CurrentAnimation->IsDone()) {
m_CurrentAnimation = m_DieLoopAnimation;
}
}
return;
}
m_BobTimer += elapsedTime;
if (m_BobTimer >= m_BobTime) {
m_BobUp = !m_BobUp;
m_BobTimer = 0.0f;
}
std::vector<Particle *> particlesToDelete {};
for (Particle* particle : m_DigParticles) {
particle->Update(elapsedTime);
@@ -200,17 +268,39 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
delete particle;
}
m_SmokeTimer += elapsedTime;
if (m_SmokeTimer >= m_SmokeTime) {
m_SmokeTimer = 0.0f;
Vector2f Dir { static_cast<float>(m_Direction == PlayerDirection::Left ? 30 : -30), 20 };
Particle* NewSmokeParticle = new Particle(m_OutletPos + m_Position, Dir, Vector2f { 0.0f, 9.81f * 30}, 5.f, TextureManager::GetInstance()->GetTexture("particles/smoke.png"));
NewSmokeParticle->SetFlipped(m_Direction == PlayerDirection::Left);
m_SmokeParticles.push_back(NewSmokeParticle);
}
std::vector<Particle*> smokeParticlesToDelete{};
for (Particle* particle : m_SmokeParticles) {
particle->Update(elapsedTime);
if (particle->IsDead()) {
smokeParticlesToDelete.push_back(particle);
}
}
for (Particle* particle : smokeParticlesToDelete) {
m_SmokeParticles.erase(std::remove(m_SmokeParticles.begin(), m_SmokeParticles.end(), particle), m_SmokeParticles.end());
delete particle;
}
//check for keys
if (m_State != PlayerState::Digging) {
if (utils::isKeyDown(SDL_SCANCODE_W)) {
m_State = PlayerState::Flying;
// m_Vel.y = m_Speed / 10;
m_Acc.y += m_Speed * 10;
if(!m_IsPropellorDeployed) {
m_currentAnimation = m_flyStartAnimation;
m_flyStartAnimation->SetFlipped(m_Direction == PlayerDirection::Right);
} else {
m_currentAnimation = m_flyAnimation;
if (!m_IsPropellorDeployed) {
m_CurrentAnimation = m_FlyStartAnimation;
m_FlyStartAnimation->SetFlipped(m_Direction == PlayerDirection::Right);
}
else {
m_CurrentAnimation = m_FlyAnimation;
}
m_Grounded = false;
}
@@ -218,8 +308,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_Grounded) {
if (this->CanDig(Collision::Bottom, level)) {
m_DigDirection = DigDirection::Down;
m_currentAnimation = m_digStartAnimation;
m_currentAnimation->Reset();
m_CurrentAnimation = m_DigStartAnimation;
m_CurrentAnimation->Reset();
m_IsDiggingPrimed = false;
this->Dig(Collision::CollisionDirection::Bottom, level);
}
@@ -227,15 +317,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
if (utils::isKeyDown(SDL_SCANCODE_A)) {
if (!m_IsTurning) {
m_walkAnimation->SetFlipped(false);
m_flyAnimation->SetFlipped(false);
m_WalkAnimation->SetFlipped(false);
m_FlyAnimation->SetFlipped(false);
}
m_Acc.x = -m_Speed;
if (m_Direction == PlayerDirection::Right) {
m_IsTurning = true;
m_currentAnimation = m_State == PlayerState::Walking ? m_turnAnimation : m_flyTurnAnimation;
m_currentAnimation->SetFlipped(true);
m_currentAnimation->Reset();
m_CurrentAnimation = m_State == PlayerState::Walking ? m_TurnAnimation : m_FlyTurnAnimation;
m_CurrentAnimation->SetFlipped(true);
m_CurrentAnimation->Reset();
}
m_Direction = PlayerDirection::Left;
if (m_Grounded && !m_DidJustDigLeft) {
@@ -255,16 +345,16 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (utils::isKeyDown(SDL_SCANCODE_D)) {
if (!m_IsTurning) {
m_walkAnimation->SetFlipped(true);
m_flyAnimation->SetFlipped(true);
m_WalkAnimation->SetFlipped(true);
m_FlyAnimation->SetFlipped(true);
}
m_Acc.x = m_Speed;
if (m_Direction == PlayerDirection::Left) {
m_IsTurning = true;
m_currentAnimation = m_State == PlayerState::Walking ? m_turnAnimation : m_flyTurnAnimation;
m_currentAnimation->SetFlipped(false);
m_currentAnimation->Reset();
m_CurrentAnimation = m_State == PlayerState::Walking ? m_TurnAnimation : m_FlyTurnAnimation;
m_CurrentAnimation->SetFlipped(false);
m_CurrentAnimation->Reset();
}
m_Direction = PlayerDirection::Right;
@@ -296,37 +386,39 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
m_Acc = Vector2f { 0, 0 };
m_currentAnimation->Update(elapsedTime);
m_CurrentAnimation->Update(elapsedTime);
if(m_State == PlayerState::Flying && m_Grounded) {
if (m_State == PlayerState::Flying && m_Grounded) {
m_State = PlayerState::Idle;
m_walkAnimation->SetFlipped(m_Direction == PlayerDirection::Right);
m_currentAnimation = m_walkAnimation;
m_WalkAnimation->SetFlipped(m_Direction == PlayerDirection::Right);
m_CurrentAnimation = m_WalkAnimation;
m_IsPropellorDeployed = false;
m_flyStartAnimation->Reset();
m_FlyStartAnimation->Reset();
}
if (m_currentAnimation->IsDone() && m_IsTurning) {
m_currentAnimation = m_State == PlayerState::Walking ? m_walkAnimation : m_flyAnimation;
if (m_CurrentAnimation->IsDone() && m_IsTurning) {
m_CurrentAnimation = m_State == PlayerState::Walking ? m_WalkAnimation : m_FlyAnimation;
m_IsTurning = false;
}
if(m_currentAnimation == m_flyStartAnimation) {
if(m_flyStartAnimation->IsDone()) {
if (m_CurrentAnimation == m_FlyStartAnimation) {
if (m_FlyStartAnimation->IsDone()) {
m_IsPropellorDeployed = true;
m_currentAnimation = m_flyAnimation;
} else {
m_currentAnimation = m_flyStartAnimation;
m_CurrentAnimation = m_FlyAnimation;
}
else {
m_CurrentAnimation = m_FlyStartAnimation;
}
}
if (m_currentAnimation == m_digStartAnimation) {
if (m_Direction == PlayerDirection::Left) {
m_OutletPos = m_OutLeftPos;
}
else {
m_OutletPos = m_OutRightPos;
}
#pragma region Collision
m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
m_ContactMap[Collision::CollisionDirection::Bottom] = nullptr;
@@ -393,7 +485,7 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect(); //TODO: fix this mess
Collision::CollisionRect rect = world_tile->GetCollisionRect().getCollisionRect();
Collision::ResolvePlayerVsRect(*this, elapsedTime, &rect);
}
#pragma endregion
@@ -408,25 +500,35 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
}
switch (m_State) {
case PlayerState::Flying:
GameManager::GetInstance().DecreaseFuel(0.06f);
ChangeSound(m_FlySound);
break;
case PlayerState::Idle:
m_walkAnimation->SetPlaying(false);
m_WalkAnimation->SetPlaying(false);
GameManager::GetInstance().DecreaseFuel(0.02f);
ChangeSound(m_IdleSound);
break;
case PlayerState::Walking:
m_walkAnimation->SetPlaying(true);
m_WalkAnimation->SetPlaying(true);
GameManager::GetInstance().DecreaseFuel(0.04f);
ChangeSound(m_WalkSound);
break;
case PlayerState::Digging: {
// m_walkAnimation->SetPlaying(false);
GameManager::GetInstance().DecreaseFuel(0.06f);
ChangeSound(m_DigSound);
//Diganimation
m_currentAnimation->Update(elapsedTime);
if (m_currentAnimation->IsDone() && m_State == PlayerState::Digging && !m_IsDiggingPrimed) {
m_CurrentAnimation->Update(elapsedTime);
if (m_CurrentAnimation->IsDone() && m_State == PlayerState::Digging && !m_IsDiggingPrimed) {
m_IsDiggingPrimed = true;
m_currentAnimation = m_digAnimation;
m_CurrentAnimation = m_DigAnimation;
}
if (m_IsDiggingPrimed) {
if (!m_Digging) { //TODO: fix for setting the start position
if (!m_Digging) {
m_Digging = true;
m_DigStart = m_Position;
m_Vel = Vector2f { 0, 0 };
@@ -436,9 +538,8 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
//lerp to the destination
float progress = utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 1.0f);
int particleProgress = (int)utils::map(m_DigProgress, 0.0f, m_DigTime, 0.0f, 100.0f);
std::cout << progress << '\n';
if (particleProgress % 2 == 0) {
m_DigParticles.push_back(new Particle(m_Position + Vector2f{ 20, 0 }, Vector2f { (float)utils::randRange(-200, 200), (float)utils::randRange(-100, -300) }, 5.f,
m_DigParticles.push_back(new Particle(m_Position + Vector2f { 20, 0 }, Vector2f { (float)utils::randRange(-200, 200), (float)utils::randRange(-100, -300) },Vector2f { 0.0f, -9.81f * 20}, 5.f,
TextureManager::GetInstance()->GetTexture("particles/dirt_" + std::to_string(utils::randRange(1, 8)) + ".png")));
}
m_Position = utils::lerp(m_DigStart, m_DigDestination, progress);
@@ -449,11 +550,30 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
}
if (progress >= 1.0f) {
m_State = PlayerState::Idle;
m_currentAnimation = m_walkAnimation;
m_CurrentAnimation = m_WalkAnimation;
m_HasDeletedTile = false;
m_Digging = false;
GameManager::GetInstance().IncreaseScore(m_ToAddPoints);
m_ToAddPoints = 0;
//Add the dug item to the inventory
PlayerInventory* inventory = GameManager::GetInstance().GetInventory();
InventoryItem item = inventory->GetItemByType(m_ToAddTile);
ItemStack stack = ItemStack { item, 1 };
inventory->AddItem(stack);
std::cout << "Added: " << PlayerInventory::GetItemName(item) << std::endl;
//Print the inventory
std::cout << "-----------------------" << std::endl;
std::cout << "Inventory: " << std::endl;
std::vector<ItemStack *> items = inventory->GetItems();
for (ItemStack* i : items) {
std::cout << PlayerInventory::GetItemName(i->m_ItemType) << " Quantity: " << i->m_Quantity << std::endl;
}
std::cout << "-----------------------" << std::endl;
}
}
@@ -466,4 +586,15 @@ void Player::Update(float elapsedTime, WorldLevel& level) {
if (m_State != PlayerState::Digging) {
m_Position = m_Position + m_Vel * elapsedTime;
}
if (m_DidSoundChange) {
m_CurrentSound->Stop();
m_DidSoundChange = false;
}
if (m_CurrentSound != nullptr) {
if (!m_CurrentSound->IsPlaying()) {
m_CurrentSound->Play(1);
}
}
}