Start mcl_bows:arrow_entity refactor
This commit is contained in:
parent
21465e4efc
commit
3961a32fb4
|
@ -56,44 +56,6 @@ S("Arrows might get stuck on solid blocks and can be retrieved again. They are a
|
|||
end,
|
||||
})
|
||||
|
||||
local ARROW_ENTITY={
|
||||
physical = true,
|
||||
pointable = false,
|
||||
visual = "mesh",
|
||||
mesh = "mcl_bows_arrow.obj",
|
||||
visual_size = {x=-1, y=1},
|
||||
textures = {"mcl_bows_arrow.png"},
|
||||
collisionbox = {-0.19, -0.125, -0.19, 0.19, 0.125, 0.19},
|
||||
collide_with_objects = false,
|
||||
_fire_damage_resistant = true,
|
||||
|
||||
_lastpos={},
|
||||
_startpos=nil,
|
||||
_damage=1, -- Damage on impact
|
||||
_is_critical=false, -- Whether this arrow would deal critical damage
|
||||
_stuck=false, -- Whether arrow is stuck
|
||||
_stucktimer=nil,-- Amount of time (in seconds) the arrow has been stuck so far
|
||||
_stuckrechecktimer=nil,-- An additional timer for periodically re-checking the stuck status of an arrow
|
||||
_stuckin=nil, --Position of node in which arow is stuck.
|
||||
_shooter=nil, -- ObjectRef of player or mob who shot it
|
||||
_is_arrow = true,
|
||||
_in_player = false,
|
||||
_blocked = false,
|
||||
_viscosity=0, -- Viscosity of node the arrow is currently in
|
||||
_deflection_cooloff=0, -- Cooloff timer after an arrow deflection, to prevent many deflections in quick succession
|
||||
}
|
||||
|
||||
-- Destroy arrow entity self at pos and drops it as an item
|
||||
local function spawn_item(self, pos)
|
||||
if not minetest.is_creative_enabled("") then
|
||||
local item = minetest.add_item(pos, "mcl_bows:arrow")
|
||||
item:set_velocity(vector.new(0, 0, 0))
|
||||
item:set_yaw(self.object:get_yaw())
|
||||
end
|
||||
mcl_burning.extinguish(self.object)
|
||||
self.object:remove()
|
||||
end
|
||||
|
||||
local function damage_particles(pos, is_critical)
|
||||
if is_critical then
|
||||
minetest.add_particlespawner({
|
||||
|
@ -114,18 +76,18 @@ local function damage_particles(pos, is_critical)
|
|||
end
|
||||
end
|
||||
|
||||
function ARROW_ENTITY.on_step(self, dtime)
|
||||
mcl_burning.tick(self.object, dtime, self)
|
||||
-- mcl_burning.tick may remove object immediately
|
||||
if not self.object:get_pos() then return end
|
||||
-- Destroy arrow entity self at pos and drops it as an item
|
||||
local function spawn_item(self, pos)
|
||||
if not minetest.is_creative_enabled("") then
|
||||
local item = minetest.add_item(pos, "mcl_bows:arrow")
|
||||
item:set_velocity(vector.new(0, 0, 0))
|
||||
item:set_yaw(self.object:get_yaw())
|
||||
end
|
||||
mcl_burning.extinguish(self.object)
|
||||
self.object:remove()
|
||||
end
|
||||
|
||||
self._time_in_air = self._time_in_air + .001
|
||||
|
||||
local pos = self.object:get_pos()
|
||||
local dpos = vector.round(vector.new(pos)) -- digital pos
|
||||
local node = minetest.get_node(dpos)
|
||||
|
||||
if self._stuck then
|
||||
local function stuck_arrow_on_step(self, dtime)
|
||||
self._stucktimer = self._stucktimer + dtime
|
||||
self._stuckrechecktimer = self._stuckrechecktimer + dtime
|
||||
if self._stucktimer > ARROW_TIMEOUT then
|
||||
|
@ -133,6 +95,9 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
self.object:remove()
|
||||
return
|
||||
end
|
||||
|
||||
local pos = self.object:get_pos()
|
||||
|
||||
-- Drop arrow as item when it is no longer stuck
|
||||
-- FIXME: Arrows are a bit slow to react and continue to float in mid air for a few seconds.
|
||||
if self._stuckrechecktimer > STUCK_RECHECK_TIME then
|
||||
|
@ -147,6 +112,7 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
end
|
||||
self._stuckrechecktimer = 0
|
||||
end
|
||||
|
||||
-- Pickup arrow if player is nearby (not in Creative Mode)
|
||||
local objects = minetest.get_objects_inside_radius(pos, 1)
|
||||
for _,obj in ipairs(objects) do
|
||||
|
@ -166,74 +132,129 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Check for object "collision". Done every tick (hopefully this is not too stressing)
|
||||
vl_projectile.register("mcl_bows:arrow_entity", {
|
||||
physical = true,
|
||||
pointable = false,
|
||||
visual = "mesh",
|
||||
mesh = "mcl_bows_arrow.obj",
|
||||
visual_size = {x=-1, y=1},
|
||||
textures = {"mcl_bows_arrow.png"},
|
||||
collisionbox = {-0.19, -0.125, -0.19, 0.19, 0.125, 0.19},
|
||||
collide_with_objects = false,
|
||||
_fire_damage_resistant = true,
|
||||
|
||||
_save_fields = {
|
||||
"last_pos", "startpos", "damage", "is_critical", "stuck", "stuckin", "stuckin_player",
|
||||
},
|
||||
|
||||
_startpos=nil,
|
||||
_damage=1, -- Damage on impact
|
||||
_is_critical=false, -- Whether this arrow would deal critical damage
|
||||
_stuck=false, -- Whether arrow is stuck
|
||||
_stucktimer=nil,-- Amount of time (in seconds) the arrow has been stuck so far
|
||||
_stuckrechecktimer=nil,-- An additional timer for periodically re-checking the stuck status of an arrow
|
||||
_stuckin=nil, --Position of node in which arow is stuck.
|
||||
_shooter=nil, -- ObjectRef of player or mob who shot it
|
||||
_is_arrow = true,
|
||||
_in_player = false,
|
||||
_blocked = false,
|
||||
_viscosity=0, -- Viscosity of node the arrow is currently in
|
||||
_deflection_cooloff=0, -- Cooloff timer after an arrow deflection, to prevent many deflections in quick succession
|
||||
|
||||
_vl_projectile = {
|
||||
survive_collision = true,
|
||||
behaviors = {
|
||||
vl_projectile.raycast_collides_with_entities,
|
||||
vl_projectile.collides_with_solids,
|
||||
},
|
||||
on_collide_with_solid = function(self, pos, node, node_def)
|
||||
local def = node_def
|
||||
local vel = self.object:get_velocity()
|
||||
local dpos = vector.round(vector.new(pos)) -- digital pos
|
||||
|
||||
-- Check for the node to which the arrow is pointing
|
||||
local dir
|
||||
if math.abs(vel.y) < 0.00001 then
|
||||
if self._last_pos.y < pos.y then
|
||||
dir = vector.new(0, 1, 0)
|
||||
else
|
||||
dir = vector.new(0, -1, 0)
|
||||
end
|
||||
else
|
||||
dir = minetest.facedir_to_dir(minetest.dir_to_facedir(minetest.yaw_to_dir(self.object:get_yaw()-YAW_OFFSET)))
|
||||
end
|
||||
self._stuckin = vector.add(dpos, dir)
|
||||
local snode = minetest.get_node(self._stuckin)
|
||||
local sdef = minetest.registered_nodes[snode.name]
|
||||
|
||||
if self._damage >= 9 and self._in_player == false then
|
||||
minetest.add_particlespawner({
|
||||
amount = 20,
|
||||
time = .2,
|
||||
minpos = vector.new(0,0,0),
|
||||
maxpos = vector.new(0,0,0),
|
||||
minvel = vector.new(-0.1,-0.1,-0.1),
|
||||
maxvel = vector.new(0.1,0.1,0.1),
|
||||
minexptime = 0.5,
|
||||
maxexptime = 0.5,
|
||||
minsize = 2,
|
||||
maxsize = 2,
|
||||
attached = self.object,
|
||||
collisiondetection = false,
|
||||
vertical = false,
|
||||
texture = "mobs_mc_arrow_particle.png",
|
||||
glow = 1,
|
||||
})
|
||||
-- If node is non-walkable, unknown or ignore, don't make arrow stuck.
|
||||
-- This causes a deflection in the engine.
|
||||
if not sdef or sdef.walkable == false or snode.name == "ignore" then
|
||||
self._stuckin = nil
|
||||
if self._deflection_cooloff <= 0 then
|
||||
-- Lose 1/3 of velocity on deflection
|
||||
local newvel = vector.multiply(vel, 0.6667)
|
||||
|
||||
self.object:set_velocity(newvel)
|
||||
-- Reset deflection cooloff timer to prevent many deflections happening in quick succession
|
||||
self._deflection_cooloff = 1.0
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
local closest_object
|
||||
local closest_distance
|
||||
-- Node was walkable, make arrow stuck
|
||||
self._stuck = true
|
||||
self._stucktimer = 0
|
||||
self._stuckrechecktimer = 0
|
||||
|
||||
if self._deflection_cooloff > 0 then
|
||||
self._deflection_cooloff = self._deflection_cooloff - dtime
|
||||
self.object:set_velocity(vector.new(0, 0, 0))
|
||||
self.object:set_acceleration(vector.new(0, 0, 0))
|
||||
|
||||
minetest.sound_play({name="mcl_bows_hit_other", gain=0.3}, {pos=self.object:get_pos(), max_hear_distance=16}, true)
|
||||
|
||||
if mcl_burning.is_burning(self.object) and snode.name == "mcl_tnt:tnt" then
|
||||
tnt.ignite(self._stuckin)
|
||||
end
|
||||
|
||||
local arrow_dir = self.object:get_velocity()
|
||||
--create a raycast from the arrow based on the velocity of the arrow to deal with lag
|
||||
local raycast = minetest.raycast(pos, vector.add(pos, vector.multiply(arrow_dir, 0.1)), true, false)
|
||||
for hitpoint in raycast do
|
||||
if hitpoint.type == "object" then
|
||||
-- find the closest object that is in the way of the arrow
|
||||
local ok = false
|
||||
if hitpoint.ref:is_player() and enable_pvp then
|
||||
ok = true
|
||||
elseif not hitpoint.ref:is_player() and hitpoint.ref:get_luaentity() then
|
||||
if (hitpoint.ref:get_luaentity().is_mob or hitpoint.ref:get_luaentity()._hittable_by_projectile) then
|
||||
ok = true
|
||||
end
|
||||
end
|
||||
if ok then
|
||||
local dist = vector.distance(hitpoint.ref:get_pos(), pos)
|
||||
if not closest_object or not closest_distance then
|
||||
closest_object = hitpoint.ref
|
||||
closest_distance = dist
|
||||
elseif dist < closest_distance then
|
||||
closest_object = hitpoint.ref
|
||||
closest_distance = dist
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Ignite Campfires
|
||||
if mod_campfire and mcl_burning.is_burning(self.object) and minetest.get_item_group(snode.name, "campfire") ~= 0 then
|
||||
mcl_campfires.light_campfire(self._stuckin)
|
||||
end
|
||||
|
||||
if closest_object then
|
||||
local obj = closest_object
|
||||
-- Activate target
|
||||
if mod_target and snode.name == "mcl_target:target_off" then
|
||||
mcl_target.hit(self._stuckin, 1) --10 redstone ticks
|
||||
end
|
||||
|
||||
-- Push the button! Push, push, push the button!
|
||||
if mod_button and minetest.get_item_group(node.name, "button") > 0 and minetest.get_item_group(node.name, "button_push_by_arrow") == 1 then
|
||||
local bdir = minetest.wallmounted_to_dir(node.param2)
|
||||
-- Check the button orientation
|
||||
if vector.equals(vector.add(dpos, bdir), self._stuckin) then
|
||||
mesecon.push_button(dpos, node)
|
||||
end
|
||||
end
|
||||
end,
|
||||
on_collide_with_entity = function(self, pos, obj)
|
||||
local is_player = obj:is_player()
|
||||
local lua = obj:get_luaentity()
|
||||
if obj == self._shooter and self._time_in_air > 1.02 or obj ~= self._shooter and (is_player or (lua and (lua.is_mob or lua._hittable_by_projectile))) then
|
||||
|
||||
-- Make sure collision is valid
|
||||
if obj == self._shooter then
|
||||
if self._time_in_air < 1.02 then return end
|
||||
else
|
||||
if not (is_player or (lua and (lua.is_mob or lua._hittable_by_projectile))) then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if obj:get_hp() > 0 then
|
||||
-- Check if there is no solid node between arrow and object
|
||||
local ray = minetest.raycast(self.object:get_pos(), obj:get_pos(), true)
|
||||
for pointed_thing in ray do
|
||||
if pointed_thing.type == "object" and pointed_thing.ref == closest_object then
|
||||
if pointed_thing.type == "object" and pointed_thing.ref == obj then
|
||||
-- Target reached! We can proceed now.
|
||||
break
|
||||
elseif pointed_thing.type == "node" then
|
||||
|
@ -257,10 +278,11 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
mcl_burning.set_on_fire(obj, 5)
|
||||
end
|
||||
if not self._in_player and not self._blocked then
|
||||
mcl_util.deal_damage(obj, self._damage, {type = "arrow", source = self._shooter, direct = self.object})
|
||||
if self._extra_hit_func then
|
||||
self._extra_hit_func(obj)
|
||||
end
|
||||
obj:punch(self.object, 1.0, {
|
||||
full_punch_interval=1.0,
|
||||
damage_groups={fleshy=self._damage},
|
||||
}, self.object:get_velocity())
|
||||
|
||||
if obj:is_player() then
|
||||
if not mcl_shields.is_blocking(obj) then
|
||||
local placement
|
||||
|
@ -312,7 +334,6 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
if is_player then
|
||||
if self._shooter and self._shooter:is_player() and not self._in_player and not self._blocked then
|
||||
-- “Ding” sound for hitting another player
|
||||
|
@ -335,95 +356,65 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
minetest.sound_play({name="mcl_bows_hit_other", gain=0.3}, {pos=self.object:get_pos(), max_hear_distance=16}, true)
|
||||
end
|
||||
end
|
||||
|
||||
if not obj:is_player() then
|
||||
mcl_burning.extinguish(self.object)
|
||||
if self._piercing == 0 then
|
||||
self.object:remove()
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
},
|
||||
on_step = function(self, dtime)
|
||||
mcl_burning.tick(self.object, dtime, self)
|
||||
|
||||
-- mcl_burning.tick may remove object immediately
|
||||
if not self.object:get_pos() then return end
|
||||
|
||||
self._time_in_air = self._time_in_air + dtime
|
||||
|
||||
local pos = self.object:get_pos()
|
||||
local dpos = vector.round(vector.new(pos)) -- digital pos
|
||||
local node = minetest.get_node(dpos)
|
||||
|
||||
if self._stuck then
|
||||
return stuck_arrow_on_step(self, dtime)
|
||||
end
|
||||
|
||||
-- Check for node collision
|
||||
if self._lastpos.x~=nil and not self._stuck then
|
||||
local def = minetest.registered_nodes[node.name]
|
||||
local vel = self.object:get_velocity()
|
||||
-- Arrow has stopped in one axis, so it probably hit something.
|
||||
-- This detection is a bit clunky, but sadly, MT does not offer a direct collision detection for us. :-(
|
||||
if (math.abs(vel.x) < 0.0001) or (math.abs(vel.z) < 0.0001) or (math.abs(vel.y) < 0.00001) then
|
||||
-- Check for the node to which the arrow is pointing
|
||||
local dir
|
||||
if math.abs(vel.y) < 0.00001 then
|
||||
if self._lastpos.y < pos.y then
|
||||
dir = vector.new(0, 1, 0)
|
||||
else
|
||||
dir = vector.new(0, -1, 0)
|
||||
end
|
||||
else
|
||||
dir = minetest.facedir_to_dir(minetest.dir_to_facedir(minetest.yaw_to_dir(self.object:get_yaw()-YAW_OFFSET)))
|
||||
end
|
||||
self._stuckin = vector.add(dpos, dir)
|
||||
local snode = minetest.get_node(self._stuckin)
|
||||
local sdef = minetest.registered_nodes[snode.name]
|
||||
|
||||
-- If node is non-walkable, unknown or ignore, don't make arrow stuck.
|
||||
-- This causes a deflection in the engine.
|
||||
if not sdef or sdef.walkable == false or snode.name == "ignore" then
|
||||
self._stuckin = nil
|
||||
if self._deflection_cooloff <= 0 then
|
||||
-- Lose 1/3 of velocity on deflection
|
||||
local newvel = vector.multiply(vel, 0.6667)
|
||||
|
||||
self.object:set_velocity(newvel)
|
||||
-- Reset deflection cooloff timer to prevent many deflections happening in quick succession
|
||||
self._deflection_cooloff = 1.0
|
||||
end
|
||||
else
|
||||
|
||||
-- Node was walkable, make arrow stuck
|
||||
self._stuck = true
|
||||
self._stucktimer = 0
|
||||
self._stuckrechecktimer = 0
|
||||
|
||||
self.object:set_velocity(vector.new(0, 0, 0))
|
||||
self.object:set_acceleration(vector.new(0, 0, 0))
|
||||
|
||||
minetest.sound_play({name="mcl_bows_hit_other", gain=0.3}, {pos=self.object:get_pos(), max_hear_distance=16}, true)
|
||||
|
||||
if mcl_burning.is_burning(self.object) and snode.name == "mcl_tnt:tnt" then
|
||||
tnt.ignite(self._stuckin)
|
||||
-- Add tracer
|
||||
if self._damage >= 9 and self._in_player == false then
|
||||
minetest.add_particlespawner({
|
||||
amount = 20,
|
||||
time = .2,
|
||||
minpos = vector.new(0,0,0),
|
||||
maxpos = vector.new(0,0,0),
|
||||
minvel = vector.new(-0.1,-0.1,-0.1),
|
||||
maxvel = vector.new(0.1,0.1,0.1),
|
||||
minexptime = 0.5,
|
||||
maxexptime = 0.5,
|
||||
minsize = 2,
|
||||
maxsize = 2,
|
||||
attached = self.object,
|
||||
collisiondetection = false,
|
||||
vertical = false,
|
||||
texture = "mobs_mc_arrow_particle.png",
|
||||
glow = 1,
|
||||
})
|
||||
end
|
||||
|
||||
-- Ignite Campfires
|
||||
if mod_campfire and mcl_burning.is_burning(self.object) and minetest.get_item_group(snode.name, "campfire") ~= 0 then
|
||||
mcl_campfires.light_campfire(self._stuckin)
|
||||
if self._deflection_cooloff > 0 then
|
||||
self._deflection_cooloff = self._deflection_cooloff - dtime
|
||||
end
|
||||
|
||||
-- Activate target
|
||||
if mod_target and snode.name == "mcl_target:target_off" then
|
||||
mcl_target.hit(self._stuckin, 1) --10 redstone ticks
|
||||
end
|
||||
|
||||
-- Push the button! Push, push, push the button!
|
||||
if mod_button and minetest.get_item_group(node.name, "button") > 0 and minetest.get_item_group(node.name, "button_push_by_arrow") == 1 then
|
||||
local bdir = minetest.wallmounted_to_dir(node.param2)
|
||||
-- Check the button orientation
|
||||
if vector.equals(vector.add(dpos, bdir), self._stuckin) then
|
||||
mesecon.push_button(dpos, node)
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif (def and def.liquidtype ~= "none") then
|
||||
-- TODO: change to use vl_physics
|
||||
local def = minetest.registered_nodes[minetest.get_node(pos).name]
|
||||
if def and def.liquidtype ~= "none" then
|
||||
-- Slow down arrow in liquids
|
||||
local v = def.liquid_viscosity
|
||||
if not v then
|
||||
v = 0
|
||||
end
|
||||
--local old_v = self._viscosity
|
||||
local v = def.liquid_viscosity or 0
|
||||
self._viscosity = v
|
||||
|
||||
local vpenalty = math.max(0.1, 0.98 - 0.1 * v)
|
||||
local vel = self.object:get_velocity()
|
||||
if math.abs(vel.x) > 0.001 then
|
||||
vel.x = vel.x * vpenalty
|
||||
end
|
||||
|
@ -432,38 +423,34 @@ function ARROW_ENTITY.on_step(self, dtime)
|
|||
end
|
||||
self.object:set_velocity(vel)
|
||||
end
|
||||
end
|
||||
|
||||
-- Process as projectile
|
||||
vl_projectile.update_projectile(self, dtime)
|
||||
|
||||
-- Update yaw
|
||||
if not self._stuck then
|
||||
local vel = self.object:get_velocity()
|
||||
if vel and not self._stuck then
|
||||
local yaw = minetest.dir_to_yaw(vel)+YAW_OFFSET
|
||||
local pitch = dir_to_pitch(vel)
|
||||
self.object:set_rotation({ x = 0, y = yaw, z = pitch })
|
||||
end
|
||||
end,
|
||||
|
||||
-- Update internal variable
|
||||
self._lastpos = pos
|
||||
end
|
||||
|
||||
-- Force recheck of stuck arrows when punched.
|
||||
-- Otherwise, punching has no effect.
|
||||
function ARROW_ENTITY.on_punch(self)
|
||||
-- Force recheck of stuck arrows when punched.
|
||||
-- Otherwise, punching has no effect.
|
||||
on_punch = function(self)
|
||||
if self._stuck then
|
||||
self._stuckrechecktimer = STUCK_RECHECK_TIME
|
||||
end
|
||||
end
|
||||
end,
|
||||
get_staticdata = function(self)
|
||||
local out = {}
|
||||
local save_fields = self._save_fields
|
||||
for i = 1,#save_fields do
|
||||
local field = save_fields[i]
|
||||
out[field] = self["_"..field]
|
||||
end
|
||||
|
||||
function ARROW_ENTITY.get_staticdata(self)
|
||||
local out = {
|
||||
lastpos = self._lastpos,
|
||||
startpos = self._startpos,
|
||||
damage = self._damage,
|
||||
is_critical = self._is_critical,
|
||||
stuck = self._stuck,
|
||||
stuckin = self._stuckin,
|
||||
stuckin_player = self._in_player,
|
||||
}
|
||||
if self._stuck then
|
||||
-- If _stucktimer is missing for some reason, assume the maximum
|
||||
if not self._stucktimer then
|
||||
|
@ -471,40 +458,35 @@ function ARROW_ENTITY.get_staticdata(self)
|
|||
end
|
||||
out.stuckstarttime = minetest.get_gametime() - self._stucktimer
|
||||
end
|
||||
|
||||
if self._shooter and self._shooter:is_player() then
|
||||
out.shootername = self._shooter:get_player_name()
|
||||
end
|
||||
return minetest.serialize(out)
|
||||
end
|
||||
end,
|
||||
on_activate = function(self, staticdata, dtime_s)
|
||||
self.object:set_armor_groups({ immortal = 1 })
|
||||
|
||||
function ARROW_ENTITY.on_activate(self, staticdata, dtime_s)
|
||||
self._time_in_air = 1.0
|
||||
local data = minetest.deserialize(staticdata)
|
||||
if data then
|
||||
self._stuck = data.stuck
|
||||
if data.stuck then
|
||||
if not data then return end
|
||||
|
||||
-- Restore arrow state
|
||||
local save_fields = self._save_fields
|
||||
for i = 1,#save_fields do
|
||||
local field = save_fields[i]
|
||||
self["_"..field] = data[field]
|
||||
end
|
||||
|
||||
if data.stuckstarttime then
|
||||
-- First, check if the stuck arrow is aleady past its life timer.
|
||||
-- If yes, delete it.
|
||||
self._stucktimer = minetest.get_gametime() - data.stuckstarttime
|
||||
if self._stucktimer > ARROW_TIMEOUT then
|
||||
mcl_burning.extinguish(self.object)
|
||||
self.object:remove()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Perform a stuck recheck on the next step.
|
||||
self._stuckrechecktimer = STUCK_RECHECK_TIME
|
||||
|
||||
self._stuckin = data.stuckin
|
||||
end
|
||||
|
||||
-- Get the remaining arrow state
|
||||
self._lastpos = data.lastpos
|
||||
self._startpos = data.startpos
|
||||
self._damage = data.damage
|
||||
self._is_critical = data.is_critical
|
||||
if data.shootername then
|
||||
local shooter = minetest.get_player_by_name(data.shootername)
|
||||
if shooter and shooter:is_player() then
|
||||
|
@ -515,9 +497,8 @@ function ARROW_ENTITY.on_activate(self, staticdata, dtime_s)
|
|||
if data.stuckin_player then
|
||||
self.object:remove()
|
||||
end
|
||||
end
|
||||
self.object:set_armor_groups({ immortal = 1 })
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
minetest.register_on_respawnplayer(function(player)
|
||||
for _, obj in pairs(player:get_children()) do
|
||||
|
@ -528,8 +509,6 @@ minetest.register_on_respawnplayer(function(player)
|
|||
end
|
||||
end)
|
||||
|
||||
minetest.register_entity("mcl_bows:arrow_entity", ARROW_ENTITY)
|
||||
|
||||
if minetest.get_modpath("mcl_core") and minetest.get_modpath("mcl_mobitems") then
|
||||
minetest.register_craft({
|
||||
output = "mcl_bows:arrow 4",
|
||||
|
@ -544,3 +523,4 @@ end
|
|||
if minetest.get_modpath("doc_identifier") then
|
||||
doc.sub.identifier.register_object("mcl_bows:arrow_entity", "craftitems", "mcl_bows:arrow")
|
||||
end
|
||||
|
||||
|
|
|
@ -76,6 +76,7 @@ function mcl_bows.shoot_arrow(arrow_item, pos, dir, yaw, shooter, power, damage,
|
|||
le._startpos = pos
|
||||
le._knockback = knockback
|
||||
le._collectable = collectable
|
||||
le._arrow_item = arrow_item
|
||||
minetest.sound_play("mcl_bows_bow_shoot", {pos=pos, max_hear_distance=16}, true)
|
||||
if shooter and shooter:is_player() then
|
||||
if obj:get_luaentity().player == "" then
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name = mcl_bows
|
||||
author = Arcelmi
|
||||
description = This mod adds bows and arrows for MineClone 2.
|
||||
depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util, mcl_shields, mcl_fovapi, mcl_luck
|
||||
depends = controls, mcl_particles, mcl_enchanting, mcl_init, mcl_util, mcl_shields, mcl_fovapi, mcl_luck, vl_projectile
|
||||
optional_depends = awards, mcl_achievements, mcl_core, mcl_mobitems, playerphysics, doc, doc_identifier, mesecons_button
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ local function snowball_particles(pos, vel)
|
|||
texture = "weather_pack_snow_snowflake"..math.random(1,2)..".png",
|
||||
})
|
||||
end
|
||||
minetest.register_entity("mcl_throwing:snowball_entity", {
|
||||
vl_projectile.register("mcl_throwing:snowball_entity", {
|
||||
physical = false,
|
||||
timer=0,
|
||||
textures = {"mcl_throwing_snowball.png"},
|
||||
|
@ -50,10 +50,6 @@ minetest.register_entity("mcl_throwing:snowball_entity", {
|
|||
|
||||
get_staticdata = mcl_throwing.get_staticdata,
|
||||
on_activate = mcl_throwing.on_activate,
|
||||
|
||||
on_step = vl_projectile.update_projectile,
|
||||
_thrower = nil,
|
||||
_lastpos = nil,
|
||||
_vl_projectile = {
|
||||
behaviors = {
|
||||
vl_projectile.collides_with_solids,
|
||||
|
|
|
@ -9,7 +9,7 @@ function mod.update_projectile(self, dtime)
|
|||
local entity_vl_projectile = entity_def._vl_projectile or {}
|
||||
|
||||
-- Update entity timer
|
||||
self.timer = self.timer + dtime
|
||||
self.timer = (self.timer or 0) + dtime
|
||||
|
||||
-- Run behaviors
|
||||
local behaviors = entity_vl_projectile.behaviors or {}
|
||||
|
@ -24,6 +24,24 @@ end
|
|||
local function no_op()
|
||||
end
|
||||
|
||||
local enable_pvp = minetest.settings:get_bool("enable_pvp")
|
||||
local function check_hitpoint(hitpoint)
|
||||
if hitpoint.type ~= "object" then return false end
|
||||
|
||||
-- find the closest object that is in the way of the arrow
|
||||
if hitpoint.ref:is_player() and enable_pvp then
|
||||
return true
|
||||
end
|
||||
|
||||
if not hitpoint.ref:is_player() and hitpoint.ref:get_luaentity() then
|
||||
if (hitpoint.ref:get_luaentity().is_mob or hitpoint.ref:get_luaentity()._hittable_by_projectile) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
function mod.collides_with_solids(self, dtime, entity_def, projectile_def)
|
||||
local pos = self.object:get_pos()
|
||||
|
||||
|
@ -37,10 +55,21 @@ function mod.collides_with_solids(self, dtime, entity_def, projectile_def)
|
|||
local node = minetest.get_node(pos)
|
||||
local node_def = minetest.registered_nodes[node.name]
|
||||
local collides_with = projectile_def.collides_with
|
||||
|
||||
if entity_def.physical then
|
||||
-- Projectile has stopped in one axis, so it probably hit something.
|
||||
-- This detection is a bit clunky, but sadly, MT does not offer a direct collision detection for us. :-(
|
||||
local vel = self.object:get_velocity()
|
||||
if not( (math.abs(vel.x) < 0.0001) or (math.abs(vel.z) < 0.0001) or (math.abs(vel.y) < 0.00001) ) then
|
||||
self._last_pos = pos
|
||||
return
|
||||
end
|
||||
else
|
||||
if node_def and not node_def.walkable and (not collides_with or not mcl_util.match_node_to_filter(node.name, collides_with)) then
|
||||
self._last_pos = pos
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Call entity collied hook
|
||||
local hook = projectile_def.on_collide_with_solid or no_op
|
||||
|
@ -69,6 +98,7 @@ function mod.collides_with_solids(self, dtime, entity_def, projectile_def)
|
|||
end
|
||||
|
||||
local function handle_entity_collision(self, entity_def, projectile_def, entity)
|
||||
local pos = self.object:get_pos()
|
||||
local dir = vector.normalize(self.object:get_velocity())
|
||||
local self_vl_projectile = self._vl_projectile
|
||||
|
||||
|
@ -81,7 +111,7 @@ local function handle_entity_collision(self, entity_def, projectile_def, entity)
|
|||
-- Call entity collied hook
|
||||
(projectile_def.on_collide_with_entity or no_op)(self, pos, entity)
|
||||
|
||||
-- Call node collided hook
|
||||
-- Call entity reverse hook
|
||||
local other_entity_def = minetest.registered_entities[entity.name] or {}
|
||||
local other_entity_vl_projectile = other_entity_def._vl_projectile or {}
|
||||
local hook = (other_entity_vl_projectile or {}).on_collide or no_op
|
||||
|
@ -126,6 +156,32 @@ function mod.collides_with_entities(self, dtime, entity_def, projectile_def)
|
|||
end
|
||||
end
|
||||
|
||||
function mod.raycast_collides_with_entities(self, dtime, entity_def, projectile_def)
|
||||
local closest_object
|
||||
local closest_distance
|
||||
|
||||
local pos = self.object:get_pos()
|
||||
local arrow_dir = self.object:get_velocity()
|
||||
|
||||
--create a raycast from the arrow based on the velocity of the arrow to deal with lag
|
||||
local raycast = minetest.raycast(pos, vector.add(pos, vector.multiply(arrow_dir, 0.1)), true, false)
|
||||
for hitpoint in raycast do
|
||||
if check_hitpoint(hitpoint) then
|
||||
local hitpoint_ref = hitpoint.ref
|
||||
local dist = vector.distance(hitpoint_ref:get_pos(), pos)
|
||||
if not closest_distance or dist < closest_distance then
|
||||
closest_object = hitpoint_ref
|
||||
closest_distance = dist
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if closest_object then
|
||||
return handle_entity_collision(self, entity_def, projectile_def, closest_object)
|
||||
end
|
||||
end
|
||||
|
||||
function mod.create(entity_id, options)
|
||||
local obj = minetest.add_entity(options.pos, entity_id, options.staticdata)
|
||||
|
||||
|
@ -150,3 +206,17 @@ function mod.create(entity_id, options)
|
|||
return obj
|
||||
end
|
||||
|
||||
function mod.register(name, def)
|
||||
assert(def._vl_projectile)
|
||||
|
||||
if not def.on_step then
|
||||
def.on_step = mod.update_projectile
|
||||
end
|
||||
|
||||
def._thrower = nil
|
||||
def._shooter = nil
|
||||
def._last_pos = nil
|
||||
|
||||
minetest.register_entity(name, def)
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue