package main
import (
"fmt"
"image/color"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)
// Rendering constants. All coordinate math goes through TilePx;
// no code should reason about window pixels directly.
const (
TilePx = 16
VirtualW = 640
VirtualH = 360
)
// roleColor maps a RoomRole to its tint. The mapping lives here so
// that ship data stays free of any rendering concerns.
func roleColor(role RoomRole) color.RGBA {
switch role {
case RolePilot:
return color.RGBA{90, 130, 180, 255} // steel blue
case RoleWeapons:
return color.RGBA{170, 80, 70, 255} // dusty red
case RoleShields:
return color.RGBA{80, 140, 120, 255} // teal green
case RoleMedBay:
return color.RGBA{190, 160, 90, 255} // warm ochre
case RoleEngines:
return color.RGBA{140, 95, 160, 255} // muted violet
default:
return color.RGBA{120, 120, 120, 255}
}
}
// dimRoleColor returns ~60% brightness of the powered tint for a role.
// Used by drawRoom when the system is unpowered.
func dimRoleColor(role RoomRole) color.RGBA {
c := roleColor(role)
return color.RGBA{
R: uint8(uint16(c.R) * 6 / 10),
G: uint8(uint16(c.G) * 6 / 10),
B: uint8(uint16(c.B) * 6 / 10),
A: 255,
}
}
// drawShip draws every room of the ship, dimming rooms whose system has no
// power. Rendering delegates to drawRoom so per-room logic lives in one place.
func drawShip(screen *ebiten.Image, s Ship, sys []System) {
for _, r := range s.Rooms {
powered := sys[int(r.Role)].PowerLevel > 0
drawRoom(screen, r, powered)
}
}
// drawRoom draws a single room: a role-tinted fill (dimmed when unpowered),
// a 1-px border, and the role name printed inside.
func drawRoom(screen *ebiten.Image, r Room, powered bool) {
x := float32(r.GridX * TilePx)
y := float32(r.GridY * TilePx)
w := float32(r.GridW * TilePx)
h := float32(r.GridH * TilePx)
var fill color.RGBA
if powered {
fill = roleColor(r.Role)
} else {
fill = dimRoleColor(r.Role)
}
vector.DrawFilledRect(screen, x, y, w, h, fill, false)
border := color.RGBA{230, 230, 230, 255}
// Four 1-px edges.
vector.DrawFilledRect(screen, x, y, w, 1, border, false)
vector.DrawFilledRect(screen, x, y+h-1, w, 1, border, false)
vector.DrawFilledRect(screen, x, y, 1, h, border, false)
vector.DrawFilledRect(screen, x+w-1, y, 1, h, border, false)
// Label: a few pixels inside the top-left corner.
ebitenutil.DebugPrintAt(screen, r.Name, int(x)+3, int(y)+2)
}
// drawHud renders the power management strip at the bottom of the screen.
// It shows a reactor readout on the left and five per-system power columns
// on the right. Cells are stacked bottom-up; filled cells use the system's
// full roleColor, empty cells use dimRoleColor, each with a 1-px white outline.
func drawHud(screen *ebiten.Image, sys []System) {
// Reactor readout.
ebitenutil.DebugPrintAt(screen,
fmt.Sprintf("Reactor: %d/%d", reactorUsed(sys), ReactorCap),
8, HudY+8)
const cellW = 12
const cellH = 10
const cellGap = 2
// cellBottom is the y of the bottom-most cell's top edge; loop-invariant.
const cellBottom = VirtualH - cellGap
outline := color.RGBA{255, 255, 255, 255}
// Role labels in display order (index == int(RoomRole)).
roleLabels := [HudColCount]string{"Pilot", "Weap", "Shld", "Med", "Eng"}
for i := 0; i < HudColCount; i++ {
role := RoomRole(i)
s := sys[i]
colLeft := HudX + i*HudColW
// Role label at the top of the column.
ebitenutil.DebugPrintAt(screen, roleLabels[i], colLeft+4, HudY+4)
// Cells stacked bottom-up. Row 0 is at the bottom of the HUD strip.
for j := 0; j < s.MaxLevel; j++ {
cy := cellBottom - (j+1)*(cellH+cellGap)
cx := colLeft + 4
var fill color.RGBA
if j < s.PowerLevel {
fill = roleColor(role)
} else {
fill = dimRoleColor(role)
}
vector.DrawFilledRect(screen, float32(cx), float32(cy), float32(cellW), float32(cellH), fill, false)
// 1-px white outline on every cell.
vector.DrawFilledRect(screen, float32(cx), float32(cy), float32(cellW), 1, outline, false)
vector.DrawFilledRect(screen, float32(cx), float32(cy+cellH-1), float32(cellW), 1, outline, false)
vector.DrawFilledRect(screen, float32(cx), float32(cy), 1, float32(cellH), outline, false)
vector.DrawFilledRect(screen, float32(cx+cellW-1), float32(cy), 1, float32(cellH), outline, false)
}
}
}
const CrewRadius = 6
var white = color.RGBA{255, 255, 255, 255}
func drawCrew(screen *ebiten.Image, c Crew, selected bool) {
var fx, fy float64
if len(c.Path) > 0 {
fx = float64(c.TileX) + (float64(c.Path[0][0])-float64(c.TileX))*c.MoveT
fy = float64(c.TileY) + (float64(c.Path[0][1])-float64(c.TileY))*c.MoveT
} else {
fx = float64(c.TileX)
fy = float64(c.TileY)
}
px := float32(fx*TilePx + TilePx/2)
py := float32(fy*TilePx + TilePx/2)
vector.DrawFilledCircle(screen, px, py, CrewRadius, c.Color, true)
if selected {
vector.StrokeCircle(screen, px, py, CrewRadius+1, 1, white, true)
}
ebitenutil.DebugPrintAt(screen, string(c.Initial), int(px)-2, int(py)-5)
}