From 2ec0daf59ef5e1cee4b5a5e375d078e5f9cf6085 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 25 Aug 2023 22:32:20 -0500 Subject: [PATCH] Add in Transformed View. --- olcCodeJam2023Entry/Constant.cpp | 5 ++- olcCodeJam2023Entry/Constant.h | 3 ++ olcCodeJam2023Entry/Unit.cpp | 17 ++++++---- olcCodeJam2023Entry/Unit.h | 4 ++- olcCodeJam2023Entry/VirusAttack.cpp | 31 ++++++++++++++---- olcCodeJam2023Entry/VirusAttack.h | 8 +++++ olcCodeJam2023Entry/assets/tile.png | Bin 0 -> 1335 bytes olcCodeJam2023Entry/olcPGEX_TransformedView.h | 2 -- 8 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 olcCodeJam2023Entry/assets/tile.png diff --git a/olcCodeJam2023Entry/Constant.cpp b/olcCodeJam2023Entry/Constant.cpp index 261bd56..3238e81 100644 --- a/olcCodeJam2023Entry/Constant.cpp +++ b/olcCodeJam2023Entry/Constant.cpp @@ -10,4 +10,7 @@ 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 +Renderable CONSTANT::SELECTION_CIRCLE; + +vi2d CONSTANT::TILE_SIZE={24,24}; +vi2d CONSTANT::WORLD_SIZE={64,64}; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Constant.h b/olcCodeJam2023Entry/Constant.h index 4da8303..4072d6c 100644 --- a/olcCodeJam2023Entry/Constant.h +++ b/olcCodeJam2023Entry/Constant.h @@ -13,4 +13,7 @@ public: static vf2d UNSELECTED; static Renderable VIRUS_IMG1,SELECTION_CIRCLE; + + static vi2d TILE_SIZE; + static vi2d WORLD_SIZE; }; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index d9bfec1..bd620eb 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -66,7 +66,14 @@ Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) -void Unit::Draw(PixelGameEngine*pge){ +void Unit::Draw(TileTransformedView&game){ + game.DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255}:Pixel{255,192,192}); + if(IsSelected()){ + game.DrawRotatedDecal(pos,CONSTANT::SELECTION_CIRCLE.Decal(),0,CONSTANT::SELECTION_CIRCLE.Sprite()->Size()/2,vf2d(img.Sprite()->Size())/CONSTANT::SELECTION_CIRCLE.Sprite()->Size(),WHITE); + } +} + +void Unit::DrawHud(TileTransformedView&game){ 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; @@ -93,15 +100,11 @@ void Unit::Draw(PixelGameEngine*pge){ for(int i=0;iFillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, + game.FillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,memory[i]?col:col/4); - pge->DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, + game.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){ diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 9fda172..51e6c0b 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -1,6 +1,7 @@ #pragma once #include "olcPixelGameEngine.h" +#include "olcPGEX_TransformedView.h" #include "Constant.h" struct Marker{ @@ -33,7 +34,8 @@ public: std::vectormemory; void Update(float fElapsedTime); virtual void Attack(Unit&victim)=0; - virtual void Draw(PixelGameEngine*pge); + virtual void Draw(TileTransformedView&game); + virtual void DrawHud(TileTransformedView&game); bool IsFriendly(); bool IsSelected(); void Select(); diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index 61ad65f..71c9617 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -2,8 +2,12 @@ #include "olcPixelGameEngine.h" #define OLC_SOUNDWAVE #include "olcSoundWaveEngine.h" -#include "VirusAttack.h" +#define OLC_PGEX_TRANSFORMEDVIEW +#include "olcPGEX_TransformedView.h" #include "olcUTIL_Geometry2D.h" +#include "olcUTIL_Camera2D.h" + +#include "VirusAttack.h" VirusAttack::VirusAttack() { @@ -12,10 +16,13 @@ VirusAttack::VirusAttack() } bool VirusAttack::OnUserCreate(){ + game.Initialise(GetScreenSize()); CONSTANT::VIRUS_IMG1.Load("assets/unit.png"); CONSTANT::SELECTION_CIRCLE.Load("assets/selection_circle.png"); + TILE.Load("assets/tile.png",nullptr,false,false); + units.push_back(std::make_unique(vf2d{32,32},CONSTANT::VIRUS_IMG1,true)); for(int i=0;i<10;i++){ if(rand()%2==0){ @@ -33,11 +40,11 @@ void VirusAttack::HandleDraggingSelection(){ u->Deselect(); } if(startingDragPos==CONSTANT::UNSELECTED){ - startingDragPos=GetMousePos(); + startingDragPos=GetWorldMousePos(); } } if(GetMouse(0).bReleased){ - vf2d endDragPos=GetMousePos(); + vf2d endDragPos=GetWorldMousePos(); if(endDragPos.x selectionRegion(startingDragPos,endDragPos-startingDragPos); @@ -52,9 +59,13 @@ void VirusAttack::HandleDraggingSelection(){ } } +vf2d VirusAttack::GetWorldMousePos(){ + return game.ScreenToWorld(GetMousePos()); +} + void VirusAttack::DrawSelectionRectangle(){ if(startingDragPos!=CONSTANT::UNSELECTED){ - FillRectDecal(startingDragPos,GetMousePos()-startingDragPos,{255,255,0,128}); + game.FillRectDecal(startingDragPos,GetWorldMousePos()-startingDragPos,{255,255,0,128}); } } @@ -64,7 +75,7 @@ void VirusAttack::HandleRightClickMove(){ for(auto&u:units){ if(!u->IsFriendly()){ geom2d::rect unitRegion(u->GetPos()-u->GetUnitSize()/2,u->GetUnitSize()); - if(geom2d::overlaps(unitRegion,GetMousePos())){ + if(geom2d::overlaps(unitRegion,GetWorldMousePos())){ for(auto&u2:units){ if(u2->IsFriendly()&&u2->IsSelected()){ u2->SetTargetUnit(u); @@ -78,7 +89,7 @@ void VirusAttack::HandleRightClickMove(){ if(!selectedTarget){ for(auto&u:units){ if(u->IsFriendly()&&u->IsSelected()){ - u->SetTargetLocation(GetMousePos()); + u->SetTargetLocation(GetWorldMousePos()); } } } @@ -110,6 +121,8 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ HandleDraggingSelection(); HandleRightClickMove(); + game.HandlePanAndZoom(); + for(auto&u:units){ Unit*closestUnit=nullptr; float closestDist=999999; @@ -120,9 +133,13 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ u->AttemptAttack(closestUnit); u->Update(fElapsedTime); } + game.DrawPartialDecal({0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,TILE.Decal(),{0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,DARK_GREEN); for(auto&u:units){ - u->Draw(this); + u->Draw(game); + } + for(auto&u:units){ + u->DrawHud(game); } DrawSelectionRectangle(); diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 7c88e62..dc7182e 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -1,5 +1,6 @@ #include "olcPixelGameEngine.h" #include "olcSoundWaveEngine.h" +#include "olcPGEX_TransformedView.h" #include "Unit.h" #include "Constant.h" class VirusAttack : public olc::PixelGameEngine @@ -7,12 +8,19 @@ class VirusAttack : public olc::PixelGameEngine private: std::vector>units; + Renderable TILE; + + vf2d camera={213,160}; + + TileTransformedView game; + 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); + vf2d GetWorldMousePos(); public: VirusAttack(); diff --git a/olcCodeJam2023Entry/assets/tile.png b/olcCodeJam2023Entry/assets/tile.png new file mode 100644 index 0000000000000000000000000000000000000000..d0cd255c941628eec3b732497c3e28d25570697d GIT binary patch literal 1335 zcmV-71<3k|P)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=VH0~-u#heSvK000?uMObu0Z*6U5Zgc=ca%Ew3 zWn>_CX>@2HM@dakSAh-}0009HNkl7z5 zB~^2P&LSJj1VMn1{qyHf_=~?UfWSY%9Wua&|M50F!7)J|dfXV!;F?rWqQr;lc)Dwogy8JpXC3 zsJT4#V-F+ z*p*&x%1*p62heJa?ny36zb1Cf4d}rg@@qv$`&jpz0bRM)LS5*r{gd~Mg&Cdl39_EJ zWBQwgrA;UFcVqASpodK1h)~%N#Em$RljIU63d>!}PVB$|97~r&y!|gKf;%fQg$s4r zuO)0k2WI3Q*IP=H4%q`8JqTUwAq&)xO^?!iNe?XsVORQlY{FIhNF#2kVC&+Zy4h`7XRxf9918*TWf81!jRuampY(?E+Ma3kc tj9{bawQ^IP1-jM!4mk`R!N2|4@*k7N4wmdmYc>D?002ovPDHLkV1fcxOv3;G literal 0 HcmV?d00001 diff --git a/olcCodeJam2023Entry/olcPGEX_TransformedView.h b/olcCodeJam2023Entry/olcPGEX_TransformedView.h index e37af85..4e10242 100644 --- a/olcCodeJam2023Entry/olcPGEX_TransformedView.h +++ b/olcCodeJam2023Entry/olcPGEX_TransformedView.h @@ -82,8 +82,6 @@ #include "olcPixelGameEngine.h" - - namespace olc { class TransformedView : public olc::PGEX