- [NEW] 'app' module
- [NEW] 'editor' module - [NEW] 'msgbox' module - [NEW] 'score' module - [NEW] 'ui' module - [NEW] 'util' module
This commit is contained in:
175
data/editor.lua
Normal file
175
data/editor.lua
Normal file
@@ -0,0 +1,175 @@
|
||||
require "menu"
|
||||
require "msgbox"
|
||||
|
||||
editor = {
|
||||
|
||||
layer = LAYER_FOREGROUND,
|
||||
brush={w=2,h=1,tiles={16,17}},
|
||||
selection=nil,
|
||||
ants=0xc936,
|
||||
modified = false,
|
||||
|
||||
enable = function()
|
||||
menu.show()
|
||||
app.update = editor.update
|
||||
sys.beat(2)
|
||||
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.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
|
||||
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
|
||||
elseif key.press(key.LEFT) and rooms.pos.x > 0 then
|
||||
rooms.pos.x = rooms.pos.x - 20
|
||||
elseif key.press(key.DOWN) and rooms.pos.y < 12*7 then
|
||||
rooms.pos.y = rooms.pos.y + 12
|
||||
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()
|
||||
elseif key.press(key.Q) then
|
||||
msgbox.show("IE MEN!", {"Hi ha canvis sense guardar.", "Vols guardar-los abans d'eixir?"}, { {"Cancel", app.pop}, {"No", function() sys.quit() end}, {"Yes", sys.quit} } )
|
||||
end
|
||||
end,
|
||||
|
||||
create_stamp=function()
|
||||
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)
|
||||
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,
|
||||
|
||||
picker = {
|
||||
|
||||
show = function()
|
||||
app.push(editor.picker.update_tiles)
|
||||
end,
|
||||
|
||||
update_tiles = function()
|
||||
view.origin(0,0)
|
||||
view.clip()
|
||||
surf.source(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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user