Improve entity system

This commit is contained in:
IamPyu 2024-12-15 14:05:25 -06:00
parent 8830e18c25
commit 8ec337bb2b
2 changed files with 40 additions and 76 deletions

View File

@ -47,59 +47,32 @@ PyuTest.get_nearest_entity = function(entity, pos, range, only_player, dont_igno
return nearest
end
local class = {}
local class = {
_heal_timer = 0,
_attack_timer = 0,
_velocity = vector.new(0, 0, 0),
_state = {
target = {
object = nil,
position = nil,
path = nil,
pathindex = nil
}
},
}
function class:do_physics()
local obj = self.object
local state = self.state
local cfg = self.options
local moveresult = self.moveresult
local pos = obj:get_pos()
if cfg.gravity then
if not moveresult.touching_ground then
end
end
-- TODO
end
function class:path_find_entity(target)
local obj = self.object
local state = self.state
local cfg = self.options
local pos = obj:get_pos()
if state.target.path == nil then
state.target.object = target
if state.target.object == nil then
return
end
state.target.position = state.target.object:get_pos()
state.target.path = core.find_path(pos, state.target.position, cfg.view_range, cfg.max_jump, cfg.max_drop,
PATH_FIND_ALGORITHM)
state.target.pathindex = 1
else
if state.target.pathindex == #state.target.path then
state.target.path = nil
state.target.object = nil
state.target.position = nil
state.target.pathindex = nil
else
local p = state.target.path[state.target.pathindex]
-- obj:set_velocity(p * dtime)
obj:move_to(p, true)
state.target.pathindex = state.target.pathindex + 1
end
end
-- TODO
end
function class:path_find_nearest_entity(follow_only_player)
local obj = self.object
local state = self.state
local cfg = self.options
local state = self._state
local cfg = self._options
local pos = obj:get_pos()
self:path_find_entity(PyuTest.get_nearest_entity(obj, pos, cfg.view_range, follow_only_player, false))
@ -108,7 +81,7 @@ end
function class:on_punch()
local pos = self.object:get_pos()
core.sound_play({
name = self.options.sounds.hurt,
name = self._options.sounds.hurt,
pos = pos
})
@ -135,23 +108,27 @@ end
function class:on_death()
local pos = self.object:get_pos()
for _, v in pairs(self.options.drops) do
for _, v in pairs(self._options.drops) do
PyuTest.drop_item(pos, v)
end
end
function class:on_step(dtime, moveresult)
self.dtime = dtime
self.moveresult = moveresult
self._dtime = dtime
self._moveresult = moveresult
self._attack_timer = self._attack_timer + (2 * dtime)
self._heal_timer = self._heal_timer + (2 * dtime)
local obj = self.object
local cfg = self.options
local cfg = self._options
local p = obj:get_properties()
local hp = obj:get_hp()
if self.options.health_regen and not (hp >= p.hp_max) then
obj:set_hp(hp + 0.5)
if self._options.health_regen and hp < p.hp_max and self._heal_timer >= 5 then
self._heal_timer = 0
obj:set_hp(hp + 1)
end
if obj:get_hp() > p.hp_max then
@ -163,26 +140,15 @@ function class:on_step(dtime, moveresult)
self:do_physics()
if not self.state.action_timer then
core.after(1, function ()
self.state.action_timer = true
core.after(1, function ()
self.action_timer = false
end)
end)
end
-- follow_player
if self.options.follow_player then
if self._options.follow_player then
self:path_find_nearest_entity(false)
end
-- follow_items
for o in core.objects_inside_radius(obj:get_pos(), cfg.view_range) do
if o ~= obj then
for _, v in pairs(self.options.follow_items) do
for _, v in pairs(self._options.follow_items) do
local i = o:get_wielded_item()
if i:get_name() == v then
@ -193,14 +159,15 @@ function class:on_step(dtime, moveresult)
end
-- attack_entities
if self.state.action_timer then
if self._attack_timer >= 3 then
self._attack_timer = 0
for o in core.objects_inside_radius(obj:get_pos(), cfg.hit_range) do
if o ~= obj then
for _, v in pairs(cfg.attack_entities) do
local lua_entity = o:get_luaentity()
if (v == "player" and o:is_player()) or (lua_entity and v == lua_entity.name) then
PyuTest.deal_damage(o, cfg.hit_damage * dtime, {
PyuTest.deal_damage(o, cfg.hit_damage, {
type = "punch",
object = obj
})
@ -209,6 +176,8 @@ function class:on_step(dtime, moveresult)
end
end
end
self.object:set_velocity(self._velocity)
end
PyuTest.make_mob = function(name, properties, options)
@ -256,15 +225,6 @@ PyuTest.make_mob = function(name, properties, options)
show_on_minimap = properties.show_on_minimap or true,
infotext = "",
}),
options = cfg,
state = {
action_timer = false,
target = {
object = nil,
position = nil,
path = nil,
pathindex = nil
}
},
_options = cfg,
}, { __index = class }))
end

View File

@ -8,6 +8,10 @@ PyuTest.get_mana = function (player)
end
PyuTest.use_mana = function(player, amount)
if core.is_creative_enabled(player) then
return true
end
if PyuTest.get_mana(player) >= amount then
manas[player] = manas[player] - amount
return true