Files
paku/data/editor.lua
Raimon Zamora cd27490426 - [NEW] Ja es poden crear items i es guarden
- [NEW] Ja es pot modificar el flip dels items
- [NEW] si tenim un item pa pintar, amb F fem flip abans de soltar-lo
- [NEW] gestió correcta dels items de cada habitació
- [FIX] Ja es carreguen els items de la primer habitació
2025-11-15 09:34:02 +01:00

292 lines
9.8 KiB
Lua

require "menu"
require "msgbox"
require "items"
editor = {
layer = LAYER_FOREGROUND,
brush={w=2,h=1,tiles={16,17}},
selection=nil,
ants=0xc936,
modified = false,
item_selected=nil,
item_hovered=nil,
flip=false,
enable = function()
app.update = editor.update
sys.beat(2)
shader.disable();
end,
update = function()
view.origin(0,0)
surf.target(0)
-- Pintar el menú (el marcador serà en el modul game.lua)
--score.draw()
menu.draw()
-- Pintar el mapa i sprites
rooms.draw()
--view.origin(0,0)
local mx, my = mouse.pos()
local tx, ty = (mx>>3), (my>>3)
mx, my = tx<<3, ty<<3
if my>=0 then
if editor.layer==LAYER_BACKGROUND or editor.layer==LAYER_FOREGROUND then
if editor.selection then
local rx1,ry1,rx2,ry2=editor.selection.x1<<3,editor.selection.y1<<3,editor.selection.x2<<3,editor.selection.y2<<3
if rx1>rx2 then rx1,rx2=rx2,rx1 end
if ry1>ry2 then ry1,ry2=ry2,ry1 end
draw.pattern(editor.ants)
draw.rect(rx1-1, ry1-1, rx2-rx1+10, ry2-ry1+10, 28)
draw.pattern(0xffff)
if sys.beat() then
editor.ants = (editor.ants<<12) | (editor.ants>>4)
end
else
draw.rect(mx-1, my-1, editor.brush.w*8+2, editor.brush.h*8+2, 28)
draw.rect(mx, my, editor.brush.w*8, editor.brush.h*8, 1)
end
if mouse.down(mouse.LEFT) then
editor.stamp(tx,ty)
editor.modified = true
--map.tile(tx,ty,editor.brush.tiles[1])
end
if mouse.down(mouse.RIGHT) then
if editor.selection then
editor.selection.x2=tx
editor.selection.y2=ty
else
editor.selection={}
editor.selection.x1=tx
editor.selection.y1=ty
editor.selection.x2=tx
editor.selection.y2=ty
end
else
if editor.selection then
editor.create_stamp()
editor.selection=nil
end
end
else
if mouse.press(mouse.LEFT) then
if editor.item_selected then
map.surf(rooms.surf_items)
--print(editor.item_selected)
map.tile(tx,ty,editor.item_selected)
local room = tx + ty * 8
table.insert(sprites.list, templates.create(items[editor.item_selected].name, {pos={x=tx*8, y=ty*8},flipped=editor.flip, room=room}))
editor.modified = true
--surf.target(0)
end
end
if mouse.press(mouse.RIGHT) then
editor.item_selected = nil
end
if editor.item_selected then
local k = items[editor.item_selected]
draw.surf(k.visual.x, k.visual.y, k.visual.w, k.visual.h, mx, my, k.visual.w, k.visual.h, editor.flip)
end
end
end
view.origin(0,0)
draw.text(rooms.pos.x//20,1,96,28)
draw.text(rooms.pos.y//12,5,96,28)
if key.press(key.ESCAPE) then
editor.quit()
elseif key.press(key.RIGHT) and rooms.pos.x < 20*7 then
rooms.pos.x = rooms.pos.x + 20
sprites.remove_out_of_room()
sprites.add_from_room(rooms.pos.x, rooms.pos.y)
elseif key.press(key.LEFT) and rooms.pos.x > 0 then
rooms.pos.x = rooms.pos.x - 20
sprites.remove_out_of_room()
sprites.add_from_room(rooms.pos.x, rooms.pos.y)
elseif key.press(key.DOWN) and rooms.pos.y < 12*7 then
rooms.pos.y = rooms.pos.y + 12
sprites.remove_out_of_room()
sprites.add_from_room(rooms.pos.x, rooms.pos.y)
elseif key.press(key.UP) and rooms.pos.y > 0 then
rooms.pos.y = rooms.pos.y - 12
sprites.remove_out_of_room()
sprites.add_from_room(rooms.pos.x, rooms.pos.y)
elseif key.press(key.TAB) or key.press(key.ESCAPE) then
if editor.layer==LAYER_BACKGROUND or editor.layer==LAYER_FOREGROUND then
editor.tilepicker.show()
else
editor.itempicker.show()
end
elseif key.press(key.P) then
sprites.hero.pos.x = tx*8-4
sprites.hero.pos.y = ty*8-9
elseif key.press(key.F9) then
editor.play()
elseif key.press(key.F11) then
reload_textures()
end
if editor.layer==LAYER_ITEMS then
if key.press(key.F) then
if editor.item_selected then
editor.flip = not editor.flip
elseif editor.item_hovered then
local sprite = sprites.list[editor.item_hovered]
sprite.flipped = not sprite.flipped
map.surf(rooms.surf_items)
local tile = map.tile(sprite.pos.x//8, sprite.pos.y//8)
if tile > 0x7f then tile = tile & 0x7f else tile = tile | 0x80 end
map.tile(sprite.pos.x//8, sprite.pos.y//8, tile)
editor.modified = true
end
end
end
end,
create_stamp=function()
if editor.layer == LAYER_FOREGROUND then
map.surf(rooms.surf_foreground)
elseif editor.layer == LAYER_BACKGROUND then
map.surf(rooms.surf_background)
end
local tx1,ty1,tx2,ty2=editor.selection.x1,editor.selection.y1,editor.selection.x2,editor.selection.y2
if tx1>tx2 then tx1,tx2=tx2,tx1 end
if ty1>ty2 then ty1,ty2=ty2,ty1 end
editor.brush.w=tx2-tx1+1
editor.brush.h=ty2-ty1+1
local w,h=editor.brush.w,editor.brush.h
local p=1
for y=1,h do
for x=1,w do
editor.brush.tiles[p]=map.tile(tx1+x-1,ty1+y-1)
--map.tile(tx+x-1,ty+y-1,editor.brush.tiles[p])
p=p+1
end
end
end,
stamp=function(tx,ty)
if editor.layer == LAYER_FOREGROUND then
map.surf(rooms.surf_foreground)
elseif editor.layer == LAYER_BACKGROUND then
map.surf(rooms.surf_background)
end
local w,h=editor.brush.w,editor.brush.h
local p=1
for y=1,h do
for x=1,w do
local fx, fy = tx+x-1, ty+y-1
if fx < rooms.pos.x+20 and fy < rooms.pos.y+12 then
map.tile(fx,fy,editor.brush.tiles[p])
end
p=p+1
end
end
end,
quit=function()
if editor.modified then
msgbox.show("IE MEN!",
{"Hi ha canvis sense guardar.", "Vols guardar-los abans d'eixir?"},
{
{"Cancel", app.pop},
{"No", sys.quit},
{"Yes", function() rooms.save() sys.quit() end}
} )
else
sys.quit()
end
end,
play=function()
if editor.modified then
msgbox.show("IE MEN!",
{"Hi ha canvis sense guardar.", "Vols guardar-los abans de jugar?"},
{
{"Cancel", app.pop},
{"No", function() app.pop() game.enable() end},
{"Yes", function() app.pop() rooms.save() game.enable() end}
} )
else
game.enable()
end
end,
tilepicker = {
show = function()
app.push(editor.tilepicker.update_tiles)
end,
update_tiles = function()
view.origin(0,0)
view.clip()
surf.source(surf_tiles)
surf.cls(1)
draw.surf(0,0,128,128,0,0)
local mx, my = mouse.pos()
local tx, ty = mx>>3, my>>3
mx, my = tx<<3, ty<<3
draw.rect(mx-1, my-1, 10, 10, 28)
draw.rect(mx, my, 8, 8, 1)
if mouse.press(mouse.LEFT) then
if tx<16 and ty<16 then
editor.brush.w=1
editor.brush.h=1
editor.brush.tiles={}
editor.brush.tiles[1]=ty*16+tx
end
app.pop()
end
if key.press(key.TAB) or key.press(key.ESCAPE) then
app.pop()
end
end
},
itempicker = {
show = function()
app.push(editor.itempicker.update_items)
end,
update_items = function()
view.origin(0,0)
view.clip()
surf.source(surf_sprites)
surf.cls(1)
local mx, my = mouse.pos()
local x,y = 0,0
for i,k in pairs(items) do
local w = math.max(k.visual.w, #k.name*4)
local ox = (w-k.visual.w)/2
draw.text(k.name, x, y, 15)
draw.surf(k.visual.x, k.visual.y, k.visual.w, k.visual.h, x+ox, y+7)
if mx>x and mx<x+w and my>y and my<y+7+k.visual.h then
draw.rect(x+ox, y+7, k.visual.w, k.visual.h, 28)
if mouse.press(mouse.LEFT) then
editor.item_selected = i
app.pop()
end
end
x=x+w+4
end
if key.press(key.TAB) or key.press(key.ESCAPE) then
app.pop()
end
end
}
}