167 lines
3.9 KiB
Lua
167 lines
3.9 KiB
Lua
|
local function random_2dvectors() end
|
||
|
local last_pls_y_velocity = {} -- pl_name = y_velocity
|
||
|
local get_node = minetest.get_node
|
||
|
local get_pl_by_name = minetest.get_player_by_name
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.replace_slot_item(player, slot, item)
|
||
|
local inv = player:get_inventory()
|
||
|
local list = inv:get_list("main")
|
||
|
list[slot] = ItemStack(item)
|
||
|
|
||
|
inv:set_list("main", list)
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.hit_player(puncher, player, damage)
|
||
|
player:set_hp(player:get_hp() - damage, {
|
||
|
type = "punch",
|
||
|
object = puncher,
|
||
|
from = "mod"
|
||
|
})
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.is_on_the_ground(player)
|
||
|
local pl_name = player:get_player_name()
|
||
|
local under_pl_feet = player:get_pos()
|
||
|
local pl_velocity = player:get_velocity()
|
||
|
local last_y_velocity = last_pls_y_velocity[pl_name] or pl_velocity.y
|
||
|
|
||
|
under_pl_feet.y = under_pl_feet.y - 0.4
|
||
|
|
||
|
local is_on_the_ground =
|
||
|
not (get_node(under_pl_feet).name == "air")
|
||
|
or (pl_velocity.y == 0 and last_y_velocity < 0)
|
||
|
|
||
|
last_pls_y_velocity[pl_name] = pl_velocity.y
|
||
|
|
||
|
return is_on_the_ground
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.area_raycast_up(center, radius, range, objects, liquids)
|
||
|
local ray_distance = 0.5
|
||
|
|
||
|
if type(radius) == "number" then
|
||
|
radius_x = radius
|
||
|
radius_z = radius
|
||
|
else
|
||
|
radius_x = radius.x
|
||
|
radius_z = radius.z
|
||
|
end
|
||
|
|
||
|
local start_pos = {x = center.x - radius_x, y = center.y + 0.5, z = center.z - radius_z}
|
||
|
local final_pos = {x = center.x + radius_x, y = center.y + 0.5, z = center.z + radius_z}
|
||
|
|
||
|
for x = start_pos.x, final_pos.x, ray_distance do
|
||
|
for z = start_pos.z, final_pos.z, ray_distance do
|
||
|
local pos1 = {x = x, y = start_pos.y, z = z}
|
||
|
local pos2 = vector.add(pos1, {x = 0, y = range, z = 0})
|
||
|
local result = minetest.raycast(pos1, pos2, objects, liquids):next()
|
||
|
|
||
|
if result then
|
||
|
return result
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return false
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
-- TODO: improve
|
||
|
function fbrawl.camera_shake(pl_name, duration, strength, offsets, next_offset, eye_offset, last)
|
||
|
local player = get_pl_by_name(pl_name)
|
||
|
local speed = 0.4
|
||
|
local last = last or false
|
||
|
offsets = offsets or random_2dvectors(duration, strength)
|
||
|
next_offset = next_offset or 1
|
||
|
|
||
|
if not player then
|
||
|
return
|
||
|
end
|
||
|
|
||
|
-- When offsets finish go back to zero
|
||
|
if next_offset > #offsets then
|
||
|
offsets[next_offset] = {x = 0, y = 0}
|
||
|
last = true
|
||
|
end
|
||
|
|
||
|
eye_offset = eye_offset or player:get_eye_offset()
|
||
|
local target_offset = offsets[next_offset]
|
||
|
local new_offset = {
|
||
|
x = fbrawl.interpolate(eye_offset.x, target_offset.x, speed),
|
||
|
y = fbrawl.interpolate(eye_offset.y, target_offset.y, speed),
|
||
|
z = 0
|
||
|
}
|
||
|
eye_offset = new_offset
|
||
|
|
||
|
-- If new_offset and eye_offset are equal go to the next offset or return if it was
|
||
|
-- the last offset
|
||
|
if target_offset.x == eye_offset.x and target_offset.y == eye_offset.y then
|
||
|
if last then
|
||
|
return
|
||
|
end
|
||
|
next_offset = next_offset + 1
|
||
|
else
|
||
|
player:set_eye_offset(new_offset, new_offset)
|
||
|
end
|
||
|
|
||
|
minetest.after(0, function()
|
||
|
fbrawl.camera_shake(pl_name, duration, strength, offsets, next_offset, eye_offset, last)
|
||
|
end)
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.reset_velocity(player)
|
||
|
player:add_velocity(vector.multiply(player:get_velocity(), -1))
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function fbrawl.interpolate(a, b, factor)
|
||
|
local distance = math.abs(a-b)
|
||
|
local min_step = 0.1
|
||
|
local step = distance * factor
|
||
|
if step < min_step then step = min_step end
|
||
|
|
||
|
if a > b then
|
||
|
a = a - step
|
||
|
if a <= b then
|
||
|
a = b
|
||
|
end
|
||
|
else
|
||
|
a = a + step
|
||
|
if a >= b then
|
||
|
a = b
|
||
|
end
|
||
|
end
|
||
|
|
||
|
return a
|
||
|
end
|
||
|
|
||
|
|
||
|
|
||
|
function random_2dvectors(amount, strength)
|
||
|
local rnd = PcgRandom(os.time())
|
||
|
local vectors = {}
|
||
|
|
||
|
for i = 1, amount, 1 do
|
||
|
local rnd_vector = {
|
||
|
x = rnd:next(-1, 1) * strength,
|
||
|
y = rnd:next(-1, 1) * strength,
|
||
|
}
|
||
|
|
||
|
table.insert(vectors, rnd_vector)
|
||
|
end
|
||
|
|
||
|
return vectors
|
||
|
end
|