-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputSwitch.lua
104 lines (87 loc) · 3.51 KB
/
InputSwitch.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function Behavior:Reset_Button()
self.input_child.transform:SetLocalPosition( Vector3:New( -1.5625 , 0 , 0.1 ) )
self.button_state = true
self:Set(true)
end
function Behavior:Set(V)
end
function Behavior:Get()
end
function Behavior:Awake()
if self.wakning == nil then
self.wakning = true
self.active = true
self.input_child = nil
self.input_trigger = {}
local child = self.gameObject:GetChildren()
-- > On positionne et configure nos enfants
if #child == 0 then
local enfant = CS.New( "Locket" , self.gameObject )
enfant:CreateComponent("ModelRenderer"):SetModel( CS.FindAsset( "Frameworks/Input/Input_switch_locket" , "Model" ) )
enfant.transform:SetLocalPosition( Vector3:New( -1.5625 , 0 , 0.1 ) )
self.input_child = enfant
elseif #child == 1 then
if child[1].modelRenderer == nil then
child[1]:CreateComponent("ModelRenderer"):SetModel( CS.FindAsset( "Frameworks/Input/Input_switch_locket" , "Model" ) )
end
child[1].transform:SetLocalPosition( Vector3:New( -1.5625 , 0 , 0.1 ) )
self.input_child = child[1]
else
self.active = false
end
-- > On crée un gameObject si inexistant
if self.gameObject.modelRenderer == nil then
self.gameObject:CreateComponent("ModelRenderer"):SetModel( CS.FindAsset( "Frameworks/Input/Input_switch" , "Model" ) )
end
-- > On crée deux modèles non-opa pour la collider.
do
local on = CS.New( "on" , self.gameObject )
on:CreateComponent("ModelRenderer"):SetModel( CS.FindAsset( "Frameworks/Input/Input_switch_locket" , "Model" ) )
on.transform:SetLocalPosition( Vector3:New( -1.5625 , 0 , 0.1 ) )
on.modelRenderer:SetOpacity(0)
local off = CS.New( "off" , self.gameObject )
off:CreateComponent("ModelRenderer"):SetModel( CS.FindAsset( "Frameworks/Input/Input_switch_locket" , "Model" ) )
off.transform:SetLocalPosition( Vector3:New( 1.5625 , 0 , 0.1 ) )
off.modelRenderer:SetOpacity(0)
self.input_trigger = {on,off}
end
self.button_hover = false
self.button_state = true
end
end
function Behavior:Switch(V)
local PosX = self.input_child.LocalPos.x
if V == "on" and not self.button_state then
self.button_state = true
self:Set(true)
PosX = -1.5625
elseif V == "off" and self.button_state then
self.button_state = false
self:Set(false)
PosX = 1.5625
end
self.input_child.transform:SetLocalPosition( Vector3:New( PosX , 0 , 0.1 ) )
end
function Behavior:Update()
if self.active then
local ray = AK.UI.ray
if ray:IntersectsModelRenderer( self.gameObject.modelRenderer ) ~= nil then
if not self.button_hover then
self.button_hover = true
end
else
if self.button_hover then
self.button_hover = false
end
end
if self.button_hover then
for k,v in pairs(self.input_trigger) do
if ray:IntersectsModelRenderer( v.modelRenderer ) ~= nil then
if CS.KeyReleased("SG") then
self:Switch(v.Name)
end
end
end
end
end
end