maidroid/maidroid_core/cores/basic.lua

67 lines
2.1 KiB
Lua
Raw Normal View History

2016-09-13 12:02:53 +02:00
------------------------------------------------------------
-- Copyright (c) 2016 tacigar. All rights reserved.
-- https://github.com/tacigar/maidroid
------------------------------------------------------------
2016-09-14 01:27:13 +02:00
local state = {IDLE = 0, ACCOMPANY = 1}
2016-09-13 12:02:53 +02:00
local function on_start(self)
self.state = state.IDLE
self.object:setacceleration{x = 0, y = -10, z = 0}
2021-04-04 16:42:55 +02:00
self.object:set_velocity{x = 0, y = 0, z = 0}
2016-09-13 12:02:53 +02:00
end
local function on_stop(self)
self.state = nil
2021-04-04 16:42:55 +02:00
self.object:set_velocity{x = 0, y = 0, z = 0}
2017-01-03 02:43:56 +01:00
self:set_animation(maidroid.animation_frames.STAND)
2016-09-13 12:02:53 +02:00
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
2021-04-04 16:42:55 +02:00
local position = self.object:get_pos()
local player_position = player:get_pos()
local direction = vector.subtract(player_position, position)
2021-04-04 16:42:55 +02:00
local velocity = self.object:get_velocity()
2016-09-13 15:11:23 +02:00
if vector.length(direction) < 3 then
if self.state == state.ACCOMPANY then
self:set_animation(maidroid.animation_frames.STAND)
2016-09-13 15:11:23 +02:00
self.state = state.IDLE
2021-04-04 16:42:55 +02:00
self.object:set_velocity{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
2021-04-04 16:42:55 +02:00
self.object:set_velocity{x = direction.x, y = velocity.y, z = direction.z} -- TODO
end
2016-09-18 01:54:52 +02:00
self:set_yaw_by_direction(direction)
2016-09-13 12:02:53 +02:00
-- if maidroid is stoped by obstacle, the maidroid must jump.
2016-09-13 15:33:02 +02:00
if velocity.y == 0 and self.state == state.ACCOMPANY then
local front_node = self:get_front_node()
2016-12-24 01:23:31 +01:00
if front_node.name ~= "air" and minetest.registered_nodes[front_node.name] ~= nil
and minetest.registered_nodes[front_node.name].walkable then
2021-04-04 16:42:55 +02:00
self.object:set_velocity{x = direction.x, y = 5, z = direction.z}
end
end
2016-09-13 12:02:53 +02:00
end
2016-09-14 01:27:13 +02:00
-- register a definition of a new core.
maidroid.register_core("maidroid_core:basic", {
description = "maidroid core : basic",
inventory_image = "maidroid_core_basic.png",
on_start = on_start,
on_stop = on_stop,
2016-12-24 01:23:31 +01:00
on_resume = on_start,
on_pause = on_stop,
2016-09-14 01:27:13 +02:00
on_step = on_step,
2016-09-13 12:02:53 +02:00
})