Add RCI indicator
This commit is contained in:
parent
03809a1476
commit
fad18d5804
|
@ -37,6 +37,8 @@ type game struct {
|
|||
|
||||
addedSystems bool
|
||||
|
||||
updateTicks int
|
||||
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
|
@ -74,6 +76,13 @@ func (g *game) Update() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
const updateSidebarDelay = 144 * 3
|
||||
g.updateTicks++
|
||||
if g.updateTicks == updateSidebarDelay {
|
||||
world.World.HUDUpdated = true
|
||||
g.updateTicks = 0
|
||||
}
|
||||
|
||||
if world.World.ResetGame {
|
||||
world.Reset()
|
||||
|
||||
|
|
4
go.mod
4
go.mod
|
@ -16,7 +16,7 @@ require (
|
|||
github.com/jfreymuth/oggvorbis v1.0.3 // indirect
|
||||
github.com/jfreymuth/vorbis v1.0.2 // indirect
|
||||
golang.org/x/exp v0.0.0-20220104160115-025e73f80486 // indirect
|
||||
golang.org/x/mobile v0.0.0-20220104184238-4a8be17bd2e3 // indirect
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816 // indirect
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe // indirect
|
||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
|
||||
)
|
||||
|
|
8
go.sum
8
go.sum
|
@ -50,8 +50,8 @@ golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeap
|
|||
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
|
||||
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
|
||||
golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5/go.mod h1:c4YKU3ZylDmvbw+H/PSvm42vhdWbuxCzbonauEAP9B8=
|
||||
golang.org/x/mobile v0.0.0-20220104184238-4a8be17bd2e3 h1:A6xMk9ozH8+K90r7CpnP00jZ92Io02pQ/XLd55pZuf0=
|
||||
golang.org/x/mobile v0.0.0-20220104184238-4a8be17bd2e3/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816 h1:jhDgkcu3yQ4tasBZ+1YwDmK7eFmuVf1w1k+NGGGxfmE=
|
||||
golang.org/x/mobile v0.0.0-20220112015953-858099ff7816/go.mod h1:pe2sM7Uk+2Su1y7u/6Z8KJ24D7lepUjFZbhFOrmDfuQ=
|
||||
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
|
@ -72,8 +72,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe h1:W8vbETX/n8S6EmY0Pu4Ix7VvpsJUESTwl0oCK8MJOgk=
|
||||
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY=
|
||||
golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
|
||||
|
||||
"code.rocketnine.space/tslocum/citylimits/component"
|
||||
"code.rocketnine.space/tslocum/citylimits/world"
|
||||
"code.rocketnine.space/tslocum/gohan"
|
||||
|
@ -11,9 +13,10 @@ import (
|
|||
)
|
||||
|
||||
type RenderHudSystem struct {
|
||||
op *ebiten.DrawImageOptions
|
||||
hudImg *ebiten.Image
|
||||
tmpImg *ebiten.Image
|
||||
op *ebiten.DrawImageOptions
|
||||
hudImg *ebiten.Image
|
||||
tmpImg *ebiten.Image
|
||||
sidebarColor color.RGBA
|
||||
}
|
||||
|
||||
func NewRenderHudSystem() *RenderHudSystem {
|
||||
|
@ -23,6 +26,9 @@ func NewRenderHudSystem() *RenderHudSystem {
|
|||
tmpImg: ebiten.NewImage(1, 1),
|
||||
}
|
||||
|
||||
sidebarShade := uint8(111)
|
||||
s.sidebarColor = color.RGBA{sidebarShade, sidebarShade, sidebarShade, 255}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -66,17 +72,95 @@ func (s *RenderHudSystem) drawHUD() {
|
|||
w = bounds.Dx()
|
||||
}
|
||||
|
||||
sidebarShade := uint8(111)
|
||||
sidebarColor := color.RGBA{sidebarShade, sidebarShade, sidebarShade, 255}
|
||||
s.tmpImg.Fill(sidebarColor)
|
||||
// Fill background.
|
||||
s.tmpImg.Fill(s.sidebarColor)
|
||||
|
||||
// Draw buttons.
|
||||
|
||||
paddingSize := 1
|
||||
borderSize := 2
|
||||
columns := 3
|
||||
|
||||
buttonWidth := world.SidebarWidth / columns
|
||||
buttonHeight := buttonWidth
|
||||
world.World.HUDButtonRects = make([]image.Rectangle, len(world.HUDButtons))
|
||||
var lastButtonY int
|
||||
for i, button := range world.HUDButtons {
|
||||
row := i / columns
|
||||
x, y := (i%columns)*buttonWidth, row*buttonHeight
|
||||
r := image.Rect(x+paddingSize, y+paddingSize, x+buttonWidth-paddingSize, y+buttonHeight-paddingSize)
|
||||
|
||||
selected := world.World.HoverStructure == button.StructureType
|
||||
|
||||
// Draw background.
|
||||
s.drawButtonBackground(s.tmpImg, r, selected)
|
||||
|
||||
// Draw sprite.
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(x+paddingSize)+button.SpriteOffsetX, float64(y+paddingSize)+button.SpriteOffsetY)
|
||||
s.tmpImg.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).DrawImage(button.Sprite, op)
|
||||
|
||||
s.drawButtonBorder(s.tmpImg, r, selected)
|
||||
|
||||
world.World.HUDButtonRects[i] = r
|
||||
lastButtonY = y
|
||||
}
|
||||
|
||||
// Draw RCI indicator.
|
||||
rciPadding := buttonWidth / 2
|
||||
const rciSize = 100
|
||||
rciX := buttonWidth
|
||||
rciY := lastButtonY + buttonHeight + rciPadding
|
||||
|
||||
// Draw RCI bars.
|
||||
colorR := color.RGBA{0, 255, 0, 255}
|
||||
colorC := color.RGBA{0, 0, 255, 255}
|
||||
colorI := color.RGBA{231, 231, 72, 255}
|
||||
demandR, demandC, demandI := world.Demand()
|
||||
drawDemandBar := func(demand float64, clr color.RGBA, i int) {
|
||||
barOffsetSize := 12
|
||||
barOffset := -barOffsetSize + (i * barOffsetSize)
|
||||
barWidth := 7
|
||||
barX := rciX + buttonWidth/2 - barWidth/2 + barOffset
|
||||
barHeight := int((float64(rciSize) / 2) * demand)
|
||||
s.tmpImg.SubImage(image.Rect(barX, rciY+(rciSize/2), barX+barWidth, rciY+(rciSize/2)-barHeight)).(*ebiten.Image).Fill(clr)
|
||||
}
|
||||
drawDemandBar(demandR, colorR, 0)
|
||||
drawDemandBar(demandC, colorC, 1)
|
||||
drawDemandBar(demandI, colorI, 2)
|
||||
|
||||
// Draw RCI button.
|
||||
const rciButtonPadding = 12
|
||||
const rciButtonHeight = 20
|
||||
const rciButtonLabelPaddingX = 6
|
||||
const rciButtonLabelPaddingY = 1
|
||||
rciButtonY := rciY + (rciSize / 2) - (rciButtonHeight / 2)
|
||||
rciButtonRect := image.Rect(rciX+rciButtonPadding, rciButtonY, rciX+buttonWidth-rciButtonPadding, rciButtonY+rciButtonHeight)
|
||||
|
||||
s.drawButtonBackground(s.tmpImg, rciButtonRect, false) // TODO
|
||||
|
||||
// Draw RCI label.
|
||||
ebitenutil.DebugPrintAt(s.tmpImg, "R C I", rciX+rciButtonPadding+rciButtonLabelPaddingX, rciButtonY+rciButtonLabelPaddingY)
|
||||
|
||||
s.drawButtonBorder(s.tmpImg, rciButtonRect, false) // TODO
|
||||
|
||||
s.hudImg.DrawImage(s.tmpImg, nil)
|
||||
}
|
||||
|
||||
func (s *RenderHudSystem) drawButtonBackground(img *ebiten.Image, r image.Rectangle, selected bool) {
|
||||
buttonShade := uint8(142)
|
||||
colorButton := color.RGBA{buttonShade, buttonShade, buttonShade, 255}
|
||||
|
||||
bgColor := colorButton
|
||||
if selected {
|
||||
bgColor = s.sidebarColor
|
||||
}
|
||||
|
||||
img.SubImage(r).(*ebiten.Image).Fill(bgColor)
|
||||
}
|
||||
|
||||
func (s *RenderHudSystem) drawButtonBorder(img *ebiten.Image, r image.Rectangle, selected bool) {
|
||||
borderSize := 2
|
||||
|
||||
lightBorderShade := uint8(216)
|
||||
colorLightBorder := color.RGBA{lightBorderShade, lightBorderShade, lightBorderShade, 255}
|
||||
|
||||
|
@ -86,41 +170,18 @@ func (s *RenderHudSystem) drawHUD() {
|
|||
darkBorderShade := uint8(42)
|
||||
colorDarkBorder := color.RGBA{darkBorderShade, darkBorderShade, darkBorderShade, 255}
|
||||
|
||||
swatchWidth := world.SidebarWidth / columns
|
||||
swatchHeight := swatchWidth
|
||||
world.World.HUDButtonRects = make([]image.Rectangle, len(world.HUDButtons))
|
||||
for i, button := range world.HUDButtons {
|
||||
row := i / columns
|
||||
x, y := (i%columns)*swatchWidth, row*swatchHeight
|
||||
r := image.Rect(x+paddingSize, y+paddingSize, x+swatchWidth-paddingSize, y+swatchHeight-paddingSize)
|
||||
|
||||
bgColor := colorButton
|
||||
topLeftBorder := colorLightBorder
|
||||
bottomRightBorder := colorMediumBorder
|
||||
if world.World.HoverStructure == button.StructureType {
|
||||
bgColor = sidebarColor
|
||||
topLeftBorder = colorDarkBorder
|
||||
bottomRightBorder = colorLightBorder
|
||||
}
|
||||
|
||||
// Draw background.
|
||||
s.tmpImg.SubImage(r).(*ebiten.Image).Fill(bgColor)
|
||||
|
||||
// Draw sprite.
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(x+paddingSize)+button.SpriteOffsetX, float64(y+paddingSize)+button.SpriteOffsetY)
|
||||
s.tmpImg.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).DrawImage(button.Sprite, op)
|
||||
|
||||
// Draw top and left border.
|
||||
s.tmpImg.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+borderSize)).(*ebiten.Image).Fill(topLeftBorder)
|
||||
s.tmpImg.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+borderSize, r.Max.Y)).(*ebiten.Image).Fill(topLeftBorder)
|
||||
|
||||
// Draw bottom and right border.
|
||||
s.tmpImg.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(bottomRightBorder)
|
||||
s.tmpImg.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(bottomRightBorder)
|
||||
|
||||
world.World.HUDButtonRects[i] = r
|
||||
topLeftBorder := colorLightBorder
|
||||
bottomRightBorder := colorMediumBorder
|
||||
if selected {
|
||||
topLeftBorder = colorDarkBorder
|
||||
bottomRightBorder = colorLightBorder
|
||||
}
|
||||
|
||||
s.hudImg.DrawImage(s.tmpImg, nil)
|
||||
// Draw top and left border.
|
||||
img.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Max.X, r.Min.Y+borderSize)).(*ebiten.Image).Fill(topLeftBorder)
|
||||
img.SubImage(image.Rect(r.Min.X, r.Min.Y, r.Min.X+borderSize, r.Max.Y)).(*ebiten.Image).Fill(topLeftBorder)
|
||||
|
||||
// Draw bottom and right border.
|
||||
img.SubImage(image.Rect(r.Min.X, r.Max.Y-borderSize, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(bottomRightBorder)
|
||||
img.SubImage(image.Rect(r.Max.X-borderSize, r.Min.Y, r.Max.X, r.Max.Y)).(*ebiten.Image).Fill(bottomRightBorder)
|
||||
}
|
||||
|
|
|
@ -483,3 +483,10 @@ func SetHoverStructure(structureType int) {
|
|||
World.HoverStructure = structureType
|
||||
World.HUDUpdated = true
|
||||
}
|
||||
|
||||
func Demand() (r, c, i float64) {
|
||||
r = (rand.Float64() * 2) - 1
|
||||
c = (rand.Float64() * 2) - 1
|
||||
i = (rand.Float64() * 2) - 1
|
||||
return r, c, i
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue