aintnothingbutapeanut/data/scripts/game.lua
2025-01-28 21:17:48 +03:00

117 lines
3.8 KiB
Lua

local traxx = require "traxx"
require "utils"
require "string"
require "math"
function start_blasting(track)
local blast = table.shallow_copy(traxx[track])
blast.channel = "blast"
ctx.udata.blast = blast
ctx.udata.last_beat = ctx.udata.time + 60 / blast.bpm
audio_play(ctx.udata.blast)
end
function process_player(state)
input_action { name = "up", control = "W" }
input_action { name = "left", control = "A" }
input_action { name = "down", control = "S" }
input_action { name = "right", control = "D" }
-- local right = vec3_norm(vec3_cross(state.player.camera_direction, { x=0, y=1, z=0 }))
-- local up = { x = right.z, y = right.y, z = -right.x }
local right = { x=-1, y=0, z=0 }
local up = { x=0, y=0, z=1 }
local direction = { x = 0, y = 0 , z = 0 }
if input_action_pressed { name = "up" } then
direction = vec3_add(direction, up)
end
if input_action_pressed { name = "down" } then
direction = vec3_sub(direction, up)
end
if input_action_pressed { name = "right" } then
direction = vec3_add(direction, right)
end
if input_action_pressed { name = "left" } then
direction = vec3_sub(direction, right)
end
if vec3_length(direction) > 0 then
direction = vec3_norm(direction)
end
state.player.position = vec3_add(state.player.position, vec3_scale(direction, state.player.speed / (60 / state.blast.bpm)))
local wobble = sinease(24, state.time - state.beat * (60 / state.blast.bpm) , (60 / state.blast.bpm) / 1.5, 0)
draw_billboard {
texture = "art/creatures/peanut/" .. string.format("dance-%s.png", pingpong(state.beat, 3)),
position = { x = state.player.position.x, y = wobble / 64, z = state.player.position.z },
size = { x = 1, y = (64 + wobble) / 64 },
}
end
function game_tick()
if ctx.initialization_needed then
ctx.udata = {
time = 0,
beat = 0,
player = {
position = { x=3, y=0, z=0 },
speed = 0.06, -- tiles per beat
camera_direction = { x=-math.sin(math.pi / 4), y=-1, z=math.cos(math.pi / 4) },
}
}
start_blasting("mod22")
end
local state = ctx.udata
process_player(state)
draw_camera {
position = state.player.position,
direction = state.player.camera_direction,
-- direction = { x=-math.sin(ctx.frame_number / 100), y=-1, z=math.cos(ctx.frame_number / 100) },
up = { x=0, y=1, z=0 },
fov = 0,
zoom = 0.15 + math.sin(ctx.frame_number / 100) / 20,
}
-- draw_rectangle {
-- rect = { w = 960, h = 540 },
-- color = { r = 125, a = 255 }
-- }
state.time = state.time + ctx.frame_duration
if state.time - state.last_beat > 60 / state.blast.bpm then
state.last_beat = state.last_beat + 60 / state.blast.bpm
state.beat = state.beat + 1
-- draw_rectangle {
-- rect = { w = 64, h = 64 },
-- color = { r = 255, a = 255 }
-- }
end
local wobble = sinease(12, state.time - state.beat * (60 / state.blast.bpm) , (60 / state.blast.bpm) / 1.5, 0)
for i = -10,10 do
draw_quad {
texture = "art/creatures/meathead/" .. string.format("dance-%s.png", pingpong(state.beat, 3)),
texture_region = { w = 32, h = 32 },
v0 = { x = 0, y = (64 + wobble) / 64, z = 0 + i/2 },
v1 = { x = 0, y = 0, z = 0 + i/2 },
v2 = { x = 0, y = 0, z = 0 + i/2 + 1 },
v3 = { x = 0, y = (64 + wobble) / 64, z = 0 + i/2 + 1 },
}
draw_billboard {
texture = "art/creatures/meathead/" .. string.format("dance-%s.png", pingpong(state.beat, 3)),
position = { x = i, y = wobble / 64, z = 0 },
size = { x = 1, y = (64 + wobble) / 64 },
}
end
end