sumo_with_spectate/items.lua

93 lines
4.0 KiB
Lua

local stick_knockback = 30 --multiplier for how hard the stick hits
local stick_vault_reach = 3 -- how close to the pointed node must the player be to vault
local stick_vault_timeout = 1 -- int timer for how long the vault cannot be used after it is used
local allow_swap_distance = 4 -- if an opponent is within this distance, then if the player uses the pushstick with the shift key pressed, the players switch positions.
minetest.register_craftitem("sumo:pushstick", {
description = "Push Stick",
inventory_image = "default_stick.png",
stack_max = 1,
wield_scale = {x = 2, y = 2, z = 2},
on_drop = function() end,
on_use = function(itemstack, user, pointed_thing)
local sound = 'swish'..math.random(1,4)
minetest.sound_play(sound, {
pos = user:get_pos(),
max_hear_distance = 5,
gain = 10.0,
})
if pointed_thing == nil then return end
if pointed_thing.type == 'object' then
if minetest.is_player(pointed_thing.ref) == true then
local dir = user:get_look_dir()
local keys = user:get_player_control()
local swap = false
local hitted_pos = pointed_thing.ref:get_pos()
local hitter_pos = user:get_pos()
if keys.sneak and vector.distance(hitted_pos,hitter_pos) < allow_swap_distance then
swap = true
user:move_to(hitted_pos, true)
pointed_thing.ref:move_to(hitter_pos, true)
pointed_thing.ref:add_player_velocity(vector.multiply({x = -dir.x, y = dir.y, z= -dir.z}, stick_knockback/2))
else
pointed_thing.ref:add_player_velocity(vector.multiply(dir, stick_knockback))
if not swap then
local sound = 'thwack'..math.random(1,3)
minetest.sound_play(sound, {
pos = user:get_pos(),
max_hear_distance = 10,
gain = 10.0,
})
local sound = 'hurt'..math.random(1,2)
minetest.sound_play(sound, {
to_player = pointed_thing.ref:get_player_name(),
pos = user:get_pos(),
gain = 10.0,
})
end
end
end
end
end,
on_place = function(itemstack, placer, pointed_thing)
if pointed_thing == nil then return end
if pointed_thing.type == 'node' then
if vector.distance(pointed_thing.under, placer:get_pos()) < stick_vault_reach then
local first_use = false
local imeta = itemstack:get_meta()
local old_time = imeta:get_int('old_time')
local current_time = minetest.get_gametime()
if old_time == 0 or old_time == nil then
first_use = true
end
if first_use or current_time > old_time + stick_vault_timeout then
local lookvect = placer:get_look_dir()
local pushvect = vector.normalize( {x=lookvect.x, z=lookvect.z, y= math.sqrt(1-(lookvect.y*lookvect.y))})
--gives a unit vector that is 90 deg offset in the vert direction
local force = 10 * vector.length(vector.normalize( {x=lookvect.x, z=lookvect.z, y= 0}))
placer:add_player_velocity(vector.multiply(pushvect, force))
--update the staff time for next check
local sound = 'jump'..math.random(1,2)
minetest.sound_play(sound, {
pos = placer:get_pos(),
max_hear_distance = 10,
gain = 10.0,
})
imeta:set_int('old_time', current_time)
return itemstack
end
end
end
end,
})