Jump to content

problem with the zombie system


βurak

Recommended Posts

How can I code a proper zombie system? I'm trying to do this using a single timer and loop in the code, but I'm having trouble controlling the animations and flow. This is the first time I've done something like this. If anyone has done something like this before, I'd appreciate some help

function findRotation(x1, y1, x2, y2) 
    local t = -math.deg( math.atan2(x2 - x1, y2 - y1))
    return t < 0 and t + 360 or t
end

local zombies = {}

function createZombie(x, y, z, firstTarget)
   local tx,ty = getElementPosition(firstTarget)
	local zombie = createPed(0, x, y, z, findRotation(x,y,tx,ty), true)

   zombies[zombie] = {
      target = firstTarget,
      updateFirstDelay = getTickCount(),
      state = "idle"
   }
   setTimer(doZombieGetupAnim, 50, 1, zombie)
end

function zombieLookTarget(zombie, target)
   if not isElement(target) then return end
   if not isElement(zombie) then return end
   if isPedDead(zombie) then return end
   local tx,ty = getElementPosition(target)
   local zx,zy = getElementPosition(zombie)
   setElementRotation(zombie, 0, 0, findRotation(zx,zy,tx,ty), "default", true)
end

function zombieFollowTarget(zombie)
   if not isElement(zombie) then return end
   if isPedDead(zombie) then return end
   local tx,ty,tz = getElementPosition(zombies[zombie].target)
   local zx,zy,zz = getElementPosition(zombie)
   local distance = getDistanceBetweenPoints3D(tx,ty,tz, zx,zy,zz)
   if distance > 1.5 then
      setPedAnimation(zombie, "ped", "run_fatold", -1, true, true, true, false)
   end
end

function zombieAttackTarget(zombie)
   if not isElement(zombie) then return end
   local tx,ty,tz = getElementPosition(zombies[zombie].target)
   local zx,zy,zz = getElementPosition(zombie)
   local distance = getDistanceBetweenPoints3D(tx,ty,tz, zx,zy,zz)
   if distance <= 1.5 and isPedDead(zombies[zombie].target) then
      setPedAnimation(zombie, "medic", "cpr", -1, true, true, true, false)
   end
end

function stopAnimation(zombie)
   if not isElement(zombie) then return end
   local tx,ty,tz = getElementPosition(zombies[zombie].target)
   local zx,zy,zz = getElementPosition(zombie)
   local distance = getDistanceBetweenPoints3D(tx,ty,tz, zx,zy,zz)
   if distance <= 1.5 then
      setPedAnimation(zombie)
   end
end

function doZombieGetupAnim(zombie)
   if not isElement(zombie) then return end
   if isPedDead(zombie) then return end
   setPedAnimation(zombie, "ped", "getup_front", 1000, false, true, true, false)
end

--I think this is the main problem
function updateZombies()
   for zombie,data in pairs(zombies) do
      zombieLookTarget(zombie, data.target)
      if getTickCount() - data.updateFirstDelay > 1000 then
         zombieFollowTarget(zombie)
      end
   end
end
local updateTimer = setTimer(updateZombies, 150, 0)

addCommandHandler("zombie", function()
   local x,y,z = getElementPosition(localPlayer)
   createZombie(x, y+4, z, localPlayer)
end)

 

Edited by βurak
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...