Version 3.0

This commit is contained in:
echoes91
2024-02-03 10:56:25 +01:00
parent 5ac99ff835
commit 534e7ecc8f
8 changed files with 115 additions and 77 deletions

View File

@ -27,7 +27,7 @@ function spears_throw (itemstack, player, pointed_thing)
return false
end
else -- Avoid hitting yourself and throw
local throw_speed = SPEARS_THROW_SPEED
local throw_speed = tonumber(minetest.settings:get("spears_throw_speed"))
while vector.distance(player_pos, throw_pos) < 1.2 do
throw_pos = vector.add(throw_pos, vector.multiply(direction, 0.1))
end
@ -90,10 +90,10 @@ function spears_set_entity(spear_type, base_damage, toughness)
local pitch = math.acos(velocity.y/speed) - math.pi/3
local spearhead_pos = vector.add(pos, vector.multiply(direction, 0.5))
self.object:set_rotation({x = 0, y = yaw + math.pi/2, z = pitch})
-- Hit someone?
local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 0.6)
-- Hit a target?
local objects_in_radius = minetest.get_objects_inside_radius(spearhead_pos, 1)
for _,object in ipairs(objects_in_radius) do
if object:get_luaentity() ~= self and object:get_armor_groups().fleshy then
if spears_check_target(self, object, spearhead_pos) and object:get_armor_groups().fleshy then
local damage = (speed + base_damage)^1.15 - 20
object:punch(self.object, 1.0, {full_punch_interval = 1.0, damage_groups = {fleshy=damage},}, direction)
self.object:remove()
@ -130,7 +130,8 @@ function spears_set_entity(spear_type, base_damage, toughness)
end
else -- Get drag
local viscosity = minetest.registered_nodes[node.name].liquid_viscosity
local drag = math.max(viscosity, SPEARS_DRAG_COEFF)
local drag_coeff = tonumber(minetest.settings:get("spears_drag_coeff"))
local drag = math.max(viscosity, drag_coeff)
local acceleration = vector.multiply(velocity, -drag)
acceleration.y = acceleration.y - 10 * ((7 - drag) / 7)
self.object:set_acceleration(acceleration)
@ -143,11 +144,12 @@ end
function spears_check_node(node_name)
local node = minetest.registered_nodes[node_name]
local cracky_limit = tonumber(minetest.settings:get("spears_node_cracky_limit"))
if node == nil then
return SPEARS_NODE_UNKNOWN
elseif node.groups.cracky ~= nil and node.groups.cracky < SPEARS_NODE_CRACKY_LIMIT then
elseif node.groups.cracky ~= nil and node.groups.cracky < cracky_limit then
return SPEARS_NODE_CRACKY
elseif node.walkable and not node.buildable then
elseif node.walkable and not node.buildable_to then
return SPEARS_NODE_STICKY
else
return SPEARS_NODE_THROUGH
@ -163,3 +165,59 @@ function spears_wear(initial_wear, toughness)
return wear
end
end
function spears_check_target(self, object, spearhead_pos)
if object:get_luaentity() == self then
return false
elseif isPointInsideEntity(object, spearhead_pos) then
-- print(self.name .. " hit " .. object:get_luaentity().name)
return true
else
-- print(self.name .. " missed " .. object:get_luaentity().name)
return false
end
end
-- Function to check if a point is inside an entity
function isPointInsideEntity(object, point)
local pos = object:get_pos()
local collisionbox = object:get_properties().collisionbox
local yaw = object:get_yaw()
-- Adjust position for the center of the collision box
local center = {
x = pos.x + (collisionbox[1] + collisionbox[4]) / 2,
y = pos.y + (collisionbox[2] + collisionbox[5]) / 2,
z = pos.z + (collisionbox[3] + collisionbox[6]) / 2
}
-- Rotate the point around the entity's center
local rotatedPoint = rotatePoint(point, center, yaw)
-- Check if the rotated point is inside the collision box
return rotatedPoint.x >= pos.x + collisionbox[1] and
rotatedPoint.x <= pos.x + collisionbox[4] and
rotatedPoint.y >= pos.y + collisionbox[2] and
rotatedPoint.y <= pos.y + collisionbox[5] and
rotatedPoint.z >= pos.z + collisionbox[3] and
rotatedPoint.z <= pos.z + collisionbox[6]
end
-- Function to rotate a point around a center and yaw angle
function rotatePoint(point, center, yaw)
local cosYaw = math.cos(yaw)
local sinYaw = math.sin(yaw)
local translatedX = point.x - center.x
local translatedZ = point.z - center.z
local rotatedX = translatedX * cosYaw - translatedZ * sinYaw
local rotatedZ = translatedX * sinYaw + translatedZ * cosYaw
return {
x = rotatedX + center.x,
y = point.y,
z = rotatedZ + center.z
}
end