Almost working from 1st try

This commit is contained in:
Alexey 2025-10-29 18:12:43 +03:00
commit c6547c1121
8 changed files with 297 additions and 0 deletions

69
mechanism.lua Normal file
View file

@ -0,0 +1,69 @@
require 'tablefuncs'
require 'gridpoint'
require 'port'
---@param xcorner GridPoint
---@param size GridPoint
---@return GridPoint
local function nextxcorner( xcorner, size )
local cornerLookup = {
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 )
}
local index = TableFind(cornerLookup, xcorner, function(value) value:equals(xcorner) end)
if index < #cornerLookup then
index = index + 1
else
index = 0
end
return cornerLookup[index]
end
---@class Mechanism
---@field position GridPoint
---@field size GridPoint
---@field xcorner GridPoint
---@field ports Port[]
---@field color table
Mechanism = {
position = GridPoint:new( 0, 0 ),
size = GridPoint:new( 1, 1 ),
xcorner = GridPoint:new( 0, 0 ),
ports = {},
color = { 0.5, 0.5, 0.5, 1 }
}
function Mechanism:new( position, size, ports, color )
local mechanism = {
position = position or self.position,
size = size or self.size,
ports = ports or {},
color = color or self.color
}
setmetatable( mechanism, { __index = self } )
return mechanism
end
function Mechanism:rotateClockwise()
self.xcorner = nextxcorner(self.xcorner, self.size)
for _, port in ipairs(self.ports) do
port:rotateClockwise()
end
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
local offset = GridPoint:new( self.position.x + self.xcorner.x, self.position.y + self.xcorner.y )
port:draw( offset )
end
end