-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
78 lines (68 loc) · 2.42 KB
/
control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require('__stdlib__/stdlib/utils/defines/color')
local Color = require('__stdlib__/stdlib/utils/color')
function drawLinesOnAllIndicators()
for _, v in pairs(game.surfaces["nauvis"].find_entities_filtered{name = "mainbus-indicator"}) do
drawLineOnIndicator(v)
addSpritesOnLine(v)
end
end
function drawLineOnIndicator(entity)
rendering.draw_line{
color = Color.set(defines.color.yellow, 0.5),
width = 12,
gap_length = 1,
dash_length = 2,
from = entity,
to = calculateShiftedPosition(entity.position, entity.direction, 100),
surface = "nauvis",
draw_on_ground = true
}
end
function addSpritesOnLine(entity)
if (entityHasSignal(entity)) then
for i = 1, 100, 3 do
rendering.draw_sprite{
sprite = convertSignalToSpritePath(entity.get_control_behavior().get_signal(1)),
orientation = entity.direction,
target = calculateShiftedPosition(entity.position, entity.direction, i),
surface = "nauvis",
x_scale = 0.85,
y_scale = 0.85
}
end
end
end
function entityHasSignal(entity)
return entity.get_control_behavior().get_signal(1)['signal'] ~= nil
end
function convertSignalToSpritePath(signal) return signal.signal.type .. "/" .. signal.signal.name end
function calculateShiftedPosition(position, direction, distance)
if direction == defines.direction.east then
return {position.x + distance, position.y}
elseif direction == defines.direction.north then
return {position.x, position.y + distance}
elseif direction == defines.direction.west then
return {position.x - distance, position.y}
elseif direction == defines.direction.south then
return {position.x, position.y - distance}
end
end
script.on_init(drawLinesOnAllIndicators)
script.on_event(
{defines.events.on_built_entity},
function(e)
if e.created_entity.name == "mainbus-indicator" then
drawLineOnIndicator(e.created_entity)
addSpritesOnLine(e.created_entity)
end
end
)
script.on_event(
{defines.events.on_player_rotated_entity},
function(e)
if e.entity.name == "mainbus-indicator" then
rendering.clear()
drawLinesOnAllIndicators()
end
end
)