TSR_ECS/main/scripts/npc.lua
2023-12-29 20:03:34 +01:00

72 lines
2.4 KiB
Lua

g_require("scripts/thread.lua")
function spawnnpc(x, y, z, follow)
local npc = ent_spawn(x, y, z, 0, 0, 0, 1, 1, 1)
mdl_set(npc, "models/test_skeletal.mdl.json")
mdl_anim(npc, "init", 0, 0)
mdl_anim(npc, "stand", 0, 0)
cct_add(npc, 0.25, 1.2)
local tick = 1 / g_gettps()
local function disttonode(ent, node_x, node_z)
local _, ent_y, _ = cct_getpos(ent)
return ent_distanceto(ent, node_x, ent_y, node_z)
end
thread(function ()
while true do
wait(1)
if ent_distance(npc, follow) > 10 then
local follow_x, follow_y, follow_z = cct_getpos(follow)
local path_found = cct_findpath(npc, follow_x, follow_y, follow_z)
print("path found: ", path_found)
if path_found then
mdl_anim(npc, "walk2", 6, 0.2)
local walk = true
while walk do
local next_node, node_x, node_y, node_z = cct_getnextpathnode(npc)
print("nextnode: ", next_node, node_x, node_y, node_z)
if not next_node then
break
end
while disttonode(npc, node_x, node_z) > 1 do
if ent_distance(npc, follow) < 5 then
walk = false
break
end
cct_turnto(npc, node_x, node_z, 3)
local move_x = 0
local move_y = -3.0
local move_z = 0
move_x, move_z = cct_getforwardxz(npc)
cct_move(npc, move_x * tick * 3, move_y * tick * 3, move_z * tick * 3)
local new_x, new_y, new_z = cct_getpos(npc)
local dist = ent_distanceto(npc, new_x, new_y, new_z)
-- blocked
if dist < tick * 0.2 then
walk = false
break
end
wait(0)
end
end
mdl_stopanim(npc, "walk2", 0.1)
end
end
end
end)
end