From d4c87fd755efe2e335727382a794b11ff0aaf239 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Sun, 27 Aug 2023 15:18:54 -0500 Subject: [PATCH] Auto targeting for enemies and proper healing fixes for bit restorers. --- olcCodeJam2023Entry/DeathAnimations.cpp | 2 +- olcCodeJam2023Entry/Unit.cpp | 69 ++++++++++++++----------- olcCodeJam2023Entry/Unit.h | 11 ++-- olcCodeJam2023Entry/VirusAttack.cpp | 9 ++-- olcCodeJam2023Entry/VirusAttack.h | 2 +- 5 files changed, 53 insertions(+), 40 deletions(-) diff --git a/olcCodeJam2023Entry/DeathAnimations.cpp b/olcCodeJam2023Entry/DeathAnimations.cpp index d885aa5..72157c2 100644 --- a/olcCodeJam2023Entry/DeathAnimations.cpp +++ b/olcCodeJam2023Entry/DeathAnimations.cpp @@ -17,7 +17,7 @@ void DeathAnimation::Draw(TileTransformedView&game,PixelGameEngine*pge){ for(int y=0;yheight;y++){ for(int x=0;xwidth;x++){ Pixel col=originalImg.Sprite()->GetPixel(x,y); - if(col.a==255&&col.r<=(CONSTANT::DEATH_FADE_TIME-fadeTimer)*255){ + if(col.a==255&&col.r<=(fadeTimer/CONSTANT::DEATH_FADE_TIME)*255){ pge->Draw(x,y,matrixImg.Sprite()->GetPixel(x+randomOffset.x,y+randomOffset.y)); } else { pge->Draw(x,y,originalImg.Sprite()->GetPixel(x+randomOffset.x,y+randomOffset.y)); diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index 6329b00..c4f0711 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -85,7 +85,7 @@ void BitRestorer::Attack(Unit&victim,std::vector>&otherUni void BitRestorer::AttemptToHealOtherAllies(std::vector>&otherUnits){ std::vectoremptyMemoryPositions; for(auto&u:otherUnits){ - if(u->IsFriendly()&&InRange(u)){ + if(u.get()!=this&&u->IsFriendly()&&InRange(u)){ for(int i=0;iGetMemorySize();i++){ if(!u->memory[i]){ emptyMemoryPositions.emplace_back(i); @@ -324,6 +324,12 @@ int Unit::GetMemorySize(){ return memory.size(); } +void Unit::RunAI(PixelGameEngine*pge){} + +void Unit::_RunAI(PixelGameEngine*pge){ + RunAI(pge); +} + void Unit::_Update(PixelGameEngine*pge){ if(!target.expired()){ auto ptrTarget=target.lock(); @@ -339,25 +345,7 @@ void Unit::_Update(PixelGameEngine*pge){ } if(!IsFriendly()){ - if(changeDirTimer==0){ - changeDirTimer=rand()%30; - switch(rand()%4){ - case 0:{ - movementVel={16,0}; - }break; - case 1:{ - movementVel={0,16}; - }break; - case 2:{ - movementVel={-16,0}; - }break; - case 3:{ - movementVel={0,-16}; - }break; - } - } - SetPos(GetPos()+movementVel*pge->GetElapsedTime()); - changeDirTimer=std::max(0.f,changeDirTimer-pge->GetElapsedTime()); + _RunAI(pge); } if(!GhostInFogOfWar()&&InFogOfWar()){ @@ -449,30 +437,47 @@ void Unit::SetPos(vf2d newPos){ } } -void Unit::AttemptAttack(Unit*unit,std::vector>&otherUnits){ +void Unit::AttemptAttack(std::weak_ptrattacker,std::weak_ptrunit,std::vector>&otherUnits){ if(reloadTimer>0)return; - Unit*finalTarget=nullptr; - if(unit!=nullptr){ + std::weak_ptrfinalTarget; + if(!unit.expired()){ finalTarget=unit; if(!target.expired()){ auto ptrTarget=target.lock(); if(InRange(ptrTarget)){ - finalTarget=ptrTarget.get(); + finalTarget=ptrTarget; } } } - if(finalTarget!=nullptr){ - if(InRange(finalTarget)){ - _Attack(finalTarget,otherUnits); //Call the parent function first, followed by the child. + if(!finalTarget.expired()){ + if(InRange(finalTarget.lock())){ + _Attack(attacker,finalTarget,otherUnits); //Call the parent function first, followed by the child. } } } void Unit::Update(PixelGameEngine*pge){} -void Unit::_Attack(Unit*finalTarget,std::vector>&otherUnits){ - Attack(*finalTarget,otherUnits); - reloadTimer=1.f/(GetAtkSpd()/2.f); +void Unit::Attacked(std::weak_ptrattacker){} + +void Unit::_Attacked(std::weak_ptrattacker){ + Attacked(attacker); + if(attacker.lock()->IsFriendly()!=IsFriendly()&&CanInteractWithEnemies()){ + SetTargetUnit(attacker); + } +} + +void Unit::_Attack(std::weak_ptrattacker,std::weak_ptrfinalTarget,std::vector>&otherUnits){ + if(GetAtkSpd()>0){ + Attack(*finalTarget.lock(),otherUnits); + finalTarget.lock()->_Attacked(attacker); + reloadTimer=1.f/(GetAtkSpd()/2.f); + if(GetCurrentTarget().expired()&&!IsFriendly()){ + if(finalTarget.lock()->IsFriendly()!=IsFriendly()&&CanInteractWithEnemies()){ + SetTargetUnit(finalTarget); + } + } + } } bool Unit::InFogOfWar(){ @@ -505,4 +510,8 @@ bool Unit::CanInteractWithEnemies(){ Renderable&Unit::GetImage(){ return img; +} + +std::weak_ptrUnit::GetCurrentTarget(){ + return target; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index d72f682..00dbbbb 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -48,7 +48,7 @@ public: void SetTargetUnit(std::weak_ptrtarget); void SetTargetLocation(vf2d targetLoc); void SetPos(vf2d newPos); - void AttemptAttack(Unit*unit,std::vector>&otherUnits); + void AttemptAttack(std::weak_ptrattacker,std::weak_ptrunit,std::vector>&otherUnits); bool InFogOfWar(); bool GhostInFogOfWar(); void HideGhost(); @@ -59,6 +59,11 @@ public: bool CanInteractWithEnemies(); bool CanInteractWithAllies(); Renderable&GetImage(); + virtual void RunAI(PixelGameEngine*pge); + void _RunAI(PixelGameEngine*pge); + virtual void Attacked(std::weak_ptrattacker); + void _Attacked(std::weak_ptrattacker); + std::weak_ptrGetCurrentTarget(); std::vector& operator <<=(const int n){ for(int i=0;i>&otherUnits); - vf2d movementVel={0,0}; - float changeDirTimer=0; + void _Attack(std::weak_ptrattacker,std::weak_ptrfinalTarget,std::vector>&otherUnits); bool moveable=true; bool friendlyInteractable=false; bool enemyInteractable=true; diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 967efdf..8af67da 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -153,7 +153,8 @@ void VirusAttack::CollisionChecking(std::shared_ptru,std::shared_ptr } } -void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2){ +void VirusAttack::IdentifyClosestTarget(std::weak_ptr&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2){ + if(u==u2)return; bool canInteract; canInteract= (u->IsFriendly()&&u->CanInteractWithEnemies()&&!u2->IsFriendly())|| @@ -163,7 +164,7 @@ void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std: if(canInteract){ geom2d::lineunitLine(u->GetPos(),u2->GetPos()); if(unitLine.length() key){return key.second<=0;}); for(auto&u:units){ - Unit*closestUnit=nullptr; + std::weak_ptrclosestUnit; float closestDist=999999; for(auto&u2:units){ IdentifyClosestTarget(closestUnit,closestDist,u,u2); @@ -326,7 +327,7 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ } } } - u->AttemptAttack(closestUnit,units); + u->AttemptAttack(u,closestUnit,units); u->_Update(this); } diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 431aaef..ed18f5a 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -44,7 +44,7 @@ private: void DrawSelectionRectangle(); void HandleRightClickMove(); void CollisionChecking(std::shared_ptru,std::shared_ptru2); - void IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2); + void IdentifyClosestTarget(std::weak_ptr&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2); vf2d GetWorldMousePos(); void HandlePanAndZoom(float fElapsedTime); void DrawMinimap();