- Classe mapa pot guardar i carregar

- [WIP] Editor bàsic
- Classe textbox
This commit is contained in:
2023-01-27 19:05:35 +01:00
parent 43ac9f26a2
commit c679a6dc39
7 changed files with 169 additions and 17 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ draft/*
*.exe *.exe
mini mini
mini_debug mini_debug
*.dll

View File

@@ -1,7 +1,62 @@
function editor_init() editor={
set_update(editor_update) paused=true,
end cam={x=0,y=0},
selected_tile=3,
function editor_update() init=function()
set_update(editor.update)
end editor.show_menu()
end,
show_menu=function()
menu.show({ {"NEW MAP", editor.new},
{"LOAD MAP", function() textbox.show("FILENAME TO LOAD:",editor.load, mapa.name) end},
{"SAVE MAP", function() if mapa.name~=nil then editor.save(mapa.name) else textbox.show("FILENAME TO SAVE:",editor.save, mapa.name) end end},
-- {"BACK TO EDITOR", function() editor.paused=false end},
{"EXIT", main_init},
-- {"ENNER LLOR NEIM", function() textbox.show("PELANDRUSC!") end}
}, function()editor.paused=false end)
end,
update=function()
cls()
camera(editor.cam.x, editor.cam.y)
map(0,0,0,0,mapa.w, mapa.h)
local mx,my=mousex()+editor.cam.x,mousey()+editor.cam.y
local tx,ty=mx>>3,my>>3
local rx,ry=tx<<3,ty<<3
rect(rx-1, ry-1, rx+8, ry+8, 10)
camera(0,0)
if not editor.paused then
if mbtn(1) then
mset(tx,ty,editor.selected_tile)
end
if btnp(KEY_RIGHT) then editor.cam.x=editor.cam.x+8 end
if btnp(KEY_LEFT) then editor.cam.x=editor.cam.x-8 end
if btnp(KEY_UP) then editor.cam.y=editor.cam.y-8 end
if btnp(KEY_DOWN) then editor.cam.y=editor.cam.y+8 end
if btnp(KEY_ESCAPE) then
editor.paused=true
editor.show_menu()
end
end
end,
new=function()
mapa.new(128,128)
editor.paused=false
end,
save=function(filename)
mapa.save(filename)
editor.paused=false
end,
load=function(filename)
mapa.load(filename)
editor.paused=false
end
}

View File

@@ -3,4 +3,4 @@ config=ja2
width=160 width=160
height=144 height=144
zoom=5 zoom=5
files=mapa.lua,editor.lua,menu.lua,main.lua files=mapa.lua,editor.lua,textbox.lua,menu.lua,main.lua

View File

@@ -7,9 +7,18 @@ function _init()
local pal=loadpal("tiles.gif") local pal=loadpal("tiles.gif")
setpal(pal) setpal(pal)
main_init()
end
function _update()
update()
end
function main_init()
set_update(menu_update) set_update(menu_update)
second_menu = {{"PEIV", function() end},{"TORNAR",show_main_menu}} second_menu = {{"PEIV", function() end},{"TORNAR",show_main_menu}}
main_menu = { {"EDITOR", editor_init}, {"EIXIR", quit}, {"TEST", function() menu.show(second_menu) end } } main_menu = { {"EDITOR", editor.init}, {"EIXIR", quit}, {"TEST", function() menu.show(second_menu) end } }
show_main_menu() show_main_menu()
end end
@@ -21,10 +30,6 @@ function set_update(new_update)
update=new_update update=new_update
end end
function _update()
update()
end
function menu_update() function menu_update()
cls() cls()
color(1) color(1)

View File

@@ -1,19 +1,56 @@
mapa={ mapa={
name=nil,
w=128, w=128,
h=128, h=128,
surface=nil, surface=nil,
new = function(w,h) new = function(w,h)
mapa.name=nil
mapa.w,mapa.h=w,h mapa.w,mapa.h=w,h
if mapa.surface~=nil then freesurf(mapa.surface) end if mapa.surface~=nil then freesurf(mapa.surface) end
mapa.surface=newsurf(w,h) mapa.surface=newsurf(w,h)
setmap(mapa.surface) setmap(mapa.surface)
for y=0,127 do
for x=0,127 do
mset(x,y,0)
end
end
end, end,
load = function(filename) load = function(filename)
file = io.open("data/"..filename, "r")
if file then
io.input(file)
mapa.w = io.read()
mapa.h = io.read()
mapa.new(mapa.w, mapa.h)
mapa.name=filename
for y=0,mapa.h-1 do
local line = io.read()
local x=0
for m in string.gmatch(line, "%d+") do
mset(x,y,m)
x=x+1
end
end
io.close(file)
end
end, end,
save = function(filename) save = function(filename)
mapa.name=filename
if file then
file = io.open("data/"..filename, "w")
io.output(file)
io.write(mapa.w.."\n")
io.write(mapa.h.."\n")
for y=0,mapa.h-1 do
for x=0,mapa.w-1 do
io.write(mget(x,y)..",")
end
io.write("\n")
end
io.close(file)
end
end end
} }

View File

@@ -2,19 +2,25 @@ menu = {
options=nil, options=nil,
old_update=nil, old_update=nil,
selected=1, selected=1,
return_function=nil,
show=function(op) show=function(op, retfun)
menu.options=op menu.options=op
menu.old_update=update menu.old_update=update
update=menu.update update=menu.update
menu.selected=1 menu.selected=1
menu.return_function=retfun
end, end,
update=function() update=function()
menu.old_update() menu.old_update()
rectfill(10,20, 150, 34+#menu.options*10,6)
rect(10,20, 150, 34+#menu.options*10,8)
for i,v in ipairs(menu.options) do for i,v in ipairs(menu.options) do
color(4) if menu.selected==i then color(15) prnt(">",10,20+i*10) end color(4) if menu.selected==i then color(15) prnt(">",20,20+i*10) end
prnt(v[1],14,20+i*10) prnt(v[1],24,20+i*10)
end end
if btnp(KEY_DOWN) then menu.selected=menu.selected+1 end if btnp(KEY_DOWN) then menu.selected=menu.selected+1 end
@@ -26,6 +32,13 @@ menu = {
update=menu.old_update update=menu.old_update
menu.options[menu.selected][2]() menu.options[menu.selected][2]()
end end
if btnp(KEY_ESCAPE) then
if menu.return_function then
update=menu.old_update
menu.return_function()
end
end
end end
} }

41
data/textbox.lua Normal file
View File

@@ -0,0 +1,41 @@
keyvalues="000abcdefghijklmnopqrstuvwxyz1234567890"
textbox={
value=nil,
title=nil,
old_update=nil,
return_function=nil,
show=function(title, retfun, default_value)
textbox.title=title
textbox.value=default_value
textbox.old_update=update
update=textbox.update
textbox.return_function=retfun
if textbox.value==nil then textbox.value="" end
end,
update=function()
rectfill(20,50, 140, 90,6)
rect(20,50, 140, 90,8)
if textbox.title then prnt(textbox.title,30,55) end
rect(40,70, 120, 80,8)
if textbox.value then
prnt(textbox.value,43,73)
if (flr(time()*4)%2)==0 then
prnt("_",43+#textbox.value*4,73)
end
end
local tecla=btnp()
if tecla>=4 and tecla<=39 then
textbox.value=textbox.value..string.sub(keyvalues,tecla,tecla)
elseif tecla==KEY_PERIOD then
textbox.value=textbox.value.."."
elseif tecla==KEY_BACKSPACE then
textbox.value=string.sub(textbox.value,1,-2)
elseif tecla==KEY_RETURN then
update=textbox.old_update
textbox.return_function(textbox.value)
end
end
}