-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
1603 lines (1380 loc) · 62 KB
/
main.lua
File metadata and controls
1603 lines (1380 loc) · 62 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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Love2D Game: Player and Tileset System
-- Love2D configuration (must be at the top)
function love.conf(t)
t.window.resizable = true
t.window.minwidth = 800
t.window.minheight = 600
t.window.fullscreen = false
t.window.vsync = 1
end
-- Player system
local player = {
x = 100, -- Player position in pixels
y = 100,
baseSize = 24, -- Base player size (will be scaled)
baseSpeed = 200, -- Base movement speed (will be scaled)
size = 24, -- Current scaled size
speed = 200, -- Current scaled speed
trail = {}, -- Particle trail
maxTrail = 20, -- Maximum trail particles
energy = 100, -- Player energy/health
maxEnergy = 100,
invulnerable = false, -- Invulnerability frames
invulnTime = 0,
invincible = false, -- Power-up invincibility
invincibleTime = 0, -- Time remaining for power-up
maxInvincibleTime = 10 -- 10 seconds of invincibility
}
-- Helper function to set player energy with automatic capping
local function setPlayerEnergy(value)
player.energy = math.max(0, math.min(value, player.maxEnergy))
end
-- Tileset system
local tileSize = 32 -- Size of each tile in pixels
local tileset = {
-- Different tile types with their colors
grass = {0.2, 0.8, 0.2}, -- Green
water = {0.2, 0.4, 1.0}, -- Blue
mountain = {0.6, 0.6, 0.6}, -- Light gray mountains
quicksand = {0.9, 0.7, 0.3}, -- Darker yellow/brown quicksand
cave = {0.1, 0.1, 0.1}, -- Black cave/tunnel
wall = {0.3, 0.3, 0.3}, -- Dark gray
empty = {0.1, 0.1, 0.1} -- Dark (transparent-ish)
}
-- MASSIVE OPEN WORLD TILEMAP (120x68 - 16:9 aspect ratio!)
local tilemap = {}
-- Generate procedural world
local function generateWorld()
local worldWidth = 120 -- Much wider for bigger world
local worldHeight = 68 -- Taller for bigger world (16:9 ratio maintained)
tilemap = {}
-- Initialize with grass
for y = 1, worldHeight do
tilemap[y] = {}
for x = 1, worldWidth do
tilemap[y][x] = "grass"
end
end
-- Add border walls
for i = 1, worldWidth do
tilemap[1][i] = "wall"
tilemap[worldHeight][i] = "wall"
end
for i = 1, worldHeight do
tilemap[i][1] = "wall"
tilemap[i][worldWidth] = "wall"
end
-- Add water features with connectivity
local waterBodies = {}
for i = 1, 15 do
local x, y = math.random(5, worldWidth-5), math.random(5, worldHeight-5)
local size = math.random(3, 6)
local waterBody = {}
for dy = -size, size do
for dx = -size, size do
local nx, ny = x + dx, y + dy
if nx > 1 and nx < worldWidth and ny > 1 and ny < worldHeight and math.sqrt(dx*dx + dy*dy) <= size then
tilemap[ny][nx] = "water"
table.insert(waterBody, {x = nx, y = ny})
end
end
end
if #waterBody > 0 then
table.insert(waterBodies, waterBody)
end
end
-- Ensure connectivity between water bodies
for i = 1, #waterBodies - 1 do
local body1 = waterBodies[i]
local body2 = waterBodies[i + 1]
if #body1 > 0 and #body2 > 0 then
local point1 = body1[math.random(#body1)]
local point2 = body2[math.random(#body2)]
-- Create a thin water connection
local dx = point2.x - point1.x
local dy = point2.y - point1.y
local steps = math.max(math.abs(dx), math.abs(dy))
for step = 0, steps do
local t = step / steps
local nx = math.floor(point1.x + dx * t + 0.5)
local ny = math.floor(point1.y + dy * t + 0.5)
if nx > 1 and nx < worldWidth and ny > 1 and ny < worldHeight then
tilemap[ny][nx] = "water"
end
end
end
end
-- Add mountain formations (larger and more imposing) - 20% less
for i = 1, 12 do -- Reduced from 15 to 12 (20% less)
local x, y = math.random(4, worldWidth-4), math.random(4, worldHeight-4)
local size = math.random(4, 8) -- Larger mountain ranges
for dy = -size, size do
for dx = -size, size do
local nx, ny = x + dx, y + dy
if nx > 1 and nx < worldWidth and ny > 1 and ny < worldHeight and math.sqrt(dx*dx + dy*dy) <= size and tilemap[ny][nx] ~= "water" then
tilemap[ny][nx] = "mountain"
end
end
end
end
-- Add smaller mountain peaks for variety - 20% less
for i = 1, 16 do -- Reduced from 20 to 16 (20% less)
local x, y = math.random(3, worldWidth-3), math.random(3, worldHeight-3)
local size = math.random(2, 4)
for dy = -size, size do
for dx = -size, size do
local nx, ny = x + dx, y + dy
if nx > 1 and nx < worldWidth and ny > 1 and ny < worldHeight and math.sqrt(dx*dx + dy*dy) <= size and tilemap[ny][nx] ~= "water" then
tilemap[ny][nx] = "mountain"
end
end
end
end
-- Add cave/tunnel systems through mountains
for i = 1, 8 do -- Create 8 cave systems
local startX, startY = math.random(5, worldWidth-5), math.random(5, worldHeight-5)
local endX, endY = math.random(5, worldWidth-5), math.random(5, worldHeight-5)
-- Create a winding tunnel between two points
local currentX, currentY = startX, startY
local steps = math.max(math.abs(endX - startX), math.abs(endY - startY)) + math.random(5, 15)
for step = 1, steps do
-- Only place cave if we're in mountain terrain
if tilemap[currentY] and tilemap[currentY][currentX] == "mountain" then
tilemap[currentY][currentX] = "cave"
-- Add some branching tunnels (narrow paths)
if math.random() < 0.3 then -- 30% chance for branch
local branchLength = math.random(2, 4)
local branchDirX = math.random(-1, 1)
local branchDirY = math.random(-1, 1)
for branch = 1, branchLength do
local branchX = currentX + branchDirX * branch
local branchY = currentY + branchDirY * branch
if branchX >= 1 and branchX <= worldWidth and branchY >= 1 and branchY <= worldHeight then
if tilemap[branchY] and tilemap[branchY][branchX] == "mountain" then
tilemap[branchY][branchX] = "cave"
end
end
end
end
end
-- Move towards end point with some randomness
if currentX < endX then
currentX = currentX + 1
elseif currentX > endX then
currentX = currentX - 1
end
if currentY < endY then
currentY = currentY + 1
elseif currentY > endY then
currentY = currentY - 1
end
-- Add some randomness to create winding paths
if math.random() < 0.2 then -- 20% chance to deviate
currentX = currentX + math.random(-1, 1)
currentY = currentY + math.random(-1, 1)
currentX = math.max(1, math.min(currentX, worldWidth))
currentY = math.max(1, math.min(currentY, worldHeight))
end
end
end
-- Add quicksand areas (dangerous terrain)
for i = 1, 20 do -- More quicksand areas in bigger world
local x, y = math.random(4, worldWidth-4), math.random(4, worldHeight-4)
local size = math.random(3, 8)
for dy = -size, size do
for dx = -size, size do
local nx, ny = x + dx, y + dy
if nx > 2 and nx < worldWidth-1 and ny > 2 and ny < worldHeight-1 and math.sqrt(dx*dx + dy*dy) <= size and tilemap[ny][nx] == "grass" then
tilemap[ny][nx] = "quicksand"
end
end
end
end
end
-- Calculate dynamic tilemap dimensions (will be set after world generation)
local mapRows = 68 -- Updated for bigger world
local mapCols = 120
-- Visual effects system
local effects = {
particles = {},
maxParticles = 100,
screenShake = 0,
screenShakeIntensity = 0,
glowIntensity = 0,
pulseTime = 0
}
-- Game mechanics
local collectibles = {}
local enemies = {}
local sharks = {}
local powerups = {}
local score = 0
local gameTime = 0
local isGameOver = false
local isVictory = false
local timeInWater = 0
local sharkSpawnTimer = 0
local powerupSpawnTimer = 0
-- Camera system
local camera = {
x = 0,
y = 0,
targetX = 0,
targetY = 0,
zoom = 1,
targetZoom = 1,
smoothness = 5
}
-- Generate collectibles
local function generateCollectibles()
collectibles = {}
local attempts = 0
local maxAttempts = 300
while #collectibles < 60 and attempts < maxAttempts do
local x = math.random(2, mapCols - 1)
local y = math.random(2, mapRows - 1)
if tilemap[y] and (tilemap[y][x] == "grass" or tilemap[y][x] == "quicksand" or tilemap[y][x] == "cave") then
table.insert(collectibles, {
x = x,
y = y,
collected = false,
pulse = math.random() * math.pi * 2,
size = math.random(6, 12),
glowIntensity = math.random() * 0.5 + 0.5
})
end
attempts = attempts + 1
end
end
-- Generate enemies
local function generateEnemies()
enemies = {}
local attempts = 0
local maxAttempts = 200
while #enemies < 20 and attempts < maxAttempts do
local x = math.random(3, mapCols - 2)
local y = math.random(3, mapRows - 2)
if tilemap[y] and (tilemap[y][x] == "grass" or tilemap[y][x] == "mountain" or tilemap[y][x] == "quicksand") then
-- Create enemy with different speeds (2 fast, rest slow)
local isFast = #enemies < 2 -- First 2 enemies are fast
local enemySpeed = isFast and math.random(1.2, 1.8) or math.random(0.2, 0.5)
table.insert(enemies, {
x = x,
y = y,
dirX = math.random(-1, 1),
dirY = math.random(-1, 1),
speed = enemySpeed,
size = math.random(12, 20),
pulse = math.random() * math.pi * 2,
attackCooldown = 0,
maxAttackCooldown = math.random(1, 3),
isFast = isFast,
searchTimer = 0, -- Timer for searching behavior
lastKnownPlayerX = 0,
lastKnownPlayerY = 0,
isSearching = false
})
end
attempts = attempts + 1
end
end
-- Generate sharks (spawn dynamically when player is in water)
local function generateSharks()
sharks = {}
-- Sharks spawn dynamically, not at world generation
end
-- Spawn a shark near the player when they're in water
local function spawnSharkNearPlayer()
if #sharks >= 5 then return end -- Max 5 sharks
local attempts = 0
local maxAttempts = 50
while attempts < maxAttempts do
local offsetX = math.random(-15, 15)
local offsetY = math.random(-15, 15)
local sharkX = (player.x / tileSize) + offsetX
local sharkY = (player.y / tileSize) + offsetY
-- Make sure shark spawns in water and within bounds
if sharkX > 1 and sharkX < mapCols and sharkY > 1 and sharkY < mapRows then
local tileX = math.floor(sharkX)
local tileY = math.floor(sharkY)
if tilemap[tileY] and tilemap[tileY][tileX] == "water" then
table.insert(sharks, {
x = sharkX,
y = sharkY,
dirX = math.random(-1, 1),
dirY = math.random(-1, 1),
speed = math.min(1.5, math.random(0.8, 1.5)), -- Faster than regular enemies, capped at 1.5
size = math.random(20, 30),
pulse = math.random() * math.pi * 2,
attackCooldown = 0,
maxAttackCooldown = 0.5,
timeAlive = 0,
maxLifetime = 10 -- Sharks disappear after 10 seconds if not in water
})
break
end
end
attempts = attempts + 1
end
end
-- Generate power-ups (invincibility stars)
local function generatePowerups()
powerups = {}
-- Power-ups spawn dynamically, not at world generation
end
-- Spawn a power-up randomly on the map
local function spawnPowerup()
if #powerups >= 2 then return end -- Max 2 power-ups
local attempts = 0
local maxAttempts = 100
while attempts < maxAttempts do
local x = math.random(3, mapCols - 2)
local y = math.random(3, mapRows - 2)
-- Only spawn on grass or cave (safe terrain)
if tilemap[y] and (tilemap[y][x] == "grass" or tilemap[y][x] == "cave") then
table.insert(powerups, {
x = x,
y = y,
collected = false,
pulse = math.random() * math.pi * 2,
size = 15,
glowIntensity = math.random() * 0.5 + 0.5,
rainbowPhase = math.random() * math.pi * 2
})
break
end
attempts = attempts + 1
end
end
-- Find the nearest safe spot (grass, sand, or water) to the given position
local function findNearestSafeSpot(x, y)
local maxSearchRadius = 10 -- Maximum distance to search
-- Check in expanding circles around the player
for radius = 1, maxSearchRadius do
for dx = -radius, radius do
for dy = -radius, radius do
-- Only check positions on the circle's edge
if math.abs(dx) == radius or math.abs(dy) == radius then
local checkX = x + dx
local checkY = y + dy
-- Make sure we're within map bounds
if checkX >= 1 and checkX <= mapCols and checkY >= 1 and checkY <= mapRows then
if tilemap[checkY] then
local tileType = tilemap[checkY][checkX]
-- Safe tiles: grass, quicksand, water, cave (but not mountains or walls)
if tileType == "grass" or tileType == "quicksand" or tileType == "water" or tileType == "cave" then
return checkX, checkY
end
end
end
end
end
end
end
-- If no safe spot found, return the original position (fallback)
return x, y
end
-- Particle system functions
local function addParticle(x, y, color, life, size, velocity)
table.insert(effects.particles, {
x = x,
y = y,
vx = velocity and velocity.x or (math.random(-50, 50)),
vy = velocity and velocity.y or (math.random(-50, 50)),
life = life,
maxLife = life,
size = size,
color = color,
alpha = 1
})
-- Limit particle count
if #effects.particles > effects.maxParticles then
table.remove(effects.particles, 1)
end
end
local function updateParticles(dt)
for i = #effects.particles, 1, -1 do
local p = effects.particles[i]
p.x = p.x + p.vx * dt
p.y = p.y + p.vy * dt
p.life = p.life - dt
p.alpha = p.life / p.maxLife
p.size = p.size * (0.98 + math.sin(p.life * 10) * 0.02)
if p.life <= 0 then
table.remove(effects.particles, i)
end
end
end
local function drawParticles()
for _, p in ipairs(effects.particles) do
love.graphics.setColor(p.color[1], p.color[2], p.color[3], p.alpha)
love.graphics.circle("fill", p.x, p.y, p.size)
end
end
-- Trail effect
local function updateTrail(dt)
-- Add new trail particle
table.insert(player.trail, {
x = player.x,
y = player.y,
life = 1.0,
size = player.size * 0.3
})
-- Update and remove old trail particles
for i = #player.trail, 1, -1 do
local t = player.trail[i]
t.life = t.life - dt * 2
t.size = t.size * 0.95
if t.life <= 0 or #player.trail > player.maxTrail then
table.remove(player.trail, i)
end
end
end
local function drawTrail()
for i, t in ipairs(player.trail) do
local alpha = t.life * 0.5
love.graphics.setColor(0.2, 0.8, 1.0, alpha)
love.graphics.circle("fill", t.x, t.y, t.size)
end
end
-- Screen shake effect
local function addScreenShake(intensity)
effects.screenShake = 1.0
effects.screenShakeIntensity = intensity
end
local function updateScreenShake(dt)
if effects.screenShake > 0 then
effects.screenShake = effects.screenShake - dt * 3
if effects.screenShake < 0 then
effects.screenShake = 0
end
end
end
function love.update(dt)
if isGameOver or isVictory then return end
-- Get window dimensions
local windowWidth = love.graphics.getWidth()
local windowHeight = love.graphics.getHeight()
-- Calculate dynamic tile size to fill entire window
local baseTileSize = math.min(windowWidth / mapCols, windowHeight / mapRows)
tileSize = baseTileSize
-- Scale player size and speed based on tile size
local scaleFactor = tileSize / 32
player.size = player.baseSize * scaleFactor
player.speed = player.baseSpeed * scaleFactor
-- Update game time and effects
gameTime = gameTime + dt
effects.pulseTime = effects.pulseTime + dt
effects.glowIntensity = 0.5 + math.sin(effects.pulseTime * 3) * 0.3
-- Update power-up spawning timer
powerupSpawnTimer = powerupSpawnTimer + dt
if powerupSpawnTimer >= 30.0 then -- Spawn power-up every 30 seconds
spawnPowerup()
powerupSpawnTimer = 0
end
-- Update invulnerability
if player.invulnerable then
player.invulnTime = player.invulnTime - dt
if player.invulnTime <= 0 then
player.invulnerable = false
end
end
-- Update invincibility power-up
if player.invincible then
player.invincibleTime = player.invincibleTime - dt
if player.invincibleTime <= 0 then
player.invincible = false
-- Check if player is stuck in impassable terrain when invincibility ends
local playerTileX = math.floor(player.x / tileSize) + 1
local playerTileY = math.floor(player.y / tileSize) + 1
if tilemap[playerTileY] and (tilemap[playerTileY][playerTileX] == "mountain" or tilemap[playerTileY][playerTileX] == "wall") then
-- Player is stuck in impassable terrain, find nearest safe spot
local safeX, safeY = findNearestSafeSpot(playerTileX, playerTileY)
if safeX and safeY then
player.x = (safeX - 1) * tileSize + tileSize / 2
player.y = (safeY - 1) * tileSize + tileSize / 2
addParticle(player.x, player.y, {1.0, 1.0, 0.0}, 1.0, 8) -- Yellow teleport effect
end
end
end
end
-- Store old position for collision detection
local oldX = player.x
local oldY = player.y
local wasMoving = false
-- Check if player is in water or quicksand
local playerTileX = math.floor(player.x / tileSize) + 1
local playerTileY = math.floor(player.y / tileSize) + 1
local isInWater = false
local isInQuicksand = false
if tilemap[playerTileY] and tilemap[playerTileY][playerTileX] == "water" then
isInWater = true
timeInWater = timeInWater + dt
sharkSpawnTimer = sharkSpawnTimer + dt
-- Spawn sharks if player has been in water too long
if sharkSpawnTimer >= 2.0 then -- Spawn shark every 2 seconds in water
spawnSharkNearPlayer()
sharkSpawnTimer = 0
end
elseif tilemap[playerTileY] and tilemap[playerTileY][playerTileX] == "quicksand" then
isInQuicksand = true
else
timeInWater = 0
sharkSpawnTimer = 0
end
-- Adjust movement speed and energy cost based on terrain
local currentSpeed = player.speed
local energyCost = 0
-- Check if invincible (ignores terrain effects)
if player.invincible then
currentSpeed = player.speed -- Full speed when invincible
energyCost = 0 -- No energy cost when invincible
elseif isInWater then
currentSpeed = player.speed * 0.4 -- 60% slower in water
energyCost = 15 * dt -- High energy cost for swimming
elseif isInQuicksand then
currentSpeed = player.speed * 0.3 -- 70% slower in quicksand (even slower than water)
energyCost = 25 * dt -- Higher energy cost than swimming
end
-- Player movement with terrain effects
if love.keyboard.isDown("left") then
player.x = player.x - currentSpeed * dt
wasMoving = true
if player.invincible then
-- Rainbow particles when invincible
local hue = (effects.pulseTime * 5) % (math.pi * 2)
local r = 0.5 + 0.5 * math.sin(hue)
local g = 0.5 + 0.5 * math.sin(hue + math.pi * 2/3)
local b = 0.5 + 0.5 * math.sin(hue + math.pi * 4/3)
addParticle(player.x + player.size/2, player.y, {r, g, b}, 1.0, 5, {x = 40, y = math.random(-15, 15)})
elseif isInWater then
addParticle(player.x + player.size/2, player.y, {0.1, 0.4, 0.8}, 0.8, 3, {x = 20, y = math.random(-10, 10)})
elseif isInQuicksand then
addParticle(player.x + player.size/2, player.y, {0.9, 0.7, 0.3}, 1.2, 4, {x = 15, y = math.random(-5, 5)})
else
addParticle(player.x + player.size/2, player.y, {0.2, 0.6, 1.0}, 0.5, 2, {x = 30, y = 0})
end
end
if love.keyboard.isDown("right") then
player.x = player.x + currentSpeed * dt
wasMoving = true
if player.invincible then
local hue = (effects.pulseTime * 5) % (math.pi * 2)
local r = 0.5 + 0.5 * math.sin(hue)
local g = 0.5 + 0.5 * math.sin(hue + math.pi * 2/3)
local b = 0.5 + 0.5 * math.sin(hue + math.pi * 4/3)
addParticle(player.x - player.size/2, player.y, {r, g, b}, 1.0, 5, {x = -40, y = math.random(-15, 15)})
elseif isInWater then
addParticle(player.x - player.size/2, player.y, {0.1, 0.4, 0.8}, 0.8, 3, {x = -20, y = math.random(-10, 10)})
elseif isInQuicksand then
addParticle(player.x - player.size/2, player.y, {0.9, 0.7, 0.3}, 1.2, 4, {x = -15, y = math.random(-5, 5)})
else
addParticle(player.x - player.size/2, player.y, {0.2, 0.6, 1.0}, 0.5, 2, {x = -30, y = 0})
end
end
if love.keyboard.isDown("up") then
player.y = player.y - currentSpeed * dt
wasMoving = true
if player.invincible then
local hue = (effects.pulseTime * 5) % (math.pi * 2)
local r = 0.5 + 0.5 * math.sin(hue)
local g = 0.5 + 0.5 * math.sin(hue + math.pi * 2/3)
local b = 0.5 + 0.5 * math.sin(hue + math.pi * 4/3)
addParticle(player.x, player.y + player.size/2, {r, g, b}, 1.0, 5, {x = math.random(-15, 15), y = 40})
elseif isInWater then
addParticle(player.x, player.y + player.size/2, {0.1, 0.4, 0.8}, 0.8, 3, {x = math.random(-10, 10), y = 20})
elseif isInQuicksand then
addParticle(player.x, player.y + player.size/2, {0.9, 0.7, 0.3}, 1.2, 4, {x = math.random(-5, 5), y = 15})
else
addParticle(player.x, player.y + player.size/2, {0.2, 0.6, 1.0}, 0.5, 2, {x = 0, y = 30})
end
end
if love.keyboard.isDown("down") then
player.y = player.y + currentSpeed * dt
wasMoving = true
if player.invincible then
local hue = (effects.pulseTime * 5) % (math.pi * 2)
local r = 0.5 + 0.5 * math.sin(hue)
local g = 0.5 + 0.5 * math.sin(hue + math.pi * 2/3)
local b = 0.5 + 0.5 * math.sin(hue + math.pi * 4/3)
addParticle(player.x, player.y - player.size/2, {r, g, b}, 1.0, 5, {x = math.random(-15, 15), y = -40})
elseif isInWater then
addParticle(player.x, player.y - player.size/2, {0.1, 0.4, 0.8}, 0.8, 3, {x = math.random(-10, 10), y = -20})
elseif isInQuicksand then
addParticle(player.x, player.y - player.size/2, {0.9, 0.7, 0.3}, 1.2, 4, {x = math.random(-5, 5), y = -15})
else
addParticle(player.x, player.y - player.size/2, {0.2, 0.6, 1.0}, 0.5, 2, {x = 0, y = -30})
end
end
-- Apply energy cost for swimming or quicksand (unless invincible)
if (isInWater or isInQuicksand) and wasMoving and not player.invincible then
player.energy = player.energy - energyCost
if player.energy < 0 then
player.energy = 0
isGameOver = true
addScreenShake(15)
end
end
-- Update trail when moving
if wasMoving then
updateTrail(dt)
end
-- Keep player within tilemap bounds and check for mountain collision
local mapWidth = mapCols * tileSize
local mapHeight = mapRows * tileSize
local offsetX = (windowWidth - mapWidth) / 2
local offsetY = (windowHeight - mapHeight) / 2
-- Check for collision with impassable terrain (mountains and walls) - unless invincible
local newPlayerTileX = math.floor(player.x / tileSize) + 1
local newPlayerTileY = math.floor(player.y / tileSize) + 1
if not player.invincible then
-- Check if player hit impassable terrain or is outside bounds
local hitImpassable = false
if newPlayerTileX < 1 or newPlayerTileX > mapCols or newPlayerTileY < 1 or newPlayerTileY > mapRows then
hitImpassable = true -- Outside world bounds (border)
elseif tilemap[newPlayerTileY] and (tilemap[newPlayerTileY][newPlayerTileX] == "mountain" or tilemap[newPlayerTileY][newPlayerTileX] == "wall") then
hitImpassable = true -- Hit mountain or wall
end
if hitImpassable then
-- Player hit impassable terrain, revert to old position
player.x = oldX
player.y = oldY
addParticle(player.x, player.y, {0.8, 0.8, 0.8}, 0.5, 4) -- Gray impact particle
else
-- Normal boundary checking (but stay within the tilemap)
local minX = offsetX + tileSize + player.size/2 -- Inside first walkable tile
local maxX = offsetX + (mapCols - 1) * tileSize - player.size/2 -- Inside last walkable tile
local minY = offsetY + tileSize + player.size/2 -- Inside first walkable tile
local maxY = offsetY + (mapRows - 1) * tileSize - player.size/2 -- Inside last walkable tile
player.x = math.max(minX, math.min(player.x, maxX))
player.y = math.max(minY, math.min(player.y, maxY))
end
else
-- When invincible, only check world bounds (can go through mountains/walls)
local minX = offsetX + player.size/2
local maxX = offsetX + mapCols * tileSize - player.size/2
local minY = offsetY + player.size/2
local maxY = offsetY + mapRows * tileSize - player.size/2
player.x = math.max(minX, math.min(player.x, maxX))
player.y = math.max(minY, math.min(player.y, maxY))
end
-- Update camera to follow player
camera.targetX = player.x - windowWidth / 2
camera.targetY = player.y - windowHeight / 2
camera.x = camera.x + (camera.targetX - camera.x) * dt * camera.smoothness
camera.y = camera.y + (camera.targetY - camera.y) * dt * camera.smoothness
-- Update enemies
for _, enemy in ipairs(enemies) do
enemy.pulse = enemy.pulse + dt * 2
enemy.searchTimer = enemy.searchTimer + dt
-- Check if player is in a cave (enemies can't see through caves)
local playerTileX = math.floor(player.x / tileSize) + 1
local playerTileY = math.floor(player.y / tileSize) + 1
local playerInCave = tilemap[playerTileY] and tilemap[playerTileY][playerTileX] == "cave"
if playerInCave then
-- Player is in cave - enemies can't see them
if not enemy.isSearching then
-- Start searching behavior
enemy.isSearching = true
enemy.searchTimer = 0
enemy.lastKnownPlayerX = player.x
enemy.lastKnownPlayerY = player.y
end
-- Search behavior: move to last known player position, then wander
if enemy.searchTimer < 3.0 then
-- Move towards last known player position for 3 seconds
local dx = (enemy.lastKnownPlayerX - (offsetX + enemy.x * tileSize)) / tileSize
local dy = (enemy.lastKnownPlayerY - (offsetY + enemy.y * tileSize)) / tileSize
local dist = math.sqrt(dx*dx + dy*dy)
if dist > 0 then
enemy.dirX = dx / dist
enemy.dirY = dy / dist
end
else
-- After 3 seconds, start wandering randomly
if enemy.searchTimer > 5.0 then
enemy.searchTimer = 0
enemy.isSearching = false
end
-- Random wandering
enemy.dirX = enemy.dirX + (math.random() - 0.5) * dt * 2
enemy.dirY = enemy.dirY + (math.random() - 0.5) * dt * 2
local dirLength = math.sqrt(enemy.dirX*enemy.dirX + enemy.dirY*enemy.dirY)
if dirLength > 0 then
enemy.dirX = enemy.dirX / dirLength
enemy.dirY = enemy.dirY / dirLength
end
end
else
-- Player is visible - normal chase behavior
enemy.isSearching = false
enemy.searchTimer = 0
local dx = (player.x - (offsetX + enemy.x * tileSize)) / tileSize
local dy = (player.y - (offsetY + enemy.y * tileSize)) / tileSize
local dist = math.sqrt(dx*dx + dy*dy)
if dist > 0 then
enemy.dirX = dx / dist
enemy.dirY = dy / dist
end
end
-- Move enemy (but not through caves)
local newEnemyX = enemy.x + enemy.dirX * enemy.speed * dt
local newEnemyY = enemy.y + enemy.dirY * enemy.speed * dt
-- Check if enemy would move into a cave (blocked)
local enemyTileX = math.floor(newEnemyX)
local enemyTileY = math.floor(newEnemyY)
if tilemap[enemyTileY] and tilemap[enemyTileY][enemyTileX] == "cave" then
-- Enemy blocked by cave, try to move around it
-- Try alternative directions
if math.random() < 0.5 then
enemy.dirX = -enemy.dirX
else
enemy.dirY = -enemy.dirY
end
else
-- Move normally
enemy.x = newEnemyX
enemy.y = newEnemyY
end
-- Keep enemy in bounds (enemies can move through mountains but not caves)
enemy.x = math.max(1, math.min(enemy.x, mapCols - 2))
enemy.y = math.max(1, math.min(enemy.y, mapRows - 2))
-- Check collision with player
local enemyScreenX = enemy.x * tileSize
local enemyScreenY = enemy.y * tileSize
local distance = math.sqrt((player.x - enemyScreenX)^2 + (player.y - enemyScreenY)^2)
if distance < (player.size + enemy.size) / 2 and not player.invulnerable and not player.invincible and not playerInCave then
player.energy = player.energy - 20
player.invulnerable = true
player.invulnTime = 1.0
addScreenShake(5)
addParticle(enemyScreenX, enemyScreenY, {1.0, 0.2, 0.2}, 1.0, 8)
if player.energy <= 0 then
isGameOver = true
addScreenShake(10)
end
end
end
-- Update sharks
for i = #sharks, 1, -1 do
local shark = sharks[i]
shark.pulse = shark.pulse + dt * 3
shark.timeAlive = shark.timeAlive + dt
-- Remove sharks that are too old or not in water
local sharkTileX = math.floor(shark.x)
local sharkTileY = math.floor(shark.y)
local sharkInWater = tilemap[sharkTileY] and tilemap[sharkTileY][sharkTileX] == "water"
if shark.timeAlive > shark.maxLifetime or not sharkInWater then
table.remove(sharks, i)
else
-- Shark AI: move towards player aggressively
local dx = (player.x - (offsetX + shark.x * tileSize)) / tileSize
local dy = (player.y - (offsetY + shark.y * tileSize)) / tileSize
local dist = math.sqrt(dx*dx + dy*dy)
if dist > 0 then
shark.dirX = dx / dist
shark.dirY = dy / dist
end
-- Move shark
shark.x = shark.x + shark.dirX * shark.speed * dt
shark.y = shark.y + shark.dirY * shark.speed * dt
-- Keep shark in bounds
shark.x = math.max(1, math.min(shark.x, mapCols - 2))
shark.y = math.max(1, math.min(shark.y, mapRows - 2))
-- Check collision with player
local sharkScreenX = shark.x * tileSize
local sharkScreenY = shark.y * tileSize
local distance = math.sqrt((player.x - sharkScreenX)^2 + (player.y - sharkScreenY)^2)
if distance < (player.size + shark.size) / 2 and not player.invincible then
-- Shark attack is instant death!
isGameOver = true
addScreenShake(20)
addParticle(sharkScreenX, sharkScreenY, {1.0, 0.1, 0.1}, 2.0, 15)
addParticle(player.x, player.y, {1.0, 0.0, 0.0}, 1.5, 12)
end
end
end
-- Check collectible collisions
for _, collectible in ipairs(collectibles) do
if not collectible.collected then
collectible.pulse = collectible.pulse + dt * 3
local collectibleScreenX = collectible.x * tileSize
local collectibleScreenY = collectible.y * tileSize
local distance = math.sqrt((player.x - collectibleScreenX)^2 + (player.y - collectibleScreenY)^2)
if distance < player.size then
collectible.collected = true
score = score + 100
setPlayerEnergy(player.energy + 10)
addParticle(collectibleScreenX, collectibleScreenY, {1.0, 1.0, 0.2}, 1.0, 6)
addScreenShake(2)
-- Check for victory condition
local remainingOrbs = 0
for _, orb in ipairs(collectibles) do
if not orb.collected then
remainingOrbs = remainingOrbs + 1
end
end
if remainingOrbs == 0 then
isVictory = true
addScreenShake(15)
-- Victory particles
for i = 1, 20 do
addParticle(player.x + math.random(-50, 50), player.y + math.random(-50, 50),
{1.0, 1.0, 0.2}, 2.0, 8,
{x = math.random(-100, 100), y = math.random(-100, 100)})
end
end
end
end
end
-- Check power-up collisions
for _, powerup in ipairs(powerups) do
if not powerup.collected then
powerup.pulse = powerup.pulse + dt * 4
powerup.rainbowPhase = powerup.rainbowPhase + dt * 6
local powerupScreenX = powerup.x * tileSize
local powerupScreenY = powerup.y * tileSize
local distance = math.sqrt((player.x - powerupScreenX)^2 + (player.y - powerupScreenY)^2)
if distance < player.size then
powerup.collected = true
player.invincible = true
player.invincibleTime = player.maxInvincibleTime
score = score + 500 -- Bonus points for power-up
addParticle(powerupScreenX, powerupScreenY, {1.0, 1.0, 1.0}, 2.0, 12)
addScreenShake(8)
end
end
end
-- Update particle effects
updateParticles(dt)
updateScreenShake(dt)
-- Regenerate energy slowly
if player.energy < player.maxEnergy then
player.energy = player.energy + dt * 5
if player.energy > player.maxEnergy then
setPlayerEnergy(player.maxEnergy)
end
end
end
-- Function to draw a single tile
local function drawTile(tileType, x, y)
if tileset[tileType] then
local color = tileset[tileType]
if tileType == "mountain" then
-- Special mountain rendering with height effect
love.graphics.setColor(color[1], color[2], color[3])
love.graphics.rectangle("fill", x, y, tileSize, tileSize)
-- Add mountain peak effect
love.graphics.setColor(color[1] + 0.2, color[2] + 0.2, color[3] + 0.2)
love.graphics.rectangle("fill", x + tileSize/4, y + tileSize/4, tileSize/2, tileSize/2)
-- Add dark shadow for depth
love.graphics.setColor(color[1] - 0.2, color[2] - 0.2, color[3] - 0.2)
love.graphics.rectangle("fill", x, y + tileSize*0.7, tileSize, tileSize*0.3)
-- Mountain border
love.graphics.setColor(0.4, 0.4, 0.4)