Last active
June 28, 2022 08:54
-
-
Save Achie72/6518e747c5c24c168f0051968c8bd361 to your computer and use it in GitHub Desktop.
Camera Shake and Global Pause Effects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Created for my Ko-fi article about my game | |
-- dev journey with my retro platformer | |
-- https://ko-fi.com/post/ENGHUN-Unnamed-Project--Making-Abilites-Look-B-X8X2DH3X2 | |
-- Code is In progress, heavily not optimized | |
-- and geared towards readability until PICO_8 | |
-- token counts come into play | |
-- Update Cycle for the game, notice that we | |
-- do not update core gameplay things if we | |
-- have pause time set up in pickups | |
-- see: https://gist.github.com/Achie72/b528c49987b2550d37c9d45a5ccbcaad | |
function update_game() | |
-- Game pause effect is we have time remaining | |
if not (global.pauseTime > time()) then | |
control_player() | |
update_fireballs() | |
update_enemies() | |
check_stomp() | |
update_scores() | |
update_pickup() | |
end | |
update_particles() | |
if player.life <= 0 then | |
-- set our state to endscreen | |
global.state = 2 | |
global.endScreenMessage = endScreenMessages[flr(rnd(#endScreenMessages)+1)] | |
global.transition = true | |
global.trans.x = player.x-64 | |
global.trans.y = player.y-64 | |
end | |
-- Check for camera shake | |
if time() > global.camShakeTime then | |
global.camShake = false | |
end | |
end | |
-- camera shake code | |
function screen_shake(x, y) | |
local fade = 0.95 | |
local offset_x=16-rnd(32) | |
local offset_y=16-rnd(32) | |
offset_x*=global.offset | |
offset_y*=global.offset | |
camera(x+offset_x,y+offset_y) | |
global.offset*=fade | |
if global.offset<0.05 then | |
global.offset=0 | |
end | |
end | |
-- game draw code, took out what is not needed | |
-- for the camera setup, but this handels | |
-- basically drawing everything in the game as well | |
function draw_game() | |
-- camera setup | |
local target_pos = { | |
x = player.x-60, | |
y = 0 | |
} | |
local camera_treshold = 16 | |
local position_minimum = { | |
x = 0, | |
y = 0 | |
} | |
local position_maximum = { | |
x = 128*8-64, | |
y = 64+182 | |
} | |
-- calculate out of buffer corrigations | |
if (cam.x + camera_treshold) < target_pos.x then | |
cam.x += min(target_pos.x - (cam.x + camera_treshold), 4) | |
end | |
if (cam.x - camera_treshold) > target_pos.x then | |
cam.x += min(target_pos.x - (cam.x - camera_treshold), 4) | |
end | |
if (cam.y + camera_treshold) < target_pos.y then | |
cam.y += min(target_pos.y - (cam.y + camera_treshold), 4) | |
end | |
if (cam.y - camera_treshold) > target_pos.y then | |
cam.y += min(target_pos.y - (cam.y - camera_treshold), 4) | |
end | |
if cam.x < position_minimum.x then | |
cam.x = position_minimum.x | |
end | |
if cam.x > position_maximum.x then | |
cam.x = position_maximum.x | |
end | |
if cam.y < position_minimum.y then | |
cam.y = position_minimum.y | |
end | |
if cam.y > position_maximum.y then | |
cam.y = position_maximum.y | |
end | |
if global.camShake then | |
screen_shake(cam.x, cam.y) | |
else | |
camera(cam.x, cam.y) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment