Initial efforts towards an editor
Getty Ritter
8 years ago
| 1 | local board = require 'board' | |
| 2 | local consts = require 'constants' | |
| 3 | local draw = require 'draw' | |
| 4 | local tile = require 'tile' | |
| 5 | ||
| 6 | local state = { | |
| 7 | mode = 'show', | |
| 8 | board = board.Board:new(), | |
| 9 | brush = 'water', | |
| 10 | } | |
| 11 | ||
| 12 | function love.load() | |
| 13 | state.canvas = love.graphics.newCanvas( | |
| 14 | consts.boardWidth * consts.tileSize, | |
| 15 | consts.boardHeight * consts.tileSize) | |
| 16 | state.canvas:setFilter('nearest', 'nearest') | |
| 17 | ||
| 18 | love.window.setMode(consts.tileSize * consts.boardWidth * 2, | |
| 19 | consts.tileSize * consts.boardHeight * 2) | |
| 20 | ||
| 21 | love.graphics.setNewFont('font.ttf', 16) | |
| 22 | end | |
| 23 | ||
| 24 | function fromScreen(x, y) | |
| 25 | return math.floor(x / consts.tileSize), math.floor(y / consts.tileSize) | |
| 26 | end | |
| 27 | ||
| 28 | function love.mousepressed(x, y) | |
| 29 | if state.mode == 'show' then | |
| 30 | local gx, gy = fromScreen(x/2, y/2) | |
| 31 | state.board:set(gx, gy, tile.getTile(state.brush)) | |
| 32 | end | |
| 33 | end | |
| 34 | ||
| 35 | function love.keypressed(key) | |
| 36 | if state.mode == 'show' then | |
| 37 | if key == 'q' then | |
| 38 | love.event.quit() | |
| 39 | elseif key == 's' then | |
| 40 | state.mode = 'select' | |
| 41 | state.brush = '' | |
| 42 | end | |
| 43 | elseif state.mode == 'select' then | |
| 44 | if key == 'return' then | |
| 45 | if not tile.getTile(state.brush) then | |
| 46 | state.brush = 'grass' | |
| 47 | end | |
| 48 | state.mode = 'show' | |
| 49 | else | |
| 50 | state.brush = state.brush .. key | |
| 51 | end | |
| 52 | end | |
| 53 | end | |
| 54 | ||
| 55 | function love.draw() | |
| 56 | love.graphics.setCanvas(state.canvas) | |
| 57 | ||
| 58 | for x = 0, consts.boardWidth - 1 do | |
| 59 | for y = 0, consts.boardHeight - 1 do | |
| 60 | if state.board:lookup(x, y) then | |
| 61 | state.board:lookup(x, y):draw(x, y, 0) | |
| 62 | end | |
| 63 | end | |
| 64 | end | |
| 65 | ||
| 66 | love.graphics.setCanvas() | |
| 67 | ||
| 68 | love.graphics.setColor(0, 0, 0) | |
| 69 | love.graphics.rectangle('fill', 0, 0, | |
| 70 | love.graphics.getWidth(), | |
| 71 | love.graphics.getHeight()) | |
| 72 | love.graphics.setColor(255, 255, 255) | |
| 73 | love.graphics.print(state.brush) | |
| 74 | love.graphics.draw(state.canvas, 0, 0, 0, 2, 2) | |
| 75 | love.graphics.setColor(255, 255, 255) | |
| 76 | love.graphics.print(state.brush) | |
| 77 | end | |
| 78 | ||
| 79 | return {} |
| 5 | 5 | local event = require 'event' |
| 6 | 6 | local menu = require 'menu' |
| 7 | 7 | local draw = require 'draw' |
| 8 | ||
| 9 | if arg[2] == 'editor' then | |
| 10 | require 'editor' | |
| 11 | return | |
| 12 | end | |
| 8 | 13 | |
| 9 | 14 | local state = { |
| 10 | 15 | loadMap = function(self, name) |
Binary diff not shown
Binary diff not shown