]> git.lizzy.rs Git - Crafter.git/blob - mods/player/init.lua
3de1c4da62fd2c18145292fec5858d934c26daf7
[Crafter.git] / mods / player / init.lua
1 --[[
2 --map
3 running - set fov set_fov(fov, is_multiplier) set_breath(value)
4 sneaking --set eye offset
5
6 ]]--
7 crafter_version = 0.03
8
9 minetest.register_on_joinplayer(function(player)
10       --add in info
11       player:hud_set_flags({minimap=true})
12       player:hud_add({
13             hud_elem_type = "text",
14             position = {x=0,y=0},
15             text = "Crafter Alpha "..crafter_version,
16             number = 000000,
17             alignment = {x=1,y=1},
18             offset = {x=2, y=2},
19       })
20       player:hud_add({
21             hud_elem_type = "text",
22             position = {x=0,y=0},
23             text = "Crafter Alpha "..crafter_version,
24             number = 0xffffff,
25             alignment = {x=1,y=1},
26             offset = {x=0, y=0},
27       })
28 end)
29
30 --hurt sound
31 minetest.register_on_player_hpchange(function(player, hp_change, reason)
32       if hp_change < 0 then
33             minetest.sound_play("hurt", {object=player, gain = 1.0, max_hear_distance = 60,pitch = math.random(80,100)/100})
34       end
35 end)
36
37 --throw all items on death
38 minetest.register_on_dieplayer(function(player, reason)
39       local pos = player:getpos()
40       print(reason)
41       local inv = player:get_inventory()
42       
43       for i = 1,inv:get_size("main") do
44             local stack = inv:get_stack("main", i)
45             local name = stack:get_name()
46             local count = stack:get_count()
47             print(name)
48             if name ~= "" then
49                   local obj = minetest.add_item(pos, name.." "..count)
50                   obj:setvelocity(vector.new(math.random(-3,3),math.random(4,8),math.random(-3,3)))
51                   inv:set_stack("main", i, ItemStack(""))
52             end
53       end
54       for i = 1,inv:get_size("craft") do
55       
56       end 
57       
58
59 end)
60
61 minetest.register_globalstep(function(dtime)
62       --collection
63       for _,player in ipairs(minetest.get_connected_players()) do
64             local run = player:get_player_control().aux1
65             local walk = player:get_player_control().up
66             local sneak = player:get_player_control().sneak
67             
68             if run and walk and not sneak then
69                   --[[ I'll impliment this in later
70                   local meta = player:get_meta()
71                   
72                   local run_time = meta:get_float("running_timer")
73                   
74                   if not run_time then
75                         run_time = 0
76                   end
77                   
78                   if run_time >= 0.1 then
79                         --take breath away
80                         local breath = player:get_breath()
81                         breath = breath - 1
82                         player:set_breath(breath)
83                         run_time = 0
84                         print(breath)
85                   end
86                   
87                   meta:set_float("running_timer", run_time + dtime)
88                   
89                   ]]--
90                   
91                   local fov = player:get_fov()
92                   if fov == 0 then
93                         fov = 1
94                   end
95                   
96                   if fov < 1.2 then
97                         player:set_fov(fov + 0.05, true)
98                   end
99                   
100                   player:set_physics_override({speed=1.5})
101             else
102                   local meta = player:get_meta()
103                   local fov = player:get_fov()
104                   if fov > 1 then
105                         player:set_fov(fov - 0.05, true)
106                   end
107                   
108                   player:set_physics_override({speed=1})
109                   --meta:set_float("running_timer", 0)
110             end
111             
112             if sneak then
113                   player:set_eye_offset({x=0,y=-1,z=0},{x=0,y=-1,z=0})
114             else
115                   player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
116             end
117       end
118 end)
119
120 minetest.register_globalstep(function(dtime)
121       --collection
122       for _,player in ipairs(minetest.get_connected_players()) do
123             if player:get_player_control().RMB then
124                   local health = player:get_wielded_item():get_definition().health
125                   if health then
126                         local meta = player:get_meta()
127                         local eating = meta:get_float("eating")
128                         
129                         if meta:get_int("eating_ps") == 0 then
130                               local ps = minetest.add_particlespawner({
131                                     amount = 100,
132                                     time = 0,
133                                     minpos = {x=0, y=-1.5, z=0.5},
134                                     maxpos = {x=0, y=1.7, z=0.5},
135                                     minvel = vector.new(-0.5,0,-0.5),
136                                     maxvel = vector.new(0.5,0,0.5),
137                                     minacc = {x=0, y=-9.81, z=1},
138                                     maxacc = {x=0, y=-9.81, z=1},
139                                     minexptime = 0.5,
140                                     maxexptime = 1.5,
141                                     minsize = 1,
142                                     maxsize = 2,
143                                     attached = player,
144                                     collisiondetection = true,
145                                     vertical = false,
146                                     texture = "treecapitator.png"
147                               })
148                               meta:set_int("eating_ps", ps)
149                         end
150                               
151                         if eating + dtime >= 2 then
152                               local stack = player:get_wielded_item()
153                               stack:take_item(1)
154                               player:set_wielded_item(stack)
155                               player:set_hp(player:get_hp() + health)
156                               eating = 0
157                               minetest.sound_play("eat", {
158                                     object = player,
159                                     gain = 1.0,  -- default
160                                     max_hear_distance = 32,  -- default, uses an euclidean metric
161                                     pitch = math.random(70,100)/100,
162                               })
163                         end
164                         meta:set_float("eating", eating + dtime)
165                   else
166                         local meta = player:get_meta()
167                         meta:set_float("eating", 0)
168                         minetest.delete_particlespawner(meta:get_int("eating_ps"))
169                         meta:set_int("eating_ps", 0)
170                         
171                   end
172             else
173                   local meta = player:get_meta()
174                   meta:set_float("eating", 0)
175                   minetest.delete_particlespawner(meta:get_int("eating_ps"))
176                   meta:set_int("eating_ps", 0)
177             end
178             
179       end
180 end)
181
182
183 local inv =     "size[9,8.75]"..
184     --"image[1,0.6;1,2;player.png]"..
185     "list[current_player;main;0,4.5;9,1;]".. --hot bar
186         "list[current_player;main;0,6;9,3;9]".. --big part
187     "list[current_player;craft;2.5,1;2,2;]"..
188     "list[current_player;craftpreview;6.1,1.5;1,1;]"..
189     "listring[current_player;main]"..
190         "listring[current_player;craft]"
191  
192
193
194 minetest.register_on_joinplayer(function(player)
195         player:set_inventory_formspec(inv)
196         local inv = player:get_inventory()
197         inv:set_width("craft", 2)
198         inv:set_width("main", 9)
199         inv:set_size("main", 9*4)
200         inv:set_size("craft", 4)
201         player:hud_set_hotbar_itemcount(9)
202 end)
203
204 --this dumps the players crafting table on closing the inventory
205 local dump_craft = function(player)
206         local inv = player:get_inventory()
207         local pos = player:getpos()
208         pos.y = pos.y + player:get_properties().eye_height
209         for i = 1,inv:get_size("craft") do
210                 local item = inv:get_stack("craft", i)
211                 local obj = minetest.add_item(pos, item)
212                 if obj then
213                         local x=math.random(-2,2)*math.random()
214             local y=math.random(2,5)
215             local z=math.random(-2,2)*math.random()
216             obj:setvelocity({x=x, y=y, z=z})
217                 end
218                 inv:set_stack("craft", i, nil)
219         end
220 end
221
222
223 minetest.register_on_player_receive_fields(function(player, formname, fields)
224         local inv = player:get_inventory()
225         dump_craft(player)
226         inv:set_width("craft", 2)
227         inv:set_size("craft", 4)
228 end)
229
230 minetest.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
231         print("inv test")
232
233 end)