~bigbes/game-prototype-ftl

ref: f9d6b6136f9ab4134496b9c1f0fac5eb21d711a9 game-prototype-ftl/main.go -rw-r--r-- 2.4 KiB
f9d6b613 — Eugene Blikh docs(systems-power): record verify summary 30 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main

import (
	"image/color"
	"time"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
)

// Game holds the top-level state for the Ebitengine run loop.
type Game struct {
	ship         Ship
	walk         map[[2]int]bool
	crew         []Crew
	selectedCrew int
	lastTime     time.Time
	systems      []System
}

// Update advances simulation by dt seconds and handles input.
func (g *Game) Update() error {
	if g.lastTime.IsZero() {
		g.lastTime = time.Now()
		return nil
	}
	dt := time.Since(g.lastTime).Seconds()
	g.lastTime = time.Now()
	if dt > 0.1 {
		dt = 0.1
	}

	for i := range g.crew {
		updateCrew(&g.crew[i], dt)
	}

	if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
		cx, cy := ebiten.CursorPosition()
		if cy >= HudY {
			// HUD strip: route to power management.
			if role, ok := hudHitTest(cx, cy); ok {
				addPower(g.systems, role, ReactorCap)
			}
			// HUD-band miss (gutter / out-of-columns) is a no-op; do not fall through.
			return nil
		}
		// Below HudY: crew-select / move logic.
		tx, ty := cx/TilePx, cy/TilePx

		for i := range g.crew {
			if g.crew[i].TileX == tx && g.crew[i].TileY == ty {
				g.selectedCrew = i
				return nil
			}
		}

		if g.selectedCrew >= 0 && g.walk[[2]int{tx, ty}] {
			from := [2]int{g.crew[g.selectedCrew].TileX, g.crew[g.selectedCrew].TileY}
			g.crew[g.selectedCrew].Path = bfsPath(g.walk, from, [2]int{tx, ty})
			g.crew[g.selectedCrew].MoveT = 0
		}
	}

	if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
		cx, cy := ebiten.CursorPosition()
		if cy >= HudY {
			if role, ok := hudHitTest(cx, cy); ok {
				removePower(g.systems, role)
			}
			// HUD-band miss is a no-op.
		}
	}

	return nil
}

// Draw renders the whole frame.
func (g *Game) Draw(screen *ebiten.Image) {
	screen.Fill(color.RGBA{10, 15, 25, 255})
	drawShip(screen, g.ship, g.systems)
	for i, c := range g.crew {
		drawCrew(screen, c, i == g.selectedCrew)
	}
	drawHud(screen, g.systems)
}

// Layout fixes the virtual resolution; the window scales to fit.
func (g *Game) Layout(_, _ int) (int, int) {
	return VirtualW, VirtualH
}

func main() {
	ship := NewPlayerShip()
	walk := walkableTiles(ship)
	crew := NewStartingCrew()
	systems := NewStartingSystems()
	ebiten.SetWindowSize(1280, 720)
	ebiten.SetWindowTitle("ftl-shape")
	if err := ebiten.RunGame(&Game{
		ship:         ship,
		walk:         walk,
		crew:         crew,
		selectedCrew: -1,
		systems:      systems,
	}); err != nil {
		panic(err)
	}
}