minetest-mod-snow/src/sled.lua

221 lines
5.2 KiB
Lua
Raw Normal View History

2014-06-04 00:34:58 +02:00
--[[
--=================
--======================================
LazyJ's Fork of Splizard's "Snow" Mod
by LazyJ
version: Umpteen and 7/5ths something or another.
2014_04_12
--======================================
--=================
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
THE LIST OF CHANGES I'VE MADE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* The HUD message that displayed when a player sat on the sled would not go away after the player
got off the sled. I spent hours on trial-and-error while reading the lua_api.txt and scrounging
the Internet for a needle-in-the-haystack solution as to why the hud_remove wasn't working.
2014-06-04 00:34:58 +02:00
Turns out Splizard's code was mostly correct, just not assembled in the right order.
The key to the solution was found in the code of leetelate's scuba mod:
http://forum.minetest.net/viewtopic.php?id=7175
* Changed the wording of the HUD message for clarity.
~~~~~~
TODO
~~~~~~
* Figure out why the player avatars remain in a seated position, even after getting off the sled,
if they flew while on the sled. 'default.player_set_animation', where is a better explanation
2014-06-04 00:34:58 +02:00
for this and what are it's available options?
* Go through, clean-up my notes and get them better sorted. Some are in the code, some are
scattered in my note-taking program. This "Oh, I'll just make a little tweak here and a
little tweak there" project has evolved into something much bigger and more complex
2014-06-04 00:34:58 +02:00
than I originally planned. :p ~ LazyJ
--]]
--=============================================================
-- CODE STUFF
--=============================================================
2013-12-09 07:19:31 +01:00
--
-- Helper functions
--
local function is_water(pos)
return minetest.get_item_group(minetest.get_node(pos).name, "water") ~= 0
2013-12-09 07:19:31 +01:00
end
--
-- Sled entity
--
local sled = {
physical = false,
collisionbox = {-0.6,-0.25,-0.6, 0.6,0.3,0.6},
visual = "mesh",
mesh = "sled.x",
textures = {"sled.png"},
2015-02-14 22:42:22 +01:00
nil,
2013-12-09 07:19:31 +01:00
driver = nil,
sliding = false,
}
local players_sled = {}
function sled:on_rightclick(clicker)
if not self.driver
and snow.sleds then
2013-12-09 07:19:31 +01:00
players_sled[clicker:get_player_name()] = true
self.driver = clicker
self.object:set_attach(clicker, "", {x=0,y=-9,z=0}, {x=0,y=90,z=0})
clicker:set_physics_override({
speed = 2, -- multiplier to default value
jump = 0, -- multiplier to default value
gravity = 1
2014-06-04 00:34:58 +02:00
})
--[[
local HUD =
2013-12-09 07:19:31 +01:00
{
hud_elem_type = "text", -- see HUD element types
position = {x=0.5, y=0.89},
name = "sled",
scale = {x=2, y=2},
text = "You are sledding, hold sneak to stop.",
direction = 0,
}
2013-12-09 07:19:31 +01:00
clicker:hud_add(HUD)
2014-06-04 00:34:58 +02:00
--]]
-- Here is part 1 of the fix. ~ LazyJ
2014-06-05 07:42:44 +02:00
self.HUD = clicker:hud_add({
2014-06-04 00:34:58 +02:00
hud_elem_type = "text",
position = {x=0.5, y=0.89},
name = "sled",
scale = {x=2, y=2},
text = "You are on the sled! Press the sneak key to get off the sled.", -- LazyJ
direction = 0,
})
-- End part 1
end
2013-12-09 07:19:31 +01:00
end
function sled:on_activate(staticdata, dtime_s)
self.object:set_armor_groups({immortal=1})
if staticdata then
self.v = tonumber(staticdata)
end
end
function sled:get_staticdata()
return tostring(v)
end
function sled:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
self.object:remove()
if puncher
and puncher:is_player() then
2013-12-09 07:19:31 +01:00
puncher:get_inventory():add_item("main", "snow:sled")
end
end
local timer = 0
2013-12-09 07:19:31 +01:00
minetest.register_globalstep(function(dtime)
timer = timer+dtime
if timer < 1 then
return
end
timer = 0
2013-12-09 07:19:31 +01:00
for _, player in pairs(minetest.get_connected_players()) do
if players_sled[player:get_player_name()] then
default.player_set_animation(player, "sit", 0)
end
end
end)
local timer = 0
2013-12-09 07:19:31 +01:00
function sled:on_step(dtime)
if not self.driver then
return
end
timer = timer+dtime
if timer < 1 then
return
end
timer = 0
local p = self.object:getpos()
p.y = p.y+0.4
local s = self.object:getpos()
s.y = s.y -0.5
2015-05-26 20:55:13 +02:00
if self.driver:get_player_control().sneak
or is_water(p)
or not minetest.find_node_near(s, 1, {"default:snow","default:snowblock","default:ice","default:dirt_with_snow", "group:icemaker"}) then -- LazyJ
self.driver:set_physics_override({
speed = 1, -- multiplier to default value
jump = 1, -- multiplier to default value
gravity = 1
})
players_sled[self.driver:get_player_name()] = false
self.object:set_detach()
--self.driver:hud_remove("sled")
self.driver:hud_remove(self.HUD) -- And here is part 2. ~ LazyJ
self.driver = nil
self.object:remove()
2014-06-04 00:34:58 +02:00
2013-12-09 07:19:31 +01:00
end
end
minetest.register_entity("snow:sled", sled)
minetest.register_craftitem("snow:sled", {
description = "Sled",
inventory_image = "snow_sled.png",
wield_image = "snow_sled.png",
wield_scale = {x=2, y=2, z=1},
liquids_pointable = true,
stack_max = 1,
on_use = function(itemstack, placer)
if players_sled[placer:get_player_name()] then
return
end
if minetest.get_node(placer:getpos()).name == "default:snow" then
local sled = minetest.add_entity({x=0,y=-1000, z=0}, "snow:sled")
sled:get_luaentity():on_rightclick(placer)
2013-12-09 07:19:31 +01:00
end
end,
})
minetest.register_craft({
output = "snow:sled",
recipe = {
{"", "", ""},
{"group:stick", "", ""},
{"group:wood", "group:wood", "group:wood"},
},
})
minetest.register_craft({
output = "snow:sled",
recipe = {
{"", "", ""},
{"", "", "group:stick"},
{"group:wood", "group:wood", "group:wood"},
},
})