Merge pull request #3 from Athemis/master
[MineClone.git] / mods / boat / init.lua
blobcfcdc4c8b579eb5d580556077b7eeb0eb0a64c09
1 --
2 -- Helper functions
3 --
4 local init = os.clock()
6 local function is_water(pos)
7 local nn = minetest.get_node(pos).name
8 return minetest.get_item_group(nn, "water") ~= 0
9 end
11 local function get_velocity(v, yaw, y)
12 local x = -math.sin(yaw)*v
13 local z = math.cos(yaw)*v
14 return {x=x, y=y, z=z}
15 end
18 -- boat entity
20 local boat = {
21 physical = true,
22 collisionbox = {-1,-0.5,-1, 1,0.5,1},
23 visual = "mesh",
24 mesh = "boat_base.x",
25 textures = {"boat_texture.png"},
26 driver = nil,
27 v = 0,
28 stepcount = 0,
29 unattended = 0
32 function boat.on_rightclick(self, clicker)
33 if not clicker or not clicker:is_player() then
34 return
35 end
36 if self.driver and clicker == self.driver then
37 self.driver = nil
38 clicker:set_detach()
39 elseif not self.driver then
40 self.driver = clicker
41 clicker:set_attach(self.object, "", {x=0,y=5,z=0}, {x=0,y=0,z=0})
42 self.object:setyaw(clicker:get_look_yaw())
43 end
44 end
46 function boat.on_activate(self, staticdata, dtime_s)
47 self.object:set_armor_groups({immortal=1})
48 if staticdata then
49 self.v = tonumber(staticdata)
50 end
51 end
53 function boat.get_staticdata()
54 return tostring(v)
55 end
57 function boat.on_punch(self, puncher, time_from_last_punch, tool_capabilities, direction)
59 if self.driver then
60 self.driver:set_detach()
61 self.driver = nil
62 boat.schedule_removal(self)
63 if not minetest.setting_getbool("creative_mode") then
64 puncher:get_inventory():add_item("main", "boat:boat")
65 end
66 else
68 boat.schedule_removal(self)
69 if not minetest.setting_getbool("creative_mode") then
70 puncher:get_inventory():add_item("main", "boat:boat")
71 end
73 end
74 end
76 function boat.on_step(self, dtime)
78 self.stepcount=self.stepcount+1
79 if self.stepcount>9 then
81 self.stepcount=0
83 if self.driver then
84 local ctrl = self.driver:get_player_control()
86 self.unattended=0
88 local yaw = self.object:getyaw()
90 if ctrl.up and self.v<3 then
91 self.v = self.v + 1
92 end
94 if ctrl.down and self.v>=-1 then
95 self.v = self.v - 1
96 end
98 if ctrl.left then
99 if ctrl.down then
100 self.object:setyaw(yaw-math.pi/12-dtime*math.pi/12)
101 else
102 self.object:setyaw(yaw+math.pi/12+dtime*math.pi/12)
105 if ctrl.right then
106 if ctrl.down then
107 self.object:setyaw(yaw+math.pi/12+dtime*math.pi/12)
108 else
109 self.object:setyaw(yaw-math.pi/12-dtime*math.pi/12)
114 local tmp_velocity = get_velocity(self.v, self.object:getyaw(), 0)
116 local tmp_pos = self.object:getpos()
118 tmp_velocity.y=0
120 if is_water(tmp_pos) then
121 tmp_velocity.y=2
124 tmp_pos.y=tmp_pos.y-0.5
126 if minetest.get_node(tmp_pos).name=="air" then
127 tmp_velocity.y=-2
130 self.object:setvelocity(tmp_velocity)
136 function boat.schedule_removal(self)
138 minetest.after(0.25,function()
139 self.object:remove()
140 end)
145 minetest.register_entity("boat:boat", boat)
147 minetest.register_craftitem("boat:boat", {
148 description = "Boat",
149 inventory_image = "boat_inventory.png",
150 liquids_pointable = true,
152 on_place = function(itemstack, placer, pointed_thing)
153 if pointed_thing.type ~= "node" then
154 return
156 if not is_water(pointed_thing.under) then
157 return
159 pointed_thing.under.y = pointed_thing.under.y+0.5
160 minetest.add_entity(pointed_thing.under, "boat:boat")
161 if not minetest.setting_getbool("creative_mode") then
162 itemstack:take_item()
164 return itemstack
165 end,
168 minetest.register_craft({
169 output = "boat:boat",
170 recipe = {
171 {"", "", ""},
172 {"", "", ""},
173 {"default:wood", "", ""},
177 local time_to_load= os.clock() - init
178 print(string.format("[MOD] "..minetest.get_current_modname().." loaded in %.4f s", time_to_load))