Files
tromi_mobile/main.lua
Squishy (C6H12O6+NaCl+H2O) d0307c8765 first commit
2024-04-11 08:33:58 +07:00

390 lines
14 KiB
Lua

require 'funcs'
-- savedir = love.filesystem.getSaveDirectory()
-- if love.system.getOS() == 'Windows' then
-- local test_for_separator = string.find(savedir, '/')
-- if test_for_separator ~= nil then savedir = table.concat(split_string(savedir, '/'), '\\') end
-- savedir = savedir..'\\saves\\'
-- configfile = savedir..'config.sav'
-- replaydir = savedir..'replays\\'
-- hiscorefile = savedir..'hiscores.sav'
-- else
-- savedir = savedir..'/saves/'
-- configfile = savedir..'config.sav'
-- replaydir = savedir..'replays/'
-- hiscorefile = savedir..'hiscores.sav'
-- end
SAVE_DIR = '/saves/'
CONFIG_FILE = 'config.sav'
REPLAY_DIR = 'replays/'
HIscoreFILE = 'hiscores.sav'
PENTO_MODE = false
function love.load()
math.randomseed(os.time())
highscores = {}
require "load.graphics"
require "load.fonts"
require "load.sounds"
require "load.save"
require "load.bigint"
loadSave()
require "scene"
love.window.setMode(love.graphics.getWidth(), love.graphics.getHeight(), {resizable = true});
love.mouse.setVisible(false)
love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(true)
-- used for screenshots
GLOBAL_CANVAS = love.graphics.newCanvas()
-- init config
initConfig()
love.window.setFullscreen(config["fullscreen"])
end
function love.draw()
love.graphics.setCanvas(GLOBAL_CANVAS)
love.graphics.clear()
love.graphics.push()
-- get offset matrix
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local scale_factor = math.min(width / 640, height / 480)
love.graphics.translate(
(width - scale_factor * 640) / 2,
(height - scale_factor * 480) / 2
)
love.graphics.scale(scale_factor)
scene:render()
love.graphics.pop()
drawText("Pressed: "..(lastPressedKey or '[NONE]').." | Released key: "..(lastReleasedKey or '[NONE]'),0,0,1000,"left")
love.graphics.setCanvas()
love.graphics.setColor(1,1,1,1)
love.graphics.draw(GLOBAL_CANVAS)
end
function love.keypressed(key, scancode)
local input_pressed=nil
-- global hotkeys
if scancode == "f4" then
config["fullscreen"] = not config["fullscreen"]
saveConfig()
love.window.setFullscreen(config["fullscreen"])
elseif scancode == "f2" and scene.title ~= "Input Config" and scene.title ~= "Game" then
scene = InputConfigScene()
-- function keys are reserved
elseif string.match(scancode, "^f[1-9]$") or string.match(scancode, "^f[1-9][0-9]+$") then
return
-- escape is reserved for menu_back
elseif scancode == "escape" then
scene:onInputPress({input="menu_back", type="key", key=key, scancode=scancode})
-- pass any other key to the scene, with its configured mapping
else
if config.input and config.input.keys then
input_pressed = config.input.keys[scancode]
end
scene:onInputPress({input=input_pressed, type="key", key=key, scancode=scancode})
end
lastPressedKey = input_pressed or scancode
end
function love.keyreleased(key, scancode)
local input_released = nil
-- escape is reserved for menu_back
if scancode == "escape" then
scene:onInputRelease({input="menu_back", type="key", key=key, scancode=scancode})
-- function keys are reserved
elseif string.match(scancode, "^f[1-9]$") or string.match(scancode, "^f[1-9][0-9]+$") then
return
-- handle all other keys; tab is reserved, but the input config scene keeps it from getting configured as a game input, so pass tab to the scene here
else
if config.input and config.input.keys then
input_released = config.input.keys[scancode]
end
scene:onInputRelease({input=input_released, type="key", key=key, scancode=scancode})
end
lastReleasedKey = input_released or scancode
end
function love.joystickpressed(joystick, button)
local input_pressed = nil
if
config.input and
config.input.joysticks and
config.input.joysticks[joystick:getName()] and
config.input.joysticks[joystick:getName()].buttons
then
input_pressed = config.input.joysticks[joystick:getName()].buttons[button]
end
scene:onInputPress({input=input_pressed, type="joybutton", name=joystick:getName(), button=button})
end
function love.joystickreleased(joystick, button)
local input_released = nil
if
config.input and
config.input.joysticks and
config.input.joysticks[joystick:getName()] and
config.input.joysticks[joystick:getName()].buttons
then
input_released = config.input.joysticks[joystick:getName()].buttons[button]
end
scene:onInputRelease({input=input_released, type="joybutton", name=joystick:getName(), button=button})
end
function love.joystickaxis(joystick, axis, value)
local input_pressed = nil
local positive_released = nil
local negative_released = nil
if
config.input and
config.input.joysticks and
config.input.joysticks[joystick:getName()] and
config.input.joysticks[joystick:getName()].axes and
config.input.joysticks[joystick:getName()].axes[axis]
then
if math.abs(value) >= 1 then
input_pressed = config.input.joysticks[joystick:getName()].axes[axis][value >= 1 and "positive" or "negative"]
end
positive_released = config.input.joysticks[joystick:getName()].axes[axis].positive
negative_released = config.input.joysticks[joystick:getName()].axes[axis].negative
end
if math.abs(value) >= 1 then
scene:onInputPress({input=input_pressed, type="joyaxis", name=joystick:getName(), axis=axis, value=value})
else
scene:onInputRelease({input=positive_released, type="joyaxis", name=joystick:getName(), axis=axis, value=value})
scene:onInputRelease({input=negative_released, type="joyaxis", name=joystick:getName(), axis=axis, value=value})
end
end
function love.touchPressed()
love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(false)
love.keyboard.setTextInput(true)
end
local last_hat_direction = ""
local directions = {
["u"] = "up",
["d"] = "down",
["l"] = "left",
["r"] = "right",
}
function love.joystickhat(joystick, hat, direction)
local input_pressed = nil
local has_hat = false
if
config.input and
config.input.joysticks and
config.input.joysticks[joystick:getName()] and
config.input.joysticks[joystick:getName()].hats and
config.input.joysticks[joystick:getName()].hats[hat]
then
if direction ~= "c" then
input_pressed = config.input.joysticks[joystick:getName()].hats[hat][direction]
end
has_hat = true
end
if input_pressed then
for i = 1, #direction do
local char = direction:sub(i, i)
local _, count = last_hat_direction:gsub(char, char)
if count == 0 then
scene:onInputPress({input=config.input.joysticks[joystick:getName()].hats[hat][char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
for i = 1, #last_hat_direction do
local char = last_hat_direction:sub(i, i)
local _, count = direction:gsub(char, char)
if count == 0 then
scene:onInputRelease({input=config.input.joysticks[joystick:getName()].hats[hat][char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
last_hat_direction = direction
elseif has_hat then
for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do
scene:onInputRelease({input=config.input.joysticks[joystick:getName()].hats[hat][direction], type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end
last_hat_direction = ""
elseif direction ~= "c" then
for i = 1, #direction do
local char = direction:sub(i, i)
local _, count = last_hat_direction:gsub(char, char)
if count == 0 then
scene:onInputPress({input=directions[char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
for i = 1, #last_hat_direction do
local char = last_hat_direction:sub(i, i)
local _, count = direction:gsub(char, char)
if count == 0 then
scene:onInputRelease({input=directions[char], type="joyhat", name=joystick:getName(), hat=hat, direction=char})
end
end
last_hat_direction = direction
else
for i, direction in ipairs{"d", "l", "ld", "lu", "r", "rd", "ru", "u"} do
scene:onInputRelease({input=nil, type="joyhat", name=joystick:getName(), hat=hat, direction=direction})
end
last_hat_direction = ""
end
end
function love.focus(f)
return
end
function love.resize(w, h)
GLOBAL_CANVAS:release()
GLOBAL_CANVAS = love.graphics.newCanvas(w, h)
end
local TARGET_FPS = 60
function love.run()
if love.load then love.load(love.arg.parseGameArguments(arg), arg) end
if love.timer then love.timer.step() end
local dt = 0
local last_time = love.timer.getTime()
local time_accumulator = 0
return function()
if love.event then
love.event.pump()
for name, a,b,c,d,e,f in love.event.poll() do
if name == "quit" then
if not love.quit or not love.quit() then
return a or 0
end
end
love.handlers[name](a,b,c,d,e,f)
end
end
if scene and scene.update and love.timer then
scene:update()
local frame_duration = 1.0 / TARGET_FPS
if time_accumulator < frame_duration then
if love.graphics and love.graphics.isActive() and love.draw then
love.graphics.origin()
love.graphics.clear(love.graphics.getBackgroundColor())
love.draw()
love.graphics.present()
end
local end_time = last_time + frame_duration
local time = love.timer.getTime()
while time < end_time do
love.timer.sleep(0.001)
time = love.timer.getTime()
end
time_accumulator = time_accumulator + time - last_time
end
time_accumulator = time_accumulator - frame_duration
end
last_time = love.timer.getTime()
end
end
minos = {'R_d', 'O_d', 'Y_d', 'G_d', 'C_d', 'B_d', 'M_d'}
main_bg_grid = {}
for x=1, 40 do
main_bg_grid[x] = {}
for y=1, 30 do
main_bg_grid[x][y] = 0
end
end
main_bg_cur_pos = {20,6}
main_bg_cur_color = minos[love.math.random(1,7)]
main_bg_cur_mino = 1
main_bg_draw_frame = 0
main_bg_last_color = nil
function mainBackground()
if config["music"] and not sounds["bgm_title"]:isPlaying() then
sounds["bgm_title"]:setVolume(0.3)
sounds["bgm_title"]:play()
end
local y = 40
if main_bg_draw_frame >= 16 then
while y > 1 do
for x = 1, 40 do
main_bg_grid[x][y] = main_bg_grid[x][y-1]
end
y = y - 1
end
for x=1, 40 do
main_bg_grid[x][1] = 0
end
main_bg_draw_frame = 0
main_bg_cur_pos[2] = main_bg_cur_pos[2] + 1
end
local directions = { {0,-1},{1,0}, {-1,0}}
local test_dir = directions[love.math.random(1,3)]
main_bg_cur_pos[1] = main_bg_cur_pos[1] + test_dir[1]
main_bg_cur_pos[2] = main_bg_cur_pos[2] + test_dir[2]
if main_bg_cur_pos[1] > 40 then main_bg_cur_pos[1] = 40 end
if main_bg_cur_pos[1] < 1 then main_bg_cur_pos[1] = 1 end
if main_bg_cur_pos[2] > 30 then main_bg_cur_pos[2] = 30 end
if main_bg_cur_pos[2] < 1 then main_bg_cur_pos[2] = 1 end
if main_bg_grid[main_bg_cur_pos[1]][main_bg_cur_pos[2]] == 0 then
main_bg_grid[main_bg_cur_pos[1]][main_bg_cur_pos[2]] = main_bg_cur_color
main_bg_cur_mino = main_bg_cur_mino + 1
end
for x=1,40 do
for y=1,30 do
if main_bg_grid[x][y] ~= 0 then
love.graphics.setColor(1, 1, 1, 0.4)
if ((x-1)*48)-560 > 0 and ((x-1)*48)-560 < 640 then love.graphics.draw(blocks["2tie"][main_bg_grid[x][y]], ((x-1)*48)-570, (((y+2)*48)+main_bg_draw_frame*3)-480,0, 3) end
love.graphics.setColor(1, 1, 1, 1)
end
end
end
for x=1,40 do
for y=1,30 do
if main_bg_grid[x][y] ~= 0 then
love.graphics.setColor(1, 1, 1, 1)
love.graphics.draw(blocks["2tie"][main_bg_grid[x][y]], (x-1)*16, ((y-1)*16)+main_bg_draw_frame)
end
end
end
for x=1,40 do
for y=1,30 do
if main_bg_grid[x][y] ~= 0 then
love.graphics.setColor(1, 1, 1, 0.6)
if ((x-1)*32)-320 > 0 and ((x-1)*32)-320 < 640 then love.graphics.draw(blocks["2tie"][main_bg_grid[x][y]], ((x-1)*32)-320, (((y+1)*32)+main_bg_draw_frame*2)-320,0, 2) end
love.graphics.setColor(1, 1, 1, 1)
end
end
end
if main_bg_cur_mino == 5 then
--if main_bg_cur_pos[2] < 4 then
-- main_bg_cur_pos[2] = 4
-- main_bg_cur_pos[1] = love.math.random(4, 36)
--end
main_bg_cur_pos = {love.math.random(16,24),6}
main_bg_last_color = main_bg_cur_color
while main_bg_cur_color == main_bg_last_color do main_bg_cur_color = minos[love.math.random(1,7)] end
main_bg_cur_mino = 1
end
main_bg_placed = false
main_bg_draw_frame = main_bg_draw_frame + 1
end