diff --git a/olcCodeJam2023Entry/Constant.cpp b/olcCodeJam2023Entry/Constant.cpp index 75c2481..32dbbc5 100644 --- a/olcCodeJam2023Entry/Constant.cpp +++ b/olcCodeJam2023Entry/Constant.cpp @@ -12,4 +12,6 @@ vf2d CONSTANT::UNSELECTED={-99,-99}; vi2d CONSTANT::TILE_SIZE={24,24}; vi2d CONSTANT::WORLD_SIZE={64,64}; -float CONSTANT::SCROLL_BOUNDARY=36; \ No newline at end of file +float CONSTANT::SCROLL_BOUNDARY=36; + +float CONSTANT::DEATH_FADE_TIME=1; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Constant.h b/olcCodeJam2023Entry/Constant.h index aa73b4f..565e23f 100644 --- a/olcCodeJam2023Entry/Constant.h +++ b/olcCodeJam2023Entry/Constant.h @@ -18,4 +18,6 @@ public: static vi2d WORLD_SIZE; static float SCROLL_BOUNDARY; + + static float DEATH_FADE_TIME; }; \ No newline at end of file diff --git a/olcCodeJam2023Entry/DeathAnimation.h b/olcCodeJam2023Entry/DeathAnimation.h new file mode 100644 index 0000000..381e455 --- /dev/null +++ b/olcCodeJam2023Entry/DeathAnimation.h @@ -0,0 +1,19 @@ +#pragma once +#include "olcPixelGameEngine.h" +#include "Constant.h" +#include "olcPGEX_TransformedView.h" + +class DeathAnimation{ + vf2d pos; + Renderable img; + Renderable&originalImg; + Renderable&matrixImg; + vf2d randomOffset; + float fadeTimer=CONSTANT::DEATH_FADE_TIME; + bool friendly; +public: + DeathAnimation(PixelGameEngine*pge,vf2d pos,Renderable&unitImg,Renderable&matrixImg,bool friendly); + void Update(float fElapsedTime); + void Draw(TileTransformedView&game,PixelGameEngine*pge); + bool IsDone(); +}; \ No newline at end of file diff --git a/olcCodeJam2023Entry/DeathAnimations.cpp b/olcCodeJam2023Entry/DeathAnimations.cpp new file mode 100644 index 0000000..d885aa5 --- /dev/null +++ b/olcCodeJam2023Entry/DeathAnimations.cpp @@ -0,0 +1,34 @@ +#include "DeathAnimation.h" + +DeathAnimation::DeathAnimation(PixelGameEngine*pge,vf2d pos,Renderable&unitImg,Renderable&matrixImg,bool friendly) +:pos(pos),matrixImg(matrixImg),originalImg(unitImg),friendly(friendly),randomOffset(rand()%168,rand()%168){ + img.Create(unitImg.Sprite()->width,unitImg.Sprite()->height); + pge->SetDrawTarget(img.Sprite()); + pge->DrawSprite({0,0},unitImg.Sprite()); + pge->SetDrawTarget(nullptr); + img.Decal()->Update(); +} +void DeathAnimation::Update(float fElapsedTime){ + fadeTimer=std::max(0.f,fadeTimer-fElapsedTime); +} +void DeathAnimation::Draw(TileTransformedView&game,PixelGameEngine*pge){ + pge->SetDrawTarget(img.Sprite()); + pge->Clear(BLANK); + 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){ + 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)); + } + } + } + img.Decal()->Update(); + pge->SetDrawTarget(nullptr); + game.DrawRotatedDecal(pos,img.Decal(),0,img.Sprite()->Size()/2,{1,1},friendly?Pixel{192,192,255,uint8_t((fadeTimer/CONSTANT::DEATH_FADE_TIME)*255)}:Pixel{255,192,192,uint8_t((fadeTimer/CONSTANT::DEATH_FADE_TIME)*255)}); +} + +bool DeathAnimation::IsDone(){ + return fadeTimer==0; +} \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index 6a3266e..6329b00 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -501,4 +501,8 @@ bool Unit::CanInteractWithAllies(){ bool Unit::CanInteractWithEnemies(){ return enemyInteractable; +} + +Renderable&Unit::GetImage(){ + return img; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 12c5b94..d72f682 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -58,6 +58,7 @@ public: void DrawRangeIndicator(PixelGameEngine*pge,TileTransformedView&game,std::map>&IMAGES); bool CanInteractWithEnemies(); bool CanInteractWithAllies(); + Renderable&GetImage(); std::vector& operator <<=(const int n){ for(int i=0;i(vf2d{128,128},IMAGES,true)); units.push_back(std::make_unique(vf2d{129,129},IMAGES,true)); units.push_back(std::make_unique(vf2d{130,130},IMAGES,true)); + units.push_back(std::make_unique(vf2d{130,140},IMAGES,true)); units.push_back(std::make_unique(vf2d{131,131},IMAGES,true)); units.push_back(std::make_unique(vf2d{132,132},IMAGES,true)); units.push_back(std::make_unique(vf2d{133,133},IMAGES,true)); @@ -328,6 +329,17 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ u->AttemptAttack(closestUnit,units); u->_Update(this); } + + std::erase_if(units,[&](std::shared_ptru){ + if(u->GetHealth()==0){ + deathAnimations.emplace_back(std::make_unique(this,u->GetPos(),u->GetImage(),*IMAGES[MATRIX],u->IsFriendly())); + return true; + } else { + return false; + } + }); + + game.DrawPartialDecal({0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,IMAGES[TILE]->Decal(),{0,0},CONSTANT::WORLD_SIZE*CONSTANT::TILE_SIZE,DARK_GREEN); for(auto&u:units){ @@ -337,6 +349,12 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ for(auto&u:units){ u->Draw(game,IMAGES); } + for(auto&deadUnit:deathAnimations){ + deadUnit->Update(fElapsedTime); + deadUnit->Draw(game,this); + } + + std::erase_if(deathAnimations,[](auto&u){return u->IsDone();}); for(auto&collectionPoint:collectionPoints){ collectionPoint->Update(this,*IMAGES[MATRIX]); diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index fa315ae..431aaef 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -4,6 +4,7 @@ #include "Unit.h" #include "Constant.h" #include "Image.h" +#include "DeathAnimation.h" struct Letter{ vf2d pos; @@ -27,6 +28,7 @@ class VirusAttack : public olc::PixelGameEngine private: std::vector>units; std::vector>collectionPoints; + std::vector>deathAnimations; std::map>IMAGES; diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj index 6fa7f16..b0599b7 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj @@ -132,6 +132,7 @@ + @@ -150,6 +151,7 @@ + diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters index d1f74a4..a73c447 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters @@ -66,6 +66,9 @@ Header Files + + Header Files + @@ -83,6 +86,9 @@ Source Files + + Source Files +