Auto targeting for enemies and proper healing fixes for bit restorers.
This commit is contained in:
parent
964b02d256
commit
d4c87fd755
@ -17,7 +17,7 @@ void DeathAnimation::Draw(TileTransformedView&game,PixelGameEngine*pge){
|
||||
for(int y=0;y<img.Sprite()->height;y++){
|
||||
for(int x=0;x<img.Sprite()->width;x++){
|
||||
Pixel col=originalImg.Sprite()->GetPixel(x,y);
|
||||
if(col.a==255&&col.r<=(CONSTANT::DEATH_FADE_TIME-fadeTimer)*255){
|
||||
if(col.a==255&&col.r<=(fadeTimer/CONSTANT::DEATH_FADE_TIME)*255){
|
||||
pge->Draw(x,y,matrixImg.Sprite()->GetPixel(x+randomOffset.x,y+randomOffset.y));
|
||||
} else {
|
||||
pge->Draw(x,y,originalImg.Sprite()->GetPixel(x+randomOffset.x,y+randomOffset.y));
|
||||
|
@ -85,7 +85,7 @@ void BitRestorer::Attack(Unit&victim,std::vector<std::shared_ptr<Unit>>&otherUni
|
||||
void BitRestorer::AttemptToHealOtherAllies(std::vector<std::shared_ptr<Unit>>&otherUnits){
|
||||
std::vector<int>emptyMemoryPositions;
|
||||
for(auto&u:otherUnits){
|
||||
if(u->IsFriendly()&&InRange(u)){
|
||||
if(u.get()!=this&&u->IsFriendly()&&InRange(u)){
|
||||
for(int i=0;i<u->GetMemorySize();i++){
|
||||
if(!u->memory[i]){
|
||||
emptyMemoryPositions.emplace_back(i);
|
||||
@ -324,6 +324,12 @@ int Unit::GetMemorySize(){
|
||||
return memory.size();
|
||||
}
|
||||
|
||||
void Unit::RunAI(PixelGameEngine*pge){}
|
||||
|
||||
void Unit::_RunAI(PixelGameEngine*pge){
|
||||
RunAI(pge);
|
||||
}
|
||||
|
||||
void Unit::_Update(PixelGameEngine*pge){
|
||||
if(!target.expired()){
|
||||
auto ptrTarget=target.lock();
|
||||
@ -339,25 +345,7 @@ void Unit::_Update(PixelGameEngine*pge){
|
||||
}
|
||||
|
||||
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*pge->GetElapsedTime());
|
||||
changeDirTimer=std::max(0.f,changeDirTimer-pge->GetElapsedTime());
|
||||
_RunAI(pge);
|
||||
}
|
||||
|
||||
if(!GhostInFogOfWar()&&InFogOfWar()){
|
||||
@ -449,30 +437,47 @@ void Unit::SetPos(vf2d newPos){
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::AttemptAttack(Unit*unit,std::vector<std::shared_ptr<Unit>>&otherUnits){
|
||||
void Unit::AttemptAttack(std::weak_ptr<Unit>attacker,std::weak_ptr<Unit>unit,std::vector<std::shared_ptr<Unit>>&otherUnits){
|
||||
if(reloadTimer>0)return;
|
||||
Unit*finalTarget=nullptr;
|
||||
if(unit!=nullptr){
|
||||
std::weak_ptr<Unit>finalTarget;
|
||||
if(!unit.expired()){
|
||||
finalTarget=unit;
|
||||
if(!target.expired()){
|
||||
auto ptrTarget=target.lock();
|
||||
if(InRange(ptrTarget)){
|
||||
finalTarget=ptrTarget.get();
|
||||
finalTarget=ptrTarget;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(finalTarget!=nullptr){
|
||||
if(InRange(finalTarget)){
|
||||
_Attack(finalTarget,otherUnits); //Call the parent function first, followed by the child.
|
||||
if(!finalTarget.expired()){
|
||||
if(InRange(finalTarget.lock())){
|
||||
_Attack(attacker,finalTarget,otherUnits); //Call the parent function first, followed by the child.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::Update(PixelGameEngine*pge){}
|
||||
|
||||
void Unit::_Attack(Unit*finalTarget,std::vector<std::shared_ptr<Unit>>&otherUnits){
|
||||
Attack(*finalTarget,otherUnits);
|
||||
reloadTimer=1.f/(GetAtkSpd()/2.f);
|
||||
void Unit::Attacked(std::weak_ptr<Unit>attacker){}
|
||||
|
||||
void Unit::_Attacked(std::weak_ptr<Unit>attacker){
|
||||
Attacked(attacker);
|
||||
if(attacker.lock()->IsFriendly()!=IsFriendly()&&CanInteractWithEnemies()){
|
||||
SetTargetUnit(attacker);
|
||||
}
|
||||
}
|
||||
|
||||
void Unit::_Attack(std::weak_ptr<Unit>attacker,std::weak_ptr<Unit>finalTarget,std::vector<std::shared_ptr<Unit>>&otherUnits){
|
||||
if(GetAtkSpd()>0){
|
||||
Attack(*finalTarget.lock(),otherUnits);
|
||||
finalTarget.lock()->_Attacked(attacker);
|
||||
reloadTimer=1.f/(GetAtkSpd()/2.f);
|
||||
if(GetCurrentTarget().expired()&&!IsFriendly()){
|
||||
if(finalTarget.lock()->IsFriendly()!=IsFriendly()&&CanInteractWithEnemies()){
|
||||
SetTargetUnit(finalTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Unit::InFogOfWar(){
|
||||
@ -506,3 +511,7 @@ bool Unit::CanInteractWithEnemies(){
|
||||
Renderable&Unit::GetImage(){
|
||||
return img;
|
||||
}
|
||||
|
||||
std::weak_ptr<Unit>Unit::GetCurrentTarget(){
|
||||
return target;
|
||||
}
|
@ -48,7 +48,7 @@ public:
|
||||
void SetTargetUnit(std::weak_ptr<Unit>target);
|
||||
void SetTargetLocation(vf2d targetLoc);
|
||||
void SetPos(vf2d newPos);
|
||||
void AttemptAttack(Unit*unit,std::vector<std::shared_ptr<Unit>>&otherUnits);
|
||||
void AttemptAttack(std::weak_ptr<Unit>attacker,std::weak_ptr<Unit>unit,std::vector<std::shared_ptr<Unit>>&otherUnits);
|
||||
bool InFogOfWar();
|
||||
bool GhostInFogOfWar();
|
||||
void HideGhost();
|
||||
@ -59,6 +59,11 @@ public:
|
||||
bool CanInteractWithEnemies();
|
||||
bool CanInteractWithAllies();
|
||||
Renderable&GetImage();
|
||||
virtual void RunAI(PixelGameEngine*pge);
|
||||
void _RunAI(PixelGameEngine*pge);
|
||||
virtual void Attacked(std::weak_ptr<Unit>attacker);
|
||||
void _Attacked(std::weak_ptr<Unit>attacker);
|
||||
std::weak_ptr<Unit>GetCurrentTarget();
|
||||
|
||||
std::vector<bool>& operator <<=(const int n){
|
||||
for(int i=0;i<GetMemorySize()-1;i++){
|
||||
@ -102,9 +107,7 @@ private:
|
||||
bool selected=false;
|
||||
bool dead=false;
|
||||
float reloadTimer=0;
|
||||
void _Attack(Unit*finalTarget,std::vector<std::shared_ptr<Unit>>&otherUnits);
|
||||
vf2d movementVel={0,0};
|
||||
float changeDirTimer=0;
|
||||
void _Attack(std::weak_ptr<Unit>attacker,std::weak_ptr<Unit>finalTarget,std::vector<std::shared_ptr<Unit>>&otherUnits);
|
||||
bool moveable=true;
|
||||
bool friendlyInteractable=false;
|
||||
bool enemyInteractable=true;
|
||||
|
@ -153,7 +153,8 @@ void VirusAttack::CollisionChecking(std::shared_ptr<Unit>u,std::shared_ptr<Unit>
|
||||
}
|
||||
}
|
||||
|
||||
void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2){
|
||||
void VirusAttack::IdentifyClosestTarget(std::weak_ptr<Unit>&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2){
|
||||
if(u==u2)return;
|
||||
bool canInteract;
|
||||
canInteract=
|
||||
(u->IsFriendly()&&u->CanInteractWithEnemies()&&!u2->IsFriendly())||
|
||||
@ -163,7 +164,7 @@ void VirusAttack::IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std:
|
||||
if(canInteract){
|
||||
geom2d::line<float>unitLine(u->GetPos(),u2->GetPos());
|
||||
if(unitLine.length()<closestDist){
|
||||
closestUnit=u2.get();
|
||||
closestUnit=u2;
|
||||
closestDist=unitLine.length();
|
||||
}
|
||||
}
|
||||
@ -313,7 +314,7 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
|
||||
std::erase_if(TileManager::visibleTiles,[](std::pair<vf2d,float> key){return key.second<=0;});
|
||||
|
||||
for(auto&u:units){
|
||||
Unit*closestUnit=nullptr;
|
||||
std::weak_ptr<Unit>closestUnit;
|
||||
float closestDist=999999;
|
||||
for(auto&u2:units){
|
||||
IdentifyClosestTarget(closestUnit,closestDist,u,u2);
|
||||
@ -326,7 +327,7 @@ bool VirusAttack::OnUserUpdate(float fElapsedTime){
|
||||
}
|
||||
}
|
||||
}
|
||||
u->AttemptAttack(closestUnit,units);
|
||||
u->AttemptAttack(u,closestUnit,units);
|
||||
u->_Update(this);
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ private:
|
||||
void DrawSelectionRectangle();
|
||||
void HandleRightClickMove();
|
||||
void CollisionChecking(std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2);
|
||||
void IdentifyClosestTarget(Unit*&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2);
|
||||
void IdentifyClosestTarget(std::weak_ptr<Unit>&closestUnit,float&closestDist,std::shared_ptr<Unit>u,std::shared_ptr<Unit>u2);
|
||||
vf2d GetWorldMousePos();
|
||||
void HandlePanAndZoom(float fElapsedTime);
|
||||
void DrawMinimap();
|
||||
|
Loading…
x
Reference in New Issue
Block a user