pxl8/src/lua/pxl8/world.lua

129 lines
3.2 KiB
Lua
Raw Normal View History

local ffi = require("ffi")
local C = ffi.C
local core = require("pxl8.core")
local world = {}
2026-01-25 09:26:30 -06:00
world.CHUNK_VXL = 0
world.CHUNK_BSP = 1
2026-01-25 09:26:30 -06:00
local Bsp = {}
Bsp.__index = Bsp
2026-01-25 09:26:30 -06:00
function Bsp:face_count()
return C.pxl8_bsp_face_count(self._ptr)
end
2026-01-25 09:26:30 -06:00
function Bsp:face_normal(face_id)
return C.pxl8_bsp_face_normal(self._ptr, face_id)
end
2026-01-25 09:26:30 -06:00
function Bsp:face_set_material(face_id, material_id)
C.pxl8_bsp_face_set_material(self._ptr, face_id, material_id)
end
2026-01-25 09:26:30 -06:00
world.Bsp = Bsp
local Chunk = {}
Chunk.__index = Chunk
function Chunk:bsp()
if self._ptr == nil then return nil end
if self._ptr.type ~= world.CHUNK_BSP then return nil end
local ptr = self._ptr.bsp
if ptr == nil then return nil end
return setmetatable({ _ptr = ptr }, Bsp)
end
2026-01-25 09:26:30 -06:00
function Chunk:ready()
if self._ptr == nil then return false end
if self._ptr.type == world.CHUNK_BSP then
return self._ptr.bsp ~= nil
elseif self._ptr.type == world.CHUNK_VXL then
2026-01-31 09:31:17 -06:00
return self._ptr.voxels ~= nil
end
2026-01-25 09:26:30 -06:00
return false
end
2026-01-31 09:31:17 -06:00
function Chunk:type()
if self._ptr == nil then return nil end
return self._ptr.type
end
function Chunk:version()
if self._ptr == nil then return 0 end
return self._ptr.version
end
2026-01-25 09:26:30 -06:00
world.Chunk = Chunk
local World = {}
World.__index = World
function World.get()
local w = C.pxl8_get_world(core.sys)
if w == nil then return nil end
return setmetatable({ _ptr = w }, World)
end
2026-01-25 09:26:30 -06:00
function World:active_chunk()
local ptr = C.pxl8_world_active_chunk(self._ptr)
if ptr == nil then return nil end
return setmetatable({ _ptr = ptr }, Chunk)
end
2026-01-31 09:31:17 -06:00
function World:get_render_distance()
return C.pxl8_world_get_render_distance(self._ptr)
end
function World:get_sim_distance()
return C.pxl8_world_get_sim_distance(self._ptr)
end
function World:init_local_player(x, y, z)
C.pxl8_world_init_local_player(self._ptr, x, y, z)
end
function World:local_player()
local ptr = C.pxl8_world_local_player(self._ptr)
if ptr == nil then return nil end
return ptr
end
function World:point_solid(x, y, z)
return C.pxl8_world_point_solid(self._ptr, x, y, z)
end
function World:ray(from_x, from_y, from_z, to_x, to_y, to_z)
local from = ffi.new("pxl8_vec3", {x = from_x, y = from_y, z = from_z})
local to = ffi.new("pxl8_vec3", {x = to_x, y = to_y, z = to_z})
return C.pxl8_world_ray(self._ptr, from, to)
end
function World:render(camera_pos)
local vec = ffi.new("pxl8_vec3", {x = camera_pos[1], y = camera_pos[2], z = camera_pos[3]})
C.pxl8_world_render(self._ptr, core.gfx, vec)
end
2026-01-31 09:31:17 -06:00
function World:set_bsp_material(material_id, material)
C.pxl8_world_set_bsp_material(self._ptr, material_id, material._ptr)
end
function World:set_render_distance(distance)
C.pxl8_world_set_render_distance(self._ptr, distance)
end
function World:set_sim_distance(distance)
C.pxl8_world_set_sim_distance(self._ptr, distance)
end
function World:sweep(from_x, from_y, from_z, to_x, to_y, to_z, radius)
local from = ffi.new("pxl8_vec3", {x = from_x, y = from_y, z = from_z})
local to = ffi.new("pxl8_vec3", {x = to_x, y = to_y, z = to_z})
2026-01-31 09:31:17 -06:00
return C.pxl8_world_sweep(self._ptr, from, to, radius)
end
world.World = World
return world