From d5e8a6945e681d86480d7c632e61db64ae561d2e Mon Sep 17 00:00:00 2001 From: 2ndbeam <2ndbeam@disroot.org> Date: Thu, 30 Oct 2025 00:09:34 +0300 Subject: [PATCH] it works --- config.lua | 2 +- gridpoint.lua | 8 ++++---- main.lua | 47 ++++++++++++++++++++++++++++++++++++++++++----- mechanism.lua | 20 +++++++++++++------- mouse.lua | 11 ++++++++--- port.lua | 17 +++++++++-------- 6 files changed, 77 insertions(+), 28 deletions(-) diff --git a/config.lua b/config.lua index 93d9be9..e7d9000 100644 --- a/config.lua +++ b/config.lua @@ -1,5 +1,5 @@ Config = { - cellSize = 64, + cellSize = 32, gridOffset = 0, portWidth = 16 } diff --git a/gridpoint.lua b/gridpoint.lua index e8d57e0..3c329d3 100644 --- a/gridpoint.lua +++ b/gridpoint.lua @@ -9,7 +9,7 @@ GridPoint = Point ---@return Point function GridPoint:absolute() return Point:new( - self.x * Config.cellSize + Config.gridOffset, + self.x * Config.cellSize, self.y * Config.cellSize ) end @@ -17,15 +17,15 @@ end -- Same as coords, but converted to global coords ---@return number, number function GridPoint:globalCoords() - return self.x * Config.cellSize - Config.cellSize / 2 + Config.gridOffset, - self.y * Config.cellSize - Config.cellSize / 2 + return self.x * Config.cellSize, + self.y * Config.cellSize end -- Returns local coords from global ---@param point Point ---@return GridPoint function GridPoint.snapCoords( point ) - local x = math.ceil( (point.x - Config.gridOffset) / Config.cellSize ) + local x = math.ceil( (point.x) / Config.cellSize ) local y = math.ceil( point.y / Config.cellSize ) return GridPoint:new( x, y ) end diff --git a/main.lua b/main.lua index 1893794..59a66a6 100644 --- a/main.lua +++ b/main.lua @@ -3,20 +3,55 @@ require 'gridpoint' require 'mouse' require 'mechanism' require 'port' +require 'tablefuncs' function love.load() Screenw, Screenh = love.window.getMode() Gridw, Gridh = Screenw / Config.cellSize, Screenh / Config.cellSize - local port = Port:new( GridPoint:new(-1, 0), PortRotation.left, { 0.8, 0.8, 0, 1 } ) - Mecha1 = Mechanism:new( GridPoint:new(5, 5), GridPoint:new(3, 3), { port }, {0.5, 0.5, 0.5, 1} ) + 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} ) + + 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() + + 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 + ) + if index ~= nil then + Mechs[index]:rotateClockwise() + 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 @@ -24,10 +59,12 @@ function love.draw() love.graphics.line( 0, j * Config.cellSize, Screenw, j * Config.cellSize ) end local mx, my = Mouse.point:globalCoords() - mx = mx - Config.cellSize / 2 - my = my - Config.cellSize / 2 local mh, mw = Config.cellSize, Config.cellSize love.graphics.rectangle("fill", mx, my, mh, mw) - Mecha1:draw() + love.graphics.print(string.format("%s, %s", Mouse.point.x, Mouse.point.y), 100, 100) + + for _, mech in ipairs(Mechs) do + mech:draw() + end end diff --git a/mechanism.lua b/mechanism.lua index 9401d58..69814a5 100644 --- a/mechanism.lua +++ b/mechanism.lua @@ -6,19 +6,26 @@ require 'port' ---@param size GridPoint ---@return GridPoint local function nextxcorner( xcorner, size ) - local cornerLookup = { + -- We're looking for old size + local cornerLookup1 = { + GridPoint:new( 0, 0 ), + GridPoint:new( size.y - 1, 0 ), + GridPoint:new( size.y - 1, size.x - 1 ), + GridPoint:new( 0, size.x - 1 ) + } + local cornerLookup2 = { GridPoint:new( 0, 0 ), GridPoint:new( size.x - 1, 0 ), GridPoint:new( size.x - 1, size.y - 1 ), - GridPoint:new( size.x - 1, size.y - 1 ) + GridPoint:new( 0, size.y - 1 ) } - local index = TableFind(cornerLookup, xcorner, function(value) value:equals(xcorner) end) - if index < #cornerLookup then + local index = TableFind(cornerLookup1, xcorner, function(xc, value) return value:equals(xc) end) + if index < #cornerLookup1 then index = index + 1 else index = 0 end - return cornerLookup[index] + return cornerLookup2[index] end ---@class Mechanism @@ -49,6 +56,7 @@ function Mechanism:new( position, size, ports, color ) end function Mechanism:rotateClockwise() + self.size = GridPoint:new(self.size.y, self.size.x) self.xcorner = nextxcorner(self.xcorner, self.size) for _, port in ipairs(self.ports) do port:rotateClockwise() @@ -58,8 +66,6 @@ end function Mechanism:draw() love.graphics.setColor(self.color) local x, y = self.position:globalCoords() - x = x - Config.cellSize / 2 - y = y - Config.cellSize / 2 local w, h = self.size:globalCoords() love.graphics.rectangle("fill", x, y, w, h) for _, port in ipairs(self.ports) do diff --git a/mouse.lua b/mouse.lua index c880cd4..aefa285 100644 --- a/mouse.lua +++ b/mouse.lua @@ -6,14 +6,19 @@ Mouse = { x = 0, y = 0, - point = GridPoint:new( 0, 0 ) + point = GridPoint:new( 0, 0 ), + pressed = false, + lastPressed = false } -- TODO: fix collision with other line's endpoint function Mouse:update() + Mouse.lastPressed = Mouse.pressed Mouse.x, Mouse.y = love.mouse.getPosition() - Mouse.x = Mouse.x + 1 - Mouse.y = Mouse.y + 1 + Mouse.x = Mouse.x - Config.cellSize + Mouse.y = Mouse.y - Config.cellSize Mouse.point = GridPoint.snapCoords( Point:new( Mouse.x, Mouse.y ) ) + + Mouse.pressed = love.mouse.isDown( 1 ) end diff --git a/port.lua b/port.lua index bed4976..d77b5c4 100644 --- a/port.lua +++ b/port.lua @@ -48,23 +48,24 @@ function Port:new(position, rotation, color) end function Port:rotateClockwise() - local swap = self.position.x - self.position.x = -self.position.y - self.position.y = swap - self.scanOffset = (self.scanOffset == 1) == self.alignment and 1 or 0 + self.position.x, self.position.y = -self.position.y, self.position.x + self.scanOffset = (self.scanOffset == 1) ~= self.alignment and 1 or 0 self.alignment = not self.alignment end ---@param offset GridPoint function Port:draw( offset ) love.graphics.setColor(self.color) + local sox = self.alignment and 0 or self.scanOffset + local soy = self.alignment and self.scanOffset or 0 + local drawPoint = GridPoint:new( - offset.x + self.position.x + self.scanOffset, - offset.y + self.position.y + self.scanOffset + offset.x + self.position.x + sox, + offset.y + self.position.y + soy ) local x, y = drawPoint:globalCoords() - x = self.alignment and x - Config.cellSize / 2 or x - Config.portWidth / 2 - Config.cellSize / 2 - y = self.alignment and y - Config.portWidth / 2 - Config.cellSize / 2 or y - Config.cellSize / 2 + x = self.alignment and x or x - Config.portWidth / 2 + y = self.alignment and y - Config.portWidth / 2 or y local w = self.alignment and Config.cellSize or Config.portWidth local h = self.alignment and Config.portWidth or Config.cellSize love.graphics.rectangle("fill", x, y, w, h)