tilerotate-demo/port.lua
2025-10-29 18:12:43 +03:00

71 lines
1.9 KiB
Lua

require 'gridpoint'
---@enum Alignment
Alignment = {
Vertical = true,
Horizontal = false
}
---@enum PortRotation
PortRotation = {
right = {false, false},
down = {false, true},
left = {true, false},
up = {true, true}
}
---@class Port
---@field position GridPoint
---@field alignment Alignment
---@field scanOffset number
---@field color table
Port = {
position = GridPoint:new(0, 0),
alignment = Alignment.Vertical,
scanOffset = 0,
color = { 0.8, 0.8, 0, 1 }
}
---@param position GridPoint
---@param rotation PortRotation
---@param color table
---@return Port
function Port:new(position, rotation, color)
local port = {
position = position or self.position,
alignment = self.alignment,
scanOffset = self.scanOffset,
color = color or self.color
}
if rotation ~= nil then
port.scanOffset = rotation[1] and 1 or 0
port.alignment = rotation[2]
end
setmetatable(port, { __index = self })
return port
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.alignment = not self.alignment
end
---@param offset GridPoint
function Port:draw( offset )
love.graphics.setColor(self.color)
local drawPoint = GridPoint:new(
offset.x + self.position.x + self.scanOffset,
offset.y + self.position.y + self.scanOffset
)
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
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)
end