]> git.lizzy.rs Git - Crafter.git/blob - mods/jetpack/init.lua
Add hover mode to jetpack
[Crafter.git] / mods / jetpack / init.lua
1 minetest.register_tool("jetpack:jetpack",{
2     description = "Jetpack",
3
4     groups = {chestplate = 1,},
5     inventory_image = "jetpack_item.png",
6     stack_max = 1,
7     wearing_texture = "jetpack.png",
8     tool_capabilities = {
9         full_punch_interval = 0,
10         max_drop_level = 0,
11         groupcaps = {
12         },
13         damage_groups = {
14
15         },
16         punch_attack_uses = 0,
17     }
18 })
19
20 local sound_handling_loop = {}
21
22 minetest.register_globalstep(function(dtime)
23     for _,player in ipairs(minetest.get_connected_players()) do
24         local player_name = player:get_player_name()
25         if player:get_hp() > 0 then
26             if player:get_player_control().jump or player:get_player_control().sneak then
27                 local inv = player:get_inventory()
28                 local stack = inv:get_stack("armor_torso",1)
29                 local name = stack:get_name()
30                 if name ~= "" and name == "jetpack:jetpack" then
31                     --boost
32                     if player:get_player_control().jump and player:get_player_velocity().y < 20 then
33                         player:add_player_velocity(vector.new(0,1,0))
34                     --hover
35                     elseif player:get_player_control().sneak then
36                         local currentvel = player:get_player_velocity()
37                         local goal = 8.1
38                                     local acceleration = vector.new(0,goal-currentvel.y,0)
39                                     acceleration = vector.multiply(acceleration, 0.05)
40                                     player:add_player_velocity(acceleration)
41                     end
42                     
43                     local particle_pos = player:get_pos()
44                     local yaw = player:get_look_horizontal()
45                     local p_dir = vector.divide(minetest.yaw_to_dir(yaw + math.pi),8)
46                     particle_pos.y = particle_pos.y + 0.7
47                     particle_pos = vector.add(particle_pos,p_dir)
48
49
50                     minetest.add_particle({
51                                                 pos = particle_pos,
52                                                 velocity = {x=0, y=-20+player:get_player_velocity().y , z=0},
53                                                 acceleration = {x=math.random(-1,1), y=0, z=math.random(-1,1)},
54                                                 expirationtime = 1+math.random(),
55                                                 size = 1+math.random(),
56                                                 texture = "smoke.png",
57                                         })
58                     stack:add_wear(10)
59                     inv:set_stack("armor_torso", 1, stack)
60
61                     if not sound_handling_loop[player_name] then
62                         sound_handling_loop[player_name] = minetest.sound_play("jetpack", {object = player,loop=true})
63                     end
64
65                     if inv:get_stack("armor_torso",1):get_name() == "" then
66                         recalculate_armor(player)
67                         set_armor_gui(player)
68                         if sound_handling_loop[player_name] then
69                             --minetest.sound_play("armor_break",{to_player=player:get_player_name(),gain=1,pitch=math.random(80,100)/100})
70                             --minetest.sound_stop(sound_handling_loop[player_name])
71                             minetest.sound_fade(sound_handling_loop[player_name], -1, 0)
72                             sound_handling_loop[player_name] = nil
73                         end
74                     end
75                 end
76             elseif sound_handling_loop[player_name] then
77                 minetest.sound_stop(sound_handling_loop[player_name])
78                 sound_handling_loop[player_name] = nil
79             end
80         end
81     end
82 end)
83
84 minetest.register_craft({
85     output = "jetpack:jetpack",
86     recipe = {
87         {"main:iron"           , "main:gold"    , "main:iron"          },
88         {"main:iron"           , "main:diamond" , "main:iron"          },
89         {"redstone:piston_off" , "redstone:dust", "redstone:piston_off"}
90     }
91 })