me.fier.engine/mods/main/script/game.lua
2023-08-11 14:17:15 +02:00

210 lines
5.1 KiB
Lua

package.path = basepath.. "/mods/main/script/?.lua;" ..basepath.. "/mods/main/script/?/main.lua;" ..package.path
function bp(pth)
return basepath .. "/mods/main/" ..pth
end
local width = 320
local height = 240
local framelimit = 60
local fovVert = 67
local fovHor = fovVert * (width / height)
framelimit = math.floor(1000 / framelimit)
local px = -1
local py = -0.4
local pz = 0.5
local prx = 0
local pry = 0
function main()
window = engine_window_create(width,height,"engine")
-- Load chars
loadchar("invalid","invalid")
for ch=0,9,1 do loadchar(tostring(ch),tostring(ch)) end
loadchar("space"," ")
loadchar("dot",".")
loadchar("minus","-")
engine_rendertarget_set(engine_window_texture_get(window))
-- Clear window
engine_color_set(0,0,0,255)
engine_rendertarget_fill(0,0,width - 1,height - 1)
while true do
event = engine_event_get()
eventData = {engine_lua_event_get_data(event)}
engine_event_free(event)
if eventData[1] ~= ENGINE_EVENT_TYPE_NONE then
handleEvent(event,eventData)
else
tick()
end
end
end
function logic()
engine_color_set(0,0,20,255)
engine_rendertarget_fill(0,0,width - 1,height - 1)
if pressedKeys[26] then -- w
prx = prx + (0.1 * frametime)
end
if pressedKeys[22] then -- s
prx = prx - (0.1 * frametime)
end
if pressedKeys[7] then -- d
pry = pry + (0.1 * frametime)
end
if pressedKeys[4] then -- a
pry = pry - (0.1 * frametime)
end
local fwdX,fwdY,fwdZ = engine_3d_rotate_position(1,0,0,0,-pry,0)
local rgtX,rgtY,rgtZ = engine_3d_rotate_position(0,0,1,0,-pry,0)
if pressedKeys[82] then -- up
px = px + (fwdX * (0.01 * frametime))
py = py + (fwdY * (0.01 * frametime))
pz = pz + (fwdZ * (0.01 * frametime))
end
if pressedKeys[81] then -- down
px = px - (fwdX * (0.01 * frametime))
py = py - (fwdY * (0.01 * frametime))
pz = pz - (fwdZ * (0.01 * frametime))
end
if pressedKeys[80] then -- left
px = px - (rgtX * (0.01 * frametime))
py = py - (rgtY * (0.01 * frametime))
pz = pz - (rgtZ * (0.01 * frametime))
end
if pressedKeys[79] then -- right
px = px + (rgtX * (0.01 * frametime))
py = py + (rgtY * (0.01 * frametime))
pz = pz + (rgtZ * (0.01 * frametime))
end
if pressedKeys[75] then -- pgup
py = py - (0.01 * frametime)
end
if pressedKeys[78] then -- pgdown
py = py + (0.01 * frametime)
end
engine_color_set(255,0,255,255)
for i=0,1,0.1 do draw3dpoint(i,0,0) end
for i=0,1,0.1 do draw3dpoint(i,1,0) end
engine_color_set(64,0,64,255)
for i=0,1,0.1 do draw3dpoint(i,0,1) end
for i=0,1,0.1 do draw3dpoint(i,1,1) end
engine_color_set(255,0,255,255)
for i=0,1,0.1 do draw3dpoint(0,i,0) end
for i=0,1,0.1 do draw3dpoint(1,i,0) end
engine_color_set(64,0,64,255)
for i=0,1,0.1 do draw3dpoint(0,i,1) end
for i=0,1,0.1 do draw3dpoint(1,i,1) end
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(0,0,i) end
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(1,0,i) end
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(0,1,i) end
for i=0,1,0.1 do engine_color_set(255 - math.ceil(192 * i),0,255 - math.ceil(192 * i),255) draw3dpoint(1,1,i) end
if framerate ~= nil then drawText(framerate,0,0) end
drawText(prettyNumber(px),0,16)
drawText(prettyNumber(py),0,24)
drawText(prettyNumber(pz),0,32)
drawText(prettyNumber(prx),0,48)
drawText(prettyNumber(pry),0,56)
end
lastFrame = engine_time_get()
frametime = framelimit
function tick()
logic()
engine_window_present(window)
trackFps()
local curFrame = engine_time_get()
frametime = (curFrame - lastFrame)
local wait = framelimit - frametime
if wait > 0 then engine_time_sleep(wait) end
lastFrame = engine_time_get()
end
pressedKeys = {}
function handleEvent()
if eventData[1] == ENGINE_EVENT_TYPE_EXIT then
os.exit()
end
if eventData[1] == ENGINE_EVENT_TYPE_INPUT_KB then
if eventData[3] == 1 then
print(tostring(eventData[2]).. " pressed")
pressedKeys[eventData[2]] = true
else
print(tostring(eventData[2]).. " released")
pressedKeys[eventData[2]] = nil
end
end
end
framerate = 0
local frameSec = 0
local lastSec = 0
function trackFps()
frameSec = frameSec + 1
local t = engine_time_get()
if t - lastSec >= 1000 then
framerate = frameSec
lastSec = t
frameSec = 0
end
end
function draw3dpoint(x,y,z)
x = px - x
y = py - y
z = pz - z
local sx,sy = engine_3d_project(x,y,z,prx,pry,0,fovHor,fovVert)
sx,sy = engine_position_2d_to_tex(sx,sy,width,height,1)
engine_rendertarget_draw_point(sx,sy)
end
chars = {}
function loadchar(fi,ch)
local tex = engine_texture_create(8,8)
engine_rendertarget_set(tex)
engine_rendertarget_draw_file(bp("texture/char/" ..fi.. ".rgba"))
chars[ch] = tex
end
function drawText(text,x,y)
local len = string.len(text)
local index = 1
while index <= len do
local char = string.sub(text,index,index)
if chars[char] == nil then char = "invalid" end
engine_rendertarget_draw_texture(chars[char],x + (8 * (index - 1)),y)
index = index + 1
end
end
function prettyNumber(nr)
local out = string.format("%.2f",nr)
if string.sub(out,1,1) ~= "-" then out = " " ..out end
return out
end
main()