me.fier.engine/mods/main/script/game.lua

218 lines
5.4 KiB
Lua
Raw Normal View History

2023-08-10 00:10:09 +00:00
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
2023-08-10 00:27:52 +00:00
local framelimit = 60
framelimit = math.floor(1000 / framelimit)
2023-08-10 00:10:09 +00:00
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()
2023-08-10 00:27:52 +00:00
frametime = framelimit
2023-08-10 00:10:09 +00:00
function tick()
logic()
engine_window_present(window)
trackFps()
local curFrame = engine_time_get()
frametime = (curFrame - lastFrame)
2023-08-10 00:27:52 +00:00
local wait = framelimit - frametime
2023-08-10 00:10:09 +00:00
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
2023-08-10 00:28:04 +00:00
local t = engine_time_get()
2023-08-10 00:10:09 +00:00
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,90);
sx = math.floor((width * 0.5) + (sx * (width * 0.5)))
sy = math.floor((height * 0.5) + (sy * (height * 0.5)))
-- Clip point within bounds (debugging)
if sx < 0 then sx = 0 end
if sx > width - 1 then sx = width - 1 end
if sy < 0 then sy = 0 end
if sy > height - 1 then sy = height - 1 end
if sx < 0 or sx > width - 1 then return end
if sy < 0 or sy > height - 1 then return end
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()