Spoiler NPCTab = {Dist = 0}
function CreateNPC(x,y,rot,model,health,gopos)
if not x or not y or not rot or not model or not health then return nil end
NPCTab.Dist = NPCTab.Dist + 1
local id = NPCTab.Dist
NPCTab[id] = {
x = x,
y = y,
rot = rot,
mdl = model,
gopos = gopos
img = image(model,0,0,1),
}
imagepos(NPCTab[id].img,x,y,rot)
return true
end
CreateNPC(143,275,0,"gfx/starkkz/models/npcs/HL2_Rebel.bmp",100,{x = 4,y = 10})
function NPCAction()
for b = 1, NPCTab.Dist do
if NPCTab[b] then
local Left,Right,Up,Down = false,false,false,false
if NPCTab[b].gopos.TX*32 < NPCTab[b].x then
if tile(math.floor(NPCTab[b].x/32)-1,math.floor(NPCTab[b].y/32),"walkable") then
Left = true
else
if tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)-1,"walkable") then
Up = true
elseif tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)+1,"walkable") then
Down = true
end
end
elseif NPCTab[b].gopos.TX*32 > NPCTab[b].x then
if tile(math.floor(NPCTab[b].x/32)+1,math.floor(NPCTab[b].y/32),"walkable") then
Right = true
else
if tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)-1,"walkable") then
Up = true
elseif tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)+1,"walkable") then
Down = true
end
end
end
if NPCTab[b].gopos.TY*32 < NPCTab[b].y then
if tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)-1,"walkable") then
Up = true
else
if tile(math.floor(NPCTab[b].x/32)+1,math.floor(NPCTab[b].y/32),"walkable") then
Right = true
elseif tile(math.floor(NPCTab[b].x/32)-1,math.floor(NPCTab[b].y/32),"walkable") then
Left = true
end
end
elseif NPCTab[b].gopos.TY*32 > NPCTab[b].y then
if tile(math.floor(NPCTab[b].x/32),math.floor(NPCTab[b].y/32)+1,"walkable") then
Down = true
else
if tile(math.floor(NPCTab[b].x/32)+1,math.floor(NPCTab[b].y/32),"walkable") then
Right = true
elseif tile(math.floor(NPCTab[b].x/32)-1,math.floor(NPCTab[b].y/32),"walkable") then
Left = true
end
end
end
if Left then
NPCTab[b].x = NPCTab[b].x - 1
elseif Right then
NPCTab[b].x = NPCTab[b].x + 1
end
if Up then
NPCTab[b].y = NPCTab[b].y - 1
elseif Down then
NPCTab[b].y = NPCTab[b].y + 1
end
imagepos(NPCTab[b].img,NPCTab[b].x,NPCTab[b].y,NPCTab[b].rot)
end
end
end
timer(1,"NPCAction","",0)