Support bearing off by dragging outside board

Resolves #3.
This commit is contained in:
Trevor Slocum 2021-11-10 08:42:14 -08:00
parent 57f265acf3
commit 1e8ba9860c
6 changed files with 51 additions and 43 deletions

View File

@ -552,6 +552,13 @@ func (b *board) setRect(x, y, w, h int) {
b.spaceWidth = ((float64(b.w) - (b.horizontalBorderSize * 2)) - b.barWidth) / 12
}
if b.barWidth < 1 {
b.barWidth = 1
}
if b.spaceWidth < 1 {
b.spaceWidth = 1
}
borderSize := b.horizontalBorderSize
if borderSize > b.barWidth/2 {
borderSize = b.barWidth / 2
@ -568,14 +575,15 @@ func (b *board) setRect(x, y, w, h int) {
b.setSpaceRects()
b.updateBackgroundImage()
b.positionCheckers()
b.ProcessState()
}
func (b *board) offsetPosition(x, y int) (int, int) {
return b.x + x + int(b.horizontalBorderSize), b.y + y + int(b.verticalBorderSize)
}
func (b *board) positionCheckers() {
// Do not call _positionCheckers directly. Call ProcessState instead.
func (b *board) _positionCheckers() {
for space := 0; space < 26; space++ {
sprites := b.spaces[space]
@ -786,16 +794,11 @@ func (b *board) ProcessState() {
}
b.Sprites.num = len(b.Sprites.sprites)
b.positionCheckers()
b._positionCheckers()
}
// _movePiece returns after moving the piece.
func (b *board) _movePiece(sprite *Sprite, from int, to int, speed int, pause bool) {
t := time.Now()
defer func() {
log.Printf("MOVE PIECE TOOK %s", time.Since(t))
}()
moveTime := (750 * time.Millisecond) / time.Duration(speed)
pauseTime := 500 * time.Millisecond
@ -876,7 +879,6 @@ func (b *board) movePiece(from int, to int) {
}
b._movePiece(moveAfter, to, bar, 1, true)
}
log.Println("FINISH MOVE PIECE", from, to)
}
func (b *board) update() {
@ -951,27 +953,27 @@ func (b *board) update() {
b.dragging = nil
}
if dropped != nil {
// TODO allow dragging anywhere outside of board to bear off
// allow dragging on to bar to bear off
index := b.spaceAt(x, y)
if index >= 0 {
if b.Client != nil {
for space, pieces := range b.spaces {
for _, piece := range pieces {
if piece == dropped {
if space != index {
b.Client.Board.SetSelection(1, space)
b.Client.Board.AddPreMove(space, index)
b.ProcessState()
}
break
// Bear off by dragging outside the board.
if index == fibs.SpaceUnknown && b.Client.Board.PlayerPieceAreHome() {
index = b.Client.Board.PlayerBearOffSpace()
}
if index >= 0 && b.Client != nil {
ADDPREMOVE:
for space, pieces := range b.spaces {
for _, piece := range pieces {
if piece == dropped {
if space != index {
b.Client.Board.SetSelection(1, space)
b.Client.Board.AddPreMove(space, index)
}
break ADDPREMOVE
}
}
}
}
b.positionCheckers()
b.ProcessState()
}
if b.dragging != nil {

View File

@ -258,6 +258,7 @@ func (g *Game) handleEvents() {
case *fibs.EventDraw:
log.Println("EVENTDRAW START")
g.Board.ProcessState()
log.Println("EVENTDRAW FINISH")
}
}
}
@ -506,15 +507,18 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
statusBufferWidth = int(float64(g.screenW) * maxStatusWidthRatio)
}
gameBufferheight := 100
showGameBufferLines := 8
gameBufferHeight := g.statusBuffer.chatFontSize * showGameBufferLines * 2
statusBufferHeight := g.screenH - gameBufferHeight
g.Board.setRect(0, 0, g.screenW-statusBufferWidth, g.screenH-gameBufferheight)
g.lobby.setRect(0, 0, g.screenW, g.screenH)
availableWidth := g.screenW - (g.Board.innerW + int(g.Board.barWidth))
g.Board.setRect(0, 0, g.screenW-statusBufferWidth, g.screenH)
availableWidth := g.screenW - (g.Board.innerW + int(g.Board.horizontalBorderSize*2))
if availableWidth > statusBufferWidth {
statusBufferWidth = availableWidth
g.Board.setRect(0, 0, g.screenW-statusBufferWidth, g.screenH-gameBufferheight)
g.Board.setRect(0, 0, g.screenW-statusBufferWidth, g.screenH)
}
if g.Board.h > g.Board.w {
@ -523,10 +527,10 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
if true || availableWidth >= 150 { // TODO allow chat window to be repositioned
g.statusBuffer.docked = true
g.statusBuffer.setRect(g.screenW-statusBufferWidth, 0, statusBufferWidth, g.screenH)
g.statusBuffer.setRect(g.screenW-statusBufferWidth, g.screenH-(statusBufferHeight), statusBufferWidth, statusBufferHeight)
g.gameBuffer.docked = true
g.gameBuffer.setRect(0, g.Board.h, g.Board.w, g.screenH-(g.Board.h))
g.gameBuffer.setRect(g.screenW-statusBufferWidth, 0, statusBufferWidth, statusBufferHeight)
} else {
// Clamp buffer position.
bx, by := g.statusBuffer.x, g.statusBuffer.y

View File

@ -1,6 +1,7 @@
package game
import (
"fmt"
"image/color"
"code.rocketnine.space/tslocum/fibs"
@ -123,14 +124,14 @@ func (t *tabbedBuffers) drawBuffer() {
lineHeight := 14
showLines := t.h / lineHeight
if showLines > 1 {
showLines--
}
if t.acceptInput {
// Leave space for the input buffer.
if showLines > 1 {
showLines--
}
if showLines > 1 {
showLines--
}
}
if l < showLines {
@ -236,10 +237,10 @@ func (t *tabbedBuffers) update() {
if t.client != nil {
if len(t.inputBuffer) > 0 {
if t.inputBuffer[0] == '/' {
// TODO add chat modes and show (kibitz/yell)
t.inputBuffer = t.inputBuffer[1:]
t.client.Out <- t.inputBuffer[1:]
} else {
t.client.Out <- []byte(fmt.Sprintf("kibitz %s", t.inputBuffer))
}
t.client.Out <- t.inputBuffer
}
} else {
fibs.StatusWriter.Write([]byte("* You have not connected to a server yet"))

View File

@ -26,6 +26,8 @@ func (b *textBuffer) Write(p []byte) {
b.wrapDirty = true
b.tab.bufferDirty = true
// TODO /boardstate results in invalid draw
ebiten.ScheduleFrame()
}

4
go.mod
View File

@ -3,7 +3,7 @@ module code.rocketnine.space/tslocum/boxcars
go 1.17
require (
code.rocketnine.space/tslocum/fibs v0.0.0-20211104004539-6041d6487dd3
code.rocketnine.space/tslocum/fibs v0.0.0-20211110163919-18c4cdbc32e9
code.rocketnine.space/tslocum/kibodo v0.0.0-20211027223129-7b870790d865
github.com/hajimehoshi/ebiten/v2 v2.2.2
github.com/llgcode/draw2d v0.0.0-20210904075650-80aa0a2a901d
@ -23,7 +23,7 @@ require (
golang.org/x/exp v0.0.0-20211109222223-9df80dc805b5 // indirect
golang.org/x/mobile v0.0.0-20211109191125-d61a72f26a1a // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3 // indirect
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect
golang.org/x/text v0.3.7 // indirect
nhooyr.io/websocket v1.8.7 // indirect
)

9
go.sum
View File

@ -1,7 +1,7 @@
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-20211104004539-6041d6487dd3 h1:3zQAtK2vsQq8BKVNWlAiNF5SRsscm2l4IqkorwXSYSU=
code.rocketnine.space/tslocum/fibs v0.0.0-20211104004539-6041d6487dd3/go.mod h1:f7Cz0zJWOnJ9DW7R/GTuE8z5XWQtkbXkKpksW9SDu2c=
code.rocketnine.space/tslocum/fibs v0.0.0-20211110163919-18c4cdbc32e9 h1:L8YUzxCVOs8aK8I6vnheKZiladcw3Ykq4fhLQz234ck=
code.rocketnine.space/tslocum/fibs v0.0.0-20211110163919-18c4cdbc32e9/go.mod h1:zxixHM4hy1D4t6peJ79AcYa0lCl5+PaeAybgaRD2qp0=
code.rocketnine.space/tslocum/kibodo v0.0.0-20211027223129-7b870790d865 h1:Sm6hHfKceNAPvGw+zOmQm5u+TePLJgZzz8zyk2Q/HC0=
code.rocketnine.space/tslocum/kibodo v0.0.0-20211027223129-7b870790d865/go.mod h1:pQfyfr10kXO/Cqw/T+bTDEg3Xbhjxb4vn/vaPzPW/Vk=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20201218220906-28db891af037/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
@ -181,7 +181,6 @@ github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/klauspost/compress v1.13.5/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@ -442,8 +441,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
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-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3 h1:T6tyxxvHMj2L1R2kZg0uNMpS8ZhB9lRa9XRGTCSA65w=
golang.org/x/sys v0.0.0-20211109184856-51b60fd695b3/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ=
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/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.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=