Last active
September 7, 2016 10:45
-
-
Save dbjorkholm/60b0e4279fed772798dd570cb6a560e2 to your computer and use it in GitHub Desktop.
Spawn ~
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Functions: | |
Game.createMonsters(identifier, monsters) | |
Game.removeMonsters(identifier) | |
Game.createObjects(identifier, objects) | |
Game.removeObjects(identifier) | |
Usage: | |
-- | |
local identifier = 'someIdentifier' | |
local monsters = { | |
{name = 'Demon', position = Position(1000, 1000, 7)}, | |
{name = 'Rat', position = Position(1100, 1000, 8)} | |
} | |
Game.createMonsters(identifier, monsters) | |
Game.removeMonsters(identifier) | |
-- | |
-- | |
local identifier = 'someIdentifier' | |
local objects = { | |
{itemId = 2195, position = Position(1000, 1000, 7)}, | |
{itemId = 2160, count = 50, position = Position(2000, 1000, 8)} | |
} | |
Game.createObjects(identifier, objects) | |
Game.removeObjects(identifier) | |
-- | |
]] | |
if not monsterData then | |
monsterData = { } | |
end | |
function Game.createMonsters(identifier, monsters) | |
if not identifier or type(identifier) ~= 'string' or monsterData[identifier] ~= nil then | |
return false | |
end | |
if not monsters or type(monsters) ~= 'table' or #monsters == 0 then | |
return false | |
end | |
local tmpMonsters = { } | |
for _, monster in ipairs(monsters) do | |
local tmpMonster = Game.createMonster(monster.name, monster.position, true, true) | |
if tmpMonster then | |
tmpMonsters[#tmpMonsters + 1] = tmpMonster:getId() | |
end | |
end | |
if #tmpMonsters == 0 then | |
return false | |
end | |
monsterData[identifier] = tmpMonsters | |
return true | |
end | |
function Game.removeMonsters(identifier) | |
if not identifier or type(identifier) ~= 'string' then | |
return false | |
end | |
local monsters = monsterData[identifier] | |
if not monsters or type(monsters) ~= 'table' or #monsters == 0 then | |
return false | |
end | |
for _, cid in ipairs(monsters) do | |
local monster = Monster(cid) | |
if monster then | |
monster:remove() | |
end | |
end | |
monsterData[identifier] = nil | |
return true | |
end | |
if not objectData then | |
objectData = { } | |
end | |
function Game.createObjects(identifier, objects) | |
if not identifier or type(identifier) ~= 'string' or objectData[identifier] ~= nil then | |
return false | |
end | |
if not objects or type(objects) ~= 'table' or #objects == 0 then | |
return false | |
end | |
local tmpObjects = { } | |
for _, object in ipairs(objects) do | |
local objectItem = Game.createItem(object.itemId, object.count or 1, object.position) | |
if objectItem then | |
tmpObjects[#tmpObjects + 1] = {itemId = object.itemId, position = object.position} | |
end | |
end | |
if #tmpObjects == 0 then | |
return false | |
end | |
objectData[identifier] = tmpObjects | |
return true | |
end | |
function Game.removeObjects(identifier) | |
if not identifier or type(identifier) ~= 'string' then | |
return false | |
end | |
local objects = objectData[identifier] | |
if not objects or type(objects) ~= 'table' or #objects == 0 then | |
return false | |
end | |
for _, object in ipairs(objects) do | |
local tile = Tile(object.position) | |
if tile then | |
local objectItem = tile:getItemById(object.itemId) | |
if objectItem then | |
objectItem:remove() | |
end | |
end | |
end | |
objectData[identifier] = nil | |
return true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment