diff --git a/input.lua b/input.lua deleted file mode 100644 index 1708e1f..0000000 --- a/input.lua +++ /dev/null @@ -1,98 +0,0 @@ --- Input system --- TODO: replace this with universal system -local Input = { - keyboardButtons = { '1', '2', '3', '4', 'r' }, - keyboardPressed = {}, - lastKeyboardPressed = {}, - actions = {} -} - -local function mapAction( input, action, keys ) - input.actions[action] = {} - for _, key in pairs( keys ) do - table.insert( input.actions[action], key ) - end -end - -local function loadMap( input ) - mapAction( input, 'first', { '1' } ) - mapAction( input, 'second', { '2' } ) - mapAction( input, 'third', { '3' } ) - mapAction( input, 'none', { '4' } ) - mapAction( input, 'rotate', { 'r' } ) -end - -local function predHolded( pressed, _ ) - return pressed -end - -local function predPressed( pressed, lastPressed ) - return pressed and not lastPressed -end - -local function predReleased( pressed, lastPressed ) - return not pressed and lastPressed -end - -local function testAction( input, action, predicate ) - for _, key in ipairs( input.actions[action] ) do - local pressed, lastPressed - - pressed = input.keyboardPressed - lastPressed = input.lastKeyboardPressed - - if predicate( pressed[key], lastPressed[key] ) then - return true - end - end - - return false -end - -function Input.init() - for btn in ipairs( Input.keyboardButtons ) do - Input.keyboardPressed[Input.keyboardButtons[btn]] = false - Input.lastKeyboardPressed[Input.keyboardButtons[btn]] = false - end - - loadMap( Input ) - - return Input -end - -function Input:update() - for k, _ in pairs( self.keyboardPressed ) do - self.lastKeyboardPressed[k] = self.keyboardPressed[k] - end - - for button in ipairs( self.keyboardButtons ) do - self.keyboardPressed[self.keyboardButtons[button]] = love.keyboard.isDown( self.keyboardButtons[button] ) - end -end - -function Input:actionHolded( action ) - return testAction( self, action, predHolded ) -end - -function Input:actionReleased( action ) - return testAction( self, action, predReleased ) -end - -function Input:actionPressed( action ) - return testAction( self, action, predPressed ) -end - -function Input:actionDiff( negative, positive ) - local negResult = testAction( self, negative, predHolded ) - local posResult = testAction( self, positive, predHolded ) - - if negResult and not posResult then - return -1 - elseif posResult and not negResult then - return 1 - else - return 0 - end -end - -return Input.init() diff --git a/main.lua b/main.lua index 8316543..6e60897 100644 --- a/main.lua +++ b/main.lua @@ -4,53 +4,48 @@ 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 = {} + local port1 = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) + local port2 = Port:new( GridPoint:new(0, -1), PortRotation.up, { 0.8, 0, 0.8, 1 } ) + local port3 = Port:new( GridPoint:new(3, 2), PortRotation.right, { 0.8, 0, 0, 1 } ) + local port4 = Port:new( GridPoint:new(2, 3), PortRotation.down, { 0, 0, 0.8, 1 } ) + local mecha1 = Mechanism:new( GridPoint:new(5, 5), GridPoint:new(3, 3), { port1, port2, port3, port4 }, {0.5, 0.5, 0.5, 1} ) - Selected = nil + local port5 = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) + local port6 = Port:new( GridPoint:new(0, -1), PortRotation.up, { 0.8, 0, 0.8, 1 } ) + local port7 = Port:new( GridPoint:new(2, 1), PortRotation.right, { 0.8, 0, 0, 1 } ) + local port8 = Port:new( GridPoint:new(1, 2), PortRotation.down, { 0, 0, 0.8, 1 } ) + local mecha2 = Mechanism:new( GridPoint:new(10, 5), GridPoint:new(2, 2), { port5, port6, port7, port8 }, {0.5, 1, 0.5, 1} ) + + local port9 = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) + local port10 = Port:new( GridPoint:new(0, -1), PortRotation.up, { 0.8, 0, 0.8, 1 } ) + local port11 = Port:new( GridPoint:new(2, 0), PortRotation.right, { 0.8, 0, 0, 1 } ) + local port12 = Port:new( GridPoint:new(1, 1), PortRotation.down, { 0, 0, 0.8, 1 } ) + local mecha3 = Mechanism:new( GridPoint:new(13, 5), GridPoint:new(2, 1), { port9, port10, port11, port12 }, {1, 0.5, 0.5, 1} ) + + local port13 = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) + local port14 = Port:new( GridPoint:new(0, -1), PortRotation.up, { 0.8, 0, 0.8, 1 } ) + local port15 = Port:new( GridPoint:new(2, 0), PortRotation.right, { 0.8, 0, 0, 1 } ) + local port16 = Port:new( GridPoint:new(1, 1), PortRotation.down, { 0, 0, 0.8, 1 } ) + local mecha4 = Mechanism:new( GridPoint:new(16, 5), GridPoint:new(2, 1), { port13, port14, port15, port16 }, {0.5, 0.5, 1, 1} ) + Mechs = {mecha1, mecha2, mecha3, mecha4} 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.position = GridPoint:new( Mouse.point.x, Mouse.point.y ) - mech.xcorner = GridPoint:new( Selected.xcorner.x, Selected.xcorner.y ) - table.insert( Mechs, mech ) - end + if Mouse.lastPressed and not Mouse.pressed then + local index = TableFind(Mechs, Mouse.point, function(mp, mech) + return mp.x >= mech.position.x and mp.x < mech.position.x + mech.size.x and + mp.y >= mech.position.y and mp.y < mech.position.y + mech.size.y 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 + ) + if index ~= nil then + Mechs[index]:rotateClockwise() end end end @@ -72,8 +67,4 @@ function love.draw() for _, mech in ipairs(Mechs) do mech:draw() end - - if Selected ~= nil then - Selected:draw() - end end diff --git a/mechanism.lua b/mechanism.lua index ec0d730..69814a5 100644 --- a/mechanism.lua +++ b/mechanism.lua @@ -55,40 +55,6 @@ function Mechanism:new( position, size, ports, color ) return mechanism end ----@return Mechanism -function Mechanism:clone() - local ports = {} - for _, port in ipairs(self.ports) do - local clonedPort = port:clone() - table.insert(ports, clonedPort) - end - local mechanism = { - position = GridPoint:new( self.position.x, self.position.y ), - size = GridPoint:new( self.size.x, self.size.y ), - ports = ports, - color = self.color - } - setmetatable( mechanism, { __index = self } ) - - return mechanism -end - -function Mechanism:intersectsPoint( point ) - return point.x >= self.position.x and - point.x < self.position.x + self.size.x and - point.y >= self.position.y and - point.y < self.position.y + self.size.y -end - -function Mechanism:intersectsOther( mechanism ) - return mechanism.position.x + mechanism.size.x > self.position.x and - mechanism.position.x < - self.position.x + self.size.x and - mechanism.position.y + mechanism.size.y > self.position.y and - mechanism.position.y < self.position.y + self.size.y - -end - function Mechanism:rotateClockwise() self.size = GridPoint:new(self.size.y, self.size.x) self.xcorner = nextxcorner(self.xcorner, self.size) diff --git a/port.lua b/port.lua index 2ddbecc..d77b5c4 100644 --- a/port.lua +++ b/port.lua @@ -47,19 +47,6 @@ function Port:new(position, rotation, color) return port end ----@return Port -function Port:clone() - local port = { - position = GridPoint:new( self.position.x, self.position.y ), - alignment = self.alignment, - scanOffset = self.scanOffset, - color = self.color - } - setmetatable( port, { __index = self } ) - - return port -end - function Port:rotateClockwise() self.position.x, self.position.y = -self.position.y, self.position.x self.scanOffset = (self.scanOffset == 1) ~= self.alignment and 1 or 0 diff --git a/selector.lua b/selector.lua deleted file mode 100644 index e55713c..0000000 --- a/selector.lua +++ /dev/null @@ -1,35 +0,0 @@ -local port1 = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) -local port2 = Port:new( GridPoint:new(0, -1), PortRotation.up, { 0.8, 0, 0.8, 1 } ) -local port3 = Port:new( GridPoint:new(3, 2), PortRotation.right, { 0.8, 0, 0, 1 } ) -local port4 = Port:new( GridPoint:new(2, 3), PortRotation.down, { 0, 0, 0.8, 1 } ) -local port5 = Port:new( GridPoint:new(2, 0), PortRotation.right, {0.8, 0.8, 0, 1} ) -local port6 = Port:new( GridPoint:new(0, 2), PortRotation.down, { 0.8, 0.4, 0.4, 1 } ) - -local mecha1 = Mechanism:new( GridPoint:new(5, 5), GridPoint:new(3, 3), { port1:clone(), port2:clone(), port3:clone(), port4:clone() }, {0.5, 0.5, 0.5, 1} ) - -local mecha2 = Mechanism:new( GridPoint:new(10, 5), GridPoint:new(2, 2), { port1:clone(), port2:clone(), port5:clone(), port6:clone() }, {0.5, 1, 0.5, 1} ) - -local mecha3 = Mechanism:new( GridPoint:new(13, 5), GridPoint:new(2, 1), { port1:clone(), port2:clone(), port5:clone() }, {1, 0.5, 0.5, 1} ) - -local mechs = { mecha1, mecha2, mecha3 } -local actions = { 'none', 'first', 'second', 'third' } - -local selected = 0 - -function selectorUpdate() - for index, action in ipairs(actions) do - if Input:actionReleased(action) then - selected = index - 1 - break - end - end -end - ----@return Mechanism|nil -function getSelected() - if selected > 0 then - return mechs[selected] - else - return nil - end -end