Lazy commit: processing stations
This commit is contained in:
@@ -1,31 +1,4 @@
|
||||
Events = {}
|
||||
local modInfo = Mod.getInfo('smc__system__events')
|
||||
local require = modInfo.require
|
||||
|
||||
---@alias Events.callback fun(eventName, ...)
|
||||
|
||||
---@type table<string, Events.callback[]>
|
||||
local registeredEvents = {}
|
||||
|
||||
---@param eventName string
|
||||
---@param callback Events.callback
|
||||
function Events.subscribe(eventName, callback)
|
||||
local callbacks = registeredEvents[eventName]
|
||||
|
||||
if callbacks == nil then
|
||||
registeredEvents[eventName] = {callback}
|
||||
else
|
||||
table.insert(callbacks, callback)
|
||||
end
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param ... any
|
||||
function Events.call(eventName, ...)
|
||||
local callbacks = registeredEvents[eventName]
|
||||
if callbacks == nil then
|
||||
return
|
||||
end
|
||||
|
||||
for _, func in ipairs(callbacks) do
|
||||
func(eventName, ...)
|
||||
end
|
||||
end
|
||||
return require('Events')
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
name = smc__system__events
|
||||
depends = smc__system__table
|
||||
depends = smc__system__table, smc__system__require, smc__system__class
|
||||
|
||||
37
mods/SYSTEM/Events/src/Events.lua
Normal file
37
mods/SYSTEM/Events/src/Events.lua
Normal file
@@ -0,0 +1,37 @@
|
||||
---@alias Events.Callback fun(eventName:string, ...)
|
||||
|
||||
---@alias Events.Types
|
||||
--- 'ProcessingStationEvents'
|
||||
|
||||
---@class Events : Class
|
||||
---@field registeredEvents table<string, Events.Callback[]>
|
||||
Events = Class:extend({
|
||||
registeredEvents = {}
|
||||
})
|
||||
|
||||
---@param eventName string
|
||||
---@param callback Events.Callback
|
||||
function Events:subscribe(eventName, callback)
|
||||
local callbacks = self.registeredEvents[eventName]
|
||||
|
||||
if callbacks == nil then
|
||||
self.registeredEvents[eventName] = {callback}
|
||||
else
|
||||
table.insert(callbacks, callback)
|
||||
end
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param ... any
|
||||
function Events:call(eventName, ...)
|
||||
local callbacks = self.registeredEvents[eventName]
|
||||
if callbacks == nil then
|
||||
return
|
||||
end
|
||||
|
||||
for _, func in ipairs(callbacks) do
|
||||
func(eventName, ...)
|
||||
end
|
||||
end
|
||||
|
||||
return Events
|
||||
21
mods/SYSTEM/Events/src/Events/ProcessingStationEvents.lua
Normal file
21
mods/SYSTEM/Events/src/Events/ProcessingStationEvents.lua
Normal file
@@ -0,0 +1,21 @@
|
||||
---@alias Events.ProcessingStationEvents.EventName 'on_push_container'
|
||||
|
||||
---@alias Events.ProcessingStationEvents.Callback fun(eventName:Events.ProcessingStationEvents.EventName, ...)
|
||||
|
||||
---@class Events.ProcessingStationEvents : Events
|
||||
---@field subscribe fun(self:Events.ProcessingStationEvents,eventName:Events.ProcessingStationEvents.EventName, callback:Events.ProcessingStationEvents.Callback)
|
||||
---@field call fun(self:Events.ProcessingStationEvents,eventName:Events.ProcessingStationEvents.EventName, ...)
|
||||
local ProcessingStationEvents = Events:extend()
|
||||
|
||||
---@param callback fun(eventName:Events.ProcessingStationEvents.EventName, pos:MapVector, energy:number)
|
||||
function ProcessingStationEvents:subscribeOnPushContainer(callback)
|
||||
self:subscribe('on_push_container', callback)
|
||||
end
|
||||
|
||||
---@param pos MapVector
|
||||
---@param energy number
|
||||
function ProcessingStationEvents:callOnPushContainer(pos, energy)
|
||||
self:call('on_push_container', pos, energy)
|
||||
end
|
||||
|
||||
return ProcessingStationEvents
|
||||
Reference in New Issue
Block a user