Browse Source

Draw marker on pre-move pieces

master
Trevor Slocum 2 days ago
parent
commit
988d69cb10
  1. 59
      game/board.go
  2. 6
      game/game.go
  3. 7
      go.mod
  4. 14
      go.sum

59
game/board.go

@ -339,7 +339,7 @@ func (b *board) draw(screen *ebiten.Image) {
b.iterateSpaces(func(space int) {
var numPieces int
for _, sprite := range b.spaces[space] {
for i, sprite := range b.spaces[space] {
if sprite == b.dragging || sprite == b.moving {
continue
}
@ -347,26 +347,37 @@ func (b *board) draw(screen *ebiten.Image) {
drawSprite(sprite)
if numPieces > 5 {
label := fmt.Sprintf("%d", numPieces)
labelColor := color.RGBA{255, 255, 255, 255}
if sprite.colorWhite {
labelColor = color.RGBA{0, 0, 0, 255}
var overlayText string
if i > 5 {
overlayText = fmt.Sprintf("%d", numPieces)
}
if sprite.premove {
if overlayText != "" {
overlayText += " "
}
overlayText += "%"
}
if overlayText == "" {
continue
}
bounds := text.BoundString(mplusNormalFont, label)
overlayImage := ebiten.NewImage(bounds.Dx()*2, bounds.Dy()*2)
text.Draw(overlayImage, label, mplusNormalFont, 0, bounds.Dy(), labelColor)
labelColor := color.RGBA{255, 255, 255, 255}
if sprite.colorWhite {
labelColor = color.RGBA{0, 0, 0, 255}
}
x, y, w, h := b.stackSpaceRect(space, numPieces)
x += (w / 2) - (bounds.Dx() / 2)
y += (h / 2) - (bounds.Dy() / 2)
x, y = b.offsetPosition(x, y)
bounds := text.BoundString(mplusNormalFont, overlayText)
overlayImage := ebiten.NewImage(bounds.Dx()*2, bounds.Dy()*2)
text.Draw(overlayImage, overlayText, mplusNormalFont, 0, bounds.Dy(), labelColor)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(overlayImage, b.op)
}
x, y, w, h := b.stackSpaceRect(space, numPieces-1)
x += (w / 2) - (bounds.Dx() / 2)
y += (h / 2) - (bounds.Dy() / 2)
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(overlayImage, b.op)
}
})
@ -732,9 +743,8 @@ func (b *board) stackSpaceRect(space int, stack int) (x, y, w, h int) {
}
func (b *board) SetState(s []string, v []int) {
// TODO setstate include playernames, use string?
b.s = s
b.v = v
copy(b.s, s)
copy(b.v, v)
b.ProcessState()
}
@ -764,11 +774,11 @@ func (b *board) ProcessState() {
var preMovesTo = b.Client.Board.Premoveto[space]
var preMovesFrom = b.Client.Board.Premovefrom[space]
for i := 0; i < (abs+preMovesTo)-preMovesFrom; i++ {
for i := 0; i < abs+(preMovesTo-preMovesFrom); i++ {
s := b.newSprite(white)
if i >= abs {
s.colorWhite = v[fibs.StatePlayerColor] == 1
// TODO draw special premove overlay?
s.premove = true
}
b.spaces[space] = append(b.spaces[space], s)
b.Sprites.sprites = append(b.Sprites.sprites, s)
@ -863,6 +873,10 @@ func (b *board) movePiece(from int, to int) {
}
func (b *board) update() {
if b.Client == nil {
return
}
if b.dragging == nil {
// TODO allow grabbing multiple pieces by grabbing further down the stack
@ -941,6 +955,7 @@ func (b *board) update() {
for _, piece := range pieces {
if piece == dropped {
if space != index {
b.Client.Board.SetSelection(1, space)
b.Client.Board.AddPreMove(space, index)
b.ProcessState()
}

6
game/game.go

@ -117,6 +117,7 @@ type Sprite struct {
toX int
toY int
colorWhite bool
premove bool
}
func (s *Sprite) Update() {
@ -179,6 +180,8 @@ func NewGame() *Game {
// TODO fetch HTTP request, set debugExtra
}()
fibs.Debug = 1 // TODO
g := &Game{
op: &ebiten.DrawImageOptions{
Filter: ebiten.FilterNearest,
@ -206,13 +209,10 @@ func (g *Game) handleEvents() {
for e := range g.Client.Event {
switch event := e.(type) {
case *fibs.EventBoardState:
log.Println("STATE")
g.Board.SetState(event.S, event.V)
case *fibs.EventMove:
log.Println("MOVE")
g.Board.movePiece(event.From, event.To)
case *fibs.EventDraw:
log.Println("DRAW")
g.Board.ProcessState()
}
}

7
go.mod

@ -3,9 +3,9 @@ module code.rocketnine.space/tslocum/boxcars
go 1.17
require (
code.rocketnine.space/tslocum/fibs v0.0.0-20210912052010-63ce61f2bf8a
code.rocketnine.space/tslocum/kibodo v0.0.0-20210911035325-31c6bed89e2e
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210912171807-64a0577c26dc
code.rocketnine.space/tslocum/fibs v0.0.0-20210914035810-391fe9721596
code.rocketnine.space/tslocum/kibodo v0.0.0-20210914054352-0aae5255ccec
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210914160304-923c84a3d6ca
github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d
@ -14,6 +14,7 @@ require (
require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 // indirect
github.com/klauspost/compress v1.13.5 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect

14
go.sum

@ -1,9 +1,9 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
code.rocketnine.space/tslocum/fibs v0.0.0-20210912052010-63ce61f2bf8a h1:tKzGKj5q4zDsRo9LvF0Xf8K8UBnKLAR8AGf7qAlUFJQ=
code.rocketnine.space/tslocum/fibs v0.0.0-20210912052010-63ce61f2bf8a/go.mod h1:f7Cz0zJWOnJ9DW7R/GTuE8z5XWQtkbXkKpksW9SDu2c=
code.rocketnine.space/tslocum/kibodo v0.0.0-20210911035325-31c6bed89e2e h1:J2CHUA1G7HCOZ3Py6U2qK3yht9BXs0WlCbIuxTbNhXE=
code.rocketnine.space/tslocum/kibodo v0.0.0-20210911035325-31c6bed89e2e/go.mod h1:aj6xi25Ett7ECA5EdLm/0Ttr5WCk9vzrTPScURMJfq8=
code.rocketnine.space/tslocum/fibs v0.0.0-20210914035810-391fe9721596 h1:phymPJwH4udif7tuvbric4zrdlZgRLV8Vrcd0GkDNZ4=
code.rocketnine.space/tslocum/fibs v0.0.0-20210914035810-391fe9721596/go.mod h1:f7Cz0zJWOnJ9DW7R/GTuE8z5XWQtkbXkKpksW9SDu2c=
code.rocketnine.space/tslocum/kibodo v0.0.0-20210914054352-0aae5255ccec h1:J+rI9MFq6A/b17ia27avsJkiYzFMKCWoNeqnW2j0O9A=
code.rocketnine.space/tslocum/kibodo v0.0.0-20210914054352-0aae5255ccec/go.mod h1:aj6xi25Ett7ECA5EdLm/0Ttr5WCk9vzrTPScURMJfq8=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
@ -132,8 +132,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/hajimehoshi/bitmapfont/v2 v2.1.3 h1:JefUkL0M4nrdVwVq7MMZxSTh6mSxOylm+C4Anoucbb0=
github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.13.0.20210909171729-37771717cc52/go.mod h1:44O6eBPGyRv8YctRbfzaqUH2sek5UdXh0aLWOP02ELI=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210912171807-64a0577c26dc h1:6wvv0ah68Vyrm/D8sEsoy39YemknugNohxpzcSJrPaA=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210912171807-64a0577c26dc/go.mod h1:S8LcoD4mxw6u6nh+hxlxGjqUXtfBjeuIRvIR9NZTXOU=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210914160304-923c84a3d6ca h1:O2pG5L34GaUq+aAc/QVvetVlRPGTX2UrMB7fI4wsqRc=
github.com/hajimehoshi/ebiten/v2 v2.2.0-alpha.14.0.20210914160304-923c84a3d6ca/go.mod h1:fS7PLZeV3mclX0J6qubENa9ms3NWmZdNJkCOeEHmF74=
github.com/hajimehoshi/file2byteslice v0.0.0-20200812174855-0e5e8a80490e/go.mod h1:CqqAHp7Dk/AqQiwuhV1yT2334qbA/tFWQW0MD2dGqUE=
github.com/hajimehoshi/go-mp3 v0.3.2/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
@ -164,6 +164,8 @@ github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmK
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/jakecoffman/cp v1.1.0/go.mod h1:JjY/Fp6d8E1CHnu74gWNnU0+b9VzEdUVPoJxg2PsTQg=
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 h1:dy+DS31tGEGCsZzB45HmJJNHjur8GDgtRNX9U7HnSX4=
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240/go.mod h1:3P4UH/k22rXyHIJD2w4h2XMqPX4Of/eySEZq9L6wqc4=
github.com/jfreymuth/oggvorbis v1.0.3/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=

Loading…
Cancel
Save