Walk back to bed instead of teleporting

This commit is contained in:
cora 2022-05-09 01:00:34 +02:00 committed by Gitea
parent 46dbf8c0ab
commit 3774044f86

@ -924,6 +924,31 @@ minetest.register_on_joinplayer(function(player)
inv:set_size("offered", 1)
end)
local function set_velocity(self, v)
local yaw = (self.object:get_yaw() or 0) + self.rotate
self.object:set_velocity({
x = (math.sin(yaw) * -v),
y = self.object:get_velocity().y,
z = (math.cos(yaw) * v),
})
end
local function go_home(entity)
local b=entity.bed
local s=entity.object:get_pos()
if not b then return end
local v = { x = b.x - s.x, z = b.z - s.z }
local yaw = (math.atan(v.z / v.x) + math.pi / 2) - entity.rotate
if b.x > s.x then yaw = yaw + math.pi end
entity.object:set_yaw(yaw)
set_velocity(entity,entity.follow_velocity)
entity.state = "go_home"
if vector.distance(b,s) < 10 then
entity.state = "stand"
set_velocity(entity,0)
end
end
--[=======[ MOB REGISTRATION AND SPAWNING ]=======]
mobs:register_mob("mobs_mc:villager", {
@ -1029,8 +1054,8 @@ mobs:register_mob("mobs_mc:villager", {
_player_scan_timer = 0,
_trading_players = {}, -- list of playernames currently trading with villager (open formspec)
do_custom = function(self, dtime)
if self.bed and vector.distance(self.object:get_pos(),self.bed) > 50 then
self.object:set_pos(self.bed)
if self.bed and ( self.state == "go_home" or vector.distance(self.object:get_pos(),self.bed) > 50 ) then
go_home(self)
end
-- Stand still if player is nearby.
if not self._player_scan_timer then