mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-29 04:06:39 +09:00
Added renderer and selection events
This commit is contained in:
@@ -8,6 +8,8 @@ local Global = require 'utils.global' --- @dep utils.global
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local config = require 'config.protection' --- @dep config.protection
|
||||
local EntityProtection = {
|
||||
protected_entity_names = table.deep_copy(config.always_protected_names),
|
||||
protected_entity_types = table.deep_copy(config.always_protected_types),
|
||||
events = {
|
||||
--- When a player mines a protected entity
|
||||
-- @event on_player_mined_protected
|
||||
@@ -59,12 +61,12 @@ end)
|
||||
|
||||
--- Get the key used in protected_entities
|
||||
local function get_entity_key(entity)
|
||||
return string.format('%i,%i', entity.position.x, entity.position.y)
|
||||
return string.format('%i,%i', math.floor(entity.position.x), math.floor(entity.position.y))
|
||||
end
|
||||
|
||||
--- Get the key used in protected_areas
|
||||
local function get_area_key(area)
|
||||
return string.format('%i,%i', area.left_top.x, area.left_top.y)
|
||||
return string.format('%i,%i', math.floor(area.left_top.x), math.floor(area.left_top.y))
|
||||
end
|
||||
|
||||
--- Check if an entity is always protected
|
||||
@@ -99,7 +101,7 @@ end
|
||||
|
||||
--- Get all protected entities on a surface
|
||||
function EntityProtection.get_entities(surface)
|
||||
return protected_entities[surface.index]
|
||||
return protected_entities[surface.index] or {}
|
||||
end
|
||||
|
||||
--- Check if an entity is protected
|
||||
@@ -129,7 +131,7 @@ end
|
||||
|
||||
--- Get all protected areas on a surface
|
||||
function EntityProtection.get_areas(surface)
|
||||
return protected_areas[surface.index]
|
||||
return protected_areas[surface.index] or {}
|
||||
end
|
||||
|
||||
--- Check if an entity is protected
|
||||
|
||||
@@ -6,7 +6,20 @@
|
||||
|
||||
local Event = require 'utils.event' --- @dep utils.event
|
||||
local Global = require 'utils.global' --- @dep utils.global
|
||||
local Selection = {}
|
||||
local Selection = {
|
||||
events = {
|
||||
--- When a player enterers selection mode
|
||||
-- @event on_player_selection_start
|
||||
-- @tparam number player_index the player index of the player who entered selection mode
|
||||
-- @tparam string selection the name of the selection being made
|
||||
on_player_selection_start = script.generate_event_name(),
|
||||
--- When a player leaves selection mode
|
||||
-- @event on_player_selection_end
|
||||
-- @tparam number player_index the player index of the player who left selection mode
|
||||
-- @tparam string selection the name of the selection which ended
|
||||
on_player_selection_end = script.generate_event_name(),
|
||||
}
|
||||
}
|
||||
|
||||
local selection_tool = { name='selection-tool' }
|
||||
|
||||
@@ -22,8 +35,18 @@ end)
|
||||
-- @tparam string selection_name The name of the selection to start, used with on_selection
|
||||
-- @tparam[opt=false] boolean single_use When true the selection will stop after first use
|
||||
function Selection.start(player, selection_name, single_use, ...)
|
||||
-- Assign the arguments if the player is valid
|
||||
if not player or not player.valid then return end
|
||||
if selections[player.index] then
|
||||
-- Raise the end event if a selection was already in progress
|
||||
script.raise_event(Selection.events.on_player_selection_end, {
|
||||
name = Selection.events.on_player_selection_end,
|
||||
tick = game.tick,
|
||||
player_index = player.index,
|
||||
selection = selections[player.index].name
|
||||
})
|
||||
end
|
||||
|
||||
-- Set the selection data
|
||||
selections[player.index] = {
|
||||
name = selection_name,
|
||||
arguments = { ... },
|
||||
@@ -31,6 +54,14 @@ function Selection.start(player, selection_name, single_use, ...)
|
||||
character = player.character
|
||||
}
|
||||
|
||||
-- Raise the event
|
||||
script.raise_event(Selection.events.on_player_selection_start, {
|
||||
name = Selection.events.on_player_selection_start,
|
||||
tick = game.tick,
|
||||
player_index = player.index,
|
||||
selection = selection_name
|
||||
})
|
||||
|
||||
-- Give a selection tool if one is not in use
|
||||
if player.cursor_stack.is_selection_tool then return end
|
||||
player.clear_cursor() -- Clear the current item
|
||||
@@ -47,8 +78,17 @@ end
|
||||
function Selection.stop(player)
|
||||
if not selections[player.index] then return end
|
||||
local character = selections[player.index].character
|
||||
local selection = selections[player.index].name
|
||||
selections[player.index] = nil
|
||||
|
||||
-- Raise the event
|
||||
script.raise_event(Selection.events.on_player_selection_end, {
|
||||
name = Selection.events.on_player_selection_end,
|
||||
tick = game.tick,
|
||||
player_index = player.index,
|
||||
selection = selection
|
||||
})
|
||||
|
||||
-- Remove the selection tool
|
||||
if player.cursor_stack.is_selection_tool then
|
||||
player.cursor_stack.clear()
|
||||
@@ -78,8 +118,8 @@ function Selection.is_selecting(player, selection_name)
|
||||
if not selections[player.index] then return false end
|
||||
return selections[player.index].name == selection_name
|
||||
else
|
||||
return player.cursor_stack.is_selection_tool
|
||||
end
|
||||
return player.cursor_stack.is_selection_tool
|
||||
end
|
||||
end
|
||||
|
||||
--- Filter on_player_selected_area to this custom selection, appends the selection arguments
|
||||
|
||||
Reference in New Issue
Block a user