boxcars/game/board.go

968 lines
22 KiB
Go
Raw Normal View History

2021-08-19 06:57:40 +00:00
package game
import (
2021-08-26 03:30:04 +00:00
"fmt"
2021-08-19 06:57:40 +00:00
"image"
"image/color"
2021-09-13 00:21:35 +00:00
"log"
2021-08-26 03:30:04 +00:00
"time"
2021-08-19 06:57:40 +00:00
2021-08-26 03:30:04 +00:00
"code.rocketnine.space/tslocum/fibs"
2021-08-19 06:57:40 +00:00
"github.com/hajimehoshi/ebiten/v2"
2021-08-26 03:30:04 +00:00
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/llgcode/draw2d/draw2dimg"
2021-08-19 06:57:40 +00:00
)
type board struct {
2021-08-26 03:30:04 +00:00
x, y int
w, h int
2021-08-19 06:57:40 +00:00
innerW, innerH int
2021-08-26 03:30:04 +00:00
op *ebiten.DrawImageOptions
2021-08-21 01:18:04 +00:00
2021-08-19 06:57:40 +00:00
backgroundImage *ebiten.Image
2021-08-26 03:30:04 +00:00
Sprites *Sprites
2021-08-19 06:57:40 +00:00
2021-08-26 03:30:04 +00:00
spaces [][]*Sprite // Space contents
spaceRects [][4]int
2021-08-20 03:18:28 +00:00
2021-08-26 03:30:04 +00:00
dragging *Sprite
moving *Sprite // Moving automatically
2021-08-20 03:18:28 +00:00
2021-08-26 03:30:04 +00:00
dragTouchId ebiten.TouchID
touchIDs []ebiten.TouchID
2021-08-20 03:18:28 +00:00
2021-08-26 03:30:04 +00:00
spaceWidth int
2021-08-20 03:18:28 +00:00
barWidth int
triangleOffset float64
horizontalBorderSize int
verticalBorderSize int
overlapSize int
2021-08-26 03:30:04 +00:00
lastDirection int
s []string
v []int
2021-08-26 03:30:04 +00:00
2021-08-31 04:26:49 +00:00
drawFrame chan bool
2021-08-26 03:30:04 +00:00
debug int // Print and draw debug information
Client *fibs.Client
dragX, dragY int
2021-08-19 06:57:40 +00:00
}
func NewBoard() *board {
2021-08-20 03:18:28 +00:00
b := &board{
barWidth: 100,
triangleOffset: float64(50),
horizontalBorderSize: 50,
verticalBorderSize: 25,
overlapSize: 97,
Sprites: &Sprites{
2021-08-21 01:18:04 +00:00
sprites: make([]*Sprite, 30),
num: 30,
2021-08-20 03:18:28 +00:00
},
2021-08-26 03:30:04 +00:00
spaces: make([][]*Sprite, 26),
spaceRects: make([][4]int, 26),
s: make([]string, 2),
v: make([]int, 42),
2021-08-31 04:26:49 +00:00
drawFrame: make(chan bool, 10),
2021-08-20 03:18:28 +00:00
}
2021-08-19 06:57:40 +00:00
for i := range b.Sprites.sprites {
2021-08-26 03:30:04 +00:00
s := b.newSprite(i%2 == 1)
2021-08-19 06:57:40 +00:00
b.Sprites.sprites[i] = s
2021-08-21 01:18:04 +00:00
2021-08-26 03:30:04 +00:00
space := i
if space > 25 {
if space%2 == 0 {
space = 0
} else {
space = 25
}
2021-08-21 01:18:04 +00:00
}
b.spaces[space] = append(b.spaces[space], s)
2021-08-19 06:57:40 +00:00
}
2021-08-31 04:26:49 +00:00
go b.handleDraw()
2021-08-19 06:57:40 +00:00
b.op = &ebiten.DrawImageOptions{}
2021-08-20 03:18:28 +00:00
b.dragTouchId = -1
2021-08-19 06:57:40 +00:00
return b
}
2021-08-31 04:26:49 +00:00
func (b *board) handleDraw() {
drawFreq := time.Second / 144 // TODO
lastDraw := time.Now()
for v := range b.drawFrame {
if !v {
return
}
since := time.Since(lastDraw)
if since < drawFreq {
t := time.NewTimer(drawFreq - since)
DELAYDRAW:
for {
select {
case <-b.drawFrame:
continue DELAYDRAW
case <-t.C:
break DELAYDRAW
}
}
}
ebiten.ScheduleFrame()
lastDraw = time.Now()
}
}
2021-08-26 03:30:04 +00:00
func (b *board) newSprite(white bool) *Sprite {
s := &Sprite{}
s.colorWhite = white
s.w, s.h = imgCheckerWhite.Size()
return s
2021-08-20 03:18:28 +00:00
}
2021-08-19 06:57:40 +00:00
func (b *board) updateBackgroundImage() {
2021-08-31 04:26:49 +00:00
borderSize := b.horizontalBorderSize
if borderSize > b.barWidth/2 {
borderSize = b.barWidth / 2
}
frameW := b.w - ((b.horizontalBorderSize - borderSize) * 2)
innerW := b.w - (b.horizontalBorderSize * 2) // Outer board width (including frame)
2021-08-19 06:57:40 +00:00
2021-08-21 01:18:04 +00:00
// Table
2021-08-20 03:18:28 +00:00
box := image.NewRGBA(image.Rect(0, 0, b.w, b.h))
img := ebiten.NewImageFromImage(box)
2021-08-21 01:18:04 +00:00
img.Fill(tableColor)
2021-08-19 06:57:40 +00:00
b.backgroundImage = ebiten.NewImageFromImage(img)
2021-08-31 04:26:49 +00:00
// Frame
box = image.NewRGBA(image.Rect(0, 0, frameW, b.h))
2021-08-21 01:18:04 +00:00
img = ebiten.NewImageFromImage(box)
2021-08-31 04:26:49 +00:00
img.Fill(frameColor)
2021-08-21 01:18:04 +00:00
b.op.GeoM.Reset()
2021-08-26 03:30:04 +00:00
b.op.GeoM.Translate(float64(b.horizontalBorderSize-borderSize), 0)
2021-08-21 01:18:04 +00:00
b.backgroundImage.DrawImage(img, b.op)
2021-08-20 03:18:28 +00:00
// Face
2021-08-31 04:26:49 +00:00
box = image.NewRGBA(image.Rect(0, 0, innerW, b.h-(b.verticalBorderSize*2)))
2021-08-19 06:57:40 +00:00
img = ebiten.NewImageFromImage(box)
2021-08-31 04:26:49 +00:00
img.Fill(faceColor)
2021-08-19 06:57:40 +00:00
b.op.GeoM.Reset()
2021-08-20 03:18:28 +00:00
b.op.GeoM.Translate(float64(b.horizontalBorderSize), float64(b.verticalBorderSize))
2021-08-19 06:57:40 +00:00
b.backgroundImage.DrawImage(img, b.op)
2021-08-20 03:18:28 +00:00
// Bar
box = image.NewRGBA(image.Rect(0, 0, b.barWidth, b.h))
img = ebiten.NewImageFromImage(box)
2021-08-31 04:26:49 +00:00
img.Fill(frameColor)
2021-08-20 03:18:28 +00:00
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64((b.w/2)-(b.barWidth/2)), 0)
b.backgroundImage.DrawImage(img, b.op)
2021-08-19 06:57:40 +00:00
// Draw triangles
2021-08-31 04:26:49 +00:00
baseImg := image.NewRGBA(image.Rect(0, 0, b.w-(b.horizontalBorderSize*2), b.h-(b.verticalBorderSize*2)))
gc := draw2dimg.NewGraphicContext(baseImg)
2021-08-19 06:57:40 +00:00
for i := 0; i < 2; i++ {
2021-08-26 03:30:04 +00:00
triangleTip := float64((b.h - (b.verticalBorderSize * 2)) / 2)
2021-08-19 06:57:40 +00:00
if i == 0 {
2021-08-20 03:18:28 +00:00
triangleTip -= b.triangleOffset
2021-08-19 06:57:40 +00:00
} else {
2021-08-20 03:18:28 +00:00
triangleTip += b.triangleOffset
2021-08-19 06:57:40 +00:00
}
for j := 0; j < 12; j++ {
2021-08-20 03:18:28 +00:00
colorA := j%2 == 0
if i == 1 {
colorA = !colorA
}
if colorA {
2021-08-31 04:26:49 +00:00
gc.SetFillColor(triangleA)
2021-08-20 03:18:28 +00:00
} else {
2021-08-31 04:26:49 +00:00
gc.SetFillColor(triangleB)
2021-08-20 03:18:28 +00:00
}
tx := b.spaceWidth * j
ty := b.h * i
if j >= 6 {
tx += b.barWidth
}
gc.MoveTo(float64(tx), float64(ty))
gc.LineTo(float64(tx+b.spaceWidth/2), triangleTip)
gc.LineTo(float64(tx+b.spaceWidth), float64(ty))
2021-08-19 06:57:40 +00:00
gc.Close()
gc.Fill()
}
}
img = ebiten.NewImageFromImage(baseImg)
b.op.GeoM.Reset()
2021-08-20 03:18:28 +00:00
b.op.GeoM.Translate(float64(b.horizontalBorderSize), float64(b.verticalBorderSize))
2021-08-19 06:57:40 +00:00
b.backgroundImage.DrawImage(img, b.op)
2021-08-31 04:26:49 +00:00
// Border
borderImage := image.NewRGBA(image.Rect(0, 0, b.w, b.h))
gc = draw2dimg.NewGraphicContext(borderImage)
gc.SetStrokeColor(borderColor)
// - Outside left
gc.SetLineWidth(2)
gc.MoveTo(float64(1), float64(0))
gc.LineTo(float64(1), float64(b.h))
// - Center
gc.SetLineWidth(2)
gc.MoveTo(float64(frameW/2), float64(0))
gc.LineTo(float64(frameW/2), float64(b.h))
// - Outside right
gc.MoveTo(float64(frameW), float64(0))
gc.LineTo(float64(frameW), float64(b.h))
gc.Close()
gc.Stroke()
// - Inside left
gc.SetLineWidth(1)
edge := float64((((innerW) - b.barWidth) / 2) + borderSize)
gc.MoveTo(float64(borderSize), float64(b.verticalBorderSize))
gc.LineTo(edge, float64(b.verticalBorderSize))
gc.LineTo(edge, float64(b.h-b.verticalBorderSize))
gc.LineTo(float64(borderSize), float64(b.h-b.verticalBorderSize))
gc.LineTo(float64(borderSize), float64(b.verticalBorderSize))
gc.Close()
gc.Stroke()
// - Inside right
edgeStart := float64((innerW / 2) + (b.barWidth / 2) + borderSize)
edgeEnd := float64(innerW + borderSize)
gc.MoveTo(float64(edgeStart), float64(b.verticalBorderSize))
gc.LineTo(edgeEnd, float64(b.verticalBorderSize))
gc.LineTo(edgeEnd, float64(b.h-b.verticalBorderSize))
gc.LineTo(float64(edgeStart), float64(b.h-b.verticalBorderSize))
gc.LineTo(float64(edgeStart), float64(b.verticalBorderSize))
gc.Close()
gc.Stroke()
img = ebiten.NewImageFromImage(borderImage)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(b.horizontalBorderSize-borderSize), 0)
b.backgroundImage.DrawImage(img, b.op)
}
func (b *board) ScheduleFrame() {
b.drawFrame <- true
2021-08-19 06:57:40 +00:00
}
2021-09-13 00:21:35 +00:00
func (b *board) resetButtonRect() (int, int, int, int) {
w := 200
h := 75
return (b.w - w) / 2, (b.h - h) / 2, w, h
}
2021-08-19 06:57:40 +00:00
func (b *board) draw(screen *ebiten.Image) {
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(b.x), float64(b.y))
screen.DrawImage(b.backgroundImage, b.op)
if b.debug == 2 {
b.iterateSpaces(func(space int) {
x, y, w, h := b.spaceRect(space)
spaceImage := ebiten.NewImage(w, h)
if space%2 == 0 {
spaceImage.Fill(color.RGBA{50, 50, 50, 150})
} else {
spaceImage.Fill(color.RGBA{255, 255, 255, 150})
}
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(spaceImage, b.op)
})
}
2021-08-26 03:30:04 +00:00
drawSprite := func(sprite *Sprite) {
x, y := float64(sprite.x), float64(sprite.y)
if !sprite.toStart.IsZero() {
progress := float64(time.Since(sprite.toStart)) / float64(sprite.toTime)
if x == float64(sprite.toX) && y == float64(sprite.toY) {
sprite.toStart = time.Time{}
sprite.x = sprite.toX
sprite.y = sprite.toY
} else {
if x < float64(sprite.toX) {
x += (float64(sprite.toX) - x) * progress
if x > float64(sprite.toX) {
x = float64(sprite.toX)
}
} else if x > float64(sprite.toX) {
x -= (x - float64(sprite.toX)) * progress
if x < float64(sprite.toX) {
x = float64(sprite.toX)
}
}
if y < float64(sprite.toY) {
y += (float64(sprite.toY) - y) * progress
if y > float64(sprite.toY) {
y = float64(sprite.toY)
}
} else if y > float64(sprite.toY) {
y -= (y - float64(sprite.toY)) * progress
if y < float64(sprite.toY) {
y = float64(sprite.toY)
}
}
}
// Schedule another frame
ebiten.ScheduleFrame()
}
2021-08-19 06:57:40 +00:00
b.op.GeoM.Reset()
b.op.GeoM.Translate(x, y)
2021-08-20 03:18:28 +00:00
if sprite.colorWhite {
screen.DrawImage(imgCheckerWhite, b.op)
} else {
screen.DrawImage(imgCheckerBlack, b.op)
}
2021-08-19 06:57:40 +00:00
}
2021-08-26 03:30:04 +00:00
b.iterateSpaces(func(space int) {
var numPieces int
2021-09-15 01:20:46 +00:00
for i, sprite := range b.spaces[space] {
2021-08-26 03:30:04 +00:00
if sprite == b.dragging || sprite == b.moving {
continue
}
numPieces++
drawSprite(sprite)
2021-09-15 01:20:46 +00:00
var overlayText string
if i > 5 {
overlayText = fmt.Sprintf("%d", numPieces)
}
if sprite.premove {
if overlayText != "" {
overlayText += " "
2021-08-26 03:30:04 +00:00
}
2021-09-15 01:20:46 +00:00
overlayText += "%"
}
if overlayText == "" {
continue
}
2021-08-26 03:30:04 +00:00
2021-09-15 01:20:46 +00:00
labelColor := color.RGBA{255, 255, 255, 255}
if sprite.colorWhite {
labelColor = color.RGBA{0, 0, 0, 255}
}
2021-08-26 03:30:04 +00:00
2021-09-15 01:20:46 +00:00
bounds := text.BoundString(mplusNormalFont, overlayText)
overlayImage := ebiten.NewImage(bounds.Dx()*2, bounds.Dy()*2)
text.Draw(overlayImage, overlayText, mplusNormalFont, 0, bounds.Dy(), labelColor)
2021-08-26 03:30:04 +00:00
2021-09-15 01:20:46 +00:00
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)
2021-08-26 03:30:04 +00:00
}
})
2021-09-13 00:21:35 +00:00
// Draw hover overlay
if b.dragging != nil {
dx, dy := b.dragX, b.dragY
x, y := ebiten.CursorPosition()
if x != 0 || y != 0 {
dx, dy = x, y
}
space := b.spaceAt(dx, dy)
if space >= 0 {
x, y, w, h := b.spaceRect(space)
spaceImage := ebiten.NewImage(w, h)
spaceImage.Fill(color.RGBA{255, 255, 255, 50})
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(spaceImage, b.op)
}
}
// Draw opponent name and dice
borderSize := b.horizontalBorderSize
if borderSize > b.barWidth/2 {
borderSize = b.barWidth / 2
}
playerColor := color.White
opponentColor := color.Black
if b.v[fibs.StatePlayerColor] == -1 {
playerColor = color.Black
opponentColor = color.White
}
2021-09-13 00:21:35 +00:00
drawLabel := func(label string, labelColor color.Color, border bool, borderColor color.Color) *ebiten.Image {
bounds := text.BoundString(mplusNormalFont, label)
w := int(float64(bounds.Dx()) * 1.5)
h := int(float64(bounds.Dy()) * 2)
baseImg := image.NewRGBA(image.Rect(0, 0, w, h))
// Draw border
if border {
gc := draw2dimg.NewGraphicContext(baseImg)
gc.SetLineWidth(5)
gc.SetStrokeColor(borderColor)
gc.MoveTo(float64(0), float64(0))
gc.LineTo(float64(w), 0)
gc.LineTo(float64(w), float64(h))
gc.LineTo(float64(0), float64(h))
gc.Close()
gc.Stroke()
}
img := ebiten.NewImageFromImage(baseImg)
text.Draw(img, label, mplusNormalFont, (w-bounds.Dx())/2, int(float64(h-(bounds.Max.Y/2))*0.75), labelColor)
return img
}
if b.s[fibs.StateOpponentName] != "" {
label := fmt.Sprintf("%s %d %d", b.s[fibs.StateOpponentName], b.v[fibs.StateOpponentDice1], b.v[fibs.StateOpponentDice2])
2021-09-13 00:21:35 +00:00
img := drawLabel(label, opponentColor, b.v[fibs.StateTurn] != b.v[fibs.StatePlayerColor], opponentColor)
bounds := img.Bounds()
x := ((b.innerW - borderSize) / 4) - (bounds.Dx() / 2)
y := (b.innerH / 2) - (bounds.Dy() / 2)
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(img, b.op)
}
// Draw player name and dice
2021-09-13 00:21:35 +00:00
if b.s[fibs.StatePlayerName] != "" {
label := fmt.Sprintf("%s %d %d", b.s[fibs.StatePlayerName], b.v[fibs.StatePlayerDice1], b.v[fibs.StatePlayerDice2])
2021-09-13 00:21:35 +00:00
img := drawLabel(label, playerColor, b.v[fibs.StateTurn] == b.v[fibs.StatePlayerColor], playerColor)
bounds := img.Bounds()
x := ((b.innerW / 4) * 3) - (bounds.Dx() / 2)
y := (b.innerH / 2) - (bounds.Dy() / 2)
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(img, b.op)
2021-09-13 00:21:35 +00:00
}
2021-09-13 00:21:35 +00:00
if len(b.Client.Board.GetPreMoves()) > 0 {
x, y, w, h := b.resetButtonRect()
baseImg := image.NewRGBA(image.Rect(0, 0, w, h))
gc := draw2dimg.NewGraphicContext(baseImg)
gc.SetLineWidth(5)
gc.SetStrokeColor(color.Black)
gc.MoveTo(0, 0)
gc.LineTo(float64(w), 0)
gc.LineTo(float64(w), float64(h))
gc.LineTo(0, float64(h))
gc.Close()
gc.Stroke()
img := ebiten.NewImage(w, h)
img.Fill(color.RGBA{225.0, 188, 125, 255})
img.DrawImage(ebiten.NewImageFromImage(baseImg), nil)
label := "Reset"
bounds := text.BoundString(mplusNormalFont, label)
text.Draw(img, label, mplusNormalFont, (w-bounds.Dx())/2, (h+(bounds.Dy()/2))/2, color.Black)
b.op.GeoM.Reset()
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(img, b.op)
}
2021-08-26 03:30:04 +00:00
// Draw moving sprite
if b.moving != nil {
drawSprite(b.moving)
}
// Draw dragged sprite
if b.dragging != nil {
drawSprite(b.dragging)
}
if b.debug == 2 {
b.iterateSpaces(func(space int) {
x, y, w, h := b.spaceRect(space)
spaceImage := ebiten.NewImage(w, h)
br := ""
if b.bottomRow(space) {
br = "B"
}
ebitenutil.DebugPrint(spaceImage, fmt.Sprintf(" %d %s", space, br))
x, y = b.offsetPosition(x, y)
b.op.GeoM.Reset()
b.op.GeoM.Scale(2, 2)
2021-08-26 03:30:04 +00:00
b.op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(spaceImage, b.op)
})
}
2021-08-19 06:57:40 +00:00
}
func (b *board) setRect(x, y, w, h int) {
if b.x == x && b.y == y && b.w == w && b.h == h {
return
}
2021-08-26 03:30:04 +00:00
const stackAllowance = 0.97 // TODO configurable
2021-08-19 06:57:40 +00:00
b.x, b.y, b.w, b.h = x, y, w, h
2021-08-21 01:18:04 +00:00
b.horizontalBorderSize = 0
2021-08-31 04:26:49 +00:00
b.triangleOffset = float64(b.h-(b.verticalBorderSize*2)) / 15
2021-08-21 01:18:04 +00:00
for {
2021-08-26 03:30:04 +00:00
b.verticalBorderSize = 7 // TODO configurable
2021-08-21 01:18:04 +00:00
b.spaceWidth = (b.w - (b.horizontalBorderSize * 2)) / 13
b.barWidth = b.spaceWidth
b.overlapSize = (((b.h - (b.verticalBorderSize * 2)) - (int(b.triangleOffset) * 2)) / 2) / 5
2021-08-26 03:30:04 +00:00
o := int(float64(b.spaceWidth) * stackAllowance)
if b.overlapSize >= o {
b.overlapSize = o
2021-08-21 01:18:04 +00:00
break
}
b.horizontalBorderSize++
}
2021-08-26 03:30:04 +00:00
extraSpace := b.w - (b.spaceWidth * 12)
largeBarWidth := int(float64(b.spaceWidth) * 1.25)
if extraSpace >= largeBarWidth {
b.barWidth = largeBarWidth
}
2021-08-21 01:18:04 +00:00
b.horizontalBorderSize = ((b.w - (b.spaceWidth * 12)) - b.barWidth) / 2
if b.horizontalBorderSize < 0 {
b.horizontalBorderSize = 0
}
2021-08-20 03:18:28 +00:00
borderSize := b.horizontalBorderSize
if borderSize > b.barWidth/2 {
borderSize = b.barWidth / 2
}
b.innerW = b.w - (b.horizontalBorderSize * 2)
b.innerH = b.h - (b.verticalBorderSize * 2)
2021-08-20 03:18:28 +00:00
loadAssets(b.spaceWidth)
2021-08-31 04:26:49 +00:00
2021-08-20 03:18:28 +00:00
for i := 0; i < b.Sprites.num; i++ {
s := b.Sprites.sprites[i]
s.w, s.h = imgCheckerWhite.Size()
}
2021-08-26 03:30:04 +00:00
b.setSpaceRects()
2021-08-19 06:57:40 +00:00
b.updateBackgroundImage()
b.positionCheckers()
}
func (b *board) offsetPosition(x, y int) (int, int) {
2021-08-20 03:18:28 +00:00
return b.x + x + b.horizontalBorderSize, b.y + y + b.verticalBorderSize
2021-08-19 06:57:40 +00:00
}
func (b *board) positionCheckers() {
2021-08-26 03:30:04 +00:00
for space := 0; space < 26; space++ {
2021-08-21 01:18:04 +00:00
sprites := b.spaces[space]
2021-08-20 03:18:28 +00:00
2021-08-21 01:18:04 +00:00
for i := range sprites {
s := sprites[i]
if b.dragging == s {
continue
}
2021-08-20 03:18:28 +00:00
2021-08-26 03:30:04 +00:00
x, y, w, _ := b.stackSpaceRect(space, i)
2021-08-21 01:18:04 +00:00
s.x, s.y = b.offsetPosition(x, y)
2021-08-26 03:30:04 +00:00
// Center piece in space
s.x += (w - s.w) / 2
2021-08-20 03:18:28 +00:00
}
2021-08-19 06:57:40 +00:00
}
b.ScheduleFrame()
2021-08-19 06:57:40 +00:00
}
2021-08-20 03:18:28 +00:00
func (b *board) spriteAt(x, y int) *Sprite {
2021-08-26 03:30:04 +00:00
space := b.spaceAt(x, y)
if space == -1 {
return nil
}
pieces := b.spaces[space]
if len(pieces) == 0 {
return nil
}
return pieces[len(pieces)-1]
}
func (b *board) spaceAt(x, y int) int {
for i := 0; i < 26; i++ {
sx, sy, sw, sh := b.spaceRect(i)
sx, sy = b.offsetPosition(sx, sy)
if x >= sx && x <= sx+sw && y >= sy && y <= sy+sh {
return i
}
}
return -1
}
func (b *board) iterateSpaces(f func(space int)) {
for space := 0; space <= 25; space++ {
2021-08-26 03:30:04 +00:00
f(space)
}
}
func (b *board) translateSpace(space int) int {
if b.v[fibs.StateDirection] == -1 {
2021-08-26 03:30:04 +00:00
// Spaces range from 24 - 1.
if space == 0 || space == 25 {
space = 25 - space
} else if space <= 12 {
space = 12 + space
} else {
space = space - 12
}
}
return space
}
func (b *board) setSpaceRects() {
var x, y, w, h int
for i := 0; i < 26; i++ {
trueSpace := i
space := b.translateSpace(i)
if !b.bottomRow(trueSpace) {
y = 0
} else {
y = (b.h / 2) - b.verticalBorderSize
}
w = b.spaceWidth
var hspace int // horizontal space
var add int
if space == 0 {
hspace = 6
w = b.barWidth
} else if space == 25 {
hspace = 6
w = b.barWidth
} else if space <= 6 {
hspace = space - 1
} else if space <= 12 {
hspace = space - 1
add = b.barWidth
} else if space <= 18 {
hspace = 24 - space
add = b.barWidth
} else {
hspace = 24 - space
}
x = (b.spaceWidth * hspace) + add
h = (b.h - (b.verticalBorderSize * 2)) / 2
b.spaceRects[trueSpace] = [4]int{x, y, w, h}
}
}
// relX, relY
func (b *board) spaceRect(space int) (x, y, w, h int) {
rect := b.spaceRects[space]
return rect[0], rect[1], rect[2], rect[3]
}
func (b *board) bottomRow(space int) bool {
bottomStart := 1
bottomEnd := 12
bottomBar := 25
if b.v[fibs.StateDirection] == 1 {
2021-08-26 03:30:04 +00:00
bottomStart = 13
bottomEnd = 24
bottomBar = 0
}
return space == bottomBar || (space >= bottomStart && space <= bottomEnd)
}
// relX, relY
func (b *board) stackSpaceRect(space int, stack int) (x, y, w, h int) {
x, y, w, h = b.spaceRect(space)
// Stack pieces
osize := float64(stack)
var o int
if stack > 4 {
osize = 3.5
}
if b.bottomRow(space) {
osize += 1.0
}
o = int(osize * float64(b.overlapSize))
if !b.bottomRow(space) {
y += o
} else {
y = y + (h - o)
}
w, h = b.spaceWidth, b.spaceWidth
if space == 0 || space == 25 {
w = b.barWidth
}
return x, y, w, h
}
func (b *board) SetState(s []string, v []int) {
2021-09-15 01:20:46 +00:00
copy(b.s, s)
copy(b.v, v)
2021-09-13 00:21:35 +00:00
b.ProcessState()
2021-08-26 03:30:04 +00:00
}
func (b *board) ProcessState() {
v := b.v
2021-08-26 03:30:04 +00:00
if b.lastDirection != v[fibs.StateDirection] {
b.setSpaceRects()
}
b.lastDirection = v[fibs.StateDirection]
b.Sprites = &Sprites{}
b.spaces = make([][]*Sprite, 26)
for space := 0; space < 26; space++ {
spaceValue := v[fibs.StateBoardSpace0+space]
white := spaceValue > 0
if spaceValue == 0 {
white = v[fibs.StatePlayerColor] == 1
}
2021-08-26 03:30:04 +00:00
abs := spaceValue
if abs < 0 {
abs *= -1
}
var preMovesTo = b.Client.Board.Premoveto[space]
var preMovesFrom = b.Client.Board.Premovefrom[space]
2021-09-15 01:20:46 +00:00
for i := 0; i < abs+(preMovesTo-preMovesFrom); i++ {
2021-08-26 03:30:04 +00:00
s := b.newSprite(white)
if i >= abs {
s.colorWhite = v[fibs.StatePlayerColor] == 1
2021-09-15 01:20:46 +00:00
s.premove = true
}
2021-08-26 03:30:04 +00:00
b.spaces[space] = append(b.spaces[space], s)
2021-08-20 03:18:28 +00:00
b.Sprites.sprites = append(b.Sprites.sprites, s)
2021-08-26 03:30:04 +00:00
}
}
b.Sprites.num = len(b.Sprites.sprites)
2021-08-19 06:57:40 +00:00
2021-08-26 03:30:04 +00:00
b.positionCheckers()
}
func (b *board) _movePiece(sprite *Sprite, from int, to int, speed int, pause bool) {
moveTime := time.Second / time.Duration(speed)
pauseTime := 750 * time.Millisecond
b.moving = sprite
2021-08-26 03:30:04 +00:00
2021-09-13 00:21:35 +00:00
space := to // Immediately go to target space
2021-09-13 00:21:35 +00:00
stack := len(b.spaces[space])
if stack == 1 && sprite.colorWhite != b.spaces[space][0].colorWhite {
stack = 0 // Hit
} else if space != to {
stack++
}
2021-08-31 04:26:49 +00:00
2021-09-13 00:21:35 +00:00
x, y, _, _ := b.stackSpaceRect(space, stack)
x, y = b.offsetPosition(x, y)
2021-08-31 04:26:49 +00:00
2021-09-13 00:21:35 +00:00
sprite.toX = x
sprite.toY = y
sprite.toTime = moveTime
sprite.toStart = time.Now()
ebiten.ScheduleFrame()
2021-10-01 01:04:04 +00:00
log.Println("SCHEDULED FRAME, SLEEP")
2021-09-13 00:21:35 +00:00
time.Sleep(sprite.toTime)
sprite.x = x
sprite.y = y
2021-10-01 01:04:04 +00:00
sprite.toStart = time.Time{}
ebiten.ScheduleFrame()
log.Println("SCHEDULED FRAME, UNSLEEP")
2021-09-13 00:21:35 +00:00
if pause {
log.Println("PAUSE ", pauseTime)
time.Sleep(pauseTime)
log.Println("UNPAUSE")
2021-10-01 01:04:04 +00:00
} else {
time.Sleep(50 * time.Millisecond)
2021-09-13 00:21:35 +00:00
}
2021-10-01 01:04:04 +00:00
/*homeSpace := b.Client.Board.PlayerHomeSpace()
2021-09-13 00:21:35 +00:00
if b.v[fibs.StateTurn] != b.v[fibs.StatePlayerColor] {
homeSpace = 25 - homeSpace
2021-08-26 03:30:04 +00:00
}
2021-10-01 01:04:04 +00:00
if to != homeSpace {*/
b.spaces[to] = append(b.spaces[to], sprite)
/*}*/
2021-08-26 03:30:04 +00:00
for i, s := range b.spaces[from] {
if s == sprite {
b.spaces[from] = append(b.spaces[from][:i], b.spaces[from][i+1:]...)
break
}
}
b.moving = nil
2021-08-31 04:26:49 +00:00
b.ScheduleFrame()
2021-09-13 00:21:35 +00:00
}
2021-08-31 04:26:49 +00:00
2021-09-13 00:21:35 +00:00
// Do not call directly
func (b *board) movePiece(from int, to int) {
pieces := b.spaces[from]
if len(pieces) == 0 {
log.Printf("%d-%d: NO PIECES AT SPACE %d", from, to, from)
return
}
2021-08-26 03:30:04 +00:00
2021-09-13 00:21:35 +00:00
sprite := pieces[len(pieces)-1]
2021-08-26 03:30:04 +00:00
2021-09-13 00:21:35 +00:00
var moveAfter *Sprite
if len(b.spaces[to]) == 1 {
if sprite.colorWhite != b.spaces[to][0].colorWhite {
moveAfter = b.spaces[to][0]
2021-08-26 03:30:04 +00:00
}
2021-09-13 00:21:35 +00:00
}
2021-08-26 03:30:04 +00:00
2021-09-13 00:21:35 +00:00
b._movePiece(sprite, from, to, 1, moveAfter == nil)
if moveAfter != nil {
2021-10-01 01:04:04 +00:00
b._movePiece(moveAfter, to, b.Client.Board.PlayerBarSpace(), 1, true)
2021-08-20 03:18:28 +00:00
}
2021-09-13 00:21:35 +00:00
log.Println("FINISH MOVE PIECE", from, to)
2021-08-20 03:18:28 +00:00
}
2021-08-19 06:57:40 +00:00
2021-08-20 03:18:28 +00:00
func (b *board) update() {
2021-09-15 01:20:46 +00:00
if b.Client == nil {
return
}
2021-08-20 03:18:28 +00:00
if b.dragging == nil {
2021-08-26 03:30:04 +00:00
// TODO allow grabbing multiple pieces by grabbing further down the stack
2021-09-13 00:21:35 +00:00
handleReset := func(x, y int) bool {
if len(b.Client.Board.GetPreMoves()) > 0 {
rx, ry, rw, rh := b.resetButtonRect()
if x >= rx && x <= rx+rw && y >= ry && y <= ry+rh {
b.Client.Board.ResetPreMoves()
b.ProcessState()
return true
}
}
return false
}
2021-08-20 03:18:28 +00:00
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
if b.dragging == nil {
2021-08-31 04:26:49 +00:00
x, y := ebiten.CursorPosition()
2021-09-13 00:21:35 +00:00
handled := handleReset(x, y)
if !handled {
s := b.spriteAt(x, y)
if s != nil {
b.dragging = s
2021-10-01 01:04:04 +00:00
// TODO set dragFrom instead of calculating later
2021-09-13 00:21:35 +00:00
}
2021-08-19 06:57:40 +00:00
}
}
}
2021-08-20 03:18:28 +00:00
b.touchIDs = inpututil.AppendJustPressedTouchIDs(b.touchIDs[:0])
for _, id := range b.touchIDs {
x, y := ebiten.TouchPosition(id)
2021-09-13 00:21:35 +00:00
handled := handleReset(x, y)
if !handled {
b.dragX, b.dragY = x, y
2021-09-13 00:21:35 +00:00
s := b.spriteAt(x, y)
if s != nil {
b.dragging = s
b.dragTouchId = id
}
2021-08-20 03:18:28 +00:00
}
}
2021-08-19 06:57:40 +00:00
}
2021-08-26 03:30:04 +00:00
x, y := ebiten.CursorPosition()
if b.dragTouchId != -1 {
x, y = ebiten.TouchPosition(b.dragTouchId)
if x != 0 || y != 0 { // 0,0 is returned when the touch is released
b.dragX, b.dragY = x, y
} else {
x, y = b.dragX, b.dragY
}
2021-08-26 03:30:04 +00:00
}
var dropped *Sprite
2021-08-20 03:18:28 +00:00
if b.dragTouchId == -1 {
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
2021-08-26 03:30:04 +00:00
dropped = b.dragging
2021-08-20 03:18:28 +00:00
b.dragging = nil
}
} else if inpututil.IsTouchJustReleased(b.dragTouchId) {
2021-08-26 03:30:04 +00:00
dropped = b.dragging
2021-08-19 06:57:40 +00:00
b.dragging = nil
}
2021-08-26 03:30:04 +00:00
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 {
2021-09-15 01:20:46 +00:00
b.Client.Board.SetSelection(1, space)
b.Client.Board.AddPreMove(space, index)
b.ProcessState()
}
break
}
2021-08-26 03:30:04 +00:00
}
}
}
2021-08-20 03:18:28 +00:00
}
2021-08-26 03:30:04 +00:00
b.positionCheckers()
}
2021-08-20 03:18:28 +00:00
2021-08-26 03:30:04 +00:00
if b.dragging != nil {
2021-08-19 06:57:40 +00:00
sprite := b.dragging
sprite.x = x - (sprite.w / 2)
sprite.y = y - (sprite.h / 2)
}
}