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