diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index a03de34..d9bfec1 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -13,7 +13,7 @@ BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) void BasicUnit::Attack(Unit&victim){ - + victim<<=1; } BasicUnit2::BasicUnit2(vf2d pos,Renderable&img,bool friendly) @@ -94,7 +94,7 @@ void Unit::Draw(PixelGameEngine*pge){ CheckColor(i,col); pge->FillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, - float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,col); + float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,memory[i]?col:col/4); pge->DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,BLACK); } @@ -153,22 +153,26 @@ void Unit::Update(float fElapsedTime){ pos+=(targetLoc-pos).norm()*GetMoveSpd()*24*fElapsedTime; } } + + reloadTimer=std::max(0.f,reloadTimer-fElapsedTime); } -Unit& operator <<(Unit&u,const int n){ +std::vector operator <<(Unit&u,const int n){ + std::vectortempMem=u.memory; for(int i=0;i>(Unit&u,const int n){ +std::vector operator >>(Unit&u,const int n){ + std::vectortempMem=u.memory; for(int i=1;i0)return; + Unit*finalTarget=nullptr; + if(unit!=nullptr){ + finalTarget=unit; + if(!target.expired()){ + auto ptrTarget=target.lock(); + if(InRange(ptrTarget)){ + finalTarget=ptrTarget.get(); + } + } + } + if(finalTarget!=nullptr){ + if(InRange(finalTarget->GetPos())){ + _Attack(finalTarget); //Call the parent function first, followed by the child. + } + } +} + +void Unit::_Attack(Unit*finalTarget){ + Attack(*finalTarget); + reloadTimer=1.f/GetAtkSpd(); } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 98088d0..9fda172 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -21,13 +21,6 @@ struct Memory{ int size; }; -struct Unit; - -struct UnitGroup{ - std::vector>group; - vf2d pos; -}; - struct Unit{ public: Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly=false); @@ -51,6 +44,24 @@ public: void SetTargetUnit(std::weak_ptrtarget); void SetTargetLocation(vf2d targetLoc); void SetPos(vf2d newPos); + void AttemptAttack(Unit*unit); + + + std::vector& operator <<=(const int n){ + for(int i=0;i& operator >>=(const int n){ + for(int i=GetMemorySize()-1;i>0;i--){ + memory[i]=memory[i-1]; + } + memory[0]=0; + return memory; + } protected: vf2d pos; bool friendly; @@ -62,13 +73,14 @@ protected: Marker procedure; std::weak_ptrtarget; vf2d targetLoc=CONSTANT::UNSELECTED; - std::weak_ptrgroup; private: int GetBits(Marker&m); bool selected=false; bool dead=false; bool InRange(std::shared_ptrtarget); bool InRange(vf2d pos); + float reloadTimer=0; + void _Attack(Unit*finalTarget); }; struct BasicUnit:Unit{ diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 3e16b5e..61ad65f 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -19,7 +19,7 @@ bool VirusAttack::OnUserCreate(){ units.push_back(std::make_unique(vf2d{32,32},CONSTANT::VIRUS_IMG1,true)); for(int i=0;i<10;i++){ if(rand()%2==0){ - units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false)); + units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,true)); } else { units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false)); } @@ -58,9 +58,7 @@ void VirusAttack::DrawSelectionRectangle(){ } } -bool VirusAttack::OnUserUpdate(float fElapsedTime){ - HandleDraggingSelection(); - +void VirusAttack::HandleRightClickMove(){ if (GetMouse(1).bPressed){ bool selectedTarget=false; for(auto&u:units){ @@ -85,18 +83,41 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ } } } +} + +void VirusAttack::CollisionChecking(std::shared_ptru,std::shared_ptru2){ + if(u!=u2&&geom2d::overlaps(geom2d::circle(u->GetPos(),u->GetUnitSize().x/2),geom2d::circle(u2->GetPos(),u2->GetUnitSize().x/2))){ + geom2d::linecollisionLine(u->GetPos(),u2->GetPos()); + float maxDist=u->GetUnitSize().x/2+u2->GetUnitSize().x/2; + float dist=maxDist-collisionLine.length(); + vf2d dir=collisionLine.vector().norm(); + u->SetPos(u->GetPos()-dir*dist/2); + u2->SetPos(u2->GetPos()+dir*dist/2); + } +} + +void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2){ + if(u->IsFriendly()!=u2->IsFriendly()){ + geom2d::lineunitLine(u->GetPos(),u2->GetPos()); + if(unitLine.length()(u->GetPos(),u->GetUnitSize().x/2),geom2d::circle(u2->GetPos(),u2->GetUnitSize().x/2))){ - geom2d::linecollisionLine(u->GetPos(),u2->GetPos()); - float maxDist=u->GetUnitSize().x/2+u2->GetUnitSize().x/2; - float dist=maxDist-collisionLine.length(); - vf2d dir=collisionLine.vector().norm(); - u->SetPos(u->GetPos()-dir*dist/2); - u2->SetPos(u2->GetPos()+dir*dist/2); - } + IdentifyClosestTarget(closestUnit,closestDist,u,u2); + CollisionChecking(u,u2); } + u->AttemptAttack(closestUnit); u->Update(fElapsedTime); } diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 0c744cf..7c88e62 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -10,6 +10,9 @@ private: vf2d startingDragPos=CONSTANT::UNSELECTED; void HandleDraggingSelection(); void DrawSelectionRectangle(); + void HandleRightClickMove(); + void CollisionChecking(std::shared_ptru,std::shared_ptru2); + void IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptru,std::shared_ptru2); public: VirusAttack();