-
Notifications
You must be signed in to change notification settings - Fork 2
/
Target Lock.ttslua
82 lines (74 loc) · 2.13 KB
/
Target Lock.ttslua
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
79
80
81
82
-- ~~~~~~
-- Script by dzikakulka
-- Issues, history at: http://github.com/tjakubo2/TTS_xwing
--
-- Target lock color and name script
-- ~~~~~~
-- Scripted token properties
__XW_Token = true
__XW_TokenIdle = true
__XW_TokenType = 'targetLock'
-- Was this lock tinted and named already?
set = false
-- Colors for tinting on pickup
colorTable = {}
colorTable['Red']= {1, 0, 0}
colorTable['Brown']= {0.6, 0.4, 0}
colorTable['White']= {1, 1, 1}
colorTable['Pink']= {1, 0.4, 0.8}
colorTable['Purple']= {0.8, 0, 0.8}
colorTable['Blue']= {0, 0, 1}
colorTable['Teal']= {0.2, 1, 0.8}
colorTable['Green']= {0, 1, 0}
colorTable['Yellow']= {1, 1, 0}
colorTable['Orange']= {1, 0.4, 0}
colorTable['Black']= {0.2, 0.2, 0.2}
-- Save self state
function onSave()
if set then
local state = {set=set}
return JSON.encode(state)
end
end
-- Restore self state
function onLoad(save_state)
if save_state ~= '' and save_state ~= 'null' and save_state ~= nil then
set = JSON.decode(save_state).set
end
end
-- Set function for external calls
function manualSet(arg)
set = true
self.setColorTint(colorTable[arg.color])
self.setName(arg.name)
end
-- Tint on pick up
function onPickedUp()
if not set and self.held_by_color ~= nil then
self.setColorTint(colorTable[self.held_by_color])
end
end
-- Set name on drop near a ship
function onDropped()
if not set then
local spos = self.getPosition()
local spos = self.getPosition()
local nearest = nil
local minDist = 3.6125 -- 100 mm
for k,ship in pairs(getAllObjects()) do
if ship.tag == 'Figurine' and ship.name ~= '' then
local pos = ship.getPosition()
local dist = math.sqrt(math.pow((spos[1]-pos[1]),2) + math.pow((spos[3]-pos[3]),2))
if dist < minDist then
nearest = ship
minDist = dist
end
end
end
if nearest ~= nil then
self.setName(nearest.getName())
printToAll('Target lock named for ' .. nearest.getName(), {0.2, 0.2, 1})
set = true
end
end
end