Compare commits
5 Commits
1b53ebd8e6
...
fb4a80cfd1
Author | SHA1 | Date |
---|---|---|
tchncs | fb4a80cfd1 | |
SmallJoker | 3cac3d8701 | |
SmallJoker | 73722136a0 | |
SmallJoker | 307b5d1f92 | |
SmallJoker | 6e01bf2bf9 |
|
@ -6,6 +6,25 @@ function boost_cart:get_sign(z)
|
|||
end
|
||||
end
|
||||
|
||||
function boost_cart:manage_attachment(player, status, obj)
|
||||
if not player then
|
||||
return
|
||||
end
|
||||
local player_name = player:get_player_name()
|
||||
if default.player_attached[player_name] == status then
|
||||
return
|
||||
end
|
||||
default.player_attached[player_name] = status
|
||||
|
||||
if status then
|
||||
player:set_attach(obj, "", {x=0, y=6, z=0}, {x=0, y=0, z=0})
|
||||
player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
|
||||
else
|
||||
player:set_detach()
|
||||
player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
|
||||
end
|
||||
end
|
||||
|
||||
function boost_cart:velocity_to_dir(v)
|
||||
if math.abs(v.x) > math.abs(v.z) then
|
||||
return {x=boost_cart:get_sign(v.x), y=boost_cart:get_sign(v.y), z=0}
|
||||
|
|
62
init.lua
62
init.lua
|
@ -5,6 +5,11 @@ boost_cart = {}
|
|||
boost_cart.modpath = minetest.get_modpath("boost_cart")
|
||||
boost_cart.speed_max = 10
|
||||
|
||||
if not boost_cart.modpath then
|
||||
error("\nWrong mod directory name! Please change it to 'boost_cart'.\n" ..
|
||||
"See also: http://dev.minetest.net/Installing_Mods")
|
||||
end
|
||||
|
||||
function vector.floor(v)
|
||||
return {
|
||||
x = math.floor(v.x),
|
||||
|
@ -25,6 +30,10 @@ if not default.player_attached then
|
|||
default.player_attached = {}
|
||||
end
|
||||
|
||||
local function get_v(v)
|
||||
return math.sqrt(v.x ^ 2 + v.z ^ 2)
|
||||
end
|
||||
|
||||
boost_cart.cart = {
|
||||
physical = false,
|
||||
collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
|
||||
|
@ -50,16 +59,17 @@ function boost_cart.cart:on_rightclick(clicker)
|
|||
local player_name = clicker:get_player_name()
|
||||
if self.driver and player_name == self.driver then
|
||||
self.driver = nil
|
||||
clicker:set_detach()
|
||||
boost_cart:manage_attachment(clicker, false)
|
||||
elseif not self.driver then
|
||||
self.driver = player_name
|
||||
default.player_attached[player_name] = true
|
||||
clicker:set_attach(self.object, "", {x=0, y=3, z=0}, {x=0, y=0, z=0})
|
||||
boost_cart:manage_attachment(clicker, true, self.object)
|
||||
end
|
||||
end
|
||||
|
||||
function boost_cart.cart:on_activate(staticdata, dtime_s)
|
||||
self.object:set_armor_groups({immortal=1})
|
||||
self.snd = 0
|
||||
self.handle = nil
|
||||
end
|
||||
|
||||
function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
|
||||
|
@ -85,11 +95,15 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||
if self.old_pos then
|
||||
self.object:setpos(self.old_pos)
|
||||
end
|
||||
default.player_attached[self.driver] = nil
|
||||
|
||||
if self.handle then
|
||||
minetest.sound_stop(self.handle)
|
||||
self.handle = nil
|
||||
self.snd = 0
|
||||
end
|
||||
|
||||
local player = minetest.get_player_by_name(self.driver)
|
||||
if player then
|
||||
player:set_detach()
|
||||
end
|
||||
boost_cart:manage_attachment(player, false)
|
||||
end
|
||||
for _,obj_ in ipairs(self.attached_items) do
|
||||
if obj_ then
|
||||
|
@ -97,8 +111,11 @@ function boost_cart.cart:on_punch(puncher, time_from_last_punch, tool_capabiliti
|
|||
end
|
||||
end
|
||||
|
||||
local leftover = puncher:get_inventory():add_item("main", "carts:cart")
|
||||
if not leftover:is_empty() then
|
||||
minetest.add_item(self.object:getpos(), leftover)
|
||||
end
|
||||
self.object:remove()
|
||||
puncher:get_inventory():add_item("main", "carts:cart")
|
||||
return
|
||||
end
|
||||
|
||||
|
@ -131,6 +148,31 @@ end
|
|||
function boost_cart.cart:on_step(dtime)
|
||||
local vel = self.object:getvelocity()
|
||||
local update = {}
|
||||
local vv = get_v(vel) ; --print ("vel", vv, self.driver)
|
||||
if vv > 1 and self.driver and self.snd == 0 then
|
||||
|
||||
self.handle = minetest.sound_play("cart_ride", {
|
||||
-- to_player = self.player,
|
||||
object = self.object,
|
||||
gain = 1.0,
|
||||
loop = true,
|
||||
})
|
||||
if self.handle then
|
||||
self.snd = 1
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if (vv < 1 or not self.driver) and self.snd == 1 then
|
||||
|
||||
if self.handle then
|
||||
print ("handle", self.handle)
|
||||
minetest.sound_stop(self.handle)
|
||||
self.handle = nil
|
||||
self.snd = 0
|
||||
end
|
||||
|
||||
end
|
||||
if self.punched then
|
||||
vel = vector.add(vel, self.velocity)
|
||||
self.object:setvelocity(vel)
|
||||
|
@ -299,7 +341,7 @@ end
|
|||
|
||||
minetest.register_entity(":carts:cart", boost_cart.cart)
|
||||
minetest.register_craftitem(":carts:cart", {
|
||||
description = "Cart",
|
||||
description = "Cart (Sneak+Click to pick up)",
|
||||
inventory_image = minetest.inventorycube("cart_top.png", "cart_side.png", "cart_side.png"),
|
||||
wield_image = "cart_side.png",
|
||||
on_place = function(itemstack, placer, pointed_thing)
|
||||
|
@ -323,4 +365,4 @@ minetest.register_craft({
|
|||
{"default:steel_ingot", "", "default:steel_ingot"},
|
||||
{"default:steel_ingot", "default:steel_ingot", "default:steel_ingot"},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue