@ -267,6 +267,7 @@ bool VirusAttack::UnitCreationClickHandled(){
std : : unique_ptr < UnitClass > buildUnit = std : : make_unique < UnitClass > ( this , u - > GetPos ( ) , IMAGES , u - > IsFriendly ( ) ) ; \
u - > SetBuildUnit ( CONSTANT : : UNIT_BUILD_TIME , std : : move ( buildUnit ) ) ; \
ExpendResources ( player_resources , UnitClass : : resourceCost ) ; \
CalculateUsedMemory ( ) ; \
} \
} \
return true ; \
@ -337,7 +338,7 @@ void VirusAttack::HandleDraggingSelection(){
bool platformSelected = false ;
bool memoryAllocatorBoxHovered = false ;
for ( auto & u : units ) {
u - > UpdateGUIState ( game , player_resources , memoryAllocatorBox , memoryAllocatorBoxHovered ) ;
u - > UpdateGUIState ( game , player_resources , memoryAllocatorBox , memoryAllocatorBoxHovered , GetTotalUsedMemory ( ) , currentLevel - > availableMemory ) ;
if ( u - > IsSelected ( ) ) {
if ( u - > IsAllocator ( ) ) {
allocatorSelected = true ;
@ -450,7 +451,7 @@ void VirusAttack::HandleRightClickMove(){
void VirusAttack : : CollisionChecking ( std : : shared_ptr < Unit > u , std : : shared_ptr < Unit > u2 ) {
if ( u ! = u2 & & geom2d : : overlaps ( geom2d : : circle < float > ( u - > GetPos ( ) , u - > GetUnitSize ( ) . x / 2 ) , geom2d : : circle < float > ( u2 - > GetPos ( ) , u2 - > GetUnitSize ( ) . x / 2 ) ) ) {
geom2d : : line < float > collisionLine ( u - > GetPos ( ) , u2 - > GetPos ( ) ) ;
geom2d : : line < float > collisionLine ( u - > GetPos ( ) , u2 - > GetPos ( ) + vf2d { 0.001 , 0.001 } ) ;
float maxDist = u - > GetUnitSize ( ) . x / 2 + u2 - > GetUnitSize ( ) . x / 2 ;
float dist = maxDist - collisionLine . length ( ) ;
vf2d dir = collisionLine . vector ( ) . norm ( ) ;
@ -458,7 +459,7 @@ void VirusAttack::CollisionChecking(std::shared_ptr<Unit>u,std::shared_ptr<Unit>
u - > SetPos ( u - > GetPos ( ) - dir * dist / 2 ) ;
}
if ( u2 - > IsMoveable ( ) | | ( ! u - > IsMoveable ( ) & & ! u2 - > IsMoveable ( ) ) ) {
u2 - > SetPos ( u2 - > GetPos ( ) + dir * ( dist + 0.001 ) / 2 ) ;
u2 - > SetPos ( u2 - > GetPos ( ) + dir * ( dist ) / 2 ) ;
}
}
}
@ -670,7 +671,7 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
std : : erase_if ( TileManager : : visibleTiles , [ ] ( std : : pair < vf2d , float > key ) { return key . second < = 0 ; } ) ;
playerUsedMemory = enemyUsedMemory = { 0 } ;
CalculateUsedMemory ( ) ;
for ( auto & u : units ) {
u - > SaveMemory ( ) ;
std : : weak_ptr < Unit > closestUnit ;
@ -690,19 +691,6 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
}
u - > AttemptAttack ( u , closestUnit , units , debuffIcons , IMAGES ) ;
u - > _Update ( this , SOUNDS , player_resources , enemy_resources , queuedUnits , resourceGainTimer , resourceGainIcons , IMAGES ) ;
if ( u - > IsFriendly ( ) ) {
playerUsedMemory [ 0 ] + = u - > health . size ;
playerUsedMemory [ 1 ] + = u - > range . size ;
playerUsedMemory [ 2 ] + = u - > atkSpd . size ;
playerUsedMemory [ 3 ] + = u - > moveSpd . size ;
playerUsedMemory [ 4 ] + = u - > procedure . size ;
} else {
enemyUsedMemory [ 0 ] + = u - > health . size ;
enemyUsedMemory [ 1 ] + = u - > range . size ;
enemyUsedMemory [ 2 ] + = u - > atkSpd . size ;
enemyUsedMemory [ 3 ] + = u - > moveSpd . size ;
enemyUsedMemory [ 4 ] + = u - > procedure . size ;
}
}
std : : erase_if ( units , [ & ] ( std : : shared_ptr < Unit > u ) {
@ -962,6 +950,7 @@ void VirusAttack::RenderFogOfWar(){
}
bool VirusAttack : : CanAfford ( Resources & resources , std : : vector < Memory > & unitCosts ) {
int totalMemoryCost = 0 ;
for ( Memory & mem : unitCosts ) {
switch ( mem . type ) {
case HEALTH : {
@ -980,8 +969,9 @@ bool VirusAttack::CanAfford(Resources&resources,std::vector<Memory>&unitCosts){
if ( resources . procedure < mem . size ) return false ;
} break ;
}
totalMemoryCost + = mem . size ;
}
return true ;
return totalMemo ryCost + GetTotalUsedMemory ( ) < = c urr entLevel - > availableMemory ;
}
void VirusAttack : : ExpendResources ( Resources & resources , std : : vector < Memory > & unitCosts ) {
@ -1024,6 +1014,22 @@ int VirusAttack::GetEnemyUsedMemory(){
return sum ;
}
void VirusAttack : : CalculateUsedMemory ( ) {
playerUsedMemory = enemyUsedMemory = { 0 } ;
for ( auto & u : units ) {
std : : array < size_t , 5 > costs = { u - > health . size , u - > range . size , u - > atkSpd . size , u - > moveSpd . size , u - > procedure . size } ;
if ( u - > IsBuilding ( ) ) {
costs = { u - > GetBuildUnit ( ) - > health . size , u - > GetBuildUnit ( ) - > range . size , u - > GetBuildUnit ( ) - > atkSpd . size , u - > GetBuildUnit ( ) - > moveSpd . size , u - > GetBuildUnit ( ) - > procedure . size } ;
}
std : : array < int , 5 > & targetArr = u - > IsFriendly ( ) ? playerUsedMemory : enemyUsedMemory ;
for ( int i = 0 ; i < targetArr . size ( ) ; i + + ) {
targetArr [ i ] + = costs [ i ] ;
}
}
}
int main ( )
{
VirusAttack app ;