remove some sled mess

This commit is contained in:
HybridDog 2015-06-03 18:37:02 +02:00
parent f2b23a0a27
commit bf0fa8df63

View File

@ -52,6 +52,15 @@ than I originally planned. :p ~ LazyJ
-- Helper functions -- Helper functions
-- --
local function table_find(t, v)
for i = 1,#t do
if t[i] == v then
return true
end
end
return false
end
local function is_water(pos) local function is_water(pos)
return minetest.get_item_group(minetest.get_node(pos).name, "water") ~= 0 return minetest.get_item_group(minetest.get_node(pos).name, "water") ~= 0
end end
@ -67,21 +76,24 @@ local sled = {
visual = "mesh", visual = "mesh",
mesh = "sled.x", mesh = "sled.x",
textures = {"sled.png"}, textures = {"sled.png"},
nil,
driver = nil,
sliding = false, sliding = false,
} }
local players_sled = {} local players_sled = {}
function sled:on_rightclick(clicker) function sled:on_rightclick(player)
if not self.driver if self.driver
and snow.sleds then or not snow.sleds then
players_sled[clicker:get_player_name()] = true return
self.driver = clicker end
self.object:set_attach(clicker, "", {x=0,y=-9,z=0}, {x=0,y=90,z=0}) local pos = self.object:getpos()
clicker:set_physics_override({ player:setpos(pos)
local pname = player:get_player_name()
players_sled[pname] = true
self.driver = pname
self.object:set_attach(player, "", {x=0,y=-9,z=0}, {x=0,y=90,z=0})
player:set_physics_override({
speed = 2, -- multiplier to default value speed = 2, -- multiplier to default value
jump = 0, -- multiplier to default value jump = 0, -- multiplier to default value
gravity = 1 gravity = 1
@ -101,7 +113,7 @@ function sled:on_rightclick(clicker)
--]] --]]
-- Here is part 1 of the fix. ~ LazyJ -- Here is part 1 of the fix. ~ LazyJ
self.HUD = clicker:hud_add({ self.HUD = player:hud_add({
hud_elem_type = "text", hud_elem_type = "text",
position = {x=0.5, y=0.89}, position = {x=0.5, y=0.89},
name = "sled", name = "sled",
@ -110,7 +122,6 @@ function sled:on_rightclick(clicker)
direction = 0, direction = 0,
}) })
-- End part 1 -- End part 1
end
end end
function sled:on_activate(staticdata, dtime_s) function sled:on_activate(staticdata, dtime_s)
@ -124,7 +135,7 @@ function sled:get_staticdata()
return tostring(v) return tostring(v)
end end
function sled:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) function sled:on_punch(puncher)
self.object:remove() self.object:remove()
if puncher if puncher
and puncher:is_player() then and puncher:is_player() then
@ -147,6 +158,17 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
local driveable_nodes = {"default:snow","default:snowblock","default:ice","default:dirt_with_snow", "group:icemaker"}
local function accelerating_possible(pos)
if is_water(pos) then
return false
end
if table_find(driveable_nodes, minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}).name) then
return true
end
return false
end
local timer = 0 local timer = 0
function sled:on_step(dtime) function sled:on_step(dtime)
if not self.driver then if not self.driver then
@ -157,26 +179,23 @@ function sled:on_step(dtime)
return return
end end
timer = 0 timer = 0
local p = self.object:getpos() local player = minetest.get_player_by_name(self.driver)
p.y = p.y+0.4 if not player then
local s = self.object:getpos() return
s.y = s.y -0.5 end
if self.driver:get_player_control().sneak if player:get_player_control().sneak
or is_water(p) or not accelerating_possible(vector.round(self.object:getpos())) then -- LazyJ
or not minetest.find_node_near(s, 1, {"default:snow","default:snowblock","default:ice","default:dirt_with_snow", "group:icemaker"}) then -- LazyJ player:set_physics_override({
self.driver:set_physics_override({
speed = 1, -- multiplier to default value speed = 1, -- multiplier to default value
jump = 1, -- multiplier to default value jump = 1, -- multiplier to default value
gravity = 1 gravity = 1
}) })
players_sled[self.driver:get_player_name()] = false players_sled[player:get_player_name()] = false
self.object:set_detach() self.object:set_detach()
--self.driver:hud_remove("sled") --self.driver:hud_remove("sled")
self.driver:hud_remove(self.HUD) -- And here is part 2. ~ LazyJ player:hud_remove(self.HUD) -- And here is part 2. ~ LazyJ
self.driver = nil
self.object:remove() self.object:remove()
end end
end end
@ -195,9 +214,9 @@ minetest.register_craftitem("snow:sled", {
if players_sled[placer:get_player_name()] then if players_sled[placer:get_player_name()] then
return return
end end
if minetest.get_node(placer:getpos()).name == "default:snow" then local pos = placer:getpos()
local sled = minetest.add_entity({x=0,y=-1000, z=0}, "snow:sled") if accelerating_possible(vector.round(pos)) then
sled:get_luaentity():on_rightclick(placer) minetest.add_entity(pos, "snow:sled")
end end
end, end,
}) })