Mobs: Add yaw rotation microtask template

This commit is contained in:
Wuzzy 2023-10-09 15:58:05 +02:00
parent 14aab87041
commit b324f6b093
4 changed files with 88 additions and 4 deletions

View File

@ -1,5 +1,6 @@
local PATH_DEBUG = true
local PATH_DISTANCE_TO_GOAL_POINT = 0.7
local YAW_PRECISION = 10000
-- Task templates
@ -79,6 +80,43 @@ rp_mobs.microtasks.pathfind_and_walk_to = function(target_pos, searchdistance, m
return rp_mobs.create_microtask(mtask)
end
-- Rotate yaw linearly over time
rp_mobs.microtasks.rotate_yaw_smooth = function(yaw, time)
local label
if yaw == "random" then
label = "rotate yaw randomly"
else
label = "rotate yaw to "..string.format("%.3f", yaw)
end
return rp_mobs.create_microtask({
label = label,
on_step = function(self, mob, dtime)
local sd = self.statedata
if not sd.target_yaw then
if yaw == "random" then
yaw = (math.random(0, YAW_PRECISION) / YAW_PRECISION) * (math.pi*2)
end
sd.target_yaw = yaw
end
if not sd.start_yaw then
sd.start_yaw = mob.object:get_yaw()
end
if not sd.timer then
sd.timer = 0
end
sd.timer = sd.timer + dtime
local timer = math.min(sd.timer, time)
local time_progress = 1 - ((time - timer) / time)
local current_yaw = sd.start_yaw + (sd.target_yaw - sd.start_yaw) * time_progress
mob.object:set_yaw(current_yaw)
end,
is_finished = function(self, mob)
return self.statedata.timer and self.statedata.timer >= time
end,
})
end
-- Do nothing for the given time in seconds
rp_mobs.microtasks.sleep = function(time)
return rp_mobs.create_microtask({

View File

@ -20,11 +20,11 @@ rp_mobs.register_mob("rp_mobs_mobs:boar", {
if #nodes > 0 then
local n = math.random(1, #nodes)
local endpos = vector.add(vector.new(0,1,0), nodes[n])
local microtask1 = rp_mobs.microtasks.pathfind_and_walk_to(endpos, 100, 1, 4)
rp_mobs.add_microtask_to_task(self, microtask1, task)
local mt_pathfind = rp_mobs.microtasks.pathfind_and_walk_to(endpos, 100, 1, 4)
rp_mobs.add_microtask_to_task(self, mt_pathfind, task)
end
local microtask2 = rp_mobs.microtasks.sleep(math.random(500, 2000)/1000)
rp_mobs.add_microtask_to_task(self, microtask2, task)
local mt_sleep = rp_mobs.microtasks.sleep(math.random(500, 2000)/1000)
rp_mobs.add_microtask_to_task(self, mt_sleep, task)
rp_mobs.add_task(self, task)
end,
entity_definition = {

View File

@ -0,0 +1,38 @@
-- TODO: Change to rp_mobs_mobs when ready
local S = minetest.get_translator("mobs")
local dummy_texture = "default_stone.png^[colorize:#88FF00:92"
-- Dummy mob only for testing
rp_mobs.register_mob("rp_mobs_mobs:dummy", {
description = S("Dummy"),
decider = function(self)
local task = rp_mobs.create_task({label="Dummy stuff"})
rp_mobs.add_microtask_to_task(self, rp_mobs.microtasks.rotate_yaw_smooth("random", 1), task)
local sleep_time = math.random(500, 2000)/1000
local mt_sleep = rp_mobs.microtasks.sleep(sleep_time)
rp_mobs.add_microtask_to_task(self, mt_sleep, task)
rp_mobs.add_task(self, task)
end,
entity_definition = {
hp_max = 1,
physical = true,
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
selectionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5, rotate=true},
visual = "cube",
textures = { dummy_texture, dummy_texture, dummy_texture, dummy_texture, dummy_texture, dummy_texture },
makes_footstep_sound = false,
on_activate = function(self)
rp_mobs.init_physics(self)
rp_mobs.init_tasks(self)
end,
on_step = function(self, dtime)
rp_mobs.handle_physics(self)
rp_mobs.handle_tasks(self, dtime)
rp_mobs.decide(self)
end,
on_death = rp_mobs.on_death_default,
},
})
rp_mobs.register_mob_item("rp_mobs_mobs:dummy", dummy_texture)

View File

@ -1,5 +1,13 @@
-- Crafting stuff
dofile(minetest.get_modpath("rp_mobs_mobs").."/crafts.lua")
-- Animals
dofile(minetest.get_modpath("rp_mobs_mobs").."/boar.lua")
-- Monsters (TODO)
-- Dummy mob used only for testing
dofile(minetest.get_modpath("rp_mobs_mobs").."/dummy.lua")
-- Other
dofile(minetest.get_modpath("rp_mobs_mobs").."/achievements.lua")