Refactor unit costs.

CorrectiveAction
sigonasr2 1 year ago
parent 2273d11ef6
commit e6b31af7de
  1. 2
      olcCodeJam2023Entry/Constant.cpp
  2. 2
      olcCodeJam2023Entry/Constant.h
  3. 94
      olcCodeJam2023Entry/Unit.cpp
  4. 12
      olcCodeJam2023Entry/Unit.h
  5. 65
      olcCodeJam2023Entry/VirusAttack.cpp
  6. 1
      olcCodeJam2023Entry/VirusAttack.h

@ -29,3 +29,5 @@ float CONSTANT::DEBUFFICON_LIFETIME=0.8;
float CONSTANT::COLLECTION_WAIT_TIME=8;
int CONSTANT::MEMORY_ALLOCATOR_COST=5;
float CONSTANT::UNIT_BUILD_TIME=10;

@ -33,4 +33,6 @@ public:
static float COLLECTION_WAIT_TIME;
static int MEMORY_ALLOCATOR_COST;
static float UNIT_BUILD_TIME;
};

@ -5,67 +5,42 @@
#include "DebuffIcon.h"
#include "olcPGEX_QuickGUI.h"
std::vector<Memory> BasicUnit::resourceCost={{HEALTH,4},{RANGE,2},{ATKSPD,2},{MOVESPD,3},{PROCEDURE,1}};
BasicUnit::BasicUnit(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{HEALTH,4},
{RANGE,2},
{ATKSPD,2},
{MOVESPD,3},
{PROCEDURE,1},
},pos,12,*IMAGES[VIRUS_IMG1],WHITE,WHITE,friendly,moveable){}
:Unit(pge,BasicUnit::resourceCost,pos,12,*IMAGES[VIRUS_IMG1],WHITE,WHITE,friendly,moveable){}
void BasicUnit::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
victim<<=1;
}
std::vector<Memory> BasicUnit2::resourceCost={{RANGE,2},{ATKSPD,2},{MOVESPD,3},{PROCEDURE,1},{HEALTH,4}};
BasicUnit2::BasicUnit2(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{RANGE,2},
{ATKSPD,2},
{MOVESPD,3},
{PROCEDURE,1},
{HEALTH,4},
},pos,12,*IMAGES[VIRUS_IMG1],WHITE,WHITE,friendly,moveable){}
:Unit(pge,BasicUnit2::resourceCost,pos,12,*IMAGES[VIRUS_IMG1],WHITE,WHITE,friendly,moveable){}
void BasicUnit2::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
victim>>=1;
}
std::vector<Memory> LeftShifter::resourceCost={{RANGE,2},{ATKSPD,2},{MOVESPD,3},{PROCEDURE,1},{HEALTH,4}};
LeftShifter::LeftShifter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{RANGE,2},
{ATKSPD,2},
{MOVESPD,3},
{PROCEDURE,1},
{HEALTH,4},
},pos,12,*IMAGES[LEFT_SHIFTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
:Unit(pge,LeftShifter::resourceCost,pos,12,*IMAGES[LEFT_SHIFTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
void LeftShifter::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
victim<<=1;
}
std::vector<Memory> RightShifter::resourceCost={{HEALTH,4},{RANGE,2},{ATKSPD,2},{MOVESPD,3},{PROCEDURE,1}};
RightShifter::RightShifter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{HEALTH,4},
{RANGE,2},
{ATKSPD,2},
{MOVESPD,3},
{PROCEDURE,1},
},pos,12,*IMAGES[RIGHT_SHIFTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
:Unit(pge,RightShifter::resourceCost,pos,12,*IMAGES[RIGHT_SHIFTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
void RightShifter::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
victim>>=1;
}
std::vector<Memory> BitRestorer::resourceCost={{PROCEDURE,6},{RANGE,1},{ATKSPD,1},{MOVESPD,1},{HEALTH,2}};
BitRestorer::BitRestorer(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{PROCEDURE,6},
{RANGE,1},
{ATKSPD,1},
{MOVESPD,1},
{HEALTH,2},
},pos,12,*IMAGES[BIT_RESTORER],CONSTANT::HEALER_TARGET_COL,CONSTANT::HEALER_ATTACK_COL,friendly,moveable,true,false){}
:Unit(pge,BitRestorer::resourceCost,pos,12,*IMAGES[BIT_RESTORER],CONSTANT::HEALER_TARGET_COL,CONSTANT::HEALER_ATTACK_COL,friendly,moveable,true,false){}
void BitRestorer::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
std::vector<int>emptyMemoryPositions;
@ -103,14 +78,9 @@ void BitRestorer::AttemptToHealOtherAllies(std::vector<std::shared_ptr<Unit>>&ot
}
}
std::vector<Memory> MemorySwapper::resourceCost={{RANGE,3},{ATKSPD,1},{HEALTH,3},{PROCEDURE,3},{MOVESPD,2}};
MemorySwapper::MemorySwapper(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{RANGE,3},
{ATKSPD,1},
{HEALTH,3},
{PROCEDURE,3},
{MOVESPD,2},
},pos,12,*IMAGES[MEMORY_SWAPPER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable,true){
:Unit(pge,MemorySwapper::resourceCost,pos,12,*IMAGES[MEMORY_SWAPPER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable,true){
autoAcquireFriendlyTarget=false;
}
@ -118,14 +88,9 @@ void MemorySwapper::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherU
}
std::vector<Memory> Corrupter::resourceCost={{ATKSPD,3},{RANGE,1},{PROCEDURE,8},{MOVESPD,4},{HEALTH,4}};
Corrupter::Corrupter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{ATKSPD,3},
{RANGE,1},
{PROCEDURE,8},
{MOVESPD,4},
{HEALTH,4},
},pos,12,*IMAGES[CORRUPTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
:Unit(pge,Corrupter::resourceCost,pos,12,*IMAGES[CORRUPTER],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,moveable){}
void Corrupter::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits){
//Chooses a bit at random and corrupts it.
@ -133,14 +98,9 @@ void Corrupter::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits
victim.memory[randomBit]=victim.ghostMemory[randomBit]=false;
}
std::vector<Memory> MemoryAllocator::resourceCost={{RANGE,1},{ATKSPD,1},{MOVESPD,1},{PROCEDURE,1},{HEALTH,1}};
MemoryAllocator::MemoryAllocator(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly,bool moveable)
:Unit(pge,{
{RANGE,1},
{ATKSPD,1},
{MOVESPD,1},
{PROCEDURE,1},
{HEALTH,1},
},pos,12,*IMAGES[UNIT_ALLOCATOR],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,true,false){
:Unit(pge,MemoryAllocator::resourceCost,pos,12,*IMAGES[UNIT_ALLOCATOR],CONSTANT::ATTACKER_TARGET_COL,CONSTANT::ATTACKER_ATTACK_COL,friendly,true,false){
isAllocator=true;
}
@ -148,14 +108,9 @@ void MemoryAllocator::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&othe
}
std::vector<Memory> RAMBank::resourceCost={{RANGE,0},{ATKSPD,0},{MOVESPD,0},{PROCEDURE,25},{HEALTH,16}};
RAMBank::RAMBank(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly)
:Unit(pge,{
{RANGE,0},
{ATKSPD,0},
{MOVESPD,0},
{PROCEDURE,25},
{HEALTH,16},
},pos,41,*IMAGES[RAM_BANK],WHITE,WHITE,friendly,false
:Unit(pge,RAMBank::resourceCost,pos,41,*IMAGES[RAM_BANK],WHITE,WHITE,friendly,false
,false,false
),randomOffset({util::random(128),util::random(128)}),matrixImg(*IMAGES[MATRIX]),
originalImg(*IMAGES[RAM_BANK]){
@ -733,11 +688,11 @@ bool Unit::IsMoveable(){
}
bool Unit::CanInteractWithAllies(){
return friendlyInteractable&&attachedPoint.expired();
return friendlyInteractable&&attachedPoint.expired()&&buildTime<=0;
}
bool Unit::CanInteractWithEnemies(){
return enemyInteractable&&attachedPoint.expired();
return enemyInteractable&&attachedPoint.expired()&&buildTime<=0;
}
Renderable&Unit::GetImage(){
@ -753,7 +708,7 @@ bool Unit::AutoAcquiresFriendlyTargets(){
}
bool Unit::CanMove(){
return moveSpd.size>0&&attachedPoint.expired();
return moveSpd.size>0&&attachedPoint.expired()&&buildTime<=0;
}
void Unit::SetTargetCollectionPoint(std::weak_ptr<CollectionPoint>targetCP,std::weak_ptr<Unit>self_ptr){
@ -795,5 +750,10 @@ bool Unit::ClickHandled(TileTransformedView&game,Resources&player_resources,std:
void Unit::UpdateGUIState(TileTransformedView&game,Resources&player_resources){};
bool Unit::IsAllocator(){
return isAllocator;
return isAllocator&&attachedPoint.expired()&&buildTime<=0;
}
void Unit::SetBuildUnit(float buildTime,std::unique_ptr<Unit>finalUnit){
this->buildTime=buildTime;
this->buildTransformUnit=std::move(finalUnit);
}

@ -74,6 +74,7 @@ public:
virtual void UpdateGUIState(TileTransformedView&game,Resources&player_resources);
virtual bool ClickHandled(TileTransformedView&game,Resources&player_resources,std::vector<std::shared_ptr<Unit>>&units,std::map<Image,std::unique_ptr<Renderable>>&IMAGES); //If you return true here, then the left click does not pass back to the main Virus Attack class.
bool IsAllocator();
void SetBuildUnit(float buildTime,std::unique_ptr<Unit>finalUnit);
std::vector<bool>& operator <<=(const int n){
for(int i=0;i<GetMemorySize()-1;i++){
@ -135,47 +136,57 @@ private:
bool willAttachWhenReachingDestination=false;
std::weak_ptr<Unit>self_ptr;
float collectionTime=0;
float buildTime=0;
std::unique_ptr<Unit>buildTransformUnit;
};
struct BasicUnit:Unit{
BasicUnit(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct BasicUnit2:Unit{
BasicUnit2(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct LeftShifter:Unit{
LeftShifter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct RightShifter:Unit{
RightShifter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct BitRestorer:Unit{
BitRestorer(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
void AttemptToHealOtherAllies(std::vector<std::shared_ptr<Unit>>&otherUnits);
static std::vector<Memory> resourceCost;
};
struct MemorySwapper:Unit{
MemorySwapper(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct Corrupter:Unit{
Corrupter(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct MemoryAllocator:Unit{
MemoryAllocator(PixelGameEngine*pge,vf2d pos,std::map<Image,std::unique_ptr<Renderable>>&IMAGES,bool friendly=false,bool moveable=true);
void Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUnits)override;
static std::vector<Memory> resourceCost;
};
struct RAMBank:Unit{
@ -195,4 +206,5 @@ struct RAMBank:Unit{
bool ClickHandled(TileTransformedView&game,Resources&player_resources,std::vector<std::shared_ptr<Unit>>&units,std::map<Image,std::unique_ptr<Renderable>>&IMAGES)override;
void UpdateGUIState(TileTransformedView&game,Resources&player_resources)override;
void DrawHud(TileTransformedView&game,std::map<Image,std::unique_ptr<Renderable>>&IMAGES)override;
static std::vector<Memory> resourceCost;
};

@ -123,21 +123,57 @@ void VirusAttack::InitializeSounds(){
bool VirusAttack::UnitCreationClickHandled(){
if(leftShifterButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(LeftShifter::resourceCost)){
std::unique_ptr<LeftShifter>buildUnit=std::make_unique<LeftShifter>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
if(rightShifterButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(RightShifter::resourceCost)){
std::unique_ptr<RightShifter>buildUnit=std::make_unique<RightShifter>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
if(bitRestorerButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(BitRestorer::resourceCost)){
std::unique_ptr<BitRestorer>buildUnit=std::make_unique<BitRestorer>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
if(memorySwapperButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(MemorySwapper::resourceCost)){
std::unique_ptr<MemorySwapper>buildUnit=std::make_unique<MemorySwapper>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
if(corrupterButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(Corrupter::resourceCost)){
std::unique_ptr<Corrupter>buildUnit=std::make_unique<Corrupter>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
if(platformButton->bPressed){
for(auto&u:units){
if(u->IsSelected()&&u->IsAllocator()&&CanAfford(MemoryAllocator::resourceCost)){
std::unique_ptr<MemoryAllocator>buildUnit=std::make_unique<MemoryAllocator>(this,u->GetPos(),IMAGES,u->IsFriendly());
u->SetBuildUnit(CONSTANT::UNIT_BUILD_TIME,std::move(buildUnit));
}
}
return true;
}
return false;
@ -153,6 +189,12 @@ void VirusAttack::HandleDraggingSelection(){
}
}
unitCreationList.DisplayAllControls(allocatorSelected);
leftShifterButton->Enable(CanAfford(LeftShifter::resourceCost));
rightShifterButton->Enable(CanAfford(RightShifter::resourceCost));
bitRestorerButton->Enable(CanAfford(BitRestorer::resourceCost));
memorySwapperButton->Enable(CanAfford(MemorySwapper::resourceCost));
corrupterButton->Enable(CanAfford(Corrupter::resourceCost));
platformButton->Enable(CanAfford(MemoryAllocator::resourceCost));
unitCreationList.Update(this);
if(GetMouse(0).bPressed){
if(NotClickingOnMinimap()){
@ -533,6 +575,29 @@ void VirusAttack::RenderFogOfWar(){
}
}
bool VirusAttack::CanAfford(std::vector<Memory>&unitCosts){
for(Memory&mem:unitCosts){
switch(mem.type){
case HEALTH:{
if(player_resources.health<mem.size)return false;
}break;
case ATKSPD:{
if(player_resources.atkSpd<mem.size)return false;
}break;
case MOVESPD:{
if(player_resources.moveSpd<mem.size)return false;
}break;
case RANGE:{
if(player_resources.range<mem.size)return false;
}break;
case PROCEDURE:{
if(player_resources.procedure<mem.size)return false;
}break;
}
}
return true;
}
int main()
{
VirusAttack app;

@ -67,6 +67,7 @@ private:
void DrawResourceBar();
bool UnitCreationClickHandled();
void InitializeUnitCreationGUI();
bool CanAfford(std::vector<Memory>&unitCosts);
public:
VirusAttack();

Loading…
Cancel
Save