Skip to content

Instantly share code, notes, and snippets.

@MikuAuahDark
Created December 10, 2022 01:40
Show Gist options
  • Save MikuAuahDark/11725e4760788f57672423346ab0fc1e to your computer and use it in GitHub Desktop.
Save MikuAuahDark/11725e4760788f57672423346ab0fc1e to your computer and use it in GitHub Desktop.
NAniTe Demo
local love = require("love")
local nanite = require("nanite")
local FOUR_DIAG_LEN = 96
local DIST_4_LETTER = FOUR_DIAG_LEN * 2 / math.sqrt(3)
local RAD = math.pi / 180
local NPadLogoData = {
timer = nil, ralewayBlack = nil, baked = nil, fontHeight = 0,
x1 = 0, x2 = 0, r = 0, fade = 1,
arc1 = 180, arc2 = 0,
drawArc = 0,
draw4 = 0,
l1x = 48,
l2x = -FOUR_DIAG_LEN, l2y = 0,
font = 0, drawFPer = 1,
explosion = 0,
}
---@type nanite
local animation
local direction = 1
local paused = false
---@type love.Font
local ralewayBlack
---@param x1 number
---@param y1 number
---@param x2 number
---@param y2 number
local function distance(x1, y1, x2, y2)
return math.sqrt((x2-x1)^2+(y2-y1)^2)
end
---@param v fun(x:number):number
local function out(v)
---@param p number
return function(p) return 1 - v(1 - p) end
end
---@param p number
local function quad(p)
return p * p
end
---@param p number
local function quart(p)
return p * p * p * p
end
local quartout = out(quart)
---@param p number
local function sine(p)
return -math.cos(p * (math.pi * .5)) + 1
end
---@param p number
local function back(p)
return p * p * (2.7 * p - 1.7)
end
function love.load()
local dist4 = distance(
-FOUR_DIAG_LEN, 0,
math.cos(30 * RAD) * (FOUR_DIAG_LEN * 2 / math.sqrt(3)) - FOUR_DIAG_LEN,
math.sin(30 * RAD) * (FOUR_DIAG_LEN * 2 / math.sqrt(3))
)
local distr = 144+dist4
animation = nanite(NPadLogoData, {
{
id = "bar",
start = 0.25,
duration = 0.5,
easing = quartout,
variables = {
x1 = -128,
x2 = 128,
drawArc = 1,
}
},
{
id = "arc",
start = "finish(bar)",
duration = 0.5,
easing = quartout,
variables = {
arc1 = 150,
arc2 = -30
}
},
{
start = "finish(arc)",
duration = 2,
easing = sine,
variables = {
fade = 0,
}
},
{
id = "rotate",
start = "finish(arc)",
duration = 2,
easing = out(back),
variables = {
r = 180+60,
draw4 = 1
}
},
{
start = "finish(rotate)",
duration = 2,
easing = quad,
variables = {
font = 1
}
},
{
id = "diagonal1",
start = "finish(rotate)",
duration = 288 / distr,
variables = {
l1x = -FOUR_DIAG_LEN
}
},
{
id = "diagonal2",
start = "finish(diagonal1)",
duration = dist4 / distr * 2,
variables = {
l2x = math.cos(30 * RAD) * DIST_4_LETTER - FOUR_DIAG_LEN,
l2y = math.sin(30 * RAD) * DIST_4_LETTER,
drawFPer = 0,
}
},
{
start = "finish(diagonal2)",
duration = 1.5,
easing = out(sine),
variables = {
explosion = 1
}
}
})
ralewayBlack = love.graphics.newFont("Raleway-Black.ttf", 36, "normal", 2)
love.keyboard.setKeyRepeat(true)
end
function love.update(dt)
if not paused then
animation:update(direction * dt)
end
end
local function drawText()
love.graphics.printf(
"IX", ralewayBlack,
math.cos(20 * RAD) * -120,
math.sin(20 * RAD) * -120,
64, "center", 0,
1, 1, 32, ralewayBlack:getHeight() / 2)
love.graphics.printf(
"III", ralewayBlack,
math.cos(20 * RAD) * 120,
math.sin(20 * RAD) * 120,
64, "center", 0,
1, 1, 32, ralewayBlack:getHeight() / 2)
end
---@param cw number
---@param ch number
---@param s number
local function drawLogoAnimated(cw, ch, s)
local eOp = 1 - NPadLogoData.explosion
love.graphics.push("all")
love.graphics.setLineWidth(10)
love.graphics.setLineJoin("miter")
love.graphics.translate(cw, ch)
love.graphics.scale(s)
if NPadLogoData.font > 0 then
if NPadLogoData.drawFPer > 0 then
local eq = 4 * math.sin(172 * NPadLogoData.font)
if eq >= 1 then
love.graphics.setColor(1, 1, 1, NPadLogoData.font * eOp)
drawText()
end
else
love.graphics.setColor(1, 1, 1, eOp)
drawText()
end
end
love.graphics.rotate(math.rad(NPadLogoData.r % 180))
love.graphics.setColor(1, 1, 1, eOp)
love.graphics.line(NPadLogoData.x1, 1, NPadLogoData.x1, 0, NPadLogoData.x2, 0, NPadLogoData.x2, -1)
if NPadLogoData.drawArc >= 1 then
love.graphics.arc("line", "open", 0, 0, 128, math.pi, NPadLogoData.arc1 * RAD, 360)
love.graphics.arc("line", "open", 0, 0, 128, 0, NPadLogoData.arc2 * RAD, 360)
end
if NPadLogoData.draw4 >= 1 then
love.graphics.rotate(-math.pi / 2)
if NPadLogoData.l2x > -FOUR_DIAG_LEN then
love.graphics.line(48, 0, NPadLogoData.l1x, 0, NPadLogoData.l2x, NPadLogoData.l2y)
else
love.graphics.line(48, 0, NPadLogoData.l1x, 0)
end
end
if NPadLogoData.explosion > 0 then
love.graphics.rotate(math.rad(90 - NPadLogoData.r))
love.graphics.setLineWidth(10 - NPadLogoData.explosion * 6)
love.graphics.setColor(1, 1, 1, eOp)
love.graphics.circle("line", 0, 0, NPadLogoData.explosion * 130)
end
love.graphics.pop()
end
function love.draw()
local w, h = love.graphics.getDimensions()
drawLogoAnimated(w / 2, h / 2, 2)
if direction < 0 then
love.graphics.print("BACKWARDS", 4, 4)
end
if paused then
love.graphics.print("PAUSED", 4, 16)
end
end
function love.keypressed(k)
if k == "left" then
animation:update(-0.005 * (love.keyboard.isDown("lshift", "rshift") and 3 or 1))
elseif k == "right" then
animation:update(0.005 * (love.keyboard.isDown("lshift", "rshift") and 3 or 1))
end
end
function love.keyreleased(k)
if k == "return" then
direction = direction * -1
elseif k == "space" then
paused = not paused
elseif k == "escape" then
love.event.quit()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment