Skip to content

Commit

Permalink
add steam ver level previews
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Aug 26, 2023
1 parent 4d1802c commit b205110
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 22 deletions.
2 changes: 0 additions & 2 deletions compat/game20/assets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ function assets.load_style_and_lua_data(pack_data)
side_counts[level_id] = tonumber(match) or side_counts[level_id]
end
end
-- default to 6 sides
side_counts[level_id] = side_counts[level_id] or 6
end
return assets.load_styles(pack_data), side_counts
end
Expand Down
2 changes: 1 addition & 1 deletion compat/game20/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ function public.draw_preview(canvas, pack, level)
end
game.level.set(pack_data.levels[level])
game.level_status.reset()
game.level_status.sides = pack_data.preview_side_counts[level]
game.level_status.sides = pack_data.preview_side_counts[level] or 6
if game.level_status.sides < 3 then
game.level_status.sides = 3
end
Expand Down
72 changes: 67 additions & 5 deletions compat/game21/assets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ local log = require("log")(...)
local args = require("args")
local json = require("extlibs.json.jsonc")
local translate_and_compile_shader = require("compat.game21.assets.shader_compat")
local threaded_assets, threadify
if not args.headless then
threadify = require("threadify")
threaded_assets = threadify.require("compat.game21.assets")
end

local loaded_packs = {}
local pack_path = "packs21/"
Expand Down Expand Up @@ -177,6 +182,55 @@ function assets.init(data, audio, config)
end
end

function assets.preload(pack_data)
if pack_data.preload_promise then
-- already pending
return pack_data.preload_promise
end
-- load preview data in thread for level preview
pack_data.preload_promise = threaded_assets.load_style_and_lua_data(pack_data):done(function(styles, side_counts)
pack_data.styles = styles
pack_data.preview_side_counts = side_counts
end)
end

function assets.load_style_and_lua_data(pack_data)
local side_counts = {}
for level_id, level_json in pairs(pack_data.levels) do
local lua_path = pack_data.path .. "/" .. level_json.luaFile
if love.filesystem.getInfo(lua_path) then
local file = love.filesystem.newFile(lua_path)
local code = file:read()
file:close()
-- match set sides calls in the lua file to get the number of sides
for match in code:gmatch("function.-onInit.-l_setSides%((.-)%).-end") do
side_counts[level_id] = tonumber(match) or side_counts[level_id]
end
end
end
return assets.load_styles(pack_data), side_counts
end

function assets.load_styles(pack_data)
-- styles have to be loaded here to draw the preview icons in the level selection
pack_data.styles = {}
for contents, filename in file_ext_read_iter(pack_data.path .. "/Styles", ".json") do
local success, style_json = decode_json(contents, filename)
if success then
pack_data.styles[style_json.id] = style_json
end
end
return pack_data.styles
end

function assets.get_pack_no_load(id)
if loaded_packs[id] then
return loaded_packs[id]
else
return pack_id_json_map[id]
end
end

function assets.get_pack_from_metadata(disambiguator, author, name)
return assets.get_pack_from_id(assets._build_pack_id(disambiguator, author, name))
end
Expand Down Expand Up @@ -267,12 +321,20 @@ function assets.get_pack(name)
end

-- styles
pack_data.styles = {}
for contents, filename in file_ext_read_iter(folder .. "/Styles", ".json") do
local success, style_json = decode_json(contents, filename)
if success then
pack_data.styles[style_json.id] = style_json
if pack_data.pack_json.preload_promise and not pack_data.pack_json.preload_promise.executed then
-- wait for styles if pending threaded loading not done yet
while not pack_data.preload_promise.executed do
threadify.update()
love.timer.sleep(0.01)
end
elseif not pack_data.pack_json.styles then
-- load them synchronously if not threaded loading is pending and styles aren't loaded yet
assets.load_styles(pack_data)
end
if pack_data.pack_json.styles then
-- move table to proper location
pack_data.styles = pack_data.pack_json.styles
pack_data.pack_json.styles = nil
end

loaded_packs[name] = pack_data
Expand Down
80 changes: 68 additions & 12 deletions compat/game21/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ function public.start(pack_id, level_id, level_options)
local style_data = game.pack_data.styles[game.level_data.styleId]
if style_data == nil then
error("Error: style with id '" .. game.level_data.styleId .. "' not found")
-- still continue with default style values
end
game.style.select(style_data)
game.style.compute_colors()
Expand Down Expand Up @@ -332,7 +331,8 @@ end
---draw the game to the current canvas
---@param screen love.Canvas
---@param frametime number
function public.draw(screen, frametime)
---@param preview boolean
function public.draw(screen, frametime, preview)
-- for lua access
game.width, game.height = screen:getDimensions()

