using a grid and all that good stuff, and it puts all the bodies, shapes and what-not into a table.
Everything works very well, but I'm having trouble wrapping my head around how I can save a table to a text file. Also, I'm quite
new to Lua, so please try not to suggest anything that may be too hard for me
![Nyu :nyu:](./images/smilies/ms-nyu.png)
If it's any help, here's my code.
![Wink ;)](./images/smilies/ms-wink.png)
Beware, it's not very organized (in my opinion anyway).
Code: Select all
function love.load()
editor = {}--Create a table to hold all the editor elements
editor.grids = {}--Create a table to hold the grid bodies and shapes
editor.gridOn = true--Is the grid on or not?
editor.gridWorld = love.physics.newWorld(960,704)--Create a world to hold the grids.
editor.gridWorld:setMeter(64)
editor.gridWorld:setGravity(0,0)
--It's better when it's a multiple of 64.
editor.mode = "blocks"
editor.modes = {}
editor.modes[1] = "blocks"
editor.modes[2] = "objects"
createGrid()
map = {}--Create a table to hold all the map elements
map.materials = {--Table to hold the images used for blocks
woodfloor = love.graphics.newImage("materials/wood-floor.png"),
grassdirt = love.graphics.newImage("materials/grass-dirt.png"),
testplatform = love.graphics.newImage("materials/testplatform.png"),
boxfloor = love.graphics.newImage("materials/box-floor.png"),
metalwall = love.graphics.newImage("materials/metal-wall.png"),
brickwall = love.graphics.newImage("materials/brick-wall.png"),
spacefloor = love.graphics.newImage("materials/space1-floor.png"),
}
map.blocks = {--Table to hold all the data for the static blocks (or just blocks)
--w = width, h = height, ox = offset x, oy = offset y
[1] = {i = map.materials.woodfloor, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[2] = {i = map.materials.grassdirt, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[3] = {i = map.materials.testplatform, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[4] = {i = map.materials.boxfloor, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[5] = {i = map.materials.metalwall, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[6] = {i = map.materials.brickwall, w = 64, h = 64, ox = 32, oy = 32, m = 0},
[7] = {i = map.materials.spacefloor, w = 64, h = 64, ox = 32, oy = 32, m = 0},
}
map.objects = {}--Table to hold all the objects
map.entities = {}--Table to hold all the entities in the map.
map.entitynum = 0
map.world = love.physics.newWorld(960,704)
map.world:setMeter(64)
map.world:setGravity(0,0)
editor.mat = map.blocks[1]
editor.matn = 1
editor.obj = nil
editor.objn = 1
love.graphics.setLineWidth(2)--Thicker lines
editor.font = love.graphics.newFont(20)
love.graphics.setFont(editor.font)
love.graphics.setMode(1440,800)--Set the resolution to 1440*800
love.graphics.setCaption("Level Editor")
end
function love.update(dt)
editor.gridWorld:update(dt)--Update the grid.
map.world:update(dt)--Update the map
if love.mouse.isDown("l") and editor.hitGrid == true and editor.selectGrid.occupied == false then
addBlock(editor.selectGrid)
end
if love.mouse.isDown("r") and editor.hitGrid == true and editor.selectGrid.occupied == true then
removeBlock(editor.selectGrid)
end
if love.keyboard.isDown('g') then--Turn on the grid when G is pressed.
if editor.gridOn == false then
editor.gridOn = true
elseif editor.gridOn == true then
editor.gridOn = false
end
love.timer.sleep(100)--To stop the grid from flickering
end
end
function love.draw()
editor.hitGrid, editor.selectGrid = hoverGrid()
love.graphics.setColor(255,255,255)
if editor.gridOn == true then--Draw the grid if it's on
drawGrid()
end
for i,v in pairs(map.entities) do--Draw all the map entities
love.graphics.draw(v.i, v.b:getX(), v.b:getY(), v.b:getAngle(), 1, 1, v.ox, v.oy)
end
showBlock()
printMode()
end
function drawGrid()--Draw the grid lines
love.graphics.setColor(255,255,255)
for a = 0, 14 do
for n = 0, 10 do
love.graphics.rectangle("line",a*64,n*64,64,64)
end
end
end
function createGrid()--Create bodies and shapes for the grids to detect when the mouse is placed on top of one of them
for a = 1,15 do
for n = 1, 11 do
local t = {}
t.b = love.physics.newBody(editor.gridWorld,a*64-32,n*64-32,0,0)
t.s = love.physics.newRectangleShape(t.b,0,0,64,64,0)
t.occupied = false
t.entity = nil
table.insert(editor.grids,t)
end
end
end
function hoverGrid()--Display a yellow square over the grid where the mouse is
for i,v in pairs(editor.grids) do
local mx,my = love.mouse.getPosition()
local hit = v.s:testPoint(mx, my)
if hit == true then
love.graphics.setColor(255,255,0)
love.graphics.rectangle("fill",v.b:getX()-32,v.b:getY()-32,64,64)
return hit,v
end
end
end
function printMode()--Print the current brush mode
love.graphics.setColor(0,150,255)
love.graphics.print("Mode: "..editor.mode, 0, 800)
end
function showBlock()--Show the image of the currently selected block
love.graphics.setColor(255,255,255)
love.graphics.draw(editor.mat.i, 1100, 100, 0, 1, 1, editor.mat.ox, editor.mat.oy)
end
function love.keypressed(k)
if k == "1" then--Use number keys to change the brush mode
editor.mode = editor.modes[1]
elseif k == "2" then
editor.mode = editor.modes[2]
end
if k == "right" then--Use the arrow keys to change the selected block
if editor.mode == "blocks" then
editor.matn = editor.matn + 1
if editor.matn > table.getn(map.blocks) then editor.matn = 1 end--Loop the list
editor.mat = map.blocks[editor.matn]
elseif editor.mode == "objects" then
editor.objn = editor.objn + 1
if editor.matn > table.getn(map.blocks) then editor.matn = 1 end--Loop the list
editor.obj = map.blocks[editor.objn]
end
elseif k == "left" then--Use the arrow keys to change the selected block
if editor.mode == "blocks" then
editor.matn = editor.matn - 1
if editor.matn < 1 then editor.matn = table.getn(map.blocks) end--Loop the list
editor.mat = map.blocks[editor.matn]
elseif editor.mode == "objects" then
editor.objn = editor.objn - 1
if editor.objn < 1 then editor.objn = table.getn(map.blocks) end--Loop the list
editor.obj = map.blocks[editor.objn]
end
elseif k == "s" then table.save(map,"level.txt") end--ignore that!
end
function addBlock(grid)--Add a block to the map at the currently selected grid
if editor.mode == "blocks" then
local t = {}
local x, y = grid.b:getPosition()
t.b = love.physics.newBody(map.world, x, y, editor.mat.m, 0)
t.s = love.physics.newRectangleShape(t.b,
t.b:getX(),
t.b:getY(),
editor.mat.w,
editor.mat.h,
0)
t.i = editor.mat.i
t.ox = editor.mat.ox
t.oy = editor.mat.oy
table.insert(map.entities,t)
grid.occupied = true
map.entitynum = map.entitynum + 1
grid.entity = map.entities[map.entitynum]
elseif editor.mode == "objects" then
local t = {}
local x, y = grid.b:getPosition()
t.b = love.physics.newBody(map.world, x, y, editor.obj.m, 0)
t.s = love.physics.newRectangleShape(t.b,
t.b:getX(),
t.b:getY(),
editor.obj.w,
editor.obj.h,
0)
t.i = editor.obj.i
t.ox = editor.obj.ox
t.oy = editor.obj.oy
table.insert(map.entities,t)
grid.occupied = true
map.entitynum = map.entitynum + 1
grid.entity = map.entities[map.entitynum]
end
end
function removeBlock(grid)--Remove a block
local entity = grid.entity
entity.b:setPosition(-10000,-10000)
love.graphics.draw(entity.i, entity.b:getX(), entity.b:getY(), entity.b:getAngle(), 1, 1, entity.ox, entity.oy)
table.remove(entity)
grid.occupied = false
end