Golem moves

He goes towards players, he doesnt hurt them yet
master
opfromthestart 2022-08-28 22:48:45 -04:00
parent 66aebf2db6
commit 7e2ebebb8e
5 changed files with 118 additions and 6 deletions

View File

@ -46,7 +46,7 @@ local function astar(s_pos, e_pos, min_cost, max_tries, max_queue, valid_node)
if not valid_node then
function valid_node(nodepos, frompos)
local offset = vector.subtract(nodepos, frompos)
if offset.x==0 and offset.z==0 then
if offset.x == 0 and offset.z == 0 then
return false
end
local node = minetest.get_node(nodepos)
@ -160,7 +160,9 @@ end
function pmb_entity_api.get_default_movement(callable)
return function(self, dtime, moveresult)
callable(self.object, dtime, moveresult)
callable(self, dtime, moveresult)
local entdef = minetest.registered_entities[self.name]
--minetest.log(dump(self.object))
-- minetest.log(dump(entity))
if not self._pmb_follow then
@ -190,17 +192,42 @@ function pmb_entity_api.get_default_movement(callable)
end
self._pmb_path_timer = self._pmb_path_timer + 1
if not entdef._pmb_range then
entdef._pmb_range = 20
end
if entdef._pmb_hostile then
if not self._pmb_target or vector.distance(self._pmb_target:get_pos(), p) > entdef._pmb_range then
self._pmb_target = nil
for _, obj in ipairs(minetest.get_objects_inside_radius(p, entdef._pmb_range)) do
--minetest.log(dump(obj))
--minetest.log(obj:get_luaentity())
if (obj:get_luaentity() and entdef._pmb_hostile[obj:get_luaentity().name]) or
(obj:is_player() and entdef._pmb_hostile["player"]) then
--minetest.log("found enemy")
self._pmb_target = obj
break
end
end
end
if self._pmb_target then
self._pmb_to_pos = self._pmb_target:get_pos()
end
end
if (not self._pmb_path or table.maxn(self._pmb_path) == 0) and self._pmb_path_timer > 70 then
--local ang = math.random()*6.28
local wander_pos = self._pmb_follow:get_pos()
self._pmb_path = astar(p, wander_pos, nil, nil)
self._pmb_path_timer = 0
if self._pmb_to_pos then
self._pmb_path = astar(p, self._pmb_to_pos, nil, nil)
self._pmb_path_timer = 0
end
end
local last_vel = self.object:get_velocity()
self.object:set_velocity(vector.offset(last_vel, 0, -12 * dtime, 0))
if not self._pmb_path or table.maxn(self._pmb_path) == 0 then
self._pmb_to_pos = nil
return
end
if min_cost_2d(p, self._pmb_path[1]) < 1.2 then
@ -208,14 +235,41 @@ function pmb_entity_api.get_default_movement(callable)
end
if table.maxn(self._pmb_path) == 0 then
self.object:set_velocity(vector.new(0, last_vel.y, 0))
self._pmb_to_pos = nil
return
end
local next_pos = self._pmb_path[1]
--minetest.log(dump(next_pos))
local dir = vector.normalize(vector.subtract(next_pos, p))
local entdef = minetest.registered_entities[self.name]
self.object:set_velocity(vector.offset(vector.multiply(dir, entdef._pmb_speed), 0, last_vel.y, 0))
self._pmb_last_pos = p
end
end
function pmb_entity_api.set_my_animation(self, animation_name, overrides, from_blacklist)
local object = self.object
local entdef = minetest.registered_entities[self.name]
if not overrides then overrides = {} end
if not self._animation then self._animation = "idle" end
-- don't allow or only allow some animations
if type(from_blacklist) == "table" and from_blacklist[self._animation] then return false end
-- stop if the animation isn't defined
local anim = entdef._animations[animation_name]
if not anim then return false end
-- keep track of the animation
self._animation = animation_name
object:set_animation(
overrides.frames or anim.frames or { x = 0, y = 0 },
overrides.speed or anim.speed or 24,
overrides.blend or anim.blend or 0,
overrides.loop or anim.loop or true
)
return true
end

View File

@ -0,0 +1,54 @@
minetest.register_entity("pmb_golem:golem", {
initial_properties = {
visual = "mesh",
mesh = "golem.b3d",
textures = {"golem.png"},
stepheight = 1,
hp_max = 10,
physical = true,
automatic_face_movement_dir = -90.0,
},
--[[
on_activate = function(self, staticdata, dtime)
local prevdata = minetest.deserialize(staticdata)
if not prevdata then return end
for key, data in pairs(prevdata) do
self[key] = data
end
minetest.log("Did init")
--self._animation = "idle"
--self._animations = {
-- idle = {x=10, y=49},
-- charge = {x=51, y=90},
--}
end,
get_staticdata = function(self)
local test = self
test.object = nil
return minetest.serialize(test)
end,]]
on_step = pmb_entity_api.get_default_movement(function(self)
--self.object:remove()
--if true then return end
if self._pmb_to_pos then
pmb_entity_api.set_my_animation(self, "charge", nil, {"charge"})
else
pmb_entity_api.set_my_animation(self, "idle", nil, {"idle"})
end
end),
_pmb_speed = 4,
_pmb_hostile = {player=1},
_animations = {
idle = {frames={x=0, y=40}},
charge = {frames={x=41, y=80}},
}
})
minetest.register_craftitem("pmb_golem:golem_spawn",
{
description = "Golem spawn egg",
on_place = function(itemstack, placer, pointed_thing)
local ent = minetest.add_entity(minetest.get_pointed_thing_position(pointed_thing), "pmb_golem:golem")
ent:set_armor_groups({fleshy=70, cracky=100})
end,
})

View File

@ -0,0 +1,4 @@
name = pmb_golem
author = PMB team
description = Adds a hostile golem
depends = pmb_entity_api

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 856 B