Add title screen

This commit is contained in:
Juhani Krekelä 2023-06-04 19:28:36 +03:00
parent d7146a1d52
commit 29f1b1f344
2 changed files with 153 additions and 21 deletions

82
bundle/font.lua Normal file
View File

@ -0,0 +1,82 @@
local font = {}
font[" "] = {}
font["a"] = {
{0, 1, 0, 0.2},
{0, 0.2, 0.2, 0},
{0.2, 0, 0.5, 0},
{0.5, 0, 0.7, 0.2},
{0.7, 0.2, 0.7, 1},
{0, 0.6, 0.7, 0.6},
}
font["c"] = {
{0, 0.2, 0.2, 0},
{0.2, 0, 0.5, 0},
{0.5, 0, 0.7, 0.2},
{0, 0.2, 0, 0.8},
{0, 0.8, 0.2, 1},
{0.2, 1, 0.5, 1},
{0.5, 1, 0.7, 0.8},
}
font["e"] = {
{0, 0, 0, 1},
{0, 0, 0.7, 0},
{0, 0.5, 0.5, 0.5},
{0, 1, 0.7, 1},
}
font["f"] = {
{0, 0, 0, 1},
{0, 0, 0.7, 0},
{0, 0.5, 0.5, 0.5},
}
font["i"] = {
{0.1, 0, 0.6, 0},
{0.35, 0, 0.35, 1},
{0.1, 1, 0.6, 1},
}
font["k"] = {
{0, 0, 0, 1},
{0, 0.5, 0.3, 0.5},
{0.3, 0.5, 0.7, 0},
{0.3, 0.5, 0.7, 1},
}
font["l"] = {
{0, 0, 0, 1},
{0, 1, 0.7, 1},
}
font["o"] = {
{0, 0.2, 0.2, 0},
{0.2, 0, 0.5, 0},
{0.5, 0, 0.7, 0.2},
{0, 0.2, 0, 0.8},
{0, 0.8, 0.2, 1},
{0.2, 1, 0.5, 1},
{0.5, 1, 0.7, 0.8},
{0.7, 0.2, 0.7, 0.8},
}
font["r"] = {
{0, 0, 0, 1},
{0, 0, 0.5, 0},
{0.5, 0, 0.7, 0.2},
{0.7, 0.2, 0.7, 0.3},
{0.7, 0.3, 0.5, 0.5},
{0.5, 0.5, 0, 0.5},
{0, 0.5, 0.7, 1}
}
font["s"] = {
{0.7, 0.2, 0.5, 0},
{0.5, 0, 0.2, 0},
{0.2, 0, 0, 0.2},
{0, 0.2, 0, 0.3},
{0, 0.3, 0.2, 0.5},
{0.2, 0.5, 0.5, 0.5},
{0.5, 0.5, 0.7, 0.7},
{0.7, 0.7, 0.7, 0.8},
{0.7, 0.8, 0.5, 1},
{0.5, 1, 0.2, 1},
{0.2, 1, 0, 0.8},
}
font["t"] = {
{0, 0, 0.7, 0},
{0.35, 0, 0.35, 1},
}
return font

View File

@ -39,6 +39,14 @@ local scale = nil
local time = nil
local states = {title = 0, gameplay = 1}
local state = nil
local title = {"r", "e", "f", "l", "e", "c", "t"}
local start_instructions = {"c", "l", "i", "c", "k", " ", "t", "o", " ", "s", "t", "a", "r", "t"}
font = require("font")
function setScreenDimensions(width, height)
window_width = width
window_height = height
@ -66,7 +74,10 @@ function fromScreenCoordinate(x, y)
return logical_x, logical_y
end
function initialize()
function startStage()
love.mouse.setVisible(false)
movePaddle(0)
missiles = {}
unreflected_missiles = 0
unreflected_missiles_allowed = 0
@ -93,15 +104,16 @@ function initialize()
spawnEnemy(0.65, 0.2)
spawnEnemy(0.75, 0.2)
spawnEnemy(0.85, 0.2)
state = states.gameplay
end
function love.load()
math.randomseed(os.time())
love.mouse.setVisible(false)
local width, height = love.graphics.getDimensions()
setScreenDimensions(width, height)
movePaddle(0)
initialize()
state = states.title
end
function spawnCities()
@ -331,17 +343,19 @@ function updateEnemies(dt)
end
function love.update(dt)
updateMissiles(dt)
updateExplosions(dt)
updateEnemies(dt)
if state == states.gameplay then
updateMissiles(dt)
updateExplosions(dt)
updateEnemies(dt)
time = time + dt
if unreflected_missiles_allowed < unreflected_missiles_max and time >= unreflected_missiles_allowed * unreflected_missiles_allowed_ramp_up then
unreflected_missiles_allowed = unreflected_missiles_allowed + 1
end
time = time + dt
if unreflected_missiles_allowed < unreflected_missiles_max and time >= unreflected_missiles_allowed * unreflected_missiles_allowed_ramp_up then
unreflected_missiles_allowed = unreflected_missiles_allowed + 1
end
if #explosions == 0 and #missiles == 0 and #enemies == 0 then
love.event.quit()
if #explosions == 0 and #missiles == 0 and #enemies == 0 then
love.event.quit()
end
end
end
@ -365,7 +379,13 @@ end
love.mousemoved = movePaddle
love.mousepressed = explodeAllReflected
function love.mousepressed(x, y, button, istouch, presses)
if state == states.gameplay then
explodeAllReflected()
elseif state == states.title then
startStage()
end
end
love.resize = setScreenDimensions
@ -471,11 +491,41 @@ function drawEnemies()
end
end
function love.draw()
drawCities()
drawWalls()
drawMissiles()
drawPaddle()
drawEnemies()
drawExplosions()
function drawText(x, y, text_scale, text)
love.graphics.setLineWidth(2 / 700 * scale)
love.graphics.setColor(1, 1, 1)
for i, char in ipairs(text) do
local glyph = font[char]
for _, line in ipairs(glyph) do
local x0, y0 = toScreenCoordinates(x + (i - 1) * text_scale + line[1] * text_scale, y + line[2] * text_scale)
local x1, y1 = toScreenCoordinates(x + (i - 1) * text_scale + line[3] * text_scale, y + line[4] * text_scale)
love.graphics.line(x0, y0, x1, y1)
end
end
end
function textWidth(text_scale, text)
-- Each character is 0.7 wide with 0.3 character spacing before scaling
-- Width of a text is therefore math.max(#text - 0.3, 0) before scaling
-- and math.max(#text - 0.3, 0) * text_scale after
return math.max(#text - 0.3, 0) * text_scale
end
function drawTextCentered(y, text_scale, text)
local x = (1 - textWidth(text_scale, text)) / 2
drawText(x, y, text_scale, text)
end
function love.draw()
if state == states.gameplay then
drawCities()
drawWalls()
drawMissiles()
drawPaddle()
drawEnemies()
drawExplosions()
elseif state == states.title then
drawTextCentered(0.3, 0.1, title)
drawTextCentered(0.45, 0.03, start_instructions)
end
end