-
-
Save cornernote/3818862 to your computer and use it in GitHub Desktop.
NPC that Walks to a Given Pos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- DEBUG | |
xp = function(self) | |
local p = self.object:getpos() | |
p.x = p.x + 5 | |
self.moveto(self, p, 1, function(self) | |
zm(self) | |
end) | |
end | |
xm = function(self) | |
local p = self.object:getpos() | |
p.x = p.x - 5 | |
p.y = p.y + 1 | |
self.moveto(self, p, 2, function(self) | |
zp(self) | |
end) | |
end | |
zp = function(self) | |
local p = self.object:getpos() | |
p.z = p.z + 5 | |
p.y = p.y - 1 | |
self.moveto(self, p, 3, function(self) | |
xp(self) | |
end) | |
end | |
zm = function(self) | |
local p = self.object:getpos() | |
p.z = p.z - 5 | |
self.moveto(self, p, 4, function(self) | |
xm(self) | |
end) | |
end | |
-- END DEBUG | |
minetest.register_entity("town_worker:worker", { | |
hp_max = 1, | |
physical = false, | |
collisionbox = {-0.4, -1, -0.4, 0.4, 1, 0.4}, | |
visual = "upright_sprite", | |
visual_size = {x=1, y=2}, | |
textures = {"player.png", "player_back.png"}, | |
makes_footstep_sound = true, | |
target = nil, | |
speed = nil, | |
after = nil, | |
on_activate = function(self, staticdata, dtime_s) | |
end, | |
on_step = function(self, dtime) | |
if self.target and self.speed then | |
local s = self.object:getpos() | |
local t = self.target | |
local diff = {x=t.x-s.x, y=t.y-s.y, z=t.z-s.z} | |
local yaw = math.atan(diff.z/diff.x)+math.pi/2 | |
if diff.z ~= 0 or diff.x > 0 then | |
yaw = yaw+math.pi | |
end | |
self.object:setyaw(yaw) | |
local amount = (diff.x^2+diff.y^2+diff.z^2)^0.5 | |
local v = self.speed | |
local vec = {x=0, y=0, z=0} | |
vec.x = diff.x*v/amount | |
vec.y = diff.y*v/amount | |
vec.z = diff.z*v/amount | |
self.object:setvelocity(vec) | |
if math.abs(diff.x) < 0.1 and math.abs(diff.y) < 0.1 and math.abs(diff.z) < 0.1 then | |
self.object:setvelocity({x=0, y=0, z=0}) | |
self.target = nil | |
self.speed = nil | |
if self.after then | |
self.after(self) | |
end | |
end | |
end | |
end, | |
on_punch = function(self, hitter) | |
end, | |
on_rightclick = function(self, clicker) | |
-- xp(self) -- DEBUG: calling this causes the NPC walk in a square | |
end, | |
get_staticdata = function(self) | |
end, | |
-- API | |
moveto = function(self, pos, speed, after) | |
self.target = pos | |
self.speed = speed | |
self.after = after | |
end | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment