From 4a6847a0cfa53e5b9d5053c3fe85b27ecfa719ca Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 25 Aug 2023 16:42:22 -0500 Subject: [PATCH] Dragging implementation --- olcCodeJam2023Entry/Constant.cpp | 13 +++ olcCodeJam2023Entry/Constant.h | 16 ++++ olcCodeJam2023Entry/Info.txt | 22 +++++ olcCodeJam2023Entry/Unit.cpp | 79 +++++++++++++++++- olcCodeJam2023Entry/Unit.h | 16 +++- olcCodeJam2023Entry/VirusAttack.cpp | 43 +++++++++- olcCodeJam2023Entry/VirusAttack.h | 7 +- .../assets/selection_circle.png | Bin 0 -> 679 bytes .../olcCodeJam2023Entry.vcxproj | 5 ++ .../olcCodeJam2023Entry.vcxproj.filters | 12 +++ 10 files changed, 201 insertions(+), 12 deletions(-) create mode 100644 olcCodeJam2023Entry/Constant.cpp create mode 100644 olcCodeJam2023Entry/Constant.h create mode 100644 olcCodeJam2023Entry/Info.txt create mode 100644 olcCodeJam2023Entry/assets/selection_circle.png diff --git a/olcCodeJam2023Entry/Constant.cpp b/olcCodeJam2023Entry/Constant.cpp new file mode 100644 index 0000000..261bd56 --- /dev/null +++ b/olcCodeJam2023Entry/Constant.cpp @@ -0,0 +1,13 @@ +#include "Constant.h" + +vf2d CONSTANT::BAR_SQUARE_SIZE={4,4}; +Pixel CONSTANT::HEALTH_COLOR={235, 210, 52}; +Pixel CONSTANT::RANGE_COLOR={52, 235, 89}; +Pixel CONSTANT::ATKSPD_COLOR={140, 21, 13}; +Pixel CONSTANT::MOVESPD_COLOR={11, 135, 212}; +Pixel CONSTANT::PROCEDURE_COLOR={212, 11, 162}; + +vf2d CONSTANT::UNSELECTED={-99,-99}; + +Renderable CONSTANT::VIRUS_IMG1; +Renderable CONSTANT::SELECTION_CIRCLE; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Constant.h b/olcCodeJam2023Entry/Constant.h new file mode 100644 index 0000000..4da8303 --- /dev/null +++ b/olcCodeJam2023Entry/Constant.h @@ -0,0 +1,16 @@ +#pragma once +#include "olcPixelGameEngine.h" + +class CONSTANT{ +public: + static vf2d BAR_SQUARE_SIZE; + static Pixel HEALTH_COLOR; + static Pixel RANGE_COLOR; + static Pixel ATKSPD_COLOR; + static Pixel MOVESPD_COLOR; + static Pixel PROCEDURE_COLOR; + + static vf2d UNSELECTED; + + static Renderable VIRUS_IMG1,SELECTION_CIRCLE; +}; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Info.txt b/olcCodeJam2023Entry/Info.txt new file mode 100644 index 0000000..f5ede8a --- /dev/null +++ b/olcCodeJam2023Entry/Info.txt @@ -0,0 +1,22 @@ +Tutorial +(Grey out non-important bars) + +Level 1: Basic Unit and Controls, move units towards enemy unit +Level 2: Introduce another Unit +Level 3: +Level 4: +Level 5: +Level 6: + +(Introduce one unit at a time) + +Memory Limits + +Minimap +Pause / Slow down time (Hotkeys to speed up/slow down time) + +Stage 1 +Stage 2 +Stage 3 +Stage 4 +Stage 5 - Hacking a Network diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index 8ebbfaf..5765f61 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -1,6 +1,5 @@ #include "Unit.h" - - +#include "Constant.h" BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) :Unit({ @@ -12,6 +11,23 @@ BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) },pos,img,friendly){} +void BasicUnit::Attack(Unit&victim){ + +} + +BasicUnit2::BasicUnit2(vf2d pos,Renderable&img,bool friendly) + :Unit({ + {RANGE,2}, + {ATKSPD,2}, + {MOVESPD,3}, + {PROCEDURE,1}, + {HEALTH,4}, + },pos,img,friendly){} + +void BasicUnit2::Attack(Unit&victim){ + +} + Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) @@ -50,7 +66,41 @@ Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) void Unit::Draw(PixelGameEngine*pge){ - pge->DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2); + int initialBarX=pos.x-GetMemorySize()/2*CONSTANT::BAR_SQUARE_SIZE.x; + int initialBarY=pos.y-CONSTANT::BAR_SQUARE_SIZE.y-img.Sprite()->height/2-2; + Pixel col=0; + + + auto CheckColor=[&](int i,Pixel&col){ + if(health.index==i){ + col=CONSTANT::HEALTH_COLOR; + } + if(range.index==i){ + col=CONSTANT::RANGE_COLOR; + } + if(atkSpd.index==i){ + col=CONSTANT::ATKSPD_COLOR; + } + if(moveSpd.index==i){ + col=CONSTANT::MOVESPD_COLOR; + } + if(procedure.index==i){ + col=CONSTANT::PROCEDURE_COLOR; + } + }; + + for(int i=0;iFillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, + float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,col); + pge->DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, + float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,BLACK); + } + pge->DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255}:Pixel{255,192,192}); + if(IsSelected()){ + pge->DrawRotatedDecal(pos,CONSTANT::SELECTION_CIRCLE.Decal(),0,CONSTANT::SELECTION_CIRCLE.Sprite()->Size()/2,vf2d(img.Sprite()->Size())/CONSTANT::SELECTION_CIRCLE.Sprite()->Size(),WHITE); + } } int Unit::GetBits(Marker&m){ @@ -87,7 +137,7 @@ int Unit::GetMemorySize(){ return memory.size(); } -void BasicUnit::Update(float fElapsedTime){ +void Unit::Update(float fElapsedTime){ } @@ -105,4 +155,25 @@ Unit& operator >>(Unit&u,const int n){ } u.memory[0]=0; return u; +} + +bool Unit::IsFriendly(){ + return friendly; +} + + +bool Unit::IsSelected(){ + return selected; +} + +void Unit::Select(){ + selected=true; +} + +void Unit::Deselect(){ + selected=false; +} + +vf2d Unit::GetPos(){ + return pos; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 993d0ca..1f290fc 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -30,8 +30,14 @@ public: int GetProcedure(); int GetMemorySize(); std::vectormemory; - virtual void Update(float fElapsedTime)=0; + void Update(float fElapsedTime); + virtual void Attack(Unit&victim)=0; virtual void Draw(PixelGameEngine*pge); + bool IsFriendly(); + bool IsSelected(); + void Select(); + void Deselect(); + vf2d GetPos(); protected: vf2d pos; bool friendly; @@ -43,9 +49,15 @@ protected: Marker procedure; private: int GetBits(Marker&m); + bool selected=false; }; struct BasicUnit:Unit{ BasicUnit(vf2d pos,Renderable&img,bool friendly=false); - void Update(float fElapsedTime)override; + void Attack(Unit&victim)override; +}; + +struct BasicUnit2:Unit{ + BasicUnit2(vf2d pos,Renderable&img,bool friendly=false); + void Attack(Unit&victim)override; }; \ No newline at end of file diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 0536164..d67fe0e 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -3,6 +3,7 @@ #define OLC_SOUNDWAVE #include "olcSoundWaveEngine.h" #include "VirusAttack.h" +#include "olcUTIL_Geometry2D.h" VirusAttack::VirusAttack() { @@ -11,16 +12,46 @@ VirusAttack::VirusAttack() } bool VirusAttack::OnUserCreate(){ - // Called once at the start, so create things here - VIRUS_IMG1.Load("assets/unit.png"); + CONSTANT::VIRUS_IMG1.Load("assets/unit.png"); + CONSTANT::SELECTION_CIRCLE.Load("assets/selection_circle.png"); - units.push_back(std::make_unique(vf2d{32,32},VIRUS_IMG1,true)); + 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)); + } else { + units.push_back(std::make_unique(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false)); + } + } return true; } bool VirusAttack::OnUserUpdate(float fElapsedTime){ // Called once per frame, draws random coloured pixels + if(GetMouse(0).bPressed){ + for(std::unique_ptr&u:units){ + u->Deselect(); + } + if(startingDragPos==CONSTANT::UNSELECTED){ + startingDragPos=GetMousePos(); + } + } + if(GetMouse(0).bReleased){ + vf2d endDragPos=GetMousePos(); + if(endDragPos.x selectionRegion(startingDragPos,endDragPos-startingDragPos); + for(std::unique_ptr&u:units){ + if(u->IsFriendly()){ + if(utils::geom2d::overlaps(selectionRegion,u->GetPos())){ + u->Select(); + } + } + } + startingDragPos=CONSTANT::UNSELECTED; + } + for(std::unique_ptr&u:units){ u->Update(fElapsedTime); } @@ -28,13 +59,17 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ for(std::unique_ptr&u:units){ u->Draw(this); } + + if(startingDragPos!=CONSTANT::UNSELECTED){ + FillRectDecal(startingDragPos,GetMousePos()-startingDragPos,{255,255,0,128}); + } return true; } int main() { VirusAttack app; - if (app.Construct(240, 160, 4, 4)) + if (app.Construct(426, 320, 4, 4)) app.Start(); return 0; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index e327271..7a349f9 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -1,11 +1,14 @@ #include "olcPixelGameEngine.h" #include "olcSoundWaveEngine.h" #include "Unit.h" +#include "Constant.h" class VirusAttack : public olc::PixelGameEngine { - Renderable VIRUS_IMG1; - +private: std::vector>units; + + vf2d startingDragPos=CONSTANT::UNSELECTED; + public: VirusAttack(); diff --git a/olcCodeJam2023Entry/assets/selection_circle.png b/olcCodeJam2023Entry/assets/selection_circle.png new file mode 100644 index 0000000000000000000000000000000000000000..7cc4f2b86f6e5c16dd278923f51fc8d85d4ece4c GIT binary patch literal 679 zcmV;Y0$BZtP)EX>4Tx04R}tkv&MmKpe$iQ^g{c4(*`gkfAzR5EXHhDi*;)X)CnqU~=gfG-*gu zTpR`0f`cE6RR%KKJJsPzojkd?N82(+!JwgLr1s z(mC%FM_5@>h|h_~4Z0xlBiCh@-#C{X7INI`^*Ix48bqP{B7NHLM7{kVsJ*zu>xC6lWH zMvev4ph9x|;D7MDTeCPd=_Uo^K<|rfe~bcsyFja9+uz5w-8upM&%l+|@z?i4s zjut%vLfgQ_bw`u;fXf|V@JW{p$&vgtg<=tSKcjET1L0esf6eW!y^qreAWK~>-v9@P zz*vd0*FD}H=VG6&wgCwPv~i000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}0001fNkl5@N1sVF z@=-=00L<08naK_-EU6csm_*1uaAcp{fg%=A7yy&CCX9be?(yi82W4ziHtpDK(or08 zUM1u7TZ<~<7x-QvwTf4Ky%uHlqKiOwk-3}|?Yey6^pDJzp`ZC}IRHh@g5evnkOBYz N002ovPDHLkV1kn`B(eYi literal 0 HcmV?d00001 diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj index 76d384c..f87db2d 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj @@ -131,6 +131,7 @@ + @@ -145,6 +146,7 @@ + @@ -154,6 +156,9 @@ + + + diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters index 9b45082..9a54560 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters @@ -13,6 +13,9 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {0cb4db37-8f50-4b8a-838f-dd0e6d0051e9} + @@ -51,6 +54,9 @@ Header Files + + Header Files + @@ -59,6 +65,9 @@ Source Files + + Source Files + @@ -70,4 +79,7 @@ Resource Files + + + \ No newline at end of file