1
0
mirror of https://github.com/ComfyFactory/ComfyFactorio.git synced 2025-01-22 03:38:48 +02:00

rename module

This commit is contained in:
Gerkiz 2022-07-10 21:37:11 +02:00
parent 897360c70c
commit 2f54542c2f

View File

@ -48,7 +48,7 @@ local speed = 0.06
---@return integer ---@return integer
function Public:new_render() function Public:new_render()
local surface = game.get_surface(self.surface_id) local surface = game.get_surface(self.surface_id)
return rendering.draw_sprite {target = self.position, sprite = self.sprite, surface = surface} self.render_id = rendering.draw_sprite {target = self.position, sprite = self.sprite, surface = surface}
end end
--- Sets a new target for a given render. --- Sets a new target for a given render.
@ -79,14 +79,14 @@ end
--- Sets the render scale. --- Sets the render scale.
function Public:set_render_scalar_size() function Public:set_render_scalar_size()
if not self.target_id then if not self.render_id then
return return self:validate()
end end
rendering.set_y_scale(self.target_id, 3.5) -- 1.5 rendering.set_y_scale(self.render_id, 3.5) -- 1.5
rendering.set_x_scale(self.target_id, 7) -- 2 rendering.set_x_scale(self.render_id, 7) -- 2
rendering.set_color( rendering.set_color(
self.target_id, self.render_id,
{ {
r = 1, r = 1,
g = 0.7, g = 0.7,
@ -135,6 +135,33 @@ function Public:switch_position()
end end
end end
--- Notifies for a new render
function Public:notify_new_beam()
if not self.notify then
self.notify = true
local surface = game.get_surface(self.surface_id)
game.print('[Orbital] A new orbital strike has been spotted at: [gps=' .. self.position.x .. ',' .. self.position.y .. ',' .. surface.name .. ']')
end
end
--- Renders a new chart
function Public:render_chart()
if self.chart then
self.chart.destroy()
end
local surface = game.get_surface(self.surface_id)
self.chart =
game.forces[self.force].add_chart_tag(
surface,
{
icon = {type = 'virtual', name = 'signal-info'},
position = self.position,
text = 'Beam'
}
)
end
--- Sets a new position for a render. --- Sets a new position for a render.
function Public:set_new_position() function Public:set_new_position()
self.position = self:change_position(speed, false) self.position = self:change_position(speed, false)
@ -166,23 +193,15 @@ end
--- Damages entities nearby. --- Damages entities nearby.
function Public:damage_entities_nearby() function Public:damage_entities_nearby()
if random(1, 5) == 1 then if random(1, 3) == 1 then
local surface = game.get_surface(self.surface_id) local surface = game.get_surface(self.surface_id)
local radius = 10
local damage = random(10, 15) local damage = random(10, 15)
local entities = surface.find_entities_filtered({area = {{self.position.x - radius - 4, self.position.y - radius - 6}, {self.position.x + radius + 4, self.position.y + radius + 6}}}) local entities = surface.find_entities_filtered({position = self.position, radius = 20, type = 'simple-entity', invert = true})
for _, entity in pairs(entities) do for _, entity in pairs(entities) do
if entity.valid then if entity.valid then
if entity.health then if entity.health then
if entity.force.name ~= 'enemy' then if entity.force.name ~= 'enemy' then
if entity.name == 'character' then entity.damage(damage, 'enemy')
entity.damage(damage, 'enemy')
else
entity.health = entity.health - damage
if entity.health <= 0 then
entity.die('enemy')
end
end
end end
end end
end end
@ -191,8 +210,11 @@ function Public:damage_entities_nearby()
end end
--- Validates if a render is valid. --- Validates if a render is valid.
---@return boolean ---@return boolean|integer
function Public:validate() function Public:validate()
if not self.render_id then
return self:new_render()
end
if rendering.is_valid(self.render_id) then if rendering.is_valid(self.render_id) then
return true return true
end end
@ -207,39 +229,86 @@ function Public:destroy_render()
return self return self
end end
--- Removes a render. --- Destroys a render.
---@param id integer function Public:destroy_chart()
function Public:remove_render(id) if self.chart then
self:destroy_render() self.chart.destroy()
end
remove(this.renders, id)
return self return self
end end
--- Removes a render.
function Public:remove_render()
self:destroy_render()
self:destroy_chart()
remove(this.renders, self.id)
return self
end
function Public:work(tick)
if tick < self.ttl then
self:render_chart()
self:notify_new_beam()
self:set_new_position()
self:render_fire_damage()
self:damage_entities_nearby()
if self.random_pos_set and tick > self.random_pos_tick then
self:switch_position()
self.random_pos_set = nil
self.random_pos_tick = nil
end
else
self:remove_render()
end
end
--- Creates a new render. --- Creates a new render.
---@param sprite string ---@param sprite string
---@param surface userdata ---@param surface userdata
---@param ttl integer|nil ---@param ttl integer|nil
---@param scalar table|nil ---@param scalar table|nil
---@param delayed number|nil
---@return table ---@return table
function Public.new(sprite, surface, ttl, scalar) function Public.new(sprite, surface, ttl, scalar, delayed)
local render = setmetatable({}, Public.metatable) local render = setmetatable({}, Public.metatable)
render.surface_id = surface.index render.surface_id = surface.index
local position, random_position = render:new_target() local position, random_position = render:new_target()
render.position = position render.position = position
render.sprite = sprite render.sprite = sprite
render.force = 'player'
render.target_position = random_position render.target_position = random_position
render.render_id = render:new_render() render.id = #this.renders + 1
render.ttl = ttl or game.tick + 7200 -- 2 minutes duration if delayed then
if not scalar then render.delayed = game.tick + delayed
render:set_render_scalar_size() render.ttl = ttl or (game.tick + delayed) + 7200 -- 2 minutes duration
else
render.ttl = ttl or game.tick + 7200 -- 2 minutes duration
render:validate()
if not scalar then
render:set_render_scalar_size()
end
end end
render.ttl = ttl or game.tick + 7200 -- 2 minutes duration
this.renders[#this.renders + 1] = render this.renders[render.id] = render
return render return render
end end
--- Creates a new defined beam
---@param surface userdata
function Public.new_beam(surface)
Public.new(Gui.beam, surface)
end
--- Creates a new defined beam with a delayed action
---@param surface userdata
---@param time number
function Public.new_beam_delayed(surface, time)
Public.new(Gui.beam, surface, nil, nil, time)
end
Event.add( Event.add(
defines.events.on_tick, defines.events.on_tick,
function() function()
@ -249,38 +318,35 @@ Event.add(
local tick = game.tick local tick = game.tick
for id, render in pairs(this.renders) do for id = 1, #this.renders, 1 do
local render = this.renders[id]
if render then if render then
if tick < render.ttl then if render.delayed then
render:set_new_position() if tick > render.delayed then
render:render_fire_damage() render:work(tick)
render:damage_entities_nearby()
if render.random_pos_set and tick > render.random_pos_tick then
render:switch_position()
render.random_pos_set = nil
render.random_pos_tick = nil
end end
else else
render:remove_render(id) render:work(tick)
end end
end end
end end
end end
) )
if _DEBUG then
commands.add_command(
'laser',
'new laser',
function()
local player = game.player
if player and player.valid then
if not player.admin then
return
end
commands.add_command( Public.new_beam_delayed(player.surface, 222)
'laser',
'new laser',
function()
local player = game.player
if player and player.valid then
if not player.admin then
return
end end
Public.new(Gui.beam, player.surface)
end end
end )
) end
return Public return Public