npcs
This commit is contained in:
174
game.lua
174
game.lua
@@ -9,12 +9,16 @@
|
||||
-- Game state
|
||||
STATE_MENU = 0
|
||||
STATE_GAME = 1
|
||||
STATE_DIALOG = 2
|
||||
gameState = STATE_MENU
|
||||
|
||||
-- Menu properties
|
||||
menuItems = {"Play", "Exit"}
|
||||
selectedMenuItem = 1
|
||||
|
||||
-- NPC properties
|
||||
dialog_text = ""
|
||||
|
||||
function draw_top_bar(title)
|
||||
rect(0, 0, 240, 10, 0)
|
||||
print(title, 3, 2, 13)
|
||||
@@ -64,27 +68,39 @@ end
|
||||
-- Screen data
|
||||
screens = {
|
||||
{ -- Screen 1
|
||||
name = "Home Screen",
|
||||
name = "Screen 1",
|
||||
platforms = {
|
||||
{x = 80, y = 110, w = 40, h = 8},
|
||||
{x = 160, y = 90, w = 40, h = 8}
|
||||
},
|
||||
npcs = {
|
||||
{x = 180, y = 82, name = "Trinity"},
|
||||
{x = 90, y = 102, name = "Oracle"}
|
||||
}
|
||||
},
|
||||
{ -- Screen 2
|
||||
name = "Second Screen",
|
||||
name = "Screen 2",
|
||||
platforms = {
|
||||
{x = 30, y = 100, w = 50, h = 8},
|
||||
{x = 100, y = 80, w = 50, h = 8},
|
||||
{x = 170, y = 60, w = 50, h = 8}
|
||||
},
|
||||
npcs = {
|
||||
{x = 120, y = 72, name = "Morpheus"},
|
||||
{x = 40, y = 92, name = "Tank"}
|
||||
}
|
||||
},
|
||||
{ -- Screen 3
|
||||
name = "Third Screen",
|
||||
name = "Screen 3",
|
||||
platforms = {
|
||||
{x = 50, y = 110, w = 30, h = 8},
|
||||
{x = 100, y = 90, w = 30, h = 8},
|
||||
{x = 150, y = 70, w = 30, h = 8},
|
||||
{x = 200, y = 50, w = 30, h = 8}
|
||||
},
|
||||
npcs = {
|
||||
{x = 210, y = 42, name = "Agent Smith"},
|
||||
{x = 160, y = 62, name = "Cypher"}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -155,7 +171,8 @@ function game_update()
|
||||
-- Apply gravity
|
||||
player.vy = player.vy + gravity
|
||||
|
||||
local currentPlatforms = screens[currentScreen].platforms
|
||||
local currentScreenData = screens[currentScreen]
|
||||
local currentPlatforms = currentScreenData.platforms
|
||||
-- Collision detection with platforms
|
||||
for i, p in ipairs(currentPlatforms) 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
|
||||
@@ -172,15 +189,30 @@ function game_update()
|
||||
player.jumps = 0
|
||||
end
|
||||
|
||||
-- NPC interaction
|
||||
if btnp(4) then
|
||||
for i, npc in ipairs(currentScreenData.npcs) do
|
||||
if math.abs(player.x - npc.x) < 12 and math.abs(player.y - npc.y) < 12 then
|
||||
dialog_text = npc.name
|
||||
gameState = STATE_DIALOG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Clear screen
|
||||
cls(13)
|
||||
|
||||
draw_top_bar(screens[currentScreen].name)
|
||||
draw_top_bar(currentScreenData.name)
|
||||
|
||||
-- Draw platforms
|
||||
for i, p in ipairs(currentPlatforms) do
|
||||
rect(p.x, p.y, p.w, p.h, 14)
|
||||
end
|
||||
|
||||
-- Draw NPCs
|
||||
for i, npc in ipairs(currentScreenData.npcs) do
|
||||
rect(npc.x, npc.y, 8, 8, 8)
|
||||
end
|
||||
|
||||
-- Draw ground
|
||||
rect(ground.x, ground.y, ground.w, ground.h, 14)
|
||||
@@ -189,12 +221,144 @@ function game_update()
|
||||
rect(player.x, player.y, player.w, player.h, 6)
|
||||
end
|
||||
|
||||
currentScreen = 1
|
||||
|
||||
-- Player properties
|
||||
player = {
|
||||
x = 120,
|
||||
y = 128,
|
||||
w = 8,
|
||||
h = 8,
|
||||
vx = 0,
|
||||
vy = 0,
|
||||
jumps = 0
|
||||
}
|
||||
|
||||
-- Ground properties
|
||||
ground = {
|
||||
x = 0,
|
||||
y = 136,
|
||||
w = 240,
|
||||
h = 8
|
||||
}
|
||||
|
||||
-- Game constants
|
||||
gravity = 0.5
|
||||
jump_power = -5
|
||||
move_speed = 1.5
|
||||
max_jumps = 2
|
||||
|
||||
function game_update()
|
||||
-- Handle input
|
||||
if btn(2) then
|
||||
player.vx = -move_speed
|
||||
elseif btn(3) then
|
||||
player.vx = move_speed
|
||||
else
|
||||
player.vx = 0
|
||||
end
|
||||
|
||||
if btnp(4) and player.jumps < max_jumps then
|
||||
player.vy = jump_power
|
||||
player.jumps = player.jumps + 1
|
||||
end
|
||||
|
||||
-- Update player position
|
||||
player.x = player.x + player.vx
|
||||
player.y = player.y + player.vy
|
||||
|
||||
-- Screen transition
|
||||
if player.x > 240 - player.w then
|
||||
if currentScreen < #screens then
|
||||
currentScreen = currentScreen + 1
|
||||
player.x = 0
|
||||
else
|
||||
player.x = 240 - player.w
|
||||
end
|
||||
elseif player.x < 0 then
|
||||
if currentScreen > 1 then
|
||||
currentScreen = currentScreen - 1
|
||||
player.x = 240 - player.w
|
||||
else
|
||||
player.x = 0
|
||||
end
|
||||
end
|
||||
|
||||
-- Apply gravity
|
||||
player.vy = player.vy + gravity
|
||||
|
||||
local currentScreenData = screens[currentScreen]
|
||||
local currentPlatforms = currentScreenData.platforms
|
||||
-- Collision detection with platforms
|
||||
for i, p in ipairs(currentPlatforms) 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
|
||||
player.y = p.y - player.h
|
||||
player.vy = 0
|
||||
player.jumps = 0
|
||||
end
|
||||
end
|
||||
|
||||
-- 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
|
||||
player.y = ground.y - player.h
|
||||
player.vy = 0
|
||||
player.jumps = 0
|
||||
end
|
||||
|
||||
-- NPC interaction
|
||||
if btnp(4) then
|
||||
for i, npc in ipairs(currentScreenData.npcs) do
|
||||
if math.abs(player.x - npc.x) < 12 and math.abs(player.y - npc.y) < 12 then
|
||||
dialog_text = npc.name
|
||||
gameState = STATE_DIALOG
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Clear screen
|
||||
cls(13)
|
||||
|
||||
draw_top_bar(currentScreenData.name)
|
||||
|
||||
-- Draw platforms
|
||||
for i, p in ipairs(currentPlatforms) do
|
||||
rect(p.x, p.y, p.w, p.h, 14)
|
||||
end
|
||||
|
||||
-- Draw NPCs
|
||||
for i, npc in ipairs(currentScreenData.npcs) do
|
||||
rect(npc.x, npc.y, 8, 8, 8)
|
||||
end
|
||||
|
||||
-- Draw ground
|
||||
rect(ground.x, ground.y, ground.w, ground.h, 14)
|
||||
|
||||
-- Draw player
|
||||
rect(player.x, player.y, player.w, player.h, 6)
|
||||
end
|
||||
|
||||
function draw_dialog()
|
||||
rect(40, 50, 160, 40, 0)
|
||||
rectb(40, 50, 160, 40, 14)
|
||||
print(dialog_text, 120 - #dialog_text * 2, 68, 14)
|
||||
end
|
||||
|
||||
function update_dialog()
|
||||
if btnp(4) or btnp(5) then
|
||||
gameState = STATE_GAME
|
||||
end
|
||||
end
|
||||
|
||||
function TIC()
|
||||
if 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user