tilerotate-demo/main.lua

78 lines
2 KiB
Lua

require 'point'
require 'gridpoint'
require 'mouse'
require 'mechanism'
require 'port'
require 'tablefuncs'
Input = require 'input'
require 'selector'
function love.load()
Screenw, Screenh = love.window.getMode()
Gridw, Gridh = Screenw / Config.cellSize, Screenh / Config.cellSize
Mechs = {}
Selected = nil
end
function love.update( dt )
dt = dt
Mouse:update()
Input:update()
selectorUpdate()
Selected = getSelected()
if Selected ~= nil then
Selected.position = Mouse.point
if Mouse.lastPressed and not Mouse.pressed then
local intersects = TableHas( Mechs, Selected, function(this, other)
return this:intersectsOther( other )
end
)
if not intersects then
local mech = Selected:clone()
mech.xcorner = GridPoint:new( Selected.xcorner.x, Selected.xcorner.y )
table.insert( Mechs, mech )
end
end
end
if Input:actionReleased( 'rotate' ) then
if Selected ~= nil then
Selected:rotateClockwise()
else
local index = TableFind(Mechs, Mouse.point, function(mp, mech)
return mech:intersectsPoint( mp )
end
)
if index ~= nil then
Mechs[index]:rotateClockwise()
end
end
end
end
function love.draw()
love.graphics.setColor(1, 1, 1, 1)
for i = 1, Gridw do
love.graphics.line( i * Config.cellSize, 0, i * Config.cellSize, Screenh )
end
for j = 1, Gridh do
love.graphics.line( 0, j * Config.cellSize, Screenw, j * Config.cellSize )
end
local mx, my = Mouse.point:globalCoords()
local mh, mw = Config.cellSize, Config.cellSize
love.graphics.rectangle("fill", mx, my, mh, mw)
love.graphics.print(string.format("%s, %s", Mouse.point.x, Mouse.point.y), 100, 100)
for _, mech in ipairs(Mechs) do
mech:draw()
end
if Selected ~= nil then
Selected:draw()
end
end