Move along players by teleporting

main
copygirl 1 year ago
parent efc5e81946
commit f515234b31
  1. 22
      contraption.lua

@ -25,7 +25,6 @@ local id_counter = 0
-- Table containing the currently active contraptions keyed by their id.
local active_contraptions = {}
-- TODO: Push along players.
-- TODO: Friction depending on ground.
-- TODO: Gravity.
-- TODO: Water physics!
@ -200,8 +199,11 @@ function contraption.load_from_string(id, str)
local result = setmetatable({
id = id,
region = obj.region,
nodes = obj.nodes,
region = {
min = vector.convert(obj.region.min),
max = vector.convert(obj.region.max)
},
nodes = obj.nodes,
num_nodes = obj.num_nodes,
motion = obj.motion and vector.convert(obj.motion ) or nil,
@ -272,10 +274,24 @@ function contraption:update(delta_time)
end
-- .. and move it 1 step into that direction.
if which ~= 0 then
local objects_to_move = {}
local pos1 = self.region.min:offset(-1, -1, -1)
local pos2 = self.region.max:offset( 1, 2, 1)
for _, obj in ipairs(minetest.get_objects_in_area(pos1, pos2)) do
if obj:is_player() then
local adjusted_pos = vector.offset(obj:get_pos(), 0, -0.5, 0)
local rel_pos = adjusted_pos:round() - self.region.min
if self.nodes[rel_pos:hash()] then
table_insert(objects_to_move, obj)
end
end
end
local step = vector.zero()
step[which] = math.sign(self.partial[which])
if self:move(step) then
self.partial = self.partial - step
for _, obj in ipairs(objects_to_move) do obj:set_pos(vector.add(obj:get_pos(), step)) end
else
-- Reset motion into the direction that we bumped into.
-- Allows for "sliding" along walls instead of stopping outright.

Loading…
Cancel
Save