diff --git a/maidroid/api.lua b/maidroid/api.lua index 9bf5d71..da89ac2 100644 --- a/maidroid/api.lua +++ b/maidroid/api.lua @@ -81,6 +81,40 @@ function maidroid.maidroid.get_nearest_player(self, range_distance) return player end +-- maidroid.maidroid.get_front_node returns a node that exists in front of the maidroid. +function maidroid.maidroid.get_front_node(self) + local direction = self.object:get_look_dir() + direction.y = 0 + direction = vector.normalize(direction) + + if direction.x >= 0.5 then + if direction.x > 0 then direction.x = 1 else direction.x = -1 end + else + direction.x = 0 + end + + if direction.z >= 0.5 then + if direction.z > 0 then direction.z = 1 else direction.z = -1 end + else + direction.z = 0 + end + + local front = vector.add(vector.round(self.object:getpos()), direction) + return minetest.get_node(front) +end + +-- maidroid.maidroid.set_animation sets the maidroid's animation. +-- this method is wrapper for self.object:set_animation. +function maidroid.maidroid.set_animation(self, frame) + self.object:set_animation(frame, 15, 0) +end + +-- maidroid.maidroid.set_yaw_by_direction sets the maidroid's yaw +-- by a direction vector. +function maidroid.maidroid.set_yaw_by_direction(self, direction) + self.object:setyaw(math.atan2(direction.z, direction.x) + math.pi / 2) +end + --------------------------------------------------------------------- -- maidroid.manufacturing_data represents a table that contains manufacturing data. @@ -298,6 +332,9 @@ function maidroid.register_maidroid(product_name, def) get_core = maidroid.maidroid.get_core, get_core_name = maidroid.maidroid.get_core_name, get_nearest_player = maidroid.maidroid.get_nearest_player, + get_front_node = maidroid.maidroid.get_front_node, + set_animation = maidroid.maidroid.set_animation, + set_yaw_by_direction = maidroid.maidroid.set_yaw_by_direction, }) -- register a spawner for debugging maidroid mods. diff --git a/maidroid_core_basic/register.lua b/maidroid_core_basic/register.lua index ff43d00..21c1a58 100644 --- a/maidroid_core_basic/register.lua +++ b/maidroid_core_basic/register.lua @@ -3,16 +3,56 @@ -- https://github.com/tacigar/maidroid ------------------------------------------------------------ -local function on_start(self) +local state = { + IDLE = 0, + ACCOMPANY = 1, +} +local function on_start(self) + self.state = state.IDLE + self.object:setacceleration{x = 0, y = -10, z = 0} + self.object:setvelocity{x = 0, y = 0, z = 0} end local function on_stop(self) - + self.state = nil + self.object:setvelocty{x = 0, y = 0, z = 0} end local function on_step(self, dtime) + local player = self:get_nearest_player(10) + if player == nil then + self:set_animation(maidroid.animation_frames.STAND) + return + end + local position = self.object:getpos() + local player_position = player:getpos() + local direction = vector.subtract(player_position, position) + local velocity = self.object:getvelocity() + + if vector.length(dir) < 3 then + if self.state == state.ACCOMPANY then + self:set_animation(maidroid.animation_frames.STAND) + self.state = state.idle + self.object:setvelocity{x = 0, y = velocity.y, z = 0} + end + else + if self.state == state.IDLE then + self:set_animation(maidroid.animation_frames.WALK) + self.state = state.ACCOMPANY + end + self.object:setvelocity{x = direction.x, y = velocity.y, z = direction.z} -- TODO + end + self:set_yaw_by_direction(direction) + + -- if maidroid is stoped by obstacle, the maidroid must jump. + if velocity == 0 and self.state = state.ACCOMPANY then + local front_node = self:get_front_node() + if front_node.name ~= "air" then + self.object:setvelocity{x = direction.x, y = 3, z = direction.z} + end + end end maidroid.register_core("maidroid_core_basic:core_basic", {