-
Notifications
You must be signed in to change notification settings - Fork 33
/
gamestate.lua
277 lines (249 loc) · 5.87 KB
/
gamestate.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
levels = {
{
offset = 0,
width = 96,
height = 16,
camera_mode = 1,
music = 38,
},
{
offset = 343,
width = 32,
height = 32,
camera_mode = 2,
music = 36,
fogmode = 1,
clouds = 0,
columns = 1
},
{
offset = 679,
width = 128,
height = 22,
camera_mode = 3,
camera_barriers_x = { 38 },
camera_barrier_y = 6,
music = 2,
title = "trailhead"
},
{
offset = 1313,
width = 128,
height = 32,
camera_mode = 4,
music = 2,
title = "glacial caves",
pal = function() pal(2, 12) pal(5, 2) end,
columns = 1
},
{
offset = 2411,
width = 128,
height = 16,
camera_mode = 5,
music = 2,
title = "golden valley",
pal = function() pal(2, 14) pal(5, 2) end,
bg = 13,
clouds = 15,
fogmode = 2
},
{
offset = 2645,
width = 128,
height = 16,
camera_mode = 6,
camera_barriers_x = { 105 },
music = 2,
pal = function() pal(2, 14) pal(5, 2) end,
bg = 13,
clouds = 15,
fogmode = 2
},
{
offset = 2880,
width = 128,
height = 16,
camera_mode = 7,
music = 2,
pal = function() pal(2, 12) pal(5, 2) end,
bg = 13,
clouds = 7,
fogmode = 2,
},
{
offset = 3079,
width = 16,
height = 62,
title = "destination",
camera_mode = 8,
music = 2,
pal = function() pal(2, 1) pal(7, 11) end,
bg = 15,
clouds = 7,
fogmode = 2,
right_edge = true
}
}
camera_x_barrier = function(tile_x, px, py)
local bx = tile_x * 8
if px < bx - 8 then
camera_target_x = min(camera_target_x, bx - 128)
elseif px > bx + 8 then
camera_target_x = max(camera_target_x, bx)
end
end
c_offset = 0
c_flag = false
camera_modes = {
-- 1: Intro
function(px, py)
if px < 42 then
camera_target_x = 0
else
camera_target_x = max(40, min(level.width * 8 - 128, px - 48))
end
end,
-- 2: Intro 2
function(px, py)
if px < 120 then
camera_target_x = 0
elseif px > 136 then
camera_target_x = 128
else
camera_target_x = px - 64
end
camera_target_y = max(min(level.height * 8 - 128, py - 64))
end,
-- 3: Level 1
function(px, py)
camera_target_x = max(min(level.width * 8 - 128, px - 56))
for i,b in ipairs(level.camera_barriers_x) do
camera_x_barrier(b, px, py)
end
if py < level.camera_barrier_y * 8 + 3 then
camera_target_y = 0
else
camera_target_y = level.camera_barrier_y * 8
end
end,
-- 4: Level 2
function(px, py)
if px % 128 > 8 and px % 128 < 120 then
px = flr(px / 128) * 128 + 64
end
if py % 128 > 4 and py % 128 < 124 then
py = flr(py / 128) * 128 + 64
end
camera_target_x = max(min(level.width * 8 - 128, px - 64))
camera_target_y = max(min(level.height * 8 - 128, py - 64))
end,
-- 5: Level 3-1
function(px, py)
camera_target_x = max(min(level.width * 8 - 128, px - 32))
end,
-- 6: Level 3-2
function(px, py)
if px > 848 then
c_offset = 48
elseif px < 704 then
c_flag = false
c_offset = 32
elseif px > 808 then
c_flag = true
c_offset = 96
end
camera_target_x = max(min(level.width * 8 - 128, px - c_offset))
for i,b in ipairs(level.camera_barriers_x) do
camera_x_barrier(b, px, py)
end
if c_flag then
camera_target_x = max(camera_target_x, 672)
end
end,
--7: Level 3-3
function (px, py)
if px > 420 then
if px < 436 then
c_offset = 32 + px - 420
elseif px > 808 then
c_offset = 48 - min(16, px - 808)
else
c_offset = 48
end
else
c_offset = 32
end
camera_target_x = max(0, min(level.width * 8 - 128, px - c_offset))
end,
--8: End
function (px, py)
camera_target_y = max(0, min(level.height * 8 - 128, py - 32))
end
}
snap_camera = function()
camera_x = camera_target_x
camera_y = camera_target_y
camera(camera_x, camera_y)
end
tile_y = function(py)
return max(0, min(flr(py / 8), level.height - 1))
end
function goto_level(index)
-- set level
level = levels[index]
level_index = index
level_checkpoint = nil
if level.title and not standalone then
level_intro = 60
end
if level_index == 2 then
psfx(17, 8, 16)
end
-- load into ram
local function vget(x, y) return peek(0x4300 + x + y * level.width) end
local function vset(x, y, v) return poke(0x4300 + x + y * level.width, v) end
px9_decomp(0, 0, 0x1000 + level.offset, vget, vset)
-- start music
if current_music != level.music and level.music then
current_music = level.music
music(level.music)
end
-- load level contents
restart_level()
end
function next_level()
level_index += 1
if standalone then
load("celeste2/" .. level_index .. ".p8")
else
goto_level(level_index)
end
end
function restart_level()
camera_x = 0
camera_y = 0
camera_target_x = 0
camera_target_y = 0
objects = {}
infade = 0
have_grapple = level_index > 2
sfx_timer = 0
for i = 0,level.width-1 do
for j = 0,level.height-1 do
local t = types[tile_at(i, j)]
if t and not collected[id(i, j)] and (not level_checkpoint or t != player) then
create(t, i * 8, j * 8)
end
end
end
end
-- gets the tile at the given location from the loaded level
function tile_at(x, y)
if x < 0 or y < 0 or x >= level.width or y >= level.height then return 0 end
if standalone then
return mget(x, y)
else
return peek(0x4300 + x + y * level.width)
end
end