-
Notifications
You must be signed in to change notification settings - Fork 17
/
portal.lua
186 lines (163 loc) · 5.59 KB
/
portal.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
178
179
180
181
182
183
184
185
186
portal = class("portal")
function portal:init(number, c1, c2)
self.number = number
self.portal1color = c1 or {60 / 255, 188 / 255, 252 / 255}
self.portal2color = c2 or {232 / 255, 130 / 255, 30 / 255}
self.animationtimer = 0
self.portalframe = 1
self.openscale = {0, 0}
self.x1, self.y1, self.facing1, self.x2, self.y2, self.facing2 = false, false, false, false, false, false
end
function portal:createportal(i, cox, coy, side, tendency)
if cox and tendency then
local otheri = 1
if i == 1 then
otheri = 2
end
moveoutportal(i)
--remove the portal temporarily so that it doesn't obstruct itself
local oldx, oldy, oldfacing
if i == 1 then
oldx, oldy, oldfacing = self.x1, self.y1, self.facing1
self.x1, self.y1 = false, false
else
oldx, oldy, oldfacing = self.x2, self.y2, self.facing2
self.x2, self.y2 = false, false
end
local newx, newy = getportalposition(i, cox, coy, side, tendency)
if newx and (newx ~= oldx or newy ~= oldy or side ~= oldfacing) then
if i == 1 then
self.x1 = newx
self.y1 = newy
self.facing1 = side
else
self.x2 = newx
self.y2 = newy
self.facing2 = side
end
--physics
--Recreate old hole
if oldfacing == "up" then
modifyportaltiles(oldx, oldy, 1, 0, self, i, "add")
elseif oldfacing == "down" then
modifyportaltiles(oldx, oldy, -1, 0, self, i, "add")
elseif oldfacing == "left" then
modifyportaltiles(oldx, oldy, 0, -1, self, i, "add")
elseif oldfacing == "right" then
modifyportaltiles(oldx, oldy, 0, 1, self, i, "add")
end
local otheri = 1
if i == 1 then
otheri = 2
end
if oldx == false then --Remove blocks from other portal
local x, y, side
if otheri == 1 then
side = self.facing1
x, y = self.x1, self.y1
else
side = self.facing2
x, y = self.x2, self.y2
end
if side == "up" then
modifyportaltiles(x, y, 1, 0, self, otheri, "remove")
elseif side == "down" then
modifyportaltiles(x, y, -1, 0, self, otheri, "remove")
elseif side == "left" then
modifyportaltiles(x, y, 0, -1, self, otheri, "remove")
elseif side == "right" then
modifyportaltiles(x, y, 0, 1, self, otheri, "remove")
end
end
if i == 1 then
playsound("portal1open")
else
playsound("portal2open")
end
modifyportalwalls()
updateranges()
self.openscale[i] = 0
else
--recreate the temporarily removed portal
self["x" .. i], self["y" .. i] = oldx, oldy
end
end
end
function portal:removeportal(i)
if self["x" .. i] then
moveoutportal(i)
local otheri = 1
if i == 1 then
otheri = 2
end
if self["facing" .. i] == "up" then
modifyportaltiles(self["x" .. i], self["y" .. i], 1, 0, self, i, "add")
elseif self["facing" .. i] == "down" then
modifyportaltiles(self["x" .. i], self["y" .. i], -1, 0, self, i, "add")
elseif self["facing" .. i] == "left" then
modifyportaltiles(self["x" .. i], self["y" .. i], 0, -1, self, i, "add")
elseif self["facing" .. i] == "right" then
modifyportaltiles(self["x" .. i], self["y" .. i], 0, 1, self, i, "add")
end
if self["x" .. otheri] then
if self["facing" .. otheri] == "up" then
modifyportaltiles(self["x" .. otheri], self["y" .. otheri], 1, 0, self, otheri, "add")
elseif self["facing" .. otheri] == "down" then
modifyportaltiles(self["x" .. otheri], self["y" .. otheri], -1, 0, self, otheri, "add")
elseif self["facing" .. otheri] == "left" then
modifyportaltiles(self["x" .. otheri], self["y" .. otheri], 0, -1, self, otheri, "add")
elseif self["facing" .. otheri] == "right" then
modifyportaltiles(self["x" .. otheri], self["y" .. otheri], 0, 1, self, otheri, "add")
end
end
self["x" .. i] = false
self["y" .. i] = false
self["facing" .. i] = false
modifyportalwalls()
for j = 1, 6 do
objects["portalwall"][self.number .. "-" .. i .. "-" .. j] = nil
end
updateranges()
end
end
function portal:update(dt)
self.animationtimer = self.animationtimer + dt
while self.animationtimer > portalanimationdelay do
self.animationtimer = 0
self.portalframe = self.portalframe + 1
if self.portalframe > portalanimationcount then
self.portalframe = 1
end
end
for i = 1, 2 do
self.openscale[i] = math.min(1, self.openscale[i]+dt*15)
end
end
function portal:draw()
for i = 1, 2 do
if self["x" .. i] ~= false then
--Standard values "up"
local rotation = 0
local offsetx, offsety = 16, -3
if self["facing" .. i] == "right" then
rotation = math.pi/2
offsetx, offsety = 11, 8
elseif self["facing" .. i] == "down" then
rotation = math.pi
offsetx, offsety = 0, 3
elseif self["facing" .. i] == "left" then
rotation = math.pi*1.5
offsetx, offsety = 5, -8
end
local glowalpha = 100
if self.x2 and self.x1 then
--portal glow
love.graphics.setColor(1, 1, 1, (80 - math.abs(self.portalframe-3)*10)/255)
love.graphics.draw(portalglowimg, math.floor(((self["x" .. i]-1-xscroll)*16+offsetx)*scale), math.floor(((self["y" .. i]-yscroll-1)*16+offsety)*scale), rotation, scale*self.openscale[i], scale, 16, 20)
love.graphics.setColor(1, 1, 1, 1)
end
love.graphics.setColor(unpack(self["portal" .. i .. "color"]))
love.graphics.draw(portalimg, portalquad[self.portalframe], math.floor(((self["x" .. i]-1-xscroll)*16+offsetx)*scale), math.floor(((self["y" .. i]-yscroll-1)*16+offsety)*scale), rotation, scale*self.openscale[i], scale, 16, 8)
end
end
end