4
0

v0.3 refact round
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-12-07 22:39:22 +01:00
parent fd4441d694
commit 0e0815d761

View File

@@ -3,134 +3,85 @@
-- desc: Life of a programmer in the Matrix -- desc: Life of a programmer in the Matrix
-- site: http://teletype.hu -- site: http://teletype.hu
-- license: MIT License -- license: MIT License
-- version: 0.1 -- version: 0.3
-- script: lua -- script: lua
-- Game constants --------------------------------------------------------------------------------
SCREEN_WIDTH = 240 -- Game Configuration
SCREEN_HEIGHT = 136 --------------------------------------------------------------------------------
local Config = {
screen = {
width = 240,
height = 136
},
colors = {
black = 0,
light_grey = 13,
dark_grey = 14,
green = 6,
npc = 8
},
player = {
w = 8,
h = 8,
start_x = 120,
start_y = 128,
},
physics = {
gravity = 0.5,
jump_power = -5,
move_speed = 1.5,
max_jumps = 2,
},
timing = {
splash_duration = 120 -- 2 seconds at 60fps
}
}
-- Colors --------------------------------------------------------------------------------
COLOR_BLACK = 0 -- Game States
COLOR_LIGHT_GREY = 13 --------------------------------------------------------------------------------
COLOR_DARK_GREY = 14 local GAME_STATE_SPLASH = 0
COLOR_GREEN = 6 local GAME_STATE_MENU = 1
COLOR_NPC = 8 local GAME_STATE_GAME = 2
local GAME_STATE_DIALOG = 3
-- Game state --------------------------------------------------------------------------------
STATE_SPLASH = 0 -- Modules
STATE_MENU = 1 --------------------------------------------------------------------------------
STATE_GAME = 2 local Splash = {}
STATE_DIALOG = 3 local Menu = {}
local Game = {}
local UI = {}
local Input = {}
-- Player constants --------------------------------------------------------------------------------
PLAYER_WIDTH = 8 -- Game State
PLAYER_HEIGHT = 8 --------------------------------------------------------------------------------
PLAYER_START_X = 120 local State = {
PLAYER_START_Y = 128 game_state = GAME_STATE_SPLASH,
current_screen = 1,
-- Ground constants dialog_text = "",
GROUND_X = 0 splash_timer = Config.timing.splash_duration,
GROUND_Y = 136 player = {
GROUND_W = 240 x = Config.player.start_x,
GROUND_H = 8 y = Config.player.start_y,
w = Config.player.w,
-- Physics constants h = Config.player.h,
GRAVITY = 0.5
JUMP_POWER = -5
MOVE_SPEED = 1.5
MAX_JUMPS = 2
-- Global variables (initialized)
local gameState = STATE_SPLASH
local currentScreen = 1
local dialog_text = ""
local splash_timer = 120 -- 2 seconds at 60fps
-- Player properties
local player = {
x = PLAYER_START_X,
y = PLAYER_START_Y,
w = PLAYER_WIDTH,
h = PLAYER_HEIGHT,
vx = 0, vx = 0,
vy = 0, vy = 0,
jumps = 0 jumps = 0
} },
ground = {
-- Ground properties x = 0,
local ground = { y = Config.screen.height,
x = GROUND_X, w = Config.screen.width,
y = GROUND_Y, h = 8
w = GROUND_W, },
h = GROUND_H menu_items = {"Play", "Exit"},
} selected_menu_item = 1,
-- Menu properties
local menuItems = {"Play", "Exit"}
local selectedMenuItem = 1
local function draw_splash()
cls(COLOR_BLACK)
print("Mr. Anderson's", 78, 60, COLOR_LIGHT_GREY)
print("Addventure", 90, 70, COLOR_LIGHT_GREY)
end
local function update_splash()
splash_timer = splash_timer - 1
if splash_timer <= 0 then
gameState = STATE_MENU
end
end
local function draw_top_bar(title)
rect(0, 0, SCREEN_WIDTH, 10, COLOR_BLACK)
print(title, 3, 2, COLOR_LIGHT_GREY)
end
local function draw_menu()
cls(COLOR_LIGHT_GREY)
draw_top_bar("Main Menu")
for i, item in ipairs(menuItems) do
local color = COLOR_DARK_GREY
if i == selectedMenuItem then
color = COLOR_GREEN
end
print(item, 108, 70 + (i-1)*10, color)
end
end
local function update_menu()
if btnp(0) then -- Up
selectedMenuItem = selectedMenuItem - 1
if selectedMenuItem < 1 then
selectedMenuItem = #menuItems
end
elseif btnp(1) then -- Down
selectedMenuItem = selectedMenuItem + 1
if selectedMenuItem > #menuItems then
selectedMenuItem = 1
end
end
if btnp(4) or btnp(5) then -- A or B button
if selectedMenuItem == 1 then -- Play
-- Reset player state and screen for a new game
player.x = PLAYER_START_X
player.y = PLAYER_START_Y
player.vx = 0
player.vy = 0
player.jumps = 0
currentScreen = 1
gameState = STATE_GAME
elseif selectedMenuItem == 2 then -- Exit
exit()
end
end
end
-- Screen data -- Screen data
local screens = { screens = {
{ -- Screen 1 { -- Screen 1
name = "Screen 1", name = "Screen 1",
platforms = { platforms = {
@@ -168,121 +119,218 @@ local screens = {
} }
} }
} }
}
local function game_update() --------------------------------------------------------------------------------
-- Handle input -- Input Module
if btn(2) then --------------------------------------------------------------------------------
player.vx = -MOVE_SPEED function Input.up() return btnp(0) end
elseif btn(3) then function Input.down() return btnp(1) end
player.vx = MOVE_SPEED function Input.left() return btn(2) end
else function Input.right() return btn(3) end
player.vx = 0 function Input.action() return btnp(4) end
function Input.back() return btnp(5) end
--------------------------------------------------------------------------------
-- UI Module
--------------------------------------------------------------------------------
function UI.draw_top_bar(title)
rect(0, 0, Config.screen.width, 10, Config.colors.black)
print(title, 3, 2, Config.colors.light_grey)
end end
if btnp(4) and player.jumps < MAX_JUMPS then function UI.draw_dialog()
player.vy = JUMP_POWER rect(40, 50, 160, 40, Config.colors.black)
player.jumps = player.jumps + 1 rectb(40, 50, 160, 40, Config.colors.dark_grey)
print(State.dialog_text, 120 - #State.dialog_text * 2, 68, Config.colors.light_grey)
end
--------------------------------------------------------------------------------
-- Splash Module
--------------------------------------------------------------------------------
function Splash.draw()
cls(Config.colors.black)
print("Mr. Anderson's", 78, 60, Config.colors.light_grey)
print("Addventure", 90, 70, Config.colors.light_grey)
end
function Splash.update()
State.splash_timer = State.splash_timer - 1
if State.splash_timer <= 0 then
State.game_state = GAME_STATE_MENU
end
end
--------------------------------------------------------------------------------
-- Menu Module
--------------------------------------------------------------------------------
function Menu.draw()
cls(Config.colors.light_grey)
UI.draw_top_bar("Main Menu")
for i, item in ipairs(State.menu_items) do
local color = Config.colors.dark_grey
if i == State.selected_menu_item then
color = Config.colors.green
end
print(item, 108, 70 + (i-1)*10, color)
end
end
function Menu.update()
if Input.up() then
State.selected_menu_item = State.selected_menu_item - 1
if State.selected_menu_item < 1 then
State.selected_menu_item = #State.menu_items
end
elseif Input.down() then
State.selected_menu_item = State.selected_menu_item + 1
if State.selected_menu_item > #State.menu_items then
State.selected_menu_item = 1
end
end
if Input.action() or Input.back() then
if State.selected_menu_item == 1 then -- Play
-- Reset player state and screen for a new game
State.player.x = Config.player.start_x
State.player.y = Config.player.start_y
State.player.vx = 0
State.player.vy = 0
State.player.jumps = 0
State.current_screen = 1
State.game_state = GAME_STATE_GAME
elseif State.selected_menu_item == 2 then -- Exit
exit()
end
end
end
--------------------------------------------------------------------------------
-- Game Module
--------------------------------------------------------------------------------
function Game.draw()
local currentScreenData = State.screens[State.current_screen]
cls(Config.colors.light_grey)
UI.draw_top_bar(currentScreenData.name)
-- Draw platforms
for _, p in ipairs(currentScreenData.platforms) do
rect(p.x, p.y, p.w, p.h, Config.colors.dark_grey)
end
-- Draw NPCs
for _, npc in ipairs(currentScreenData.npcs) do
rect(npc.x, npc.y, Config.player.w, Config.player.h, Config.colors.npc)
end
-- Draw ground
rect(State.ground.x, State.ground.y, State.ground.w, State.ground.h, Config.colors.dark_grey)
-- Draw player
rect(State.player.x, State.player.y, State.player.w, State.player.h, Config.colors.green)
end
function Game.update()
-- Handle input
if Input.left() then
State.player.vx = -Config.physics.move_speed
elseif Input.right() then
State.player.vx = Config.physics.move_speed
else
State.player.vx = 0
end
if Input.action() and State.player.jumps < Config.physics.max_jumps then
State.player.vy = Config.physics.jump_power
State.player.jumps = State.player.jumps + 1
end end
-- Update player position -- Update player position
player.x = player.x + player.vx State.player.x = State.player.x + State.player.vx
player.y = player.y + player.vy State.player.y = State.player.y + State.player.vy
-- Screen transition -- Screen transition
if player.x > SCREEN_WIDTH - player.w then if State.player.x > Config.screen.width - State.player.w then
if currentScreen < #screens then if State.current_screen < #State.screens then
currentScreen = currentScreen + 1 State.current_screen = State.current_screen + 1
player.x = 0 State.player.x = 0
else else
player.x = SCREEN_WIDTH - player.w State.player.x = Config.screen.width - State.player.w
end end
elseif player.x < 0 then elseif State.player.x < 0 then
if currentScreen > 1 then if State.current_screen > 1 then
currentScreen = currentScreen - 1 State.current_screen = State.current_screen - 1
player.x = SCREEN_WIDTH - player.w State.player.x = Config.screen.width - State.player.w
else else
player.x = 0 State.player.x = 0
end end
end end
-- Apply gravity -- Apply gravity
player.vy = player.vy + GRAVITY State.player.vy = State.player.vy + Config.physics.gravity
local currentScreenData = screens[currentScreen] local currentScreenData = State.screens[State.current_screen]
local currentPlatforms = currentScreenData.platforms
-- Collision detection with platforms -- Collision detection with platforms
for i, p in ipairs(currentPlatforms) do for _, p in ipairs(currentScreenData.platforms) do
if player.vy > 0 and player.y + player.h >= p.y and player.y + player.h <= p.y + p.h and player.x + player.w > p.x and player.x < p.x + p.w then if State.player.vy > 0 and State.player.y + State.player.h >= p.y and State.player.y + State.player.h <= p.y + p.h and State.player.x + State.player.w > p.x and State.player.x < p.x + p.w then
player.y = p.y - player.h State.player.y = p.y - State.player.h
player.vy = 0 State.player.vy = 0
player.jumps = 0 State.player.jumps = 0
end end
end end
-- Collision detection with ground -- Collision detection with ground
if player.y + player.h > ground.y and player.x + player.w > ground.x and player.x < ground.x + ground.w then if State.player.y + State.player.h > State.ground.y then
player.y = ground.y - player.h State.player.y = State.ground.y - State.player.h
player.vy = 0 State.player.vy = 0
player.jumps = 0 State.player.jumps = 0
end end
-- NPC interaction -- NPC interaction
if btnp(4) then if Input.action() then
for i, npc in ipairs(currentScreenData.npcs) do for _, npc in ipairs(currentScreenData.npcs) do
if math.abs(player.x - npc.x) < 12 and math.abs(player.y - npc.y) < 12 then if math.abs(State.player.x - npc.x) < 12 and math.abs(State.player.y - npc.y) < 12 then
dialog_text = npc.name State.dialog_text = npc.name
gameState = STATE_DIALOG State.game_state = GAME_STATE_DIALOG
end
end end
end end
end end
-- Clear screen function Game.update_dialog()
cls(COLOR_LIGHT_GREY) if Input.action() or Input.back() then
State.game_state = GAME_STATE_GAME
draw_top_bar(currentScreenData.name)
-- Draw platforms
for i, p in ipairs(currentPlatforms) do
rect(p.x, p.y, p.w, p.h, COLOR_DARK_GREY)
end
-- Draw NPCs
for i, npc in ipairs(currentScreenData.npcs) do
rect(npc.x, npc.y, PLAYER_WIDTH, PLAYER_HEIGHT, COLOR_NPC)
end
-- Draw ground
rect(ground.x, ground.y, ground.w, ground.h, COLOR_DARK_GREY)
-- Draw player
rect(player.x, player.y, player.w, player.h, COLOR_GREEN)
end
local function draw_dialog()
rect(40, 50, 160, 40, COLOR_BLACK)
rectb(40, 50, 160, 40, COLOR_DARK_GREY)
print(dialog_text, 120 - #dialog_text * 2, 68, COLOR_DARK_GREY)
end
local function update_dialog()
if btnp(4) or btnp(5) then
gameState = STATE_GAME
end end
end end
--------------------------------------------------------------------------------
-- Main Game Loop
--------------------------------------------------------------------------------
local STATE_HANDLERS = {
[GAME_STATE_SPLASH] = function()
Splash.update()
Splash.draw()
end,
[GAME_STATE_MENU] = function()
Menu.update()
Menu.draw()
end,
[GAME_STATE_GAME] = function()
Game.update()
Game.draw()
end,
[GAME_STATE_DIALOG] = function()
Game.draw() -- Draw game behind dialog
UI.draw_dialog()
Game.update_dialog()
end,
}
function TIC() function TIC()
if gameState == STATE_SPLASH then local handler = STATE_HANDLERS[State.game_state]
update_splash() if handler then
draw_splash() handler()
elseif gameState == STATE_MENU then
update_menu()
draw_menu()
elseif gameState == STATE_GAME then
game_update()
elseif gameState == STATE_DIALOG then
game_update() -- keep drawing the game state in the background
draw_dialog()
update_dialog()
end end
end end
@@ -315,4 +363,3 @@ end
-- <PALETTE> -- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57 -- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE> -- </PALETTE>