1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-14 10:13:13 +02:00
RedMew/Diggy/Template.lua

63 lines
1.8 KiB
Lua
Raw Normal View History

2018-09-08 18:29:27 +02:00
-- this
local Template = {}
Template.events = {
--[[--
When an entity is placed via the template function.
- event.entity LuaEntity
]]
on_placed_entity = script.generate_event_name(),
--[[--
Triggers when an 'out-of-map' tile is replaced by a rock in
DiggyHole.diggy_hole.
Can be fired for each position replaced. It's recommended to avoid
performance heavy listeners. Off-load them into a queue processed on
ticks.
]]
on_void_removed = script.generate_event_name(),
2018-09-08 18:29:27 +02:00
}
--[[--
Inserts a batch of tiles and then entities.
@see LuaSurface.set_tiles
@see LuaSurface.entity
@param surface LuaSurface to put the tiles and entities on
@param tiles table of tiles as required by set_tiles
@param entities table of entities as required by create_entity
]]
function Template.insert(surface, tiles, entities, fire_void_removed_event)
2018-09-08 18:29:27 +02:00
surface.set_tiles(tiles)
if (fire_void_removed_event) then
for _, tile in pairs(tiles) do
2018-09-13 20:56:58 +02:00
if ('out-of-map' ~= tile.name) then
script.raise_event(Template.events.on_void_removed, {
surface = surface,
position = {x = tile.position.x, y = tile.position.y}
})
end
end
end
2018-09-12 23:13:54 +02:00
local created_entities = {}
2018-09-08 18:29:27 +02:00
for _, entity in pairs(entities) do
2018-09-12 23:13:54 +02:00
created_entity = surface.create_entity(entity)
if (nil == created_entity) then
error('Failed creating entity ' .. entity.name .. ' on surface.')
end
2018-09-12 23:13:54 +02:00
table.insert(created_entities, created_entity)
end
for _, entity in pairs(created_entities) do
script.raise_event(Template.events.on_placed_entity, {entity = entity})
2018-09-08 18:29:27 +02:00
end
end
return Template