tilerotate-demo/port.lua
2025-10-30 00:24:49 +03:00

85 lines
2.2 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
---@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
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 + sox,
offset.y + self.position.y + soy
)
local x, y = drawPoint:globalCoords()
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)
end