Dragging implementation
This commit is contained in:
parent
a413e45842
commit
4a6847a0cf
13
olcCodeJam2023Entry/Constant.cpp
Normal file
13
olcCodeJam2023Entry/Constant.cpp
Normal file
@ -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;
|
16
olcCodeJam2023Entry/Constant.h
Normal file
16
olcCodeJam2023Entry/Constant.h
Normal file
@ -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;
|
||||
};
|
22
olcCodeJam2023Entry/Info.txt
Normal file
22
olcCodeJam2023Entry/Info.txt
Normal file
@ -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 "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::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){
|
||||
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){
|
||||
@ -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;
|
||||
}
|
@ -30,8 +30,14 @@ public:
|
||||
int GetProcedure();
|
||||
int GetMemorySize();
|
||||
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);
|
||||
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;
|
||||
};
|
@ -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<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;
|
||||
}
|
||||
|
||||
bool VirusAttack::OnUserUpdate(float fElapsedTime){
|
||||
// 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){
|
||||
u->Update(fElapsedTime);
|
||||
}
|
||||
@ -28,13 +59,17 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
|
||||
for(std::unique_ptr<Unit>&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;
|
||||
}
|
@ -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<std::unique_ptr<Unit>>units;
|
||||
|
||||
vf2d startingDragPos=CONSTANT::UNSELECTED;
|
||||
|
||||
public:
|
||||
VirusAttack();
|
||||
|
||||
|
BIN
olcCodeJam2023Entry/assets/selection_circle.png
Normal file
BIN
olcCodeJam2023Entry/assets/selection_circle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 679 B |
@ -131,6 +131,7 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Constant.h" />
|
||||
<ClInclude Include="olcPGEX_PopUpMenu.h" />
|
||||
<ClInclude Include="olcPGEX_QuickGUI.h" />
|
||||
<ClInclude Include="olcPGEX_SplashScreen.h" />
|
||||
@ -145,6 +146,7 @@
|
||||
<ClInclude Include="VirusAttack.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Constant.cpp" />
|
||||
<ClCompile Include="Unit.cpp" />
|
||||
<ClCompile Include="VirusAttack.cpp" />
|
||||
</ItemGroup>
|
||||
@ -154,6 +156,9 @@
|
||||
<ItemGroup>
|
||||
<Image Include="assets\MAINICON.ico" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="Info.txt" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
|
@ -13,6 +13,9 @@
|
||||
<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>
|
||||
</Filter>
|
||||
<Filter Include="Documents">
|
||||
<UniqueIdentifier>{0cb4db37-8f50-4b8a-838f-dd0e6d0051e9}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="olcPixelGameEngine.h">
|
||||
@ -51,6 +54,9 @@
|
||||
<ClInclude Include="Unit.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Constant.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="VirusAttack.cpp">
|
||||
@ -59,6 +65,9 @@
|
||||
<ClCompile Include="Unit.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Constant.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="olcCodeJam2023Entry.rc">
|
||||
@ -70,4 +79,7 @@
|
||||
<Filter>Resource Files</Filter>
|
||||
</Image>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Text Include="Info.txt" />
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
x
Reference in New Issue
Block a user