change the bullet spawns

This commit is contained in:
Jouni 2023-04-24 14:09:46 +03:00
parent 0d7ac9e4a3
commit 3373a6d7d1
2 changed files with 72 additions and 83 deletions

94
api.lua
View File

@ -81,7 +81,11 @@ guns.fire_bullet = function(self, itemstack, player)
velocity = velocity + bullet_velocity
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()
ent.itemstack = itemstack
ent.owner = player
@ -146,22 +150,14 @@ guns.register_gun = function(self, name, def)
def.range = 0
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)
if minetest.is_player(player) then
local pname = player:get_player_name()
if not self.cooldown[pname] then
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] then
if def.groups and def.groups.gun_automatic == 1 then
self.cooldown[pname] = true
self:fire_auto(player)
end
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
elseif self:has_bullets(itemstack) then
local meta = itemstack:get_meta()
if def.groups and def.groups.gun_single == 1 then
if meta:get_int("loaded") == 1 then
@ -184,47 +180,49 @@ guns.register_gun = function(self, name, def)
return itemstack
end
end
return nil
end
return nil
end
def.on_secondary_use = function(itemstack, player, pointed_thing)
local meta = itemstack:get_meta()
local bullets_loaded = meta:get_int("bullets_loaded")
local bullets_name = meta:get_string("bullets_name")
if minetest.is_player(player) then
local meta = itemstack:get_meta()
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
self:run_on_reload(itemstack, player, "reload")
meta:set_int("loaded", 1)
return itemstack
end
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")
meta:set_int("loaded", 1)
return itemstack
end
if def._gun_magazine and def._gun_ammunition then
self:run_on_reload(itemstack, player, "magazine")
local inv = player:get_inventory()
for _, stack in ipairs(def._gun_ammunition) do
local name = stack:get_name()
local s = ItemStack(name)
if inv:contains_item("main", s) then
local bullets_name = meta:get_string("bullets_name")
if bullets_name ~= name then
local l = ItemStack({
name = bullets_name,
count = bullets_loaded
})
if inv:room_for_item("main", l) then
inv:add_item("main", l)
bullets_loaded = 0
else
break
if def._gun_magazine and def._gun_ammunition then
local inv = player:get_inventory()
for _, stack in ipairs(def._gun_ammunition) do
local name = stack:get_name()
local s = ItemStack(name)
if inv:contains_item("main", s) then
self:run_on_reload(itemstack, player, "magazine")
local bullets_name = meta:get_string("bullets_name")
if bullets_name ~= name then
local l = ItemStack({
name = bullets_name,
count = bullets_loaded
})
if inv:room_for_item("main", l) then
inv:add_item("main", l)
bullets_loaded = 0
else
break
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
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

View File

@ -1,7 +1,7 @@
local bullet = {
timer = 0,
initial_properties = {
collide_with_objects = false,
collide_with_objects = true,
physical = true,
visual = "wielditem"
},
@ -23,44 +23,35 @@ end
bullet.on_step = function(self, dtime, moveresult)
self.timer = self.timer + dtime
if self.timer < 10 and self.owner and self.itemstack then
if moveresult.collides then
local col = moveresult.collisions[1]
if col then
self:run_on_collision(self.itemstack, self.owner, col)
if col.type == "node" then
local node = minetest.registered_nodes[minetest.get_node(col.node_pos).name]
if node.tiles then
for i=1,math.random(4,8) do
if node.tiles[1] then
minetest.add_particle({
pos = self.object:get_pos(),
velocity = {x=math.random(-10,10), y=math.random(-10,10), z=math.random(-10,10)},
expirationtime = 0.5,
size = math.random(1,2),
collisiondetection = true,
bounce = {0.5, 2},
texture = node.tiles[1].."^[resize:4x4"
})
end
local col = moveresult.collisions[1]
if col then
self:run_on_collision(self.itemstack, self.owner, col)
if col.type == "node" then
local node = minetest.registered_nodes[minetest.get_node(col.node_pos).name]
if node.tiles then
for i=1,math.random(4,8) do
if node.tiles[1] then
minetest.add_particle({
pos = self.object:get_pos(),
velocity = {
x=math.random(-10,10),
y=math.random(-10,10),
z=math.random(-10,10)
},
expirationtime = 0.5,
size = math.random(1,2),
collisiondetection = true,
bounce = {0.5, 2},
texture = node.tiles[1].."^[resize:4x4"
})
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
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
elseif self.timer > 0.05 then
--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
})
self.object:remove()
end
else
self.object:remove()