-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
682 lines (600 loc) · 22.4 KB
/
Copy pathmain.lua
File metadata and controls
682 lines (600 loc) · 22.4 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
-- Required Libraries
require("sprites")
require("map")
require("particles")
require("items")
-- Global Variables
local camera = require("libraries/camera")
local cam
local camSpeed = 350
local miningSrc, breakSrc, musicSrc
local spriteToBlock = 50 / 32
local chestOpen = nil
local fullscreenX, fullscreenY
local particleTriggered = false
local pickaxes = {}
local pick
local cx, cy, cw, ch
local exitButtonSize = 16
local chestMenuScale = 3
local chestMenuX, chestMenuY
local canMine = true
local currentPick = pickaxes.flintPick
local currentPickFrame = 1
local pickSwinging = false
local loadedChunks = {}
local renderDistance = 1000
local inventory = {}
local dynamiteThrown = false
local currentDynamiteFrame = 1
local dynamiteScale = 1
local explosionScale = 1
local explosionTimer = 0
-- Load Function
function love.load()
cam = camera()
love.graphics.setDefaultFilter("nearest", "nearest")
-- Load audio sources
miningSrc = love.audio.newSource("Assets/mining.wav", "stream")
breakSrc = love.audio.newSource("Assets/break.wav", "stream")
musicSrc = love.audio.newSource("Assets/music.mp3", "stream")
musicSrc:setLooping(true)
musicSrc:setVolume(0.1)
musicSrc:play()
-- Initialize canvas and shader
canvas = love.graphics.newCanvas(love.graphics.getWidth(), love.graphics.getHeight())
shaderCode = love.filesystem.read("shaders/shader.glsl")
shader = love.graphics.newShader(shaderCode)
screenshot = love.graphics.newCanvas(love.graphics.getWidth(), love.graphics.getHeight())
-- Set fullscreen button position
fullscreenX = love.graphics.getWidth() - 16
fullscreenY = 0
-- Initialize game components
initializePickaxes()
initializeChestMenu()
initializeInventory(6)
generateMap(20, 20)
end
-- Handle window resize
function love.resize(w, h)
screenshot = love.graphics.newCanvas(w, h)
end
-- Initialize Pickaxes
function initializePickaxes()
pickaxes = {
flintPick = {
idle = flintPickIdle,
swing = flintPickSwing,
final = flintPickFinal,
damage = 1,
frames = flintPickFrames
},
ironPick = {
idle = ironPickIdle,
swing = ironPickSwing,
final = ironPickFinal,
damage = 2.5,
frames = ironPickFrames
},
goldPick = {
idle = goldPickIdle,
swing = goldPickSwing,
final = goldPickFinal,
damage = 5,
frames = goldPickFrames
},
diamondPick = {
idle = diamondPickIdle,
swing = diamondPickSwing,
final = diamondPickFinal,
damage = 7.5,
frames = diamondPickFrames
},
amethystPick = {
idle = amethystPickIdle,
swing = amethystPickSwing,
final = amethystPickFinal,
damage = 10,
frames = amethystPickFrames
},
forrestitePick = {
idle = forrestitePickIdle,
swing = forrestitePickSwing,
final = forrestitePickFinal,
damage = 15,
frames = forrestitePickFrames
}
}
pick = {
type = pickaxes.flintPick,
x = 100,
y = 100
}
currentPickSprite = pick.type.idle
end
-- Initialize Chest Menu
function initializeChestMenu()
cx, cy, cw, ch = chestMenu:getViewport()
chestMenuX = (love.graphics.getWidth() / 2) - ((cw * chestMenuScale) / 2)
chestMenuY = (love.graphics.getHeight() / 2) - ((ch * chestMenuScale) / 2)
end
-- Initialize Inventory
function initializeInventory(number)
for i = 1, number do
local slot = {
item = nil,
amount = 0
}
table.insert(inventory, slot)
end
end
-- Draw Function
function love.draw()
-- Draw the entire scene to the canvas
love.graphics.setCanvas(canvas)
love.graphics.clear()
drawBackground()
cam:attach()
drawMap()
drawParticles()
cam:detach()
-- HUD/Overlay elements drawn after shader
drawPick(pick.x, pick.y)
drawDynamite(targetX, targetY)
drawExplosion(targetX, targetY)
drawInventory()
love.graphics.draw(spriteSheet, fullscreenIcon, fullscreenX, fullscreenY)
if chestOpen ~= nil then
drawChest(chestOpen)
end
-- Apply shader effect
love.graphics.setCanvas() -- Back to main screen
love.graphics.setShader(shader)
love.graphics.draw(canvas)
love.graphics.setShader() -- Turn off the shader
drawBorderedText(love.timer.getFPS(), 0, 0)
end
-- Draw Map
function drawMap()
local camX, camY = cam:position()
-- Clear the loadedChunks table at the start of each frame
loadedChunks = {}
-- Load chunks within the render distance
for chunkIndex, chunk in ipairs(chunks) do
local chunkCenterX = chunk.x
local chunkCenterY = chunk.y
-- Check if the chunk is within the render distance
if math.abs(chunkCenterX - camX) < renderDistance and math.abs(chunkCenterY - camY) < renderDistance then
table.insert(loadedChunks, chunk)
end
end
-- Render loaded chunks
for chunkIndex, chunk in ipairs(loadedChunks) do
for blockIndex, block in ipairs(chunk.blocks) do
if block.blockType then
love.graphics.draw(spriteSheet, block.blockType.sprite, block.x, block.y, 0, spriteToBlock,
spriteToBlock)
drawBlockDamage(block)
end
end
end
end
-- Draw Background
function drawBackground()
local parallaxSpeeds = {0.1, 0.2, 0.4, 0.7, 1.2}
local parallaxImages = {parallax5, parallax4, parallax3, parallax2, parallax1}
for i, image in ipairs(parallaxImages) do
local speed = parallaxSpeeds[i]
local scale = love.graphics.getHeight() / image:getHeight() * 1.5
local imageWidth = image:getWidth() * scale
local imageHeight = image:getHeight() * scale
local camX, camY = cam:position()
local offsetX = (camX * speed / 10) % imageWidth
local offsetY = (camY * speed / 10) % imageHeight
for x = -1, math.ceil(love.graphics.getWidth() / imageWidth) do
for y = -1, math.ceil(love.graphics.getHeight() / imageHeight) do
love.graphics.draw(image, x * imageWidth - offsetX, y * imageHeight - offsetY, 0, scale, scale)
end
end
end
end
-- Draw Pick
function drawPick(x, y)
love.graphics.draw(spriteSheet, pick.type.frames[math.floor(currentPickFrame)], x - 15, y - 15)
end
-- Draw Dynamite
function drawDynamite(x, y)
if dynamiteThrown then
x,y = cam:cameraCoords(x, y)
love.graphics.draw(spriteSheet, dynamiteFrames[math.floor(currentDynamiteFrame)], x, y, 0, dynamiteScale,
dynamiteScale)
end
end
function drawExplosion(x, y)
if explosion then
-- Get explosion sprite dimensions
local ex, ey, ew, eh = explosionSprite:getViewport()
x,y = cam:cameraCoords(x, y)
-- Calculate centered position
local drawX = x - (ew * explosionScale) / 2
local drawY = y - (eh * explosionScale) / 2
love.graphics.draw(
spriteSheet,
explosionSprite,
drawX, -- Already centered via math above
drawY,
0,
explosionScale,
explosionScale
)
end
end
-- Draw Block Damage
function drawBlockDamage(block)
if block.blockType.type == "mineable" then
if block.health < (9.9 / 10) * block.blockType.durability and block.health >= (6.5 / 10) *
block.blockType.durability then
love.graphics.draw(spriteSheet, break1, block.x, block.y, 0, spriteToBlock, spriteToBlock)
elseif block.health < (6.5 / 10) * block.blockType.durability and block.health >= (3 / 10) *
block.blockType.durability then
love.graphics.draw(spriteSheet, break2, block.x, block.y, 0, spriteToBlock, spriteToBlock)
elseif block.health < (3 / 10) * block.blockType.durability and block.health >= 0 then
love.graphics.draw(spriteSheet, break3, block.x, block.y, 0, spriteToBlock, spriteToBlock)
end
end
end
-- Draw Particles
function drawParticles()
love.graphics.draw(rockParticleSystem)
love.graphics.draw(ironParticleSystem)
love.graphics.draw(goldParticleSystem)
love.graphics.draw(diamondParticleSystem)
love.graphics.draw(amethystParticleSystem)
love.graphics.draw(forrestiteParticleSystem)
end
-- Draw Inventory
function drawInventory()
local slotMargin = 5
local slotSize = 40
local inventoryScale = 2
for slotIndex, slot in ipairs(inventory) do
local slotX = slotIndex * slotSize - slotSize + slotMargin * slotIndex
local slotY = love.graphics.getHeight() - slotSize - slotMargin
love.graphics.setColor(0.7, 0.5, 0.3)
love.graphics.rectangle("fill", slotX, slotY, slotSize, slotSize)
if slot.item then
love.graphics.setColor(1, 1, 1)
local quad = slot.item.sprite
local _, _, quadWidth, quadHeight = quad:getViewport()
local itemWidth = quadWidth * inventoryScale
local itemHeight = quadHeight * inventoryScale
local itemX = slotX + (slotSize - itemWidth) / 2
local itemY = slotY + (slotSize - itemHeight) / 2
love.graphics.draw(spriteSheet, quad, itemX, itemY, 0, inventoryScale, inventoryScale)
end
love.graphics.setColor(0.5, 0.3, 0.1)
love.graphics.setLineWidth(3)
love.graphics.rectangle("line", slotX, slotY, slotSize, slotSize)
drawBorderedText(slot.amount, slotX, slotY)
end
end
-- Draw Chest
function drawChest(chest)
local itemMargin = 11 * chestMenuScale
local itemSpacing = 31 * chestMenuScale
local chestColumns = 7
local chestRows = 4
love.graphics.draw(spriteSheet, chestMenu, chestMenuX, chestMenuY, 0, chestMenuScale, chestMenuScale)
love.graphics.draw(spriteSheet, exitIcon, chestMenuX - exitButtonSize, chestMenuY - exitButtonSize, 0,
chestMenuScale, chestMenuScale)
if not chest then
print("Error: chest is nil")
return
end
for itemIndex, item in ipairs(chest.items) do
item.x = chestMenuX + itemIndex * itemMargin
local row = 1
if 0 < itemIndex and itemIndex <= 7 then
item.x = chestMenuX + itemMargin + ((itemIndex - 1) * itemSpacing)
row = 0
elseif 7 < itemIndex and itemIndex <= 14 then
item.x = chestMenuX + ((itemIndex - 1) - 7) * itemSpacing + itemMargin
row = 1
elseif 14 < itemIndex and itemIndex <= 21 then
item.x = chestMenuX + ((itemIndex - 1) - 14) * itemSpacing + itemMargin
row = 2
elseif 21 < itemIndex and itemIndex <= 28 then
item.x = chestMenuX + ((itemIndex - 1) - 21) * itemSpacing + itemMargin
row = 2
end
rowY = row * itemSpacing + itemMargin
item.y = row * itemSpacing + itemMargin + chestMenuY
love.graphics.draw(spriteSheet, item.itemType.sprite, item.x, item.y, 0, chestMenuScale, chestMenuScale)
drawBorderedText(item.amount, item.x, chestMenuY + rowY)
end
end
-- Draw Bordered Text
function drawBorderedText(text, x, y)
love.graphics.setColor(0, 0, 0)
love.graphics.print(text, x, y + 1)
love.graphics.print(text, x + 1, y)
love.graphics.print(text, x + 2, y + 1)
love.graphics.print(text, x + 1, y + 2)
love.graphics.setColor(1, 1, 1)
love.graphics.print(text, x + 1, y + 1)
end
-- Update Function
function love.update(dt)
worldMouseX, worldMouseY = cam:worldCoords(love.mouse.getX(), love.mouse.getY())
shader:send("time", love.timer.getTime())
shader:send("textureSize", {love.graphics.getWidth(), love.graphics.getHeight()})
updateParticles(dt)
updatePick(dt)
updateCamera(dt)
updateDynamite(dt)
updateExplosion(dt)
if love.keyboard.isDown("q") then
if not dynamiteThrown and not explosion and canMine then
for chunkIndex, chunk in ipairs(loadedChunks) do
for blockIndex, block in ipairs(chunk.blocks) do
if block.blockType then
if checkCollision(block.x, block.y, worldMouseX, worldMouseY, BLOCK_WIDTH, BLOCK_HEIGHT) and
block.blockType.type == "mineable" then
dynamiteThrown = true
particleTriggered = false
targetX, targetY = block.x + BLOCK_WIDTH/2, block.y + BLOCK_HEIGHT/2
end
end
end
end
end
end
end
-- Update Particles
function updateParticles(dt)
rockParticleSystem:update(dt)
ironParticleSystem:update(dt)
goldParticleSystem:update(dt)
diamondParticleSystem:update(dt)
amethystParticleSystem:update(dt)
forrestiteParticleSystem:update(dt)
end
-- Update Pick
function updatePick(dt)
pick.x, pick.y = love.mouse.getX(), love.mouse.getY()
local worldPickX, worldPickY = cam:worldCoords(pick.x, pick.y)
if love.mouse.isDown(1) then
if not pickSwinging and canMine then
pickSwinging = true
particleTriggered = false
for chunkIndex, chunk in ipairs(loadedChunks) do
for blockIndex, block in ipairs(chunk.blocks) do
if block.blockType then
if checkCollision(block.x, block.y, worldPickX, worldPickY, BLOCK_WIDTH, BLOCK_HEIGHT) and
block.blockType.type == "mineable" then
if not particleTriggered then
local particleX, particleY = block.x + BLOCK_WIDTH / 2, block.y + BLOCK_HEIGHT / 2
rockParticleSystem:setPosition(particleX, particleY)
rockParticleSystem:emit(1)
particleTriggered = true
end
if not block.health then
block.health = block.blockType.durability
end
if block.health <= 0 then
breakBlock(block, blockIndex, chunk)
else
block.health = block.health - pick.type.damage
miningSrc:play()
end
end
end
end
end
end
end
if pickSwinging then
currentPickFrame = currentPickFrame + dt * 10
if currentPickFrame > 4 then
pickSwinging = false
currentPickFrame = 1
end
end
end
-- Kablamo!
function explodeDynamite()
-- Find the target block's grid position
local targetGridX = math.floor(targetX / BLOCK_WIDTH) * BLOCK_WIDTH
local targetGridY = math.floor(targetY / BLOCK_HEIGHT) * BLOCK_HEIGHT
-- Define the explosion radius (in blocks)
local explosionRadius = 2 -- Adjust this value to change the size of the explosion
local radiusSquared = explosionRadius * explosionRadius * BLOCK_WIDTH * BLOCK_HEIGHT -- Squared radius for distance comparison
-- Break the target block and adjacent blocks within the circular radius
for dx = -explosionRadius, explosionRadius do
for dy = -explosionRadius, explosionRadius do
-- Calculate the position of the block to check
local checkX = targetGridX + dx * BLOCK_WIDTH
local checkY = targetGridY + dy * BLOCK_HEIGHT
-- Calculate the squared distance from the explosion center
local distanceSquared = (checkX - targetGridX) * (checkX - targetGridX) + (checkY - targetGridY) * (checkY - targetGridY)
-- Check if the block is within the circular radius
if distanceSquared <= radiusSquared then
-- Check all loaded chunks for a block at (checkX, checkY)
for _, chunk in ipairs(loadedChunks) do
for i = #chunk.blocks, 1, -1 do -- Iterate backwards to safely remove
local block = chunk.blocks[i]
if block.x == checkX and block.y == checkY then
breakBlock(block, i, chunk)
break -- No need to check further
end
end
end
end
end
end
explosion = true
end
-- Update Dynamite
function updateDynamite(dt)
if dynamiteThrown then
currentDynamiteFrame = currentDynamiteFrame + dt * 15
if currentDynamiteFrame <= 2 then
dynamiteScale = 15
elseif currentDynamiteFrame <= 3 then
dynamiteScale = 10
elseif currentDynamiteFrame <= 4 then
dynamiteScale = 5
elseif currentDynamiteFrame <= 5 then
dynamiteScale = 2.5
elseif currentDynamiteFrame <= 6 then
dynamiteScale = 1
else
currentDynamiteFrame = 1
dynamiteScale = 15
explodeDynamite()
dynamiteThrown = false
end
end
end
function updateExplosion(dt)
if explosion then
explosionTimer = explosionTimer + dt * 200
explosionScale = explosionScale + 0.25
if explosionScale >= 3 then
explosionScale = 3
end
if explosionTimer >= 150 then
explosion = false
explosionTimer = 0
explosionScale = 0
end
end
end
-- Update Camera
function updateCamera(dt)
local moveX, moveY = 0, 0
local camX, camY = cam:position()
local screenWidth, screenHeight = love.graphics.getWidth(), love.graphics.getHeight()
local worldWidth, worldHeight = chunksX * (CHUNK_WIDTH * BLOCK_WIDTH), chunksY * (CHUNK_HEIGHT * BLOCK_HEIGHT)
if love.keyboard.isDown("right") or love.keyboard.isDown("d") then
if camX + screenWidth / 2 < worldWidth then
moveX = moveX + camSpeed * dt
end
end
if love.keyboard.isDown("left") or love.keyboard.isDown("a") then
if camX - screenWidth / 2 > 0 then
moveX = moveX - camSpeed * dt
end
end
if love.keyboard.isDown("down") or love.keyboard.isDown("s") then
if camY + screenHeight / 2 < worldHeight then
moveY = moveY + camSpeed * dt
end
end
if love.keyboard.isDown("up") or love.keyboard.isDown("w") then
if camY - screenHeight / 2 > 0 then
moveY = moveY - camSpeed * dt
end
end
cam:move(moveX, moveY)
end
-- Input Functions
function love.mousepressed(mx, my, button)
if button == 1 then
-- Left click: Toggle fullscreen or close chest
if checkCollision(fullscreenX, fullscreenY, mx, my, 16, 16) then
toggleFullscreen()
elseif chestOpen and checkCollision(chestMenuX, chestMenuY, mx, my, 16 * chestMenuScale, 16 * chestMenuScale) then
chestOpen = nil
canMine = true
end
elseif button == 2 then
-- Right click: Open chest
for chunkIndex, chunk in ipairs(loadedChunks) do
for blockIndex, block in ipairs(chunk.blocks) do
-- Check if the block is a chest
if block.blockType == blockTypes.chest then
-- Convert block world coordinates to screen coordinates
local camBlockX, camBlockY = cam:cameraCoords(block.x, block.y)
-- Check if the mouse is over the chest
if checkCollision(camBlockX, camBlockY, mx, my, BLOCK_WIDTH, BLOCK_HEIGHT) then
chestOpen = block
canMine = false
break -- Exit the loop after opening the chest
end
end
end
end
end
end
-- Handle Key Presses
function love.keypressed(key)
if not pickSwinging then
if key == "1" then
pick.type = pickaxes.flintPick
elseif key == "2" then
pick.type = pickaxes.ironPick
elseif key == "3" then
pick.type = pickaxes.goldPick
elseif key == "4" then
pick.type = pickaxes.diamondPick
elseif key == "5" then
pick.type = pickaxes.amethystPick
elseif key == "6" then
pick.type = pickaxes.forrestitePick
end
currentPickSprite = pick.type.idle
end
end
-- Utility Functions
function checkCollision(x, y, pickX, pickY, width, height)
return pickX >= x and pickX <= x + width and pickY >= y and pickY <= y + height
end
-- Toggle Fullscreen
function toggleFullscreen()
if love.window.getFullscreen() then
love.window.setFullscreen(false)
else
love.window.setFullscreen(true, "exclusive")
end
end
-- Break Block
function breakBlock(block, blockIndex, chunk)
if block.blockType.type == "mineable" then
table.remove(chunk.blocks, blockIndex)
breakSrc:play()
miningSrc:stop()
local particleX, particleY = block.x + BLOCK_WIDTH / 2, block.y + BLOCK_HEIGHT / 2
rockParticleSystem:setPosition(particleX, particleY)
rockParticleSystem:emit(5)
if block.blockType == blockTypes.iron then
ironParticleSystem:setPosition(particleX, particleY)
ironParticleSystem:emit(3)
elseif block.blockType == blockTypes.gold then
goldParticleSystem:setPosition(particleX, particleY)
goldParticleSystem:emit(3)
elseif block.blockType == blockTypes.diamonds then
diamondParticleSystem:setPosition(particleX, particleY)
diamondParticleSystem:emit(3)
elseif block.blockType == blockTypes.amethyst then
amethystParticleSystem:setPosition(particleX, particleY)
amethystParticleSystem:emit(3)
elseif block.blockType == blockTypes.forrestite then
forrestiteParticleSystem:setPosition(particleX, particleY)
forrestiteParticleSystem:emit(3)
end
local slotFilled = false
for slotIndex, slot in ipairs(inventory) do
if not slotFilled then
if slot.item == block.blockType.item then
slotFilled = true
slot.amount = slot.amount + 1
elseif slot.item == nil then
slotFilled = true
slot.item = block.blockType.item
slot.amount = slot.amount + 1
end
end
end
end
end