Expand All @@ -341,7 +341,9 @@ function public.draw(screen, frametime)
-- apply pulse as well
local zoom = pulse.get_zoom(zoom_factor)
love.graphics.scale(zoom, zoom)
camera_shake.apply()
if not preview then
camera_shake.apply()
end
pseudo3d.apply_skew()
rotation.apply(game)

Expand Down Expand Up @@ -382,7 +384,9 @@ function public.draw(screen, frametime)
player_tris:clear()
pivot_quads:clear()
cap_tris:clear()
if game.status.started then
if preview then
game.player.draw_pivot(game.level_status.sides, game.style, pivot_quads, cap_tris, black_and_white)
elseif game.status.started then
game.player.draw(
game.level_status.sides,
game.style,
Expand All @@ -397,14 +401,16 @@ function public.draw(screen, frametime)
love.graphics.setColor(1, 1, 1, 1)
pseudo3d.draw(set_render_stage, wall_quads, pivot_quads, player_tris, black_and_white)

if game.config.get("show_player_trail") and game.status.show_player_trail then
love.graphics.setShader()
trail_particles.draw()
end
if not preview then
if game.config.get("show_player_trail") and game.status.show_player_trail then
love.graphics.setShader()
trail_particles.draw()
end

if game.config.get("show_swap_particles") then
love.graphics.setShader()
swap_particles.draw()
if game.config.get("show_swap_particles") then
love.graphics.setShader()
swap_particles.draw()
end
end

set_render_stage(4)
Expand All @@ -419,7 +425,9 @@ function public.draw(screen, frametime)
-- text shouldn't be affected by rotation/pulse
love.graphics.origin()
love.graphics.scale(zoom_factor, zoom_factor)
camera_shake.apply()
if not preview then
camera_shake.apply()
end
set_render_stage(8)
if game.message_text ~= "" then
-- text
Expand Down Expand Up @@ -506,4 +514,52 @@ function public.init(data, input_handler, config, _, audio)
end
end

---draws a minimal level preview to a canvas
---@param canvas love.Canvas
---@param pack string
---@param level string
---@return table?
function public.draw_preview(canvas, pack, level)
local pack_data = assets.get_pack_no_load(pack)
if not pack_data then
error("pack with id '" .. pack .. "not found")
end
assets.preload(pack_data)
if pack_data.preload_promise and not pack_data.preload_promise.executed then
return pack_data.preload_promise
end
game.pack_data = pack_data
game.level_data = game.pack_data.levels[level]
if game.level_data == nil then
error("Error: level with id '" .. level .. "' not found")
end
game.level_status.reset(game.config.get("sync_music_to_dm"), assets)
local style_data = game.pack_data.styles[game.level_data.styleId]
if style_data == nil then
error("Error: style with id '" .. game.level_data.styleId .. "' not found")
end
game.style.select(style_data)
game.style.compute_colors()
game.status.reset_all_data()
game.player.reset(
game.get_swap_cooldown(),
game.config.get("player_size"),
game.config.get("player_speed"),
game.config.get("player_focus_speed")
)
game.player.update_position(game.status.radius)
game.walls.reset(game.level_status)
game.custom_walls.cw_clear()
flash.init(game)
game.current_rotation = 0
local sides = pack_data.preview_side_counts[level] or 6
if sides < 3 then
sides = 3
end
game.level_status.sides = sides
pulse.init(game)
beat_pulse.init(game)
public.draw(canvas, 0, true)
end

return public
4 changes: 2 additions & 2 deletions compat/game21/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function player.get_color_adjusted_for_swap(color)
end
end

local function draw_pivot(sides, style, pivotquads, cap_tris, black_and_white)
function player.draw_pivot(sides, style, pivotquads, cap_tris, black_and_white)
local pr, pg, pb, pa = style.get_main_color()
local cr, cg, cb, ca = style.get_cap_color_result()
if black_and_white then
Expand Down Expand Up @@ -126,7 +126,7 @@ function player.draw(
if black_and_white then
_color[1], _color[2], _color[3] = 255, 255, 255
end
draw_pivot(sides, style, pivotquads, cap_tris, black_and_white)
player.draw_pivot(sides, style, pivotquads, cap_tris, black_and_white)
if _dead_effect_timer.running then
draw_death_effect(pivotquads)
end
Expand Down
1 change: 1 addition & 0 deletions game_handler/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ end
---@param game_version number
---@param pack string
---@param level string
---@return table?
function game_handler.draw_preview(canvas, game_version, pack, level)
if games[game_version].draw_preview then
return games[game_version].draw_preview(canvas, pack, level)
Expand Down

0 comments on commit b205110

Please sign in to comment.