diff --git a/olcCodeJam2023Entry/TileManager.cpp b/olcCodeJam2023Entry/TileManager.cpp new file mode 100644 index 0000000..4e1309f --- /dev/null +++ b/olcCodeJam2023Entry/TileManager.cpp @@ -0,0 +1,3 @@ +#include "TileManager.h" + +std::map TileManager::visibleTiles; \ No newline at end of file diff --git a/olcCodeJam2023Entry/TileManager.h b/olcCodeJam2023Entry/TileManager.h new file mode 100644 index 0000000..42a0707 --- /dev/null +++ b/olcCodeJam2023Entry/TileManager.h @@ -0,0 +1,7 @@ +#pragma once +#include "olcPixelGameEngine.h" + +class TileManager{ +public: + static std::mapvisibleTiles; +}; \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.cpp b/olcCodeJam2023Entry/Unit.cpp index bd620eb..04ce039 100644 --- a/olcCodeJam2023Entry/Unit.cpp +++ b/olcCodeJam2023Entry/Unit.cpp @@ -1,6 +1,7 @@ #include "Unit.h" #include "Constant.h" #include "olcUTIL_Geometry2D.h" +#include "TileManager.h" BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) :Unit({ @@ -32,11 +33,12 @@ void BasicUnit2::Attack(Unit&victim){ Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) -:pos(pos),img(img),friendly(friendly){ +:pos(pos),ghostPos(pos),img(img),friendly(friendly){ int marker=0; for(Memory&mem:memory){ for(int i=0;imemory.push_back(true); + this->ghostMemory.push_back(true); } switch(mem.type){ case HEALTH:{ @@ -67,15 +69,15 @@ Unit::Unit(std::vectormemory,vf2d pos,Renderable&img,bool friendly) 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}); + game.DrawRotatedDecal(ghostPos,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); + game.DrawRotatedDecal(ghostPos,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; + int initialBarX=ghostPos.x-GetMemorySize()/2*CONSTANT::BAR_SQUARE_SIZE.x; + int initialBarY=ghostPos.y-CONSTANT::BAR_SQUARE_SIZE.y-img.Sprite()->height/2-2; Pixel col=0; @@ -101,7 +103,7 @@ void Unit::DrawHud(TileTransformedView&game){ CheckColor(i,col); game.FillRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, - float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,memory[i]?col:col/4); + float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,ghostMemory[i]?col:col/4); game.DrawRectDecal({float(initialBarX)+i*CONSTANT::BAR_SQUARE_SIZE.x, float(initialBarY)},CONSTANT::BAR_SQUARE_SIZE,BLACK); } @@ -145,7 +147,7 @@ void Unit::Update(float fElapsedTime){ if(!target.expired()){ auto ptrTarget=target.lock(); if(!InRange(ptrTarget)){ - pos+=(ptrTarget->GetPos()-pos).norm()*GetMoveSpd()*24*fElapsedTime; + SetPos(GetPos()+(ptrTarget->GetPos()-pos).norm()*GetMoveSpd()*24*fElapsedTime); } else { //TODO Attack here. } @@ -153,10 +155,32 @@ void Unit::Update(float fElapsedTime){ if(targetLoc!=CONSTANT::UNSELECTED){ float dist=geom2d::line(pos,targetLoc).length(); if(dist>24){ - pos+=(targetLoc-pos).norm()*GetMoveSpd()*24*fElapsedTime; + SetPos(GetPos()+(targetLoc-pos).norm()*GetMoveSpd()*24*fElapsedTime); } } + if(!IsFriendly()){ + if(changeDirTimer==0){ + changeDirTimer=rand()%30; + switch(rand()%4){ + case 0:{ + movementVel={16,0}; + }break; + case 1:{ + movementVel={0,16}; + }break; + case 2:{ + movementVel={-16,0}; + }break; + case 3:{ + movementVel={0,-16}; + }break; + } + } + SetPos(GetPos()+movementVel*fElapsedTime); + changeDirTimer=std::max(0.f,changeDirTimer-fElapsedTime); + } + reloadTimer=std::max(0.f,reloadTimer-fElapsedTime); } @@ -229,6 +253,9 @@ bool Unit::InRange(vf2d pos){ void Unit::SetPos(vf2d newPos){ pos=newPos; + if(!InFogOfWar()){ + ghostPos=pos; + } } void Unit::AttemptAttack(Unit*unit){ @@ -253,4 +280,20 @@ void Unit::AttemptAttack(Unit*unit){ void Unit::_Attack(Unit*finalTarget){ Attack(*finalTarget); reloadTimer=1.f/GetAtkSpd(); +} + +bool Unit::InFogOfWar(){ + return TileManager::visibleTiles.count(GetPos()/96)==0; +} + +bool Unit::GhostInFogOfWar(){ + return TileManager::visibleTiles.count(ghostPos/96)==0; +} + +void Unit::HideGhost(){ + ghostPos={99999,-99999}; +} + +vf2d Unit::GetGhostPos(){ + return ghostPos; } \ No newline at end of file diff --git a/olcCodeJam2023Entry/Unit.h b/olcCodeJam2023Entry/Unit.h index 51e6c0b..f48f76c 100644 --- a/olcCodeJam2023Entry/Unit.h +++ b/olcCodeJam2023Entry/Unit.h @@ -32,6 +32,7 @@ public: int GetProcedure(); int GetMemorySize(); std::vectormemory; + std::vectorghostMemory; void Update(float fElapsedTime); virtual void Attack(Unit&victim)=0; virtual void Draw(TileTransformedView&game); @@ -47,13 +48,19 @@ public: void SetTargetLocation(vf2d targetLoc); void SetPos(vf2d newPos); void AttemptAttack(Unit*unit); - + bool InFogOfWar(); + bool GhostInFogOfWar(); + void HideGhost(); + vf2d GetGhostPos(); std::vector& operator <<=(const int n){ for(int i=0;itarget; vf2d targetLoc=CONSTANT::UNSELECTED; private: + vf2d pos; + vf2d ghostPos; int GetBits(Marker&m); bool selected=false; bool dead=false; @@ -83,6 +94,8 @@ private: bool InRange(vf2d pos); float reloadTimer=0; void _Attack(Unit*finalTarget); + vf2d movementVel={0,0}; + float changeDirTimer=0; }; struct BasicUnit:Unit{ diff --git a/olcCodeJam2023Entry/VirusAttack.cpp b/olcCodeJam2023Entry/VirusAttack.cpp index ffbeec5..3603488 100644 --- a/olcCodeJam2023Entry/VirusAttack.cpp +++ b/olcCodeJam2023Entry/VirusAttack.cpp @@ -2,6 +2,7 @@ #define OLC_SOUNDWAVE #define OLC_PGEX_TRANSFORMEDVIEW #include "olcUTIL_Geometry2D.h" +#include "TileManager.h" #include "VirusAttack.h" @@ -132,7 +133,7 @@ void VirusAttack::DrawMinimap(){ } DrawRect((game.GetWorldOffset()/worldPixelSize*64),viewingTilesPct*64/game.GetWorldScale()); for(auto&u:units){ - FillRect(u->GetPos()/worldPixelSize*64,vf2d{2,2}*u->GetUnitSize()/24,u->IsFriendly()?GREEN:RED); + FillRect(u->GetGhostPos()/worldPixelSize*64,vf2d{2,2}*u->GetUnitSize()/24,u->IsFriendly()?GREEN:RED); } MINIMAP_OUTLINE.Decal()->Update(); SetDrawTarget(nullptr); @@ -179,11 +180,11 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ HandlePanAndZoom(fElapsedTime); HandleMinimapClick(); - for(auto&tile:visibleTiles){ + for(auto&tile:TileManager::visibleTiles){ tile.second-=fElapsedTime; } - std::erase_if(visibleTiles,[](std::pair key){return key.second<=0;}); + std::erase_if(TileManager::visibleTiles,[](std::pair key){return key.second<=0;}); for(auto&u:units){ Unit*closestUnit=nullptr; @@ -195,13 +196,18 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ if(u->IsFriendly()){ for(int y=-1;y<2;y++){ for(int x=-1;x<2;x++){ - visibleTiles[u->GetPos()/24/4+vi2d(x,y)]=5; + TileManager::visibleTiles[u->GetPos()/24/4+vi2d(x,y)]=5; } } } u->AttemptAttack(closestUnit); u->Update(fElapsedTime); } + for(auto&u:units){ + if(!u->GhostInFogOfWar()&&u->InFogOfWar()){ + u->HideGhost(); + } + } 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){ @@ -214,7 +220,7 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){ DrawSelectionRectangle(); for(int y=game.GetTopLeftTile().y/96-1;y<=game.GetBottomRightTile().y/96+1;y++){ for(int x=game.GetTopLeftTile().x/96-1;x<=game.GetBottomRightTile().x/96+1;x++){ - if(visibleTiles.count(vi2d{x,y})==0){ + if(TileManager::visibleTiles.count(vi2d{x,y})==0){ if(x>=0&&y>=0&&x<=CONSTANT::WORLD_SIZE.x*CONSTANT::TILE_SIZE.x&&y<=CONSTANT::WORLD_SIZE.y*CONSTANT::TILE_SIZE.y){ game.FillRectDecal(vf2d{float(x),float(y)}*96,{96,96},{0,0,0,128}); } diff --git a/olcCodeJam2023Entry/VirusAttack.h b/olcCodeJam2023Entry/VirusAttack.h index 93a530d..1c6871f 100644 --- a/olcCodeJam2023Entry/VirusAttack.h +++ b/olcCodeJam2023Entry/VirusAttack.h @@ -7,7 +7,6 @@ class VirusAttack : public olc::PixelGameEngine { private: std::vector>units; - std::mapvisibleTiles; Renderable TILE,MINIMAP_HUD,OUTLINE,MINIMAP_OUTLINE; diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj index f02d7bc..315b7cf 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj @@ -142,11 +142,13 @@ + + diff --git a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters index 340a711..9f7ce7e 100644 --- a/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters +++ b/olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters @@ -57,6 +57,9 @@ Header Files + + Header Files + @@ -68,6 +71,9 @@ Source Files + + Source Files +