From 5c211ce9b3799febbaf345002589dc9a39381e08 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 25 Aug 2023 18:04:06 -0500 Subject: [PATCH] Unit Collisions --- olcCodeJam2023Entry/Unit.cpp | 50 +++++++++++++++++++++++- olcCodeJam2023Entry/Unit.h | 19 +++++++++ olcCodeJam2023Entry/VirusAttack.cpp | 47 +++++++++++++++++++--- olcCodeJam2023Entry/VirusAttack.h | 2 +- olcCodeJam2023Entry/olcUTIL_Geometry2D.h | 4 +- 5 files changed, 112 insertions(+), 10 deletions(-) diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index 5765f61..a03de34 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -1,5 +1,6 @@ #include "Unit.h" #include "Constant.h" +#include "olcUTIL_Geometry2D.h" BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) :Unit({ @@ -25,7 +26,7 @@ BasicUnit2::BasicUnit2(vf2d pos,Renderable&img,bool friendly) },pos,img,friendly){} void BasicUnit2::Attack(Unit&victim){ - + } @@ -138,7 +139,20 @@ int Unit::GetMemorySize(){ } void Unit::Update(float fElapsedTime){ - + if(!target.expired()){ + auto ptrTarget=target.lock(); + if(!InRange(ptrTarget)){ + pos+=(ptrTarget->GetPos()-pos).norm()*GetMoveSpd()*24*fElapsedTime; + } else { + //TODO Attack here. + } + } else + if(targetLoc!=CONSTANT::UNSELECTED){ + float dist=geom2d::line(pos,targetLoc).length(); + if(dist>24){ + pos+=(targetLoc-pos).norm()*GetMoveSpd()*24*fElapsedTime; + } + } } Unit& operator <<(Unit&u,const int n){ @@ -176,4 +190,36 @@ void Unit::Deselect(){ vf2d Unit::GetPos(){ return pos; +} + +bool Unit::IsDead(){ + return dead; +} + +vf2d Unit::GetUnitSize(){ + return img.Sprite()->Size(); +} + +void Unit::SetTargetUnit(std::weak_ptrtarget){ + this->target=target; + this->targetLoc=CONSTANT::UNSELECTED; +} + +void Unit::SetTargetLocation(vf2d targetLoc){ + this->target.reset(); + this->targetLoc=targetLoc; +} + +bool Unit::InRange(std::shared_ptrtarget){ + float dist=geom2d::line(GetPos(),target->GetPos()).length(); + return dist<24*(GetRange()+1); +} + +bool Unit::InRange(vf2d pos){ + float dist=geom2d::line(GetPos(),pos).length(); + return dist<24*(GetRange()+1); +} + +void Unit::SetPos(vf2d newPos){ + pos=newPos; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 1f290fc..98088d0 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -1,6 +1,7 @@ #pragma once #include "olcPixelGameEngine.h" +#include "Constant.h" struct Marker{ size_t index; @@ -20,6 +21,13 @@ 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); @@ -38,6 +46,11 @@ public: void Select(); void Deselect(); vf2d GetPos(); + bool IsDead(); + vf2d GetUnitSize(); + void SetTargetUnit(std::weak_ptrtarget); + void SetTargetLocation(vf2d targetLoc); + void SetPos(vf2d newPos); protected: vf2d pos; bool friendly; @@ -47,9 +60,15 @@ protected: Marker atkSpd; Marker moveSpd; 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); }; struct BasicUnit:Unit{ diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 776cf4f..3e16b5e 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -29,7 +29,7 @@ bool VirusAttack::OnUserCreate(){ void VirusAttack::HandleDraggingSelection(){ if(GetMouse(0).bPressed){ - for(std::unique_ptr&u:units){ + for(auto&u:units){ u->Deselect(); } if(startingDragPos==CONSTANT::UNSELECTED){ @@ -40,10 +40,10 @@ void VirusAttack::HandleDraggingSelection(){ vf2d endDragPos=GetMousePos(); if(endDragPos.x selectionRegion(startingDragPos,endDragPos-startingDragPos); - for(std::unique_ptr&u:units){ + geom2d::rect selectionRegion(startingDragPos,endDragPos-startingDragPos); + for(auto&u:units){ if(u->IsFriendly()){ - if(utils::geom2d::overlaps(selectionRegion,u->GetPos())){ + if(geom2d::overlaps(selectionRegion,u->GetPos())){ u->Select(); } } @@ -61,11 +61,46 @@ void VirusAttack::DrawSelectionRectangle(){ bool VirusAttack::OnUserUpdate(float fElapsedTime){ HandleDraggingSelection(); - for(std::unique_ptr&u:units){ + if (GetMouse(1).bPressed){ + bool selectedTarget=false; + for(auto&u:units){ + if(!u->IsFriendly()){ + geom2d::rect unitRegion(u->GetPos()-u->GetUnitSize()/2,u->GetUnitSize()); + if(geom2d::overlaps(unitRegion,GetMousePos())){ + for(auto&u2:units){ + if(u2->IsFriendly()&&u2->IsSelected()){ + u2->SetTargetUnit(u); + } + } + selectedTarget=true; + break; + } + } + } + if(!selectedTarget){ + for(auto&u:units){ + if(u->IsFriendly()&&u->IsSelected()){ + u->SetTargetLocation(GetMousePos()); + } + } + } + } + + for(auto&u:units){ + for(auto&u2:units){ + 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); + } + } u->Update(fElapsedTime); } - for(std::unique_ptr&u:units){ + for(auto&u:units){ u->Draw(this); } diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 8166405..0c744cf 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -5,7 +5,7 @@ class VirusAttack : public olc::PixelGameEngine { private: - std::vector>units; + std::vector>units; vf2d startingDragPos=CONSTANT::UNSELECTED; void HandleDraggingSelection(); diff --git a/olcCodeJam2023Entry/olcUTIL_Geometry2D.h b/olcCodeJam2023Entry/olcUTIL_Geometry2D.h index 3b8f363..2842f50 100644 --- a/olcCodeJam2023Entry/olcUTIL_Geometry2D.h +++ b/olcCodeJam2023Entry/olcUTIL_Geometry2D.h @@ -1043,4 +1043,6 @@ namespace olc::utils::geom2d return {}; } -} \ No newline at end of file +} + +using namespace utils; \ No newline at end of file