2024-10-03 23:07:11 +00:00
|
|
|
local is_window_setup = false
|
2024-10-03 01:29:13 +00:00
|
|
|
|
2024-09-29 19:59:48 +00:00
|
|
|
---Return the userdata's name from its metatable.
|
|
|
|
---
|
|
|
|
---Returns nil if the userdata doesn't have a metatable.
|
2024-04-27 04:52:47 +00:00
|
|
|
---@param val userdata
|
2024-09-29 19:59:48 +00:00
|
|
|
---@return string|nil
|
2024-04-27 04:52:47 +00:00
|
|
|
function udname(val)
|
2024-09-29 19:59:48 +00:00
|
|
|
local tbl = debug.getmetatable(val)
|
|
|
|
|
|
|
|
if tbl == nil then
|
|
|
|
return nil
|
2024-09-28 16:32:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return tbl.__name
|
2024-04-27 04:52:47 +00:00
|
|
|
end
|
2024-10-13 15:43:49 +00:00
|
|
|
|
|
|
|
function dump(o)
|
|
|
|
if type(o) == 'table' then
|
|
|
|
local s = '{ '
|
|
|
|
for k,v in pairs(o) do
|
|
|
|
if type(k) ~= 'number' then k = '"'..k..'"' end
|
|
|
|
s = s .. '['..k..'] = ' .. dump(v) .. ','
|
|
|
|
end
|
|
|
|
return s .. '} '
|
|
|
|
else
|
|
|
|
return tostring(o)
|
|
|
|
end
|
|
|
|
end
|
2024-09-28 16:32:37 +00:00
|
|
|
|
2024-04-27 04:52:47 +00:00
|
|
|
|
|
|
|
function on_init()
|
2024-10-05 03:48:58 +00:00
|
|
|
local cube = world:request_asset("../assets/cube-texture-embedded.gltf") --[[@as GltfHandle]]
|
2024-04-27 04:52:47 +00:00
|
|
|
print("Loaded textured cube (" .. udname(cube) .. ")")
|
|
|
|
|
2024-09-29 19:59:48 +00:00
|
|
|
cube:wait_until_loaded()
|
2024-04-27 04:52:47 +00:00
|
|
|
local scenes = cube:scenes()
|
|
|
|
local cube_scene = scenes[1]
|
|
|
|
|
|
|
|
local pos = Transform.from_translation(Vec3.new(0, 0, -8.0))
|
|
|
|
|
|
|
|
local e = world:spawn(pos, cube_scene)
|
2024-09-29 19:59:48 +00:00
|
|
|
print("spawned entity " .. tostring(e))
|
2024-04-27 04:52:47 +00:00
|
|
|
end
|
|
|
|
|
2024-10-03 01:29:13 +00:00
|
|
|
function on_first()
|
2024-10-03 23:07:11 +00:00
|
|
|
if not is_window_setup then
|
|
|
|
world:view(
|
2024-10-04 19:07:42 +00:00
|
|
|
---@param w Window
|
|
|
|
function (w)
|
|
|
|
if w.cursor_grab == CursorGrabMode.NONE then
|
|
|
|
w.cursor_grab = CursorGrabMode.LOCKED
|
|
|
|
w.cursor_visible = false
|
|
|
|
return w
|
|
|
|
else
|
|
|
|
is_window_setup = true
|
|
|
|
print("Window setup")
|
|
|
|
end
|
|
|
|
end, Window
|
|
|
|
)
|
2024-10-03 01:29:13 +00:00
|
|
|
end
|
2024-10-13 15:43:49 +00:00
|
|
|
|
|
|
|
---@type EventReader
|
|
|
|
local reader = world:read_event(DeviceEvent)
|
|
|
|
|
|
|
|
---@param ev DeviceEvent
|
|
|
|
for ev in reader:read() do
|
|
|
|
if ev.event.kind == DeviceEventKind.MOTION then
|
|
|
|
local motion_ev = ev.event --[[@as DeviceEventMotion]]
|
|
|
|
print("axis: " .. tostring(motion_ev.axis) .. " = " .. tostring(motion_ev.value))
|
|
|
|
end
|
|
|
|
end
|
2024-04-27 04:52:47 +00:00
|
|
|
end
|
|
|
|
|
2024-10-03 01:29:13 +00:00
|
|
|
--[[ function on_pre_update()
|
2024-04-27 04:52:47 +00:00
|
|
|
print("Lua's pre-update function was called")
|
|
|
|
end ]]
|
|
|
|
|
|
|
|
function on_update()
|
2024-10-22 01:58:22 +00:00
|
|
|
-- Although WorldTransform isn't used, I only want to
|
|
|
|
-- modify entities with that component.
|
2024-10-23 20:31:47 +00:00
|
|
|
local view = View.new(Transform, Has(WorldTransform), Res(DeltaTime))
|
2024-10-19 21:45:59 +00:00
|
|
|
local res = world:view_query(view)
|
2024-10-22 01:58:22 +00:00
|
|
|
|
|
|
|
---@param transform Transform
|
|
|
|
---@param dt DeltaTime
|
2024-10-23 20:31:47 +00:00
|
|
|
for entity, transform, dt in res:iter() do
|
2024-10-19 21:45:59 +00:00
|
|
|
transform:translate(0, 0.15 * dt, 0)
|
|
|
|
entity:update(transform)
|
|
|
|
end
|
2024-10-21 01:20:43 +00:00
|
|
|
|
|
|
|
local changed_view = View.new(Changed(Transform))
|
|
|
|
local changed_res = world:view_query(changed_view)
|
2024-10-22 01:58:22 +00:00
|
|
|
---@param transform Transform
|
2024-10-21 01:20:43 +00:00
|
|
|
for _, transform in changed_res:iter() do
|
2024-10-22 01:58:22 +00:00
|
|
|
print("Entity transform changed to: '" .. tostring(transform) .. "' on tick " .. tostring(world:get_tick()))
|
2024-10-21 01:20:43 +00:00
|
|
|
end
|
2024-04-27 04:52:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[ function on_post_update()
|
|
|
|
print("Lua's post-update function was called")
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_last()
|
|
|
|
print("Lua's last function was called")
|
|
|
|
end ]]
|