-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand_grenade.lua
149 lines (129 loc) · 4.65 KB
/
hand_grenade.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
minetest.register_craftitem("rangedweapons:pin", {
wield_scale = {x=2.5,y=2.5,z=1.0},
inventory_image = "rangedweapons_pin.png",
})
local rangedweapons_grenade_pin = {
physical = false,
timer = 0,
visual = "wielditem",
visual_size = {x=0.15, y=0.15},
textures = {"rangedweapons:pin"},
lastpos= {},
collisionbox = {0, 0, 0, 0, 0, 0},
}
rangedweapons_grenade_pin.on_step = function(self, dtime, pos)
self.timer = self.timer + dtime
local pos = self.object:get_pos()
local node = minetest.get_node(pos)
if self.lastpos.x ~= nil then
if minetest.registered_nodes[node.name].walkable then
self.object:remove()
minetest.sound_play("rangedweapons_bulletdrop", {pos = self.lastpos, gain = 0.8})
end
end
self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
end
minetest.register_entity("rangedweapons:grenade_pin", rangedweapons_grenade_pin)
local grenade_boom = {
name = "rangedweapons:grenade_explosion",
--description = "DuN mInD mEh...",
radius = 3,
tiles = {
side = "rangedweapons_invisible.png",
top = "rangedweapons_invisible.png",
bottom = "rangedweapons_invisible.png",
burning = "rangedweapons_invisible.png"
},
}
local gtimer = 0
minetest.register_craftitem("rangedweapons:hand_grenade", {
stack_max= 1,
wield_scale = {x=1.1,y=1.1,z=1.05},
description = "" ..core.colorize("#35cdff","Silver Hand Grenade\n") ..core.colorize("#FFFFFF", "Explosion radius: 3\n")..core.colorize("#FFFFFF", "Throw force: 12\n") ..core.colorize("#FFFFFF", "Grenade gravitational pull: 6\n") ..core.colorize("#ffc000", "Right-click to unpin, Left click to throw after unpinning\n") ..core.colorize("#ffc000", "Thrown or not, it will explode after 3 secons from unpinning, be careful!"),
range = 0,
inventory_image = "rangedweapons_hand_grenade.png",
on_secondary_use = function(itemstack, user, pointed_thing)
if minetest.find_node_near(user:get_pos(), 10,"rangedweapons:antigun_block")
then
minetest.chat_send_player(user:get_player_name(), "" ..core.colorize("#ff0000","Grenades are prohibited in this area!"))
return itemstack
end
gtimer = 0
minetest.sound_play("rangedweapons_reload_a", {user})
itemstack = "rangedweapons:hand_grenade_nopin"
local pos = user:get_pos()
pos.y = pos.y + 1.5
local pinEnt = minetest.add_entity(pos, "rangedweapons:grenade_pin")
if pinEnt then
local dir = user:get_look_dir()
local yaw = user:get_look_yaw()
local svertical = user:get_look_vertical()
pinEnt:setvelocity({x=dir.x * -10, y=dir.y * -10, z=dir.z * -10})
pinEnt:setacceleration({x=dir.x * -5, y= -10, z=dir.z * -5})
pinEnt:set_rotation({x=0,y=yaw + math.pi,z=-svertical})
end
return itemstack end,
})
minetest.register_craftitem("rangedweapons:hand_grenade_nopin", {
stack_max= 1,
wield_scale = {x=1.1,y=1.1,z=1.05},
description = "***HURRY UP AND THROW IT!!!***",
range = 0,
inventory_image = "rangedweapons_hand_grenade_nopin.png",
groups = {not_in_creative_inventory = 1},
on_use = function(itemstack, user, pointed_thing)
local pos = user:get_pos()
local dir = user:get_look_dir()
local yaw = user:get_look_yaw()
if pos and dir and yaw then
pos.y = pos.y + 1.6
local obj = minetest.add_entity(pos, "rangedweapons:grenade")
if obj then
obj:set_velocity({x=dir.x * 12, y=dir.y * 12, z=dir.z * 12})
obj:set_acceleration({x=0, y=-6, z=0})
obj:set_yaw(yaw + math.pi)
btimer = gtimer
local ent = obj:get_luaentity()
if ent then
ent.player = ent.player or user
itemstack = ""
end
end
end
return itemstack
end,
})
tnt.register_tnt(grenade_boom)
minetest.register_globalstep(function(dtime, player, pos)
gtimer = gtimer + dtime;
if gtimer >= 3.0 then
for _, player in pairs(minetest.get_connected_players()) do
local pos = player:get_pos()
if player:get_wielded_item():get_name() == "rangedweapons:hand_grenade_nopin" then
player:set_wielded_item("")
gtimer = 0
tnt.boom(pos, grenade_boom)
end end end end)
local rangedweapons_grenade = {
physical = true,
btimer = 0,
timer = 0,
hp_max = 420,
visual = "sprite",
visual_size = {x=0.5, y=0.5},
textures = {"rangedweapons_hand_grenade_nopin.png"},
lastpos= {},
collisionbox = {-0.1, -0.1, -0.1, 0.1, 0.1, 0.1},
}
rangedweapons_grenade.on_step = function(self, dtime, pos)
local pos = self.object:get_pos()
local node = minetest.get_node(pos)
local btimer = btimer or 0
self.timer = self.timer + dtime
if self.timer > (3.0 - btimer) then
tnt.boom(pos, grenade_boom)
self.object:remove()
end
self.lastpos= {x = pos.x, y = pos.y, z = pos.z}
end
minetest.register_entity("rangedweapons:grenade", rangedweapons_grenade)