-
Notifications
You must be signed in to change notification settings - Fork 4
/
Door.txt
65 lines (55 loc) · 1.85 KB
/
Door.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
--@name Door
--@author Sparky
--@server
--@include libs/criticalpd.txt
local pd = require("libs/criticalpd.txt")
local door = class("door")
function door:initialize(ent)
self.ent = ent
--ent:makeUsable()
self.pd = pd:new(ent, 0, 400)
self.open = false
self.matrix = self.pd.targetAng
self.nextToggle = 0
self.ang = 0
self.angVel = math.rad(100) -- 100 deg/sec
self.angOpen = math.rad(100) -- 100 deg
self.up = (ent:getMassCenterW() - chip():getPos()):cross(chip():getUp())
self.up = Vector(0,0,math.sign(self.up[3]))
hook.add("playeruse","door",function(pl,used)
if used == ent then
self:toggle()
end
end)
end
function door:toggle()
if timer.curtime() < self.nextToggle then return end
self.nextToggle = timer.curtime() + 0.5
self.ent:emitSound("doors/door1_move.wav")
self.open = not self.open
self.shutsound = false
self.pd.phys:enableMotion(true)
chip():enableMotion(true)
hook.add("think","door",function() self:simulate() end)
end
function door:simulate()
local angVel = (self.open and self.angVel or -self.angVel)
self.ang = math.clamp(self.ang + angVel*timer.frametime(), 0, self.angOpen)
if self.ang == self.angOpen or self.ang == 0 then
self.pd.targetAngVel = Vector()
if self.ang == 0 and not self.shutsound then self.shutsound = true self.ent:emitSound("doors/door1_stop.wav") end
if self.pd.angError:getLengthSqr()<0.005 then
self.pd.phys:enableMotion(false)
chip():enableMotion(false)
hook.remove("think","door")
return
end
else
self.pd.targetAngVel = self.up * math.deg(angVel)
end
local m = Matrix()
m:setAxisAngle(self.up, self.ang)
self.pd.targetAng = self.matrix * m
self.pd:simulateAngForce()
end
door:new(chip():isWeldedTo())