-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
438 lines (382 loc) · 15.8 KB
/
main.lua
File metadata and controls
438 lines (382 loc) · 15.8 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
local mod = RegisterMod("Item Info", 1)
-- Highest valid Item ID in this game's version
local NUM_COLLECTIBLES = CollectibleType.NUM_COLLECTIBLES - 1
local NUM_TRINKETS = TrinketType.NUM_TRINKETS - 1
local MAX_LINES = 12
-- TODO: Text on menus to denote what menu is currently being viewed
-- TODO: Change buttons to prompt menus?
-- Table holding Active/Passive/Trinkets in Isaac's inventory
local collectedItems = {}
-- Table holding ID of Active/Passive/Trinkets in Isaac's inventory
local collectedItemIDs = {
collectibles = {},
trinkets = {},
}
-- Table holding sprite of every item owned
local collectedItemSprites = {}
local floorItems = {}
local floorItemIDs = {}
local floorItemSprites = {}
local heldMenuOpen = false
local floorMenuOpen = false
local menuCursorPos = Vector(1, 1)
-- Offset 0 shows items 1-16, 1 shows items 17-32, etc.
local menuItemsOffset = 0
local descTextOffset = 0
local descTextOffsetStep = 4
local itemMenu = Sprite()
itemMenu:Load("gfx/ui/itemmenu.anm2", true)
local itemMenuAttrs = {
-- Where to create the menu
pos = Vector(30, 150),
scale = Vector(0.7, 0.7),
-- Where to begin drawing items ON the menu
origin = Vector(-12, 105),
spacing = Vector(55, 55),
-- Number of columns and rows to display items in
layout = Vector(4, 4)
}
local textAttrs = {
header = {
font = "font/upheaval.fnt",
color = KColor(1, 1, 1, 1),
offset = Vector(0, 0),
pos = Vector(240, 45),
scale = Vector(1, 1),
boxWidth = 200,
center = true,
writer = Font(),
},
body = {
font = "font/pftempestasevencondensed.fnt",
color = KColor(1, 1, 1, 1),
offset = Vector(0, 0),
pos = Vector(240, 75),
scale = Vector(1, 1),
boxWidth = 0,
center = false,
writer = Font(),
},
}
textAttrs.header.writer:Load(textAttrs.header.font)
textAttrs.body.writer:Load(textAttrs.body.font)
-- Debug strings
local str1 = ''
local str2 = ''
local str3 = ''
-- Check if table contains value
local function contains(tbl, val)
for _, v in pairs(tbl) do
if v == val then
return true
end
end
return false
end
local function updateHeldTrinkets()
local player = Isaac.GetPlayer(0)
local index = 1
while index <= #collectedItems do
-- This can be the ID of a trinket
if collectedItems[index]:IsTrinket() then
-- Isaac no longer has this trinket
if not player:HasTrinket(collectedItemIDs[index]) then
collectedItemIDs.trinkets[collectedItemIDs[index]] = nil
table.remove(collectedItems, index)
table.remove(collectedItemIDs, index)
table.remove(collectedItemSprites, index)
index = index - 1
end
end
index = index + 1
end
for i=1, NUM_TRINKETS do
if player:HasTrinket(i) then
local trinket = Isaac.GetItemConfig():GetTrinket(i)
if not collectedItemIDs.trinkets[trinket.ID] then
table.insert(collectedItems, trinket)
table.insert(collectedItemIDs, trinket.ID)
table.insert(collectedItemSprites, Sprite())
collectedItemIDs.trinkets[trinket.ID] = trinket.ID
end
end
end
end
-- Update collectedItemIDs list with ID of every currently held collectible (Actives/Passives)
local function updateHeldCollectibles()
local player = Isaac.GetPlayer(0)
local index = 1
-- Remove items the player no longer has
while index <= #collectedItemIDs do
-- This can be the ID of a collectible
if collectedItems[index]:IsCollectible() then
-- Isaac no longer has this collectible
if not player:HasCollectible(collectedItemIDs[index]) then
-- Do not increment on same index removed, otherwise we skip an item
collectedItemIDs.collectibles[collectedItemIDs[index]] = nil
table.remove(collectedItemIDs, index)
table.remove(collectedItemSprites, index)
table.remove(collectedItems, index)
index = index - 1
end
end
index = index + 1
end
-- Get player's active & passive items
for i=1, NUM_COLLECTIBLES do
if player:HasCollectible(i) then
local item = Isaac.GetItemConfig():GetCollectible(i)
if not collectedItemIDs.collectibles[item.ID] then
table.insert(collectedItemIDs, item.ID)
table.insert(collectedItemSprites, Sprite())
table.insert(collectedItems, item)
collectedItemIDs.collectibles[item.ID] = item.ID
end
end
end
end
-- settings is a table with the same properties as textAttrs.header/body
local function renderText(str, settings --[[table]])
settings.writer:DrawStringScaled(str, settings.pos.X + settings.offset.X, settings.pos.Y + settings.offset.Y, settings.scale.X, settings.scale.Y, settings.color, settings.boxWidth, settings.center)
end
-- Takes number of lines the item description contains
local function handleTextScroll(lines --[[int]])
if not Game():IsPaused() then
if Input.IsActionTriggered(ButtonAction.ACTION_UP, 0) then
if descTextOffset ~= 0 then
descTextOffset = descTextOffset - MAX_LINES
end
end
if Input.IsActionTriggered(ButtonAction.ACTION_DOWN, 0) then
if (descTextOffset + 1) * MAX_LINES <= lines then
descTextOffset = descTextOffset + MAX_LINES
end
end
end
end
-- Handles displaying all relevant text of selected item
local function renderSelectedItemText(collection --[[table]], collectionIDs --[[table]])
-- Index of selected item in collectedItemIDs
local index = (menuCursorPos.Y - 1) * itemMenuAttrs.layout.X + menuCursorPos.X + menuItemsOffset
-- local item = require('resources/items/collectibles/'..tostring(collectedItemIDs[index])..'.lua')
local item
-- TODO: I would love to make this not look like eye vomit. More like the declaration above
if collection[index] then
if collection[index]:IsCollectible() then
item = require('resources/items/collectibles/'..tostring(collectionIDs[index])..'.lua')
else
item = require('resources/items/trinkets/'..tostring(collectionIDs[index])..'.lua')
end
else
item = require('resources/items/collectibles/nil.lua')
end
renderText(item.title, textAttrs.header)
local yOffset = 16
local limit = MAX_LINES + descTextOffset
for i=1 + descTextOffset, limit do
if item.description[i] then
-- Add ellipses if there is more text to be scrolled through than rendered on screen
if i == limit and limit < #item.description then
renderText(item.description[i]..'...', textAttrs.body)
else
renderText(item.description[i], textAttrs.body)
end
textAttrs.body.offset.Y = yOffset + textAttrs.body.offset.Y
else
break
end
end
handleTextScroll(#item.description)
textAttrs.body.offset.Y = 0
end
-- Render the collected item's icons to the menu screen
-- Offset is number denoting starting position in collectedItemSprites table
-- Used to display items when there are more than can fit on one screen
local function renderMenuItems(collection --[[table]], collectionSprites --[[table]], offset --[[int]])
-- Ensure player has at least one item to render
if next(collection) then
local itemPosInMenu
local index
local item
for i=1, itemMenuAttrs.layout.Y do
for j=1, itemMenuAttrs.layout.X do
index = (i - 1) * itemMenuAttrs.layout.X + j + offset
-- Render a player's item if available
if collectionSprites[index] then
item = collectionSprites[index]
item:Load("gfx/ui/menuitem.anm2", true)
item:ReplaceSpritesheet(0, collection[index].GfxFileName)
item:LoadGraphics()
item:SetFrame("Idle", 0)
itemPosInMenu = Vector(itemMenuAttrs.spacing.X * j + itemMenuAttrs.origin.X,
itemMenuAttrs.spacing.Y * i + itemMenuAttrs.origin.Y)
item:RenderLayer(0, Isaac.WorldToRenderPosition(itemPosInMenu))
item:SetOverlayRenderPriority(true)
-- If no items available, render nothing
else
item:Load("gfx/ui/menuitem.anm2", true)
item:SetFrame("Active", 0)
item:LoadGraphics()
itemPosInMenu = Vector(itemMenuAttrs.spacing.X * j + itemMenuAttrs.origin.X,
itemMenuAttrs.spacing.Y * i + itemMenuAttrs.origin.Y)
-- Layer 0 is transparency layer, 1 is gray bg, 2 is square brackets
-- item:RenderLayer(0, Isaac.WorldToRenderPosition(itemPosInMenu))
item:RenderLayer(0, Isaac.WorldToRenderPosition(itemPosInMenu))
end
end
end
end
end
-- Handle moving the cursor in menu and updating related data
local function handleCursorMovement()
-- The game is not actually "paused", the player's inputs are essentially hijacked though
-- Basically, you can still be attacked by enemies while this menu is open
if not Game():IsPaused() then
-- Move cursor down
if Input.IsActionTriggered(ButtonAction.ACTION_MENUDOWN, 0) then
menuCursorPos.Y = menuCursorPos.Y + 1
-- Moving beyond bounds current menu page
if menuCursorPos.Y > itemMenuAttrs.layout.Y then
-- There are enough items to render another page
if #collectedItemIDs > menuItemsOffset + (itemMenuAttrs.layout.X * itemMenuAttrs.layout.Y) then
menuItemsOffset = menuItemsOffset + (itemMenuAttrs.layout.X * itemMenuAttrs.layout.Y)
-- Not enough items to render another page, wrap around to initial page
else
menuItemsOffset = 0
end
menuCursorPos.Y = 1
end
descTextOffset = 0
end
-- Move cursor up
if Input.IsActionTriggered(ButtonAction.ACTION_MENUUP, 0) then
menuCursorPos.Y = menuCursorPos.Y - 1
if menuCursorPos.Y < 1 then
-- There is a previous "page"/layout to move to
if menuItemsOffset > 0 then
menuItemsOffset = menuItemsOffset - (itemMenuAttrs.layout.X * itemMenuAttrs.layout.Y)
-- If there are more items than can be shown on first page, move to last page
else
-- Offset + 16 gives the index of every item that will be rendered with that offset
-- Ex: ceil(49 items / (4 * 4)) = 4. 4 - 1 = 3. 3 * 4 * 4 = 48, exactly the offset the 49th item should have
menuItemsOffset = math.ceil(#collectedItemIDs / (itemMenuAttrs.layout.X * itemMenuAttrs.layout.Y) - 1) * itemMenuAttrs.layout.X * itemMenuAttrs.layout.Y
end
menuCursorPos.Y = itemMenuAttrs.layout.Y
end
descTextOffset = 0
end
-- Move cursor right
if Input.IsActionTriggered(ButtonAction.ACTION_MENURIGHT, 0) then
menuCursorPos.X = menuCursorPos.X + 1
if menuCursorPos.X > itemMenuAttrs.layout.X then
menuCursorPos.X = 1
end
descTextOffset = 0
end
-- Move cursor left
if Input.IsActionTriggered(ButtonAction.ACTION_MENULEFT, 0) then
menuCursorPos.X = menuCursorPos.X - 1
if menuCursorPos.X < 1 then
menuCursorPos.X = itemMenuAttrs.layout.X
end
descTextOffset = 0
end
end
end
local function renderMenuCursor()
local cursor = Sprite()
cursor:Load("gfx/ui/menuitem.anm2", true)
cursor:SetFrame("Idle", 0)
cursor:LoadGraphics()
local cursorDrawPos = Vector(itemMenuAttrs.spacing.X * menuCursorPos.X + itemMenuAttrs.origin.X,
itemMenuAttrs.spacing.Y * menuCursorPos.Y + itemMenuAttrs.origin.Y)
-- cursor:SetOverlayRenderPriority(true)
cursor:RenderLayer(2, Isaac.WorldToRenderPosition(cursorDrawPos))
end
function mod:onRender()
if Input.IsButtonTriggered(Keyboard.KEY_J, 0) and not Game():IsPaused() and not floorMenuOpen then
updateHeldCollectibles()
updateHeldTrinkets()
heldMenuOpen = not heldMenuOpen
-- Reset cursor to beginning when menu is closed
menuCursorPos = Vector(1, 1)
menuItemsOffset = 0
descTextOffset = 0
end
if Input.IsButtonTriggered(Keyboard.KEY_N, 0) and not Game():IsPaused() and not heldMenuOpen then
-- TODO: This is pretty damn ugly too
for _, v in ipairs(Isaac.GetRoomEntities()) do
-- It is something we can pickup
if v.Type == EntityType.ENTITY_PICKUP then
-- It is a collectible
if v.Variant == PickupVariant.PICKUP_COLLECTIBLE then
local item = Isaac.GetItemConfig():GetCollectible(v.SubType)
table.insert(floorItems, item)
table.insert(floorItemIDs, item.ID)
table.insert(floorItemSprites, Sprite())
-- It is a trinket
elseif v.Variant == PickupVariant.PICKUP_TRINKET then
local item = Isaac.GetItemConfig():GetTrinket(v.SubType)
table.insert(floorItems, item)
table.insert(floorItemIDs, item.ID)
table.insert(floorItemSprites, Sprite())
end
end
end
floorMenuOpen = not floorMenuOpen
menuCursorPos = Vector(1, 1)
menuItemsOffset = 0
descTextOffset = 0
if not floorMenuOpen then
floorItems = {}
floorItemIDs = {}
floorItemSprites = {}
end
end
if heldMenuOpen then
-- Close menu
if Input.IsActionTriggered(ButtonAction.ACTION_MENUBACK, 0) then
heldMenuOpen = false
menuCursorPos = Vector(1, 1)
menuItemsOffset = 0
end
-- Create menu that items will be drawn on upon
itemMenu:SetFrame("Idle", 0)
itemMenu:RenderLayer(0, Isaac.WorldToRenderPosition(itemMenuAttrs.pos))
renderMenuItems(collectedItems, collectedItemSprites, menuItemsOffset)
renderSelectedItemText(collectedItems, collectedItemIDs)
handleCursorMovement()
renderMenuCursor()
elseif floorMenuOpen then
if Input.IsActionTriggered(ButtonAction.ACTION_MENUBACK, 0) then
floorMenuOpen = false
menuCursorPos = Vector(1, 1)
menuItemsOffset = 0
end
-- Create menu that items will be drawn on upon
itemMenu:SetFrame("Idle", 0)
itemMenu:RenderLayer(0, Isaac.WorldToRenderPosition(itemMenuAttrs.pos))
renderMenuItems(floorItems, floorItemSprites, menuItemsOffset)
renderSelectedItemText(floorItems, floorItemIDs)
handleCursorMovement()
renderMenuCursor()
end
end
function mod:onInput(entity, hook, button)
if heldMenuOpen and entity ~= nil then
if hook == InputHook.GET_ACTION_VALUE then
return 0
else
return false
end
end
end
function mod:debugText()
Isaac.RenderText(str1, 75, 25, 255, 0, 0, 255)
Isaac.RenderText(str2, 75, 50, 0, 255, 0, 255)
Isaac.RenderText(str3, 75, 75, 0, 0, 255, 255)
end
-- Callbacks
mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.onRender)
mod:AddCallback(ModCallbacks.MC_POST_RENDER, mod.debugText)
mod:AddCallback(ModCallbacks.MC_INPUT_ACTION, mod.onInput)