Advertisement
Guest User

Raters NPC Api

a guest
Aug 26th, 2015
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.73 KB | None | 0 0
  1.  
  2. --the table for storing all the functions
  3. NPCEngine = {}
  4.  
  5. --for storing all the data for the NPCs
  6. NPCEngine.Data = {}
  7.  
  8. --the list of stored entities
  9. NPCEngine.Data.entities = {}
  10. --the list of stored objects
  11. NPCEngine.Data.objects = {}
  12.  
  13. --for creating a basic fireable event
  14. local function createEvent()
  15.     --the returning table
  16.     local ret = {}
  17.    
  18.     --for storeing the list of instances that we create for bindable functions
  19.     ret.instances = {}
  20.    
  21.     --for fireing the event, with varied arguments
  22.     function ret:fire(...)
  23.         for i, v in pairs(ret.instances) do
  24.             v(...)
  25.         end
  26.     end
  27.    
  28.     --for registering new events
  29.     function ret:connect(_f)
  30.         table.insert(ret.instances, _f)
  31.     end
  32.    
  33.     --then returning the new bindable event
  34.     return ret
  35. end
  36.  
  37. --for registering new NPCs
  38. function NPCEngine:new(npcname)
  39.     local npc = {}
  40.  
  41.     function npc:spawn(...)
  42.         local args = {...}
  43.         if #args == 1 then
  44.             --using pos
  45.             pos = args[1]
  46.             npc.Events.OnSpawn:fire(pos)
  47.             print("[Info] NPC SPAWNING COMING SOON!")
  48.         elseif #args >= 3 then
  49.             --use x, y, z
  50.             local x, y, z = args[1], args[2], args[3]
  51.             local pos = {x = x, y = y, z = z}
  52.             npc.Events.OnSpawn:fire(pos)
  53.             print("[Info] NPC SPAWNING COMING SOON!")
  54.         else
  55.             error("Invalid arguments, npc:spawn(pos) or npc:spawn(x,y,z)")
  56.         end
  57.     end
  58.    
  59.     --for creating new events
  60.     --usage: event.EventName:connect(FiredFunction)
  61.     --event.EventName:fire(...) will throw the event with the arguments you specify
  62.     npc.Events = {}
  63.     npc.Events.OnSpawn = createEvent()
  64.     npc.Events.OnDied = createEvent()
  65.     npc.Events.OnUpdate = createEvent()
  66.     npc.Events.OnSave = createEvent()
  67.     npc.Events.OnLoad = createEvent()
  68.    
  69.     table.insert(NPCEngine.Data.objects)
  70.    
  71. end
  72.  
  73.  
  74.  
  75. minetest.NPC = NPCEngine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement