There are a number of ways to do zombie attack.
This one is simple, but I wouldn't use it.
Instead I would use something like they have on zombie apocolypse, where the zombies press a button to attack, and only if they are close enough will it do damage. This also allows for more than one zombie to be attacking a player at once.
However here is what you asked for.
This is not that efficient, if you have team lists like humans[] and zombies[] this would be a lot faster.
pawn Код:
public OnGameModeInit()
{
...
SetTimer("ZombieAttack", 5000, true);
...
}
forward ZombieAttack();
public ZombieAttack()
{
Float:X, Float:Y, Float:Z, zombie, Float:pHealth;
for(new playerid=0; playerid<GetMaxPlayers(); playerid++)
{
if(!IsPlayerConnected(playerid) || /*is player zombie*/ || GetPlayerState(playerid) != PLAYER_STATE_ONFOOT) continue;
zombie = GetClosestZombie(playerid);
if(zombie != -1)
{
GetPlayerPos(playerid, X, Y, Z);
if(IsPlayerInRangeOfPoint(zombieid,0.5, X, Y, Z))
{
pHealth = GetPlayerHealth(playerid);
SetPlayerHealth(playerid, pHealth-15.0);
}
}
}
}
stock GetClosestZombie(playerid)
{
new
Float:dist== 30.0, //won't detect zombies over 30 units away
Float:X, Float:Y, Float:Z,
closest = -1;
GetPlayerPos(playerid, X, Y, Z);
//if player is not connected, a zombie, or not on foot skip
if(!IsPlayerConnected(playerid) || /*is player zombie*/ || GetPlayerState(playerid) != PLAYER_STATE_ONFOOT) return 0;
for(new zombieid=0; zombieid<GetMaxPlayers(); zombieid++)
{
//again if player is not connected, a human, or not on foot skip
//also here, if they are within less than the range of current closest
if(!IsPlayerConnected(zombieid) || /*is player human */ || GetPlayerState(playerid) != PLAYER_STATE_ONFOOT || playerid == zombieid || !IsPlayerInRangeOfPoint(zombieid,dist-0.1, X, Y, Z)) continue;
closeset = zombieid;
new Float:pX, Float:Py, Float:Pz;
GetPlayerPos(pid, pX, Py, Pz);
distance = floatsqroot(floatpower(pX-X,2)+floatpower(pY-Y,2)+floatpower(pZ-Z,2));
}
return closest;
}
If you want a version where players press a button let me know, i can help you out