-
Notifications
You must be signed in to change notification settings - Fork 4
/
Keypad.txt
147 lines (134 loc) · 3.5 KB
/
Keypad.txt
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
--@name
--@author
--@shared
-- To use, simply place the chip and link a starfall screen to it
local keypad = class("keypad")
local button = class("button")
if SERVER then
function keypad:initialize()
end
function keypad:enter(password)
if not self.password then self.password = password end
local success = password == self.password
if success then
wire.ports.Success = 1
chip():emitSound("buttons/button24.wav")
timer.simple(2, function() wire.ports.Success = 0 end)
else
wire.ports.Fail = 1
chip():emitSound("buttons/button8.wav")
timer.simple(2, function() wire.ports.Fail = 0 end)
end
net.start("password")
net.writeBool(success)
net.send()
end
else
function keypad:initialize()
self.entry = {}
self.waiting = false
self.color = Color(255,255,255)
self.buttons = {}
for x=512/4,512-1,512/4 do
for y=512/4,512-1,512/4 do
self.buttons[#self.buttons+1] = button:new(x,y)
end
end
end
end
function keypad:reset()
self.color = Color(255,255,255)
self.entry = {}
self.waiting = false
for i=1, #self.buttons do
self.buttons[i].used = false
end
end
function keypad:recvConfirm(success)
if success then self.color = Color(0,255,0) else self.color = Color(255,0,0) end
timer.simple(1,function() self:reset() end)
end
function keypad:draw()
if not self.waiting then
if not self.using and player():keyDown(IN_KEY.USE) and player():getEyeTrace().Entity == render.getScreenEntity() then
self.using = true
end
if self.using then
if player():keyDown(IN_KEY.USE) then
local x,y = render.cursorPos(self.user)
if x and y then
if #self.entry>0 then
local b = self.buttons[self.entry[#self.entry]]
render.drawLine(b.x, b.y, x, y)
end
for i=1, #self.buttons do
if self.buttons[i]:select(x,y) then
self.entry[#self.entry+1] = i
end
end
end
else
self.using = false
self.waiting = true
net.start("password")
net.writeString(table.concat(self.entry))
net.send()
end
end
end
render.setColor(self.color)
for i=1, #self.entry-1 do
local b1, b2 = self.buttons[self.entry[i]], self.buttons[self.entry[i+1]]
render.drawLine(b1.x, b1.y, b2.x, b2.y)
end
for i=1, #self.buttons do
self.buttons[i]:draw()
end
end
function button:initialize(x,y)
self.x = x
self.y = y
self.blip = 0
self.used = false
end
function button:select(ox, oy)
if not self.used and (ox-self.x)^2+(oy-self.y)^2 < 1500 then
self.used = true
self.blip = timer.curtime()+0.5
return true
end
return false
end
function button:draw()
local blipdiff = self.blip-timer.curtime()
if blipdiff>0 then
render.drawCircle(self.x,self.y,20+(10/0.5)*blipdiff)
else
render.drawCircle(self.x,self.y,20)
end
end
local mykeypad = keypad:new()
if SERVER then
wire.adjustOutputs({"Success","Fail"},{"normal","normal"})
net.receive("password",function() mykeypad:enter(net.readString()) end)
else
hook.add("render","",function() mykeypad:draw() end)
net.receive("password",function() mykeypad:recvConfirm(net.readBool()) end)
render.createRenderTarget("circle")
hook.add("renderoffscreen","",function()
render.selectRenderTarget("circle")
local poly = {}
for i=1, 360 do
local theta = i*math.pi/180
poly[i] = {x=math.cos(theta)*512+512, y=math.sin(theta)*512+512}
end
render.clear(Color(0,0,0,0))
render.drawPoly(poly)
render.selectRenderTarget()
hook.remove("renderoffscreen","")
end)
function render.drawCircle(x,y,r)
render.setRenderTargetTexture("circle")
render.drawTexturedRect(x-r,y-r,2*r,2*r)
end
end