|
|
|
@ -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::vector<Memory>memory,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;i<mem.size;i++){ |
|
|
|
|
this->memory.push_back(true); |
|
|
|
|
this->ghostMemory.push_back(true); |
|
|
|
|
} |
|
|
|
|
switch(mem.type){ |
|
|
|
|
case HEALTH:{ |
|
|
|
@ -67,15 +69,15 @@ Unit::Unit(std::vector<Memory>memory,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<float>(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; |
|
|
|
|
} |