2024-04-03 08:12:18 +02:00
|
|
|
local mod = mcl_physics
|
|
|
|
|
|
|
|
local registered_environment_effects = {}
|
|
|
|
|
2024-04-03 08:19:11 +02:00
|
|
|
function mod.register_environment_effect(effect)
|
|
|
|
local list = registered_environment_effects
|
2024-04-03 08:12:18 +02:00
|
|
|
list[#list + 1] = effect
|
|
|
|
end
|
|
|
|
|
2024-05-04 13:27:53 +02:00
|
|
|
function mod.get_environment_effect(pos, vel, staticdata, mass, entity)
|
|
|
|
local v = vector.zero()
|
|
|
|
local a = vector.zero()
|
2024-04-03 08:12:18 +02:00
|
|
|
|
|
|
|
-- Accumulate all enviornmental effects
|
|
|
|
for _,effect in ipairs(registered_environment_effects) do
|
2024-05-04 13:27:53 +02:00
|
|
|
local dv,da = effect(pos, vel, staticdata, entity)
|
2024-04-03 08:12:18 +02:00
|
|
|
if dv then
|
|
|
|
v = v + dv
|
|
|
|
end
|
|
|
|
if da then
|
|
|
|
a = a + da
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-03 08:48:01 +02:00
|
|
|
-- Disable small effects
|
|
|
|
if vector.length(v) < 0.01 then v = nil end
|
|
|
|
if vector.length(a) < 0.01 then a = nil end
|
|
|
|
|
|
|
|
return v,a
|
2024-04-03 08:12:18 +02:00
|
|
|
end
|
|
|
|
|
2024-05-04 13:27:53 +02:00
|
|
|
local DEFAULT_ENTITY_PHYSICS = {
|
|
|
|
mass = 1,
|
|
|
|
}
|
2024-04-03 08:12:18 +02:00
|
|
|
function mod.apply_entity_environmental_physics(self, data)
|
|
|
|
data = data or {}
|
|
|
|
|
2024-05-04 13:27:53 +02:00
|
|
|
local physics = self._mcl_physics or DEFAULT_ENTITY_PHYSICS
|
|
|
|
local mass = physics.mass or DEFAULT_ENTITY_PHYSICS.mass
|
|
|
|
|
2024-04-03 08:12:18 +02:00
|
|
|
local pos = self.object:get_pos()
|
|
|
|
local vel = self.object:get_velocity()
|
2024-05-04 13:27:53 +02:00
|
|
|
local new_velocity,new_acceleration = mcl_physics.get_environment_effect(pos, vel, data, mass, self)
|
2024-04-03 08:12:18 +02:00
|
|
|
|
2024-05-04 13:27:53 +02:00
|
|
|
--if new_velocity then print("new_velocity="..tostring(new_velocity)) end
|
|
|
|
--if new_acceleration then print("new_acceleration="..tostring(new_acceleration)) end
|
2024-04-03 08:48:01 +02:00
|
|
|
|
2024-04-03 08:12:18 +02:00
|
|
|
-- Update entity states
|
|
|
|
self._flowing = data.flowing
|
|
|
|
|
|
|
|
-- Apply environmental effects if there are any
|
|
|
|
if new_velocity or new_acceleration then
|
|
|
|
if new_acceleration then self.object:set_acceleration(new_acceleration) end
|
|
|
|
if new_velocity then self.object:set_velocity(new_velocity) end
|
|
|
|
|
|
|
|
self.physical_state = true
|
|
|
|
self._flowing = true
|
|
|
|
self.object:set_properties({
|
|
|
|
physical = true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|