Guest User

Throwing mod

a guest
Feb 10th, 2012
236
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Bow and arrow mod
  2. -- Topic on the forum: http://c55.me/minetest/forum/viewtopic.php?id=687
  3.  
  4. ARROW_DAMAGE=1
  5. ARROW_GRAVITY=9
  6. ARROW_VELOCITY=19
  7.  
  8. throwing_shoot_arrow=function (item, player, pointed_thing)
  9.     -- Check if arrows in Inventory and remove one of them
  10.     local i=1
  11.     if player:get_inventory():contains_item("main", "throwing:arrow") then
  12.         player:get_inventory():remove_item("main", "throwing:arrow")
  13.         -- Shoot Arrow
  14.         local playerpos=player:getpos()
  15.         local obj=minetest.env:add_entity({x=playerpos.x,y=playerpos.y+1.5,z=playerpos.z}, "throwing:arrow_entity")
  16.         local dir=player:get_look_dir()
  17.         obj:setvelocity({x=dir.x*ARROW_VELOCITY, y=dir.y*ARROW_VELOCITY, z=dir.z*ARROW_VELOCITY})
  18.         obj:setacceleration({x=dir.x*-3, y=-ARROW_GRAVITY, z=dir.z*-3})
  19.     end
  20.     return
  21. end
  22.  
  23. minetest.register_craftitem("throwing:string", {
  24.     image = "throwing_string.png",
  25.     on_place_on_ground = minetest.craftitem_place_item,
  26. })
  27.  
  28. minetest.register_craftitem("throwing:bow", {
  29.     image = "throwing_bow.png",
  30.     stack_max = 1,
  31.     on_place_on_ground = minetest.craftitem_place_item,
  32.     on_use = throwing_shoot_arrow,
  33. })
  34.  
  35. minetest.register_craftitem("throwing:arrow", {
  36.     image = "throwing_arrow.png",
  37.     on_place_on_ground = minetest.craftitem_place_item,
  38. })
  39.  
  40. -- The Arrow Entity
  41.  
  42. THROWING_ARROW_ENTITY={
  43.     physical = false,
  44.     timer=0,
  45.     textures = {"throwing_arrow_back.png"},
  46.     lastpos={},
  47.     collisionbox = {0,0,0,0,0,0},
  48. }
  49.  
  50.  
  51. -- Arrow_entity.on_step()--> called when arrow is moving
  52. THROWING_ARROW_ENTITY.on_step = function(self, dtime)
  53.     self.timer=self.timer+dtime
  54.     local pos = self.object:getpos()
  55.     local node = minetest.env:get_node(pos)
  56.  
  57.     -- When arrow is away from player (after 0.2 seconds): Cause damage to mobs and players
  58.     if self.timer>0.2 then
  59.         local objs = minetest.env:get_objects_inside_radius({x=pos.x,y=pos.y,z=pos.z}, 2)
  60.         for k, obj in pairs(objs) do
  61.             obj:set_hp(obj:get_hp()-ARROW_DAMAGE)
  62.             if obj:get_entity_name() ~= "throwing:arrow_entity" then
  63.                 if obj:get_hp()<=0 then
  64.                     obj:remove()
  65.                 end
  66.                 self.object:remove()
  67.             end
  68.         end
  69.     end
  70.  
  71.     -- Become item when hitting a node
  72.     if self.lastpos.x~=nil then --If there is no lastpos for some reason
  73.         if node.name ~= "air" then
  74.             minetest.env:add_item(self.lastpos, 'throwing:arrow')
  75.             self.object:remove()
  76.         end
  77.     end
  78.     self.lastpos={x=pos.x, y=pos.y, z=pos.z} -- Set lastpos-->Item will be added at last pos outside the node
  79. end
  80.  
  81. minetest.register_entity("throwing:arrow_entity", THROWING_ARROW_ENTITY)
  82.  
  83.  
  84.  
  85. --CRAFTS
  86. minetest.register_craft({
  87.     output = 'throwing:string',
  88.     recipe = {
  89.         {'default:junglegrass'},
  90.         {'default:junglegrass'},
  91.     }
  92. })
  93.  
  94. minetest.register_craft({
  95.     output = 'throwing:bow',
  96.     recipe = {
  97.         {'throwing:string', 'default:wood', ''},
  98.         {'throwing:string', '', 'default:wood'},
  99.         {'throwing:string', 'default:wood', ''},
  100.     }
  101. })
  102.  
  103. minetest.register_craft({
  104.     output = 'throwing:arrow 16',
  105.     recipe = {
  106.         {'default:stick', 'default:stick', 'default:steel_ingot'},
  107.     }
  108. })
  109.  
  110. print ("[Throwing_mod] Loaded!")
RAW Paste Data