-
Notifications
You must be signed in to change notification settings - Fork 17
/
door.lua
177 lines (157 loc) · 4.49 KB
/
door.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
door = class("door")
function door:init(x, y, r)
self.cox = x
self.coy = y
self.dir = "ver"
self.open = false
self.timer = 0
self.input1state = "off"
--Input list
self.r = {unpack(r)}
table.remove(self.r, 1)
table.remove(self.r, 1)
--DIRECTION
if #self.r > 0 and self.r[1] ~= "link" then
self.dir = self.r[1]
table.remove(self.r, 1)
end
--POWER
if #self.r > 0 and self.r[1] ~= "link" then
self.open = (self.r[1] == "true")
table.remove(self.r, 1)
end
--FORCE CLOSE (for the padawans)
if #self.r > 0 and self.r[1] ~= "link" then
self.forceclose = (self.r[1] == "true")
table.remove(self.r, 1)
end
--PHYSICS STUFF
if self.dir == "hor" then
self.x = x-1
self.y = y-12/16
self.width = 32/16
self.height = 8/16
else
self.x = x-12/16
self.y = y-2
self.width = 8/16
self.height = 32/16
end
self.category = 25
self.mask = {true}
self.static = true
self.active = true
self.drawable = false
self.firstupdate = true
self:closeopen(self.open)
self.targetopen = self.open
end
function door:link()
while #self.r > 3 do
for j, w in pairs(outputs) do
for i, v in pairs(objects[w]) do
if tonumber(self.r[3]) == v.cox and tonumber(self.r[4]) == v.coy then
v:addoutput(self, self.r[2])
end
end
end
table.remove(self.r, 1)
table.remove(self.r, 1)
table.remove(self.r, 1)
table.remove(self.r, 1)
end
end
function door:update(dt)
if self.targetopen ~= self.open then
if self.forceclose then
self:closeopen(self.targetopen)
else
if #checkrect(self.x, self.y, self.width, self.height, {"exclude", self}) == 0 then
self:closeopen(self.targetopen)
end
end
end
if self.open then
if self.timer < 1 then
self.timer = self.timer + doorspeed*dt
if self.timer >= 1 then
self.timer = 1
self.active = false
updateranges()
end
end
else
if self.timer > 0 then
self.timer = self.timer - doorspeed*dt
if self.timer <= 0 then
self.timer = 0
end
end
end
end
function door:draw()
local ymod = 0
local rot = math.pi/2
if self.timer > 0.5 then
ymod = (self.timer - 0.5) * 2
else
rot = self.timer * math.pi
end
if self.dir == "hor" then
love.graphics.draw(doorpieceimg, math.floor((self.x+14/16-xscroll-ymod)*16*scale), (self.y-yscroll-4/16)*16*scale, math.pi*.5, scale, scale, 4, 0)
love.graphics.draw(doorpieceimg, math.floor((self.x+18/16-xscroll+ymod)*16*scale), (self.y-yscroll-4/16)*16*scale, math.pi*1.5, scale, scale, 4, 0)
love.graphics.draw(doorcenterimg, math.floor((self.x+16/16-xscroll-ymod)*16*scale), (self.y-yscroll-4/16)*16*scale, math.pi*.5-rot, scale, scale, 4, 2)
love.graphics.draw(doorcenterimg, math.floor((self.x+16/16-xscroll+ymod)*16*scale), (self.y-yscroll-4/16)*16*scale, math.pi*1.5-rot, scale, scale, 4, 2)
else
love.graphics.draw(doorpieceimg, math.floor((self.x+0.25-xscroll)*16*scale), (self.y-yscroll+6/16-ymod)*16*scale, math.pi, scale, scale, 4, 0)
love.graphics.draw(doorpieceimg, math.floor((self.x+0.25-xscroll)*16*scale), (self.y-yscroll+10/16+ymod)*16*scale, 0, scale, scale, 4, 0)
love.graphics.draw(doorcenterimg, math.floor((self.x+0.25-xscroll)*16*scale), (self.y-yscroll+8/16-ymod)*16*scale, rot, scale, scale, 4, 2)
love.graphics.draw(doorcenterimg, math.floor((self.x+0.25-xscroll)*16*scale), (self.y-yscroll+8/16+ymod)*16*scale, math.pi+rot, scale, scale, 4, 2)
end
end
function door:pushstuff()
local col = checkrect(self.x, self.y, self.width, self.height, "all")
for i = 1, #col, 2 do
local v = objects[col[i]][col[i+1]]
if self.dir == "ver" then
if v.speedx >= 0 then
v.x = self.x - v.width
else
v.x = self.x + self.width
end
elseif self.dir == "hor" then
if v.speedy >= 0 then
v.y = self.y - v.height
else
v.y = self.y + self.height
end
end
end
end
function door:input(t, input)
if input == "open" then
if t == "on" and self.input1state == "off" then
self.targetopen = not self.targetopen
elseif t == "off" and self.input1state == "on" then
self.targetopen = not self.targetopen
elseif t == "toggle" then
self.targetopen = not self.targetopen
end
self.input1state = t
end
end
function door:closeopen(open)
local prev = self.open
self.open = open
if self.open then
if self.timer == 1 then
self.active = false
end
else
self.active = true
self:pushstuff()
end
if self.open ~= prev then
updateranges()
end
end