Dragging implementation

CorrectiveAction
sigonasr2 1 year ago
parent a413e45842
commit 4a6847a0cf
  1. 13
      olcCodeJam2023Entry/Constant.cpp
  2. 16
      olcCodeJam2023Entry/Constant.h
  3. 22
      olcCodeJam2023Entry/Info.txt
  4. 79
      olcCodeJam2023Entry/Unit.cpp
  5. 16
      olcCodeJam2023Entry/Unit.h
  6. 43
      olcCodeJam2023Entry/VirusAttack.cpp
  7. 7
      olcCodeJam2023Entry/VirusAttack.h
  8. BIN
      olcCodeJam2023Entry/assets/selection_circle.png
  9. 5
      olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj
  10. 12
      olcCodeJam2023Entry/olcCodeJam2023Entry.vcxproj.filters

@ -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;

@ -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;
};

@ -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

@ -1,6 +1,5 @@
#include "Unit.h" #include "Unit.h"
#include "Constant.h"
BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly) BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly)
:Unit({ :Unit({
@ -12,6 +11,23 @@ BasicUnit::BasicUnit(vf2d pos,Renderable&img,bool friendly)
},pos,img,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::vector<Memory>memory,vf2d pos,Renderable&img,bool friendly) Unit::Unit(std::vector<Memory>memory,vf2d pos,Renderable&img,bool friendly)
@ -50,7 +66,41 @@ Unit::Unit(std::vector<Memory>memory,vf2d pos,Renderable&img,bool friendly)
void Unit::Draw(PixelGameEngine*pge){ 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;i<GetMemorySize();i++){
CheckColor(i,col);
pge->FillRectDecal({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){ int Unit::GetBits(Marker&m){
@ -87,7 +137,7 @@ int Unit::GetMemorySize(){
return memory.size(); 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; u.memory[0]=0;
return u; 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;
} }

@ -30,8 +30,14 @@ public:
int GetProcedure(); int GetProcedure();
int GetMemorySize(); int GetMemorySize();
std::vector<bool>memory; std::vector<bool>memory;
virtual void Update(float fElapsedTime)=0; void Update(float fElapsedTime);
virtual void Attack(Unit&victim)=0;
virtual void Draw(PixelGameEngine*pge); virtual void Draw(PixelGameEngine*pge);
bool IsFriendly();
bool IsSelected();
void Select();
void Deselect();
vf2d GetPos();
protected: protected:
vf2d pos; vf2d pos;
bool friendly; bool friendly;
@ -43,9 +49,15 @@ protected:
Marker procedure; Marker procedure;
private: private:
int GetBits(Marker&m); int GetBits(Marker&m);
bool selected=false;
}; };
struct BasicUnit:Unit{ struct BasicUnit:Unit{
BasicUnit(vf2d pos,Renderable&img,bool friendly=false); 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;
}; };

@ -3,6 +3,7 @@
#define OLC_SOUNDWAVE #define OLC_SOUNDWAVE
#include "olcSoundWaveEngine.h" #include "olcSoundWaveEngine.h"
#include "VirusAttack.h" #include "VirusAttack.h"
#include "olcUTIL_Geometry2D.h"
VirusAttack::VirusAttack() VirusAttack::VirusAttack()
{ {
@ -11,16 +12,46 @@ VirusAttack::VirusAttack()
} }
bool VirusAttack::OnUserCreate(){ 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<BasicUnit>(vf2d{32,32},VIRUS_IMG1,true)); units.push_back(std::make_unique<BasicUnit>(vf2d{32,32},CONSTANT::VIRUS_IMG1,true));
for(int i=0;i<10;i++){
if(rand()%2==0){
units.push_back(std::make_unique<BasicUnit>(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false));
} else {
units.push_back(std::make_unique<BasicUnit2>(vf2d{float(rand()%ScreenWidth()),float(rand()%ScreenHeight())},CONSTANT::VIRUS_IMG1,false));
}
}
return true; return true;
} }
bool VirusAttack::OnUserUpdate(float fElapsedTime){ bool VirusAttack::OnUserUpdate(float fElapsedTime){
// Called once per frame, draws random coloured pixels // Called once per frame, draws random coloured pixels
if(GetMouse(0).bPressed){
for(std::unique_ptr<Unit>&u:units){
u->Deselect();
}
if(startingDragPos==CONSTANT::UNSELECTED){
startingDragPos=GetMousePos();
}
}
if(GetMouse(0).bReleased){
vf2d endDragPos=GetMousePos();
if(endDragPos.x<startingDragPos.x){std::swap(startingDragPos.x,endDragPos.x);}
if(endDragPos.y<startingDragPos.y){std::swap(startingDragPos.y,endDragPos.y);}
utils::geom2d::rect<float> selectionRegion(startingDragPos,endDragPos-startingDragPos);
for(std::unique_ptr<Unit>&u:units){
if(u->IsFriendly()){
if(utils::geom2d::overlaps(selectionRegion,u->GetPos())){
u->Select();
}
}
}
startingDragPos=CONSTANT::UNSELECTED;
}
for(std::unique_ptr<Unit>&u:units){ for(std::unique_ptr<Unit>&u:units){
u->Update(fElapsedTime); u->Update(fElapsedTime);
} }
@ -28,13 +59,17 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
for(std::unique_ptr<Unit>&u:units){ for(std::unique_ptr<Unit>&u:units){
u->Draw(this); u->Draw(this);
} }
if(startingDragPos!=CONSTANT::UNSELECTED){
FillRectDecal(startingDragPos,GetMousePos()-startingDragPos,{255,255,0,128});
}
return true; return true;
} }
int main() int main()
{ {
VirusAttack app; VirusAttack app;
if (app.Construct(240, 160, 4, 4)) if (app.Construct(426, 320, 4, 4))
app.Start(); app.Start();
return 0; return 0;
} }

@ -1,11 +1,14 @@
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#include "olcSoundWaveEngine.h" #include "olcSoundWaveEngine.h"
#include "Unit.h" #include "Unit.h"
#include "Constant.h"
class VirusAttack : public olc::PixelGameEngine class VirusAttack : public olc::PixelGameEngine
{ {
Renderable VIRUS_IMG1; private:
std::vector<std::unique_ptr<Unit>>units; std::vector<std::unique_ptr<Unit>>units;
vf2d startingDragPos=CONSTANT::UNSELECTED;
public: public:
VirusAttack(); VirusAttack();

Binary file not shown.

After

Width:  |  Height:  |  Size: 679 B

@ -131,6 +131,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Constant.h" />
<ClInclude Include="olcPGEX_PopUpMenu.h" /> <ClInclude Include="olcPGEX_PopUpMenu.h" />
<ClInclude Include="olcPGEX_QuickGUI.h" /> <ClInclude Include="olcPGEX_QuickGUI.h" />
<ClInclude Include="olcPGEX_SplashScreen.h" /> <ClInclude Include="olcPGEX_SplashScreen.h" />
@ -145,6 +146,7 @@
<ClInclude Include="VirusAttack.h" /> <ClInclude Include="VirusAttack.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Constant.cpp" />
<ClCompile Include="Unit.cpp" /> <ClCompile Include="Unit.cpp" />
<ClCompile Include="VirusAttack.cpp" /> <ClCompile Include="VirusAttack.cpp" />
</ItemGroup> </ItemGroup>
@ -154,6 +156,9 @@
<ItemGroup> <ItemGroup>
<Image Include="assets\MAINICON.ico" /> <Image Include="assets\MAINICON.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Text Include="Info.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

@ -13,6 +13,9 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
<Filter Include="Documents">
<UniqueIdentifier>{0cb4db37-8f50-4b8a-838f-dd0e6d0051e9}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="olcPixelGameEngine.h"> <ClInclude Include="olcPixelGameEngine.h">
@ -51,6 +54,9 @@
<ClInclude Include="Unit.h"> <ClInclude Include="Unit.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Constant.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="VirusAttack.cpp"> <ClCompile Include="VirusAttack.cpp">
@ -59,6 +65,9 @@
<ClCompile Include="Unit.cpp"> <ClCompile Include="Unit.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="Constant.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="olcCodeJam2023Entry.rc"> <ResourceCompile Include="olcCodeJam2023Entry.rc">
@ -70,4 +79,7 @@
<Filter>Resource Files</Filter> <Filter>Resource Files</Filter>
</Image> </Image>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Text Include="Info.txt" />
</ItemGroup>
</Project> </Project>
Loading…
Cancel
Save