- [NEW] (EDITOR) selector de items

- [WIP] borrat i creació de items segons s'eix o entra de les habitacions
- [FIX] Items indexats per número, per a poder-se guardar en un mapa
- [NEW] room.current()
- [NEW] resaltat del item seleccionat
This commit is contained in:
2025-11-13 12:08:43 +01:00
parent eefab3f9b0
commit cf103dd02c
6 changed files with 145 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
require "menu"
require "msgbox"
require "items"
editor = {
@@ -8,6 +9,7 @@ editor = {
selection=nil,
ants=0xc936,
modified = false,
item_selected=nil,
enable = function()
app.update = editor.update
@@ -32,40 +34,58 @@ editor = {
mx, my = tx<<3, ty<<3
if my>=0 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.layer==LAYER_BACKGROUND or editor.layer==LAYER_FOREGROUND then
if editor.selection then
editor.selection.x2=tx
editor.selection.y2=ty
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
editor.selection={}
editor.selection.x1=tx
editor.selection.y1=ty
editor.selection.x2=tx
editor.selection.y2=ty
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 editor.selection then
editor.create_stamp()
editor.selection=nil
if mouse.press(mouse.LEFT) then
if editor.item_selected then
surf.target(rooms.surf_items)
surf.pixel(tx,ty,editor.item_selected)
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)
end
end
end
@@ -85,7 +105,11 @@ editor = {
elseif key.press(key.UP) and rooms.pos.y > 0 then
rooms.pos.y = rooms.pos.y - 12
elseif key.press(key.TAB) or key.press(key.ESCAPE) then
editor.picker.show()
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
@@ -166,10 +190,10 @@ editor = {
end
end,
picker = {
tilepicker = {
show = function()
app.push(editor.picker.update_tiles)
app.push(editor.tilepicker.update_tiles)
end,
update_tiles = function()
@@ -201,5 +225,39 @@ editor = {
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
}
}

View File

@@ -30,6 +30,7 @@ game = {
game.chg_adv.y = y
game.chg_step = 8
sprites.pause_ia = true
-- [TODO] Crear els sprites per als items de l'habitació a la que entrem
app.push(game.update_change_room)
end,
@@ -49,6 +50,7 @@ game = {
sprites.update()
game.chg_step = game.chg_step - 1
if game.chg_step == 0 then
sprites.remove_out_of_room()
sprites.pause_ia = false
app.pop()
end

View File

@@ -1,5 +1,10 @@
items = {
["mummy"] = {
[1] = {
name="mummy",
visual = {x=0,y=24,w=16,h=16}
},
[2] = {
name="mummy2",
visual = {x=16,y=24,w=16,h=16}
}
}

View File

@@ -15,6 +15,10 @@ rooms = {
visibility = LAYER_ALL,
pos = {x=0, y=4*12},
current = function()
return (rooms.pos.x//20) + (rooms.pos.y//12) * 8
end,
init = function()
rooms.pos.x, rooms.pos.y = 0,4*12
if rooms.surf_background ~= nil then surf.free(rooms.surf_background) end
@@ -87,7 +91,7 @@ rooms = {
-- Pintem els sprites de negre
if rooms.is_visible(LAYER_SPRITES | LAYER_SHADOWS) then
sprites.draw()
sprites.draw(true)
--draw.surf(0, 0, 16, 17, 20, 15, 16, 17)
end

View File

@@ -16,6 +16,19 @@ sprites = {
end
end,
remove_out_of_room = function()
for i,v in ipairs(sprites.list) do
if v.room ~= rooms.current() then
table.remove(sprites.list, i)
print("Sprite removed: "..v.type)
end
end
end,
add_from_room = function()
end,
init = function()
sprites.hero = {
type = "hero",
@@ -287,10 +300,16 @@ sprites = {
sprites.set_animation(sprites.hero, anim)
end,
draw = function()
draw = function(ignore_selected)
local mx,my = mouse.pos()
surf.source(surf_sprites)
if editor.item_selected or editor.layer~=LAYER_ITEMS then ignore_selected = true end
for i,v in ipairs(sprites.list) do
sprites.draw_sprite(v)
if not ignore_selected and app.update == editor.update and mx>=v.pos.x and mx<=v.pos.x+v.size.w and my>=v.pos.y and my<=v.pos.y+v.size.h then
sprites.draw_sprite_selected(v)
else
sprites.draw_sprite(v)
end
end
sprites.draw_sprite(sprites.hero)
end,
@@ -305,5 +324,24 @@ sprites = {
draw.surf(frame.frame.x, frame.frame.y, frame.frame.w, frame.frame.h, sprite.pos.x, sprite.pos.y, frame.frame.w, frame.frame.h, (not reversed) ~= (not sprite.flipped))
local x,y,w,h = util.aabb(sprite)
--draw.rect(x,y,w,h,8)
end,
draw_sprite_selected = function(sprite)
pal.subpal(0,32,28)
local cycle = animations[sprite.animation].cycle[sprite.current_frame]
local frame = animations[sprite.animation].frames[cycle]
local reversed = frame.reversed or false
local x, y, w, h, sx, sy, f = sprite.pos.x, sprite.pos.y, frame.frame.w, frame.frame.h, frame.frame.x, frame.frame.y, (not reversed) ~= (not sprite.flipped)
draw.surf(sx, sy, w, h, x-1, y-1, w, h, f)
draw.surf(sx, sy, w, h, x, y-1, w, h, f)
draw.surf(sx, sy, w, h, x+1, y-1, w, h, f)
draw.surf(sx, sy, w, h, x-1, y, w, h, f)
draw.surf(sx, sy, w, h, x+1, y, w, h, f)
draw.surf(sx, sy, w, h, x-1, y+1, w, h, f)
draw.surf(sx, sy, w, h, x, y, w, h, f)
draw.surf(sx, sy, w, h, x+1, y+1, w, h, f)
pal.subpal()
draw.surf(sx, sy, w, h, x, y, w, h, f)
end
}

View File

@@ -35,6 +35,7 @@ templates = {
else
error("Template not recognized")
end
sprite.room = rooms.current()
print("creat sprite de tipus "..type)
return sprite
end,