From c679a6dc398f613b225dbee1e9ea83cdc443fbf5 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Fri, 27 Jan 2023 19:05:35 +0100 Subject: [PATCH] =?UTF-8?q?-=20Classe=20mapa=20pot=20guardar=20i=20carrega?= =?UTF-8?q?r=20-=20[WIP]=20Editor=20b=C3=A0sic=20-=20Classe=20textbox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + data/editor.lua | 67 +++++++++++++++++++++++++++++++++++++++++++----- data/game.ini | 2 +- data/main.lua | 15 +++++++---- data/mapa.lua | 39 +++++++++++++++++++++++++++- data/menu.lua | 21 ++++++++++++--- data/textbox.lua | 41 +++++++++++++++++++++++++++++ 7 files changed, 169 insertions(+), 17 deletions(-) create mode 100644 data/textbox.lua diff --git a/.gitignore b/.gitignore index a681663..5bef2f2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ draft/* *.exe mini mini_debug +*.dll \ No newline at end of file diff --git a/data/editor.lua b/data/editor.lua index 0abf3c9..36c88a9 100644 --- a/data/editor.lua +++ b/data/editor.lua @@ -1,7 +1,62 @@ -function editor_init() - set_update(editor_update) -end +editor={ + paused=true, + cam={x=0,y=0}, + selected_tile=3, -function editor_update() - -end \ No newline at end of file + init=function() + set_update(editor.update) + 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 +} diff --git a/data/game.ini b/data/game.ini index 762d076..0fb3cbb 100644 --- a/data/game.ini +++ b/data/game.ini @@ -3,4 +3,4 @@ config=ja2 width=160 height=144 zoom=5 -files=mapa.lua,editor.lua,menu.lua,main.lua +files=mapa.lua,editor.lua,textbox.lua,menu.lua,main.lua diff --git a/data/main.lua b/data/main.lua index b844585..4942cf4 100644 --- a/data/main.lua +++ b/data/main.lua @@ -7,9 +7,18 @@ function _init() local pal=loadpal("tiles.gif") setpal(pal) + main_init() +end + +function _update() + update() +end + + +function main_init() set_update(menu_update) 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() end @@ -21,10 +30,6 @@ function set_update(new_update) update=new_update end -function _update() - update() -end - function menu_update() cls() color(1) diff --git a/data/mapa.lua b/data/mapa.lua index af03d0e..9c7a50b 100644 --- a/data/mapa.lua +++ b/data/mapa.lua @@ -1,19 +1,56 @@ mapa={ + name=nil, w=128, h=128, surface=nil, new = function(w,h) + mapa.name=nil mapa.w,mapa.h=w,h if mapa.surface~=nil then freesurf(mapa.surface) end mapa.surface=newsurf(w,h) setmap(mapa.surface) + for y=0,127 do + for x=0,127 do + mset(x,y,0) + end + end end, 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, 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 } \ No newline at end of file diff --git a/data/menu.lua b/data/menu.lua index 0993876..d6833ae 100644 --- a/data/menu.lua +++ b/data/menu.lua @@ -2,19 +2,25 @@ menu = { options=nil, old_update=nil, selected=1, + return_function=nil, - show=function(op) + show=function(op, retfun) menu.options=op menu.old_update=update update=menu.update menu.selected=1 + menu.return_function=retfun end, update=function() 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 - color(4) if menu.selected==i then color(15) prnt(">",10,20+i*10) end - prnt(v[1],14,20+i*10) + color(4) if menu.selected==i then color(15) prnt(">",20,20+i*10) end + prnt(v[1],24,20+i*10) end if btnp(KEY_DOWN) then menu.selected=menu.selected+1 end @@ -26,6 +32,13 @@ menu = { update=menu.old_update menu.options[menu.selected][2]() end - + + if btnp(KEY_ESCAPE) then + if menu.return_function then + update=menu.old_update + menu.return_function() + end + end + end } diff --git a/data/textbox.lua b/data/textbox.lua new file mode 100644 index 0000000..b230937 --- /dev/null +++ b/data/textbox.lua @@ -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 +} \ No newline at end of file