Improve particle effect positioning and make optional

This commit is contained in:
stujones11 2014-06-25 20:32:55 +01:00
parent a19923e6aa
commit 33a2cf2933
2 changed files with 25 additions and 18 deletions

View file

@ -2,7 +2,10 @@
-- Global Constants (defaults) -- Global Constants (defaults)
-- Particle texture used when a player or entity it hit -- Enable particle effects
SHOOTER_ENABLE_PARTICLE_FX = true
-- Particle texture used when a player or entity is hit
SHOOTER_EXPLOSION_TEXTURE = "shooter_hit.png" SHOOTER_EXPLOSION_TEXTURE = "shooter_hit.png"
-- Allow node destruction -- Allow node destruction

View file

@ -5,6 +5,7 @@ shooter = {
shots = {}, shots = {},
} }
SHOOTER_ENABLE_PARTICLE_FX = true
SHOOTER_EXPLOSION_TEXTURE = "shooter_hit.png" SHOOTER_EXPLOSION_TEXTURE = "shooter_hit.png"
SHOOTER_ALLOW_NODES = true SHOOTER_ALLOW_NODES = true
SHOOTER_ALLOW_ENTITIES = false SHOOTER_ALLOW_ENTITIES = false
@ -51,13 +52,15 @@ local function get_dot_product(v1, v2)
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
end end
local function spawn_particles(p, v, d, texture) local function get_particle_pos(p, v, d)
if p and v and d then return vector.add(p, vector.multiply(v, {x=d, y=d, z=d}))
end
local function spawn_particles(pos, texture)
if SHOOTER_ENABLE_PARTICLE_FX == true then
if type(texture) ~= "string" then if type(texture) ~= "string" then
texture = SHOOTER_EXPLOSION_TEXTURE texture = SHOOTER_EXPLOSION_TEXTURE
end end
local pos = vector.add(p, vector.multiply(v, {x=d, y=d, z=d}))
pos.y = pos.y + 0.75
local spread = {x=0.1, y=0.1, z=0.1} local spread = {x=0.1, y=0.1, z=0.1}
minetest.add_particlespawner(15, 0.3, minetest.add_particlespawner(15, 0.3,
vector.subtract(pos, spread), vector.add(pos, spread), vector.subtract(pos, spread), vector.add(pos, spread),
@ -125,8 +128,8 @@ local function process_round(round)
for _,ref in ipairs(shooter.objects) do for _,ref in ipairs(shooter.objects) do
local p2 = vector.add(ref.pos, ref.offset) local p2 = vector.add(ref.pos, ref.offset)
if p1 and p2 and ref.name ~= round.name then if p1 and p2 and ref.name ~= round.name then
local x = vector.distance(p1, p2) local d = vector.distance(p1, p2)
if x < round.def.step then if d < round.def.step then
local n = vector.multiply(v1, {x=-1, y=0, z=-1}) local n = vector.multiply(v1, {x=-1, y=0, z=-1})
local v2 = vector.subtract(p1, p2) local v2 = vector.subtract(p1, p2)
local r1 = get_dot_product(n, v2) local r1 = get_dot_product(n, v2)
@ -141,7 +144,7 @@ local function process_round(round)
math.abs(pd.z) < ref.collisionbox[6] then math.abs(pd.z) < ref.collisionbox[6] then
target.object = ref.object target.object = ref.object
target.pos = pt target.pos = pt
target.distance = x target.distance = d
end end
end end
end end
@ -153,15 +156,14 @@ local function process_round(round)
local user = minetest.get_player_by_name(round.name) local user = minetest.get_player_by_name(round.name)
if user then if user then
target.object:punch(user, nil, round.def.tool_caps, v1) target.object:punch(user, nil, round.def.tool_caps, v1)
spawn_particles({x=p1.x, y=p1.y - 1, z=p1.z}, v1, spawn_particles(target.pos, SHOOTER_EXPLOSION_TEXTURE)
target.distance, SHOOTER_EXPLOSION_TEXTURE)
end end
return 1 return 1
elseif pos and SHOOTER_ALLOW_NODES == true then elseif pos and SHOOTER_ALLOW_NODES == true then
local texture = punch_node(pos, round.def) local texture = punch_node(pos, round.def)
if texture then if texture then
spawn_particles({x=p1.x, y=p1.y - 1, z=p1.z}, local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
v1, vector.distance(p1, pos), texture) spawn_particles(pp, texture)
end end
return 1 return 1
end end
@ -172,8 +174,8 @@ local function process_round(round)
if pos then if pos then
local texture = punch_node(pos, round.def) local texture = punch_node(pos, round.def)
if texture then if texture then
spawn_particles({x=p1.x, y=p1.y - 1, z=p1.z}, local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
v1, vector.distance(p1, pos), texture) spawn_particles(pp, texture)
end end
return 1 return 1
end end
@ -202,16 +204,18 @@ function shooter:fire_weapon(user, pointed_thing, def)
local pos = minetest.get_pointed_thing_position(pointed_thing, false) local pos = minetest.get_pointed_thing_position(pointed_thing, false)
local texture = punch_node(pos, def) local texture = punch_node(pos, def)
if texture then if texture then
spawn_particles({x=p1.x, y=p1.y + 0.75, z=p1.z}, local pp = get_particle_pos(p1, v1, vector.distance(p1, pos))
v1, vector.distance(p1, pos), texture) pp.y = pp.y + 1.75
spawn_particles(pp, texture)
end end
elseif pointed_thing.type == "object" then elseif pointed_thing.type == "object" then
local object = pointed_thing.ref local object = pointed_thing.ref
if is_valid_object(object) == true then if is_valid_object(object) == true then
object:punch(user, nil, def.tool_caps, v1) object:punch(user, nil, def.tool_caps, v1)
local p2 = object:getpos() local p2 = object:getpos()
spawn_particles({x=p1.x, y=p1.y + 0.75, z=p1.z}, v1, local pp = get_particle_pos(p1, v1, vector.distance(p1, p2))
vector.distance(p1, p2), SHOOTER_EXPLOSION_TEXTURE) pp.y = pp.y + 1.75
spawn_particles(pp, SHOOTER_EXPLOSION_TEXTURE)
end end
else else
shooter:update_objects() shooter:update_objects()