local FRAME_MS = 1000.0 / 60.0 local BLACK = { 0, 0, 0, 255 } -- Taiko 14 dojo/tutorial.swg laid over the PyTaikoGreen tutorial chart: the swg's eleven scenes are -- timed to the chart's ten caption slots (big notes come before the roll here, the arcade card scene is -- left out). Positions are in the swg's 640x480 space; K scales them to the screen. local SLOTS = { { start = 150, len = 240, box = 0, voice = "tutorial_voice_1" }, { start = 390, len = 440, box = 0, voice = "tutorial_voice_2" }, { start = 830, len = 360, box = 1, voice = "tutorial_voice_3" }, { start = 1190, len = 380, box = 2, voice = "tutorial_voice_4" }, { start = 1570, len = 430, box = 3, voice = "tutorial_voice_5" }, { start = 2000, len = 360, box = 2, voice = "tutorial_voice_6" }, { start = 2360, len = 440, box = 1, voice = "tutorial_voice_7" }, { start = 2800, len = 1001, box = 1, voice = "tutorial_voice_8" }, { start = 3801, len = 520, box = 0, voice = "tutorial_voice_9" }, { start = 4321, len = 520, box = 0, voice = "tutorial_voice_10" }, } local END_FRAME = 4841 local GAUGE = { {750, 1}, {1489, 2}, {1869, 3}, {2299, 4}, {4000, 5}, {4121, 6} } for i, f in ipairs({ 20, 33, 45, 56, 66, 75, 83, 90, 96, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153 }) do GAUGE[#GAUGE + 1] = { 4321 + f, 6 + i } end for i = 0, 19 do GAUGE[#GAUGE + 1] = { 4321 + 279 + i * 4, 30 + i } end GAUGE[#GAUGE + 1] = { 4321 + 359, 50 } local SOUNDS = { { 0, "tutorial_voice_title", "voice" } } for _, s in ipairs(SLOTS) do SOUNDS[#SOUNDS + 1] = { s.start, s.voice, "voice" } end -- the cat's side hits and the bop (side demo frames 89 / 105 / 121 / 140, 8 before the slot) for _, m in ipairs({ 89, 105, 121 }) do SOUNDS[#SOUNDS + 1] = { SLOTS[3].start + m - 8, "tutorial_sidebody", "sound" } end SOUNDS[#SOUNDS + 1] = { SLOTS[3].start + 140 - 8, "tutorial_tarai", "sound" } table.sort(SOUNDS, function(a, b) return a[1] < b[1] end) local SKIP_HITS = 5 local SKIP_ICON_X = { 179, 136.15, 93.35, 50.55, 7.75 } local SKIP_BOB = { {54, 63, 0, -20, "out"}, {64, 72, -20, 0, "in"}, {73, 78, 0, -10, "out"}, {79, 83, -10, 0, "in"}, {84, 86, 0, -2, "out"}, {87, 89, -2, 0, "in"} } -- drummer group origin; the drum and drummer rest on it at (-110, 20) and (60, -30.35) local GX, GY = 470, 130 local DRUMMER_IDLE, DRUMMER_RIGHT_HIT, DRUMMER_RIGHT_REST = 0, 4, 5 local DRUMMER_LEFT_HIT, DRUMMER_RIM, DRUMMER_BIG, DRUMMER_SCOLD = 9, 10, 11, 12 local CAPTION_FONT = 24 local CAPTION_ROWS = { { 0.5 }, { 0.32, 0.68 }, { 0.2, 0.5, 0.8 } } local CAPTION_ICON_GAP = 3 local function ease(p, e) p = math.max(0, math.min(1, p)) if e == "in" then return p * p end if e == "out" then return p * (2 - p) end return p end -- a over frames [f0, f0 + n] from a to b local function tween(f, f0, n, a, b, e) return a + (b - a) * ease((f - f0) / n, e) end TutorialOverlay = {} TutorialOverlay.__index = TutorialOverlay function TutorialOverlay.new() local self = setmetatable({}, TutorialOverlay) self.K = tex.get_screen_width() / 640 self.tex, self.size = {}, {} for _, name in ipairs({ "title", "header_bg", "dojo_bg", "dojo_top_band", "don_chan", "beginner_mark", "caption_box", "skip_text", "skip_icon", "drum", "face_flash", "rim_flash", "drummer", "hit_burst", "ka_ring", "side_cat", "side_spark", "cat_mask", "falling_don", "cross", "icon_don", "icon_kat", "icon_big_don", "icon_big_kat", "icon_roll", "icon_balloon", "icon_kusudama" }) do self.tex[name] = tex.get_texture("game/tutorial/" .. name) local info = tex.get_texture_info("tutorial", name) self.size[name] = info and { info.width or info.x2[1], info.height or info.y2[1] } or { 0, 0 } end self.last = nil self.captions = {} self.rim_count = 0 self.rim_ms = {} return self end local function play(name, preset) if audio.has_sound(name) then audio.play_sound(name, preset) end end function TutorialOverlay:update(ms) local frame = ms / FRAME_MS if self.sound_frame == nil or frame < self.sound_frame then self.next_sound = 1 self.bgm_started = false end self.sound_frame = frame if not self.bgm_started and frame >= 0 then self.bgm_started = true play("tutorial_bgm", "music") end while SOUNDS[self.next_sound] and frame >= SOUNDS[self.next_sound][1] do local snd = SOUNDS[self.next_sound] if frame - snd[1] < 30 then play(snd[2], snd[3]) end self.next_sound = self.next_sound + 1 end end function TutorialOverlay:hit(kind, side, ms) local prev = self.last local big = prev and prev.kind == kind and prev.side ~= side and ms - prev.ms < FRAME_MS * 1.5 self.last = { kind = kind, side = side, ms = big and prev.ms or ms, big = big } end function TutorialOverlay:gauge(ms) local f, cells = ms / FRAME_MS, 0 for _, g in ipairs(GAUGE) do if f >= g[1] then cells = g[2] else break end end return cells / 50 end function TutorialOverlay:is_finished(ms) return ms / FRAME_MS >= END_FRAME end -- texture.json rest position + (dx, dy) swg px function TutorialOverlay:at(name, frame, dx, dy, fade, color) tex.draw_texture(self.tex[name], { frame = frame, x = dx * self.K, y = dy * self.K, fade = fade, color = color }) end -- centred on (cx, cy) swg px, for textures without a rest position function TutorialOverlay:put(name, frame, cx, cy, scale, fade, rotation) local w, h = self.size[name][1], self.size[name][2] if rotation then tex.draw_texture(self.tex[name], { frame = frame, x = cx * self.K, y = cy * self.K, origin = { w / 2, h / 2 }, rotation = rotation, fade = fade }) else tex.draw_texture(self.tex[name], { frame = frame, x = cx * self.K - w / 2, y = cy * self.K - h / 2, scale = scale or 1, center = true, fade = fade }) end end -- rest position stretched by (sx, sy) about its centre function TutorialOverlay:stretch(name, sx, sy) local w, h = self.size[name][1], self.size[name][2] tex.draw_texture(self.tex[name], { x = (1 - sx) * w / 2, y = (1 - sy) * h / 2, x2 = (sx - 1) * w, y2 = (sy - 1) * h }) end local function shade(v) v = math.floor(math.max(0, math.min(1, v)) * 255 + 0.5) return { v, v, v, 255 } end function TutorialOverlay:draw_scenery(f) local dy = f < 21 and tween(f, 0, 21, 200, 0) or 0 self:at("dojo_bg", 0, 0, dy, nil, shade((f - 21) / 21)) if f >= 21 then self:at("dojo_top_band", 0, 0, tween(f, 21, 10, 21, 0, "out")) end if f >= 14 then local scroll = -((f - 14) * 0.8 % 255) local hy = tween(f, 14, 14, -190, 0, "out") local color = shade((f - 28) / 7) for i = -1, 3 do self:at("header_bg", 0, i * 255 + scroll, hy, nil, color) end end if f >= 88 then local u = f - 88 local sx, sy if u < 15 then sx, sy = 0.2, tween(u, 0, 15, 0.2, 1.1, "in") elseif u < 30 then sx, sy = tween(u, 15, 15, 0.2, 1.1, "out"), tween(u, 15, 15, 1.1, 0.95, "out") elseif u < 45 then sx, sy = tween(u, 30, 15, 1.1, 0.95), tween(u, 30, 15, 0.95, 1.05) elseif u < 55 then sx, sy = tween(u, 45, 10, 0.95, 1.05, "out"), tween(u, 45, 10, 1.05, 1, "out") else sx, sy = tween(u, 55, 15, 1.05, 1, "out"), 1 end self:stretch("beginner_mark", sx, sy) end self:at("title", 0, 0, tween(f, 0, 28, -80, 0, "out")) end function TutorialOverlay:draw_don_chan(f) if f < 14 then return end local u = f - 14 local frame = u < 120 and math.floor(u / 20) % 2 or u < 140 and 2 or 3 self:at("don_chan", frame, tween(u, 0, 30, -230, 0, "out"), 0) end function TutorialOverlay:drum(frame, dx, dy, fade) self:at("drum", frame, dx, dy, fade) end -- the drummer's pose and the drum's hit effects follow the autoplay hits function TutorialOverlay:draw_reactive(ms, dx, fade) local h = self.last local e = h and (ms - h.ms) / FRAME_MS or math.huge local pose, py = DRUMMER_IDLE, 0 if h and h.big then pose = e < 16 and DRUMMER_BIG or DRUMMER_IDLE elseif h and h.kind == "kat" then if e < 16 then pose, py = DRUMMER_RIM, -12 end elseif h and h.side == "r" then pose = e < 2 and DRUMMER_RIGHT_HIT or DRUMMER_RIGHT_REST elseif h then pose = e < 2 and DRUMMER_LEFT_HIT or DRUMMER_IDLE end self:at("drummer", pose, dx, py, fade) if e < 15 then local p = e / 15 if h.kind == "kat" then self:put("ka_ring", 0, GX - 80.35, GY - 28.35, 1 + 0.25 * p, (1 - p) * (fade or 1)) else self:put("hit_burst", 0, GX - 80.2, GY - 28.2, 0.6 + 0.4 * p, (1 - p) * (fade or 1)) end end end local SHAKE = { -1, -2, -1, 0, -1, -2, -1, 0 } function TutorialOverlay:drum_shake(ms) local h = self.last if not h or h.kind == "kat" then return 0 end local e = math.floor((ms - h.ms) / FRAME_MS) + 1 return SHAKE[e] or 0 end -- slot 2: the drum and the drummer arrive, the drummer leaves at the end function TutorialOverlay:draw_hit_slot(m, ms) if m < 0 then return end if m < 30 then self:drum(0, tween(m, 0, 29, 410, 0, "out"), -40) else self:drum(0, self:drum_shake(ms), tween(m, 30, 5, -40, 0, "in")) end if m < 35 then return end if m < 65 then self:at("drummer", DRUMMER_IDLE, tween(m, 35, 30, 320, 0, "out"), 0) elseif m < 404 then self:draw_reactive(ms, 0) else self:at("drummer", DRUMMER_RIGHT_REST, tween(m, 404, 14, 0, 300, "out"), 0) end end -- slot 3: a masked cat hits the drum's side, gets bopped, and the drummer scolds function TutorialOverlay:draw_side_slot(m) if m < 31 then self:drum(0, 0, 0) elseif m < 39 then self:drum(0, tween(m, 31, 7, 0, 60, "out"), 0) elseif m < 42 then self:drum(0, 60, tween(m, 39, 2, 0, -13.35)) elseif m < 45 then self:drum(1, 60, tween(m, 42, 2, -20, -33.35)) elseif m < 291 then self:drum(2, 60, -40) elseif m < 295 then self:drum(1, 60, tween(m, 291, 3, -20, -5)) else self:drum(0, tween(m, 295, 5, 60, 0), 0) end if m >= 50 and m < 140 then local x = m < 72 and tween(m, 50, 22, -581, -132, "out") or tween(m, 72, 10, -132, -152) local swing = (m >= 89 and m < 97) or (m >= 105 and m < 113) or m >= 121 self:put("side_cat", swing and 1 or 0, GX + x, GY - 10, 1) elseif m >= 140 and m < 257 then local by = m < 150 and 20 * math.sin(math.pi * (m - 140) / 10) or 0 self:put("side_cat", 2, GX - 179, GY - 10 + by, 1, m < 231 and 1 or tween(m, 231, 25, 1, 0)) end for _, s in ipairs({ { 89, -85.65, -18.65, 1 }, { 105, -62.65, 6.35, 1 }, { 121, -80.65, -10.65, 1 }, { 140, -195, -49, 1.5 } }) do if m >= s[1] and m < s[1] + 8 then local p = ease((m - s[1]) / 8, "out") self:put("side_spark", 0, GX + s[2], GY + s[3], s[4] + (2 - s[4]) * p, 1 - p) end end if m >= 120 and m < 140 then self:put("falling_don", 0, GX - 208, GY + tween(m, 120, 20, -208, -57, "in"), 1) elseif m >= 140 and m < 154 then local t = (m - 140) / 13 self:put("falling_don", 0, GX - 208 - 117 * t, GY - 57 - 140 * t + 290 * t * t, nil, nil, -720 * t) end if m >= 140 and m < 169 then local t = (m - 140) / 28 self:put("cat_mask", 0, GX - 142 + 224 * t, GY - 34 - 320 * t + 463 * t * t, nil, nil, -1080 * t) end if m >= 154 and m < 257 then local s = m < 174 and tween(m, 154, 20, 2, 0.9) or tween(m, 174, 5, 0.9, 1, "out") local fade = m < 174 and (m - 154) / 20 or m < 231 and 1 or tween(m, 231, 25, 1, 0) self:put("cross", 0, GX - 130, GY - 10, s, fade) end if m >= 164 and m < 335 then local x if m < 188 then x = tween(m, 164, 24, 337, 60, "out") elseif m < 194 then x = tween(m, 188, 6, 60, 100) elseif m < 305 then x = 100 elseif m < 310 then x = tween(m, 305, 5, 100, 70, "out") else x = tween(m, 310, 24, 70, 340, "in") end self:at("drummer", DRUMMER_SCOLD, x - 60, -9.65) end end -- slots 4 and 5: the drum turns to face the player and the face / rim flashes twice function TutorialOverlay:draw_face_slot(m, ms, flash, drummer_enters) local frame = (m >= 103 and m < 107 or m >= 196 and m < 199) and 3 or (m >= 107 and m < 196) and 4 or 0 self:drum(frame, self:drum_shake(ms), 0) for _, f0 in ipairs({ 122, 162 }) do if m >= f0 and m < f0 + 22 then self:at(flash, 0, 0, 0, m < f0 + 15 and 1 or tween(m, f0 + 15, 6, 0.855, 0)) end end if drummer_enters and m < 70 then if m >= 40 then self:at("drummer", DRUMMER_IDLE, tween(m, 40, 30, 320, 0, "out"), 0) end else self:draw_reactive(ms, 0) end end function TutorialOverlay:draw_drummer(slot, g, ms) if slot == 2 then self:draw_hit_slot(g - 7, ms) elseif slot == 3 then self:draw_side_slot(g + 8) elseif slot == 4 then self:draw_face_slot(g + 8, ms, "face_flash", true) elseif slot == 5 then self:draw_face_slot(g + 8, ms, "rim_flash", false) elseif slot >= 6 and slot <= 9 then local fade = slot == 9 and g > SLOTS[9].len - 7 and tween(g, SLOTS[9].len - 7, 6, 1, 0) or nil self:drum(0, self:drum_shake(ms), 0, fade) self:draw_reactive(ms, 0, fade) end end function TutorialOverlay:slot_at(f) local slot, g for i, s in ipairs(SLOTS) do if f >= s.start then slot, g = i, f - s.start end end return slot, g end function TutorialOverlay:draw_back(ms) local f = ms / FRAME_MS if f < 0 then return end self:draw_scenery(f) local slot, g = self:slot_at(f) if slot then self:draw_drummer(slot, math.floor(g), ms) end self:draw_don_chan(f) end function TutorialOverlay:caption(i) local c = self.captions[i] if c then return c end c = {} local str = tex.skin_text("tutorial_caption_" .. i) for line in (str .. "\n"):gmatch("(.-)\n") do local l = { parts = {}, width = 0 } local pos = 1 while pos <= #line do local a, b, name = line:find("{([%w_]+)}", pos) local chunk = line:sub(pos, (a or #line + 1) - 1) if #chunk > 0 then local t = text.create_raw_text(chunk, CAPTION_FONT, BLACK, { 255, 255, 255, 0 }, false, 4, 2.0) l.parts[#l.parts + 1] = { text = t, w = t.width } l.width = l.width + t.width end if not a then break end local key = "icon_" .. name if self.tex[key] then local w = self.size[key][1] + CAPTION_ICON_GAP * 2 l.parts[#l.parts + 1] = { icon = key, w = w } l.width = l.width + w end pos = b + 1 end c[#c + 1] = l end self.captions[i] = c return c end -- text area: the swg's 432x112 text texture, 8 / 11.8 px into the box function TutorialOverlay:draw_caption_text(i, x0) local K = self.K local left, top, avail, height = (x0 + 8) * K, (292 + 11.8) * K, 432 * K, 112 * K local lines = self:caption(i) local rows = CAPTION_ROWS[math.min(#lines, #CAPTION_ROWS)] for k, l in ipairs(lines) do if k > #rows then break end local s = math.min(1, avail / math.max(l.width, 1)) local w = l.width * s local x = left + (#lines == 1 and (avail - w) / 2 or (avail - w) * (k - 1) / (#lines - 1)) local cy = top + height * rows[k] for _, part in ipairs(l.parts) do if part.text then part.text:draw({ x = x, y = cy - part.text.height * s / 2, scale = s }) else local iw, ih = self.size[part.icon][1], self.size[part.icon][2] tex.draw_texture(self.tex[part.icon], { x = x + (part.w * s - iw) / 2, y = cy - ih / 2, scale = s, center = true }) end x = x + part.w * s end end end function TutorialOverlay:draw_caption(slot, g) local s = SLOTS[slot] local cx = 320 if g < 7 then cx = tween(g, 0, 7, 870, 320, "out") elseif g >= s.len - 7 then cx = tween(g, s.len - 7, 7, 320, -240, "in") end self:at("caption_box", s.box, cx - 320, 0) self:draw_caption_text(slot, cx - 224) end function TutorialOverlay:draw_skip(ms, rim_count) while self.rim_count < rim_count do self.rim_count = self.rim_count + 1 self.rim_ms[self.rim_count] = ms end local f = ms / FRAME_MS - SLOTS[1].start local dx = tween(f, 0, 12, -650.8, 0, "out") local bob, b = 0, f % 90 for _, s in ipairs(SKIP_BOB) do if b >= s[1] and b <= s[2] + 1 then bob = tween(b, s[1], s[2] + 1 - s[1], s[3], s[4], s[5]) end end self:at("skip_text", 0, dx, bob) for i = 1, math.min(self.rim_count, SKIP_HITS) do local p = (ms - self.rim_ms[i]) / FRAME_MS local pop = p < 5 and tween(p, 0, 5, 0.1, 1.2, "out") or tween(p, 5, 5, 1.2, 1) self:put("skip_icon", 0, 459.35 + 0.8 * SKIP_ICON_X[i] + dx, 405.35, 0.8 * pop) end end function TutorialOverlay:draw(ms, rim_count) local f = ms / FRAME_MS local slot, g = self:slot_at(f) if not slot then return end self:draw_caption(slot, g) self:draw_skip(ms, rim_count) end