mirror of
https://github.com/SmallJoker/boost_cart.git
synced 2024-11-27 17:03:44 +01:00
Punch update - wat?
This commit is contained in:
parent
f88f0be7ec
commit
eb610a5e9c
38
init.lua
38
init.lua
@ -25,7 +25,7 @@ boost_cart.cart = {
|
|||||||
textures = {"cart.png"},
|
textures = {"cart.png"},
|
||||||
|
|
||||||
driver = nil,
|
driver = nil,
|
||||||
update = false, -- used to re-send velocity and position
|
punched = false, -- used to re-send velocity and position
|
||||||
velocity = {x=0, y=0, z=0}, -- only used on punch
|
velocity = {x=0, y=0, z=0}, -- only used on punch
|
||||||
old_dir = {x=0, y=0, z=0},
|
old_dir = {x=0, y=0, z=0},
|
||||||
old_pos = nil,
|
old_pos = nil,
|
||||||
@ -98,13 +98,12 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||||||
|
|
||||||
self.velocity = vector.multiply(cart_dir, f)
|
self.velocity = vector.multiply(cart_dir, f)
|
||||||
self.old_pos = nil
|
self.old_pos = nil
|
||||||
self.update = true
|
self.punched = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function boost_cart.cart:on_step(dtime)
|
function boost_cart.cart:on_step(dtime)
|
||||||
local vel = self.object:getvelocity()
|
local vel = self.object:getvelocity()
|
||||||
local send_pos = (not self.update)
|
if self.punched then
|
||||||
if self.update then
|
|
||||||
vel = vector.add(vel, self.velocity)
|
vel = vector.add(vel, self.velocity)
|
||||||
self.object:setvelocity(vel)
|
self.object:setvelocity(vel)
|
||||||
elseif vector.equals(vel, {x=0, y=0, z=0}) then
|
elseif vector.equals(vel, {x=0, y=0, z=0}) then
|
||||||
@ -113,7 +112,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
|
|
||||||
local dir, last_switch = nil, nil
|
local dir, last_switch = nil, nil
|
||||||
local pos = self.object:getpos()
|
local pos = self.object:getpos()
|
||||||
if self.old_pos and not self.update then
|
if self.old_pos and not self.punched then
|
||||||
local flo_pos = vector.floor(pos)
|
local flo_pos = vector.floor(pos)
|
||||||
local flo_old = vector.floor(self.old_pos)
|
local flo_old = vector.floor(self.old_pos)
|
||||||
if vector.equals(flo_pos, flo_old) then
|
if vector.equals(flo_pos, flo_old) then
|
||||||
@ -121,10 +120,10 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local update = false
|
local update = {}
|
||||||
local ctrl = nil
|
local ctrl, player = nil, nil
|
||||||
if self.driver then
|
if self.driver then
|
||||||
local player = minetest.get_player_by_name(self.driver)
|
player = minetest.get_player_by_name(self.driver)
|
||||||
if player then
|
if player then
|
||||||
ctrl = player:get_player_control()
|
ctrl = player:get_player_control()
|
||||||
end
|
end
|
||||||
@ -138,7 +137,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
dir = false
|
dir = false
|
||||||
pos = vector.new(expected_pos)
|
pos = vector.new(expected_pos)
|
||||||
update = true
|
update.pos = true
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
@ -149,7 +148,7 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
for _,v in ipairs({"x", "z"}) do
|
for _,v in ipairs({"x", "z"}) do
|
||||||
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
|
if vel[v] ~= 0 and math.abs(vel[v]) < 0.9 then
|
||||||
vel[v] = 0
|
vel[v] = 0
|
||||||
update = true
|
update.vel = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -163,26 +162,26 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
local new_acc = {x=0, y=0, z=0}
|
local new_acc = {x=0, y=0, z=0}
|
||||||
if vector.equals(dir, {x=0, y=0, z=0}) then
|
if vector.equals(dir, {x=0, y=0, z=0}) then
|
||||||
vel = {x=0, y=0, z=0}
|
vel = {x=0, y=0, z=0}
|
||||||
update = true
|
update.vel = true
|
||||||
else
|
else
|
||||||
-- If the direction changed
|
-- If the direction changed
|
||||||
if dir.x ~= 0 and self.old_dir.z ~= 0 then
|
if dir.x ~= 0 and self.old_dir.z ~= 0 then
|
||||||
vel.x = dir.x * math.abs(vel.z)
|
vel.x = dir.x * math.abs(vel.z)
|
||||||
vel.z = 0
|
vel.z = 0
|
||||||
pos.z = math.floor(pos.z + 0.5)
|
pos.z = math.floor(pos.z + 0.5)
|
||||||
update = true
|
update.pos = true
|
||||||
end
|
end
|
||||||
if dir.z ~= 0 and self.old_dir.x ~= 0 then
|
if dir.z ~= 0 and self.old_dir.x ~= 0 then
|
||||||
vel.z = dir.z * math.abs(vel.x)
|
vel.z = dir.z * math.abs(vel.x)
|
||||||
vel.x = 0
|
vel.x = 0
|
||||||
pos.x = math.floor(pos.x + 0.5)
|
pos.x = math.floor(pos.x + 0.5)
|
||||||
update = true
|
update.pos = true
|
||||||
end
|
end
|
||||||
-- Up, down?
|
-- Up, down?
|
||||||
if dir.y ~= self.old_dir.y then
|
if dir.y ~= self.old_dir.y then
|
||||||
vel.y = dir.y * math.abs(vel.x + vel.z)
|
vel.y = dir.y * math.abs(vel.x + vel.z)
|
||||||
pos = vector.round(pos)
|
pos = vector.round(pos)
|
||||||
update = true
|
update.pos = true
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Slow down or speed up..
|
-- Slow down or speed up..
|
||||||
@ -219,11 +218,11 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
for _,v in ipairs({"x","y","z"}) do
|
for _,v in ipairs({"x","y","z"}) do
|
||||||
if math.abs(vel[v]) > max_vel then
|
if math.abs(vel[v]) > max_vel then
|
||||||
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
|
vel[v] = boost_cart:get_sign(vel[v]) * max_vel
|
||||||
update = true
|
update.vel = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not send_pos then
|
if self.punched then
|
||||||
-- Collect dropped items
|
-- Collect dropped items
|
||||||
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
for _,obj_ in ipairs(minetest.get_objects_inside_radius(pos, 1)) do
|
||||||
if not obj_:is_player() and
|
if not obj_:is_player() and
|
||||||
@ -234,10 +233,10 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
self.attached_items[#self.attached_items + 1] = obj_
|
self.attached_items[#self.attached_items + 1] = obj_
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
self.punched = false
|
||||||
end
|
end
|
||||||
|
|
||||||
self.update = false
|
if not (update.vel or update.pos) then
|
||||||
if not update then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -260,9 +259,10 @@ function boost_cart.cart:on_step(dtime)
|
|||||||
self.object:set_animation(anim, 1, 0)
|
self.object:set_animation(anim, 1, 0)
|
||||||
|
|
||||||
self.object:setvelocity(vel)
|
self.object:setvelocity(vel)
|
||||||
if send_pos then
|
if update.pos then
|
||||||
self.object:setpos(pos)
|
self.object:setpos(pos)
|
||||||
end
|
end
|
||||||
|
update = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_entity(":carts:cart", boost_cart.cart)
|
minetest.register_entity(":carts:cart", boost_cart.cart)
|
||||||
|
Loading…
Reference in New Issue
Block a user