change the bullet spawns
This commit is contained in:
parent
0d7ac9e4a3
commit
3373a6d7d1
94
api.lua
94
api.lua
@ -81,7 +81,11 @@ guns.fire_bullet = function(self, itemstack, player)
|
|||||||
velocity = velocity + bullet_velocity
|
velocity = velocity + bullet_velocity
|
||||||
accuracy = math.max(0, (100 - accuracy) / 10)
|
accuracy = math.max(0, (100 - accuracy) / 10)
|
||||||
|
|
||||||
local obj = minetest.add_entity({x=pos.x, y=pos.y + 1.7, z=pos.z}, "guns:bullet")
|
local obj = minetest.add_entity({
|
||||||
|
x=pos.x + dir.x * 2.5,
|
||||||
|
y=pos.y + dir.y * 2.5 + 1.5,
|
||||||
|
z=pos.z + dir.z * 2.5
|
||||||
|
}, "guns:bullet")
|
||||||
local ent = obj:get_luaentity()
|
local ent = obj:get_luaentity()
|
||||||
ent.itemstack = itemstack
|
ent.itemstack = itemstack
|
||||||
ent.owner = player
|
ent.owner = player
|
||||||
@ -146,22 +150,14 @@ guns.register_gun = function(self, name, def)
|
|||||||
def.range = 0
|
def.range = 0
|
||||||
def.inventory_image = def.inventory_image or "blank_gun.png"
|
def.inventory_image = def.inventory_image or "blank_gun.png"
|
||||||
|
|
||||||
if def.groups and def.groups.gun_automatic == 1 then
|
def.on_use = function(itemstack, player, pointed_thing)
|
||||||
def.on_use = function(itemstack, player, pointed_thing)
|
if minetest.is_player(player) then
|
||||||
if minetest.is_player(player) then
|
local pname = player:get_player_name()
|
||||||
local pname = player:get_player_name()
|
if not self.cooldown[pname] then
|
||||||
if not self.cooldown[pname] then
|
if def.groups and def.groups.gun_automatic == 1 then
|
||||||
self.cooldown[pname] = true
|
self.cooldown[pname] = true
|
||||||
self:fire_auto(player)
|
self:fire_auto(player)
|
||||||
end
|
elseif self:has_bullets(itemstack) then
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
else
|
|
||||||
def.on_use = function(itemstack, player, pointed_thing)
|
|
||||||
if minetest.is_player(player) then
|
|
||||||
local pname = player:get_player_name()
|
|
||||||
if not self.cooldown[pname] and self:has_bullets(itemstack) then
|
|
||||||
local meta = itemstack:get_meta()
|
local meta = itemstack:get_meta()
|
||||||
if def.groups and def.groups.gun_single == 1 then
|
if def.groups and def.groups.gun_single == 1 then
|
||||||
if meta:get_int("loaded") == 1 then
|
if meta:get_int("loaded") == 1 then
|
||||||
@ -184,47 +180,49 @@ guns.register_gun = function(self, name, def)
|
|||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def.on_secondary_use = function(itemstack, player, pointed_thing)
|
def.on_secondary_use = function(itemstack, player, pointed_thing)
|
||||||
local meta = itemstack:get_meta()
|
if minetest.is_player(player) then
|
||||||
local bullets_loaded = meta:get_int("bullets_loaded")
|
local meta = itemstack:get_meta()
|
||||||
local bullets_name = meta:get_string("bullets_name")
|
local bullets_loaded = meta:get_int("bullets_loaded")
|
||||||
|
local bullets_name = meta:get_string("bullets_name")
|
||||||
|
|
||||||
if def.groups and def.groups.gun_single == 1 and meta:get_int("loaded") == 0 and self:has_bullets(itemstack) then
|
if def.groups and def.groups.gun_single == 1 and meta:get_int("loaded") == 0 and self:has_bullets(itemstack) then
|
||||||
self:run_on_reload(itemstack, player, "reload")
|
self:run_on_reload(itemstack, player, "reload")
|
||||||
meta:set_int("loaded", 1)
|
meta:set_int("loaded", 1)
|
||||||
return itemstack
|
return itemstack
|
||||||
end
|
end
|
||||||
|
|
||||||
if def._gun_magazine and def._gun_ammunition then
|
if def._gun_magazine and def._gun_ammunition then
|
||||||
self:run_on_reload(itemstack, player, "magazine")
|
local inv = player:get_inventory()
|
||||||
local inv = player:get_inventory()
|
for _, stack in ipairs(def._gun_ammunition) do
|
||||||
for _, stack in ipairs(def._gun_ammunition) do
|
local name = stack:get_name()
|
||||||
local name = stack:get_name()
|
local s = ItemStack(name)
|
||||||
local s = ItemStack(name)
|
if inv:contains_item("main", s) then
|
||||||
if inv:contains_item("main", s) then
|
self:run_on_reload(itemstack, player, "magazine")
|
||||||
local bullets_name = meta:get_string("bullets_name")
|
local bullets_name = meta:get_string("bullets_name")
|
||||||
if bullets_name ~= name then
|
if bullets_name ~= name then
|
||||||
local l = ItemStack({
|
local l = ItemStack({
|
||||||
name = bullets_name,
|
name = bullets_name,
|
||||||
count = bullets_loaded
|
count = bullets_loaded
|
||||||
})
|
})
|
||||||
if inv:room_for_item("main", l) then
|
if inv:room_for_item("main", l) then
|
||||||
inv:add_item("main", l)
|
inv:add_item("main", l)
|
||||||
bullets_loaded = 0
|
bullets_loaded = 0
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
s:set_count(def._gun_magazine - bullets_loaded)
|
||||||
|
local r = inv:remove_item("main", s)
|
||||||
|
meta:set_int("bullets_loaded", bullets_loaded + r:get_count())
|
||||||
|
meta:set_int("bullets_needed", stack:get_count())
|
||||||
|
meta:set_string("bullets_name", name)
|
||||||
|
return itemstack
|
||||||
end
|
end
|
||||||
s:set_count(def._gun_magazine - bullets_loaded)
|
|
||||||
local r = inv:remove_item("main", s)
|
|
||||||
meta:set_int("bullets_loaded", bullets_loaded + r:get_count())
|
|
||||||
meta:set_int("bullets_needed", stack:get_count())
|
|
||||||
meta:set_string("bullets_name", name)
|
|
||||||
return itemstack
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
61
bullet.lua
61
bullet.lua
@ -1,7 +1,7 @@
|
|||||||
local bullet = {
|
local bullet = {
|
||||||
timer = 0,
|
timer = 0,
|
||||||
initial_properties = {
|
initial_properties = {
|
||||||
collide_with_objects = false,
|
collide_with_objects = true,
|
||||||
physical = true,
|
physical = true,
|
||||||
visual = "wielditem"
|
visual = "wielditem"
|
||||||
},
|
},
|
||||||
@ -23,44 +23,35 @@ end
|
|||||||
bullet.on_step = function(self, dtime, moveresult)
|
bullet.on_step = function(self, dtime, moveresult)
|
||||||
self.timer = self.timer + dtime
|
self.timer = self.timer + dtime
|
||||||
if self.timer < 10 and self.owner and self.itemstack then
|
if self.timer < 10 and self.owner and self.itemstack then
|
||||||
if moveresult.collides then
|
local col = moveresult.collisions[1]
|
||||||
local col = moveresult.collisions[1]
|
if col then
|
||||||
if col then
|
self:run_on_collision(self.itemstack, self.owner, col)
|
||||||
self:run_on_collision(self.itemstack, self.owner, col)
|
if col.type == "node" then
|
||||||
if col.type == "node" then
|
local node = minetest.registered_nodes[minetest.get_node(col.node_pos).name]
|
||||||
local node = minetest.registered_nodes[minetest.get_node(col.node_pos).name]
|
if node.tiles then
|
||||||
if node.tiles then
|
for i=1,math.random(4,8) do
|
||||||
for i=1,math.random(4,8) do
|
if node.tiles[1] then
|
||||||
if node.tiles[1] then
|
minetest.add_particle({
|
||||||
minetest.add_particle({
|
pos = self.object:get_pos(),
|
||||||
pos = self.object:get_pos(),
|
velocity = {
|
||||||
velocity = {x=math.random(-10,10), y=math.random(-10,10), z=math.random(-10,10)},
|
x=math.random(-10,10),
|
||||||
expirationtime = 0.5,
|
y=math.random(-10,10),
|
||||||
size = math.random(1,2),
|
z=math.random(-10,10)
|
||||||
collisiondetection = true,
|
},
|
||||||
bounce = {0.5, 2},
|
expirationtime = 0.5,
|
||||||
texture = node.tiles[1].."^[resize:4x4"
|
size = math.random(1,2),
|
||||||
})
|
collisiondetection = true,
|
||||||
end
|
bounce = {0.5, 2},
|
||||||
|
texture = node.tiles[1].."^[resize:4x4"
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif col.type == "object" then
|
|
||||||
if col.object == self.owner then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local dmg = {full_punch_interval = 1, damage_groups = self.damage}
|
|
||||||
col.object:punch(self.owner, 1, dmg, nil)
|
|
||||||
end
|
end
|
||||||
self.object:remove()
|
elseif col.type == "object" then
|
||||||
|
local dmg = {full_punch_interval = 1, damage_groups = self.damage}
|
||||||
|
col.object:punch(self.owner, 1, dmg, nil)
|
||||||
end
|
end
|
||||||
elseif self.timer > 0.05 then
|
self.object:remove()
|
||||||
--This is a hack. There is probably a way to calculate the
|
|
||||||
--position where the player is looking a little ways away and
|
|
||||||
--spawn the bullet there so that the it doesn't collide with
|
|
||||||
--the player, but i don't know how to do that...
|
|
||||||
self.object:set_properties({
|
|
||||||
collide_with_objects = true
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
self.object:remove()
|
self.object:remove()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user