Draw space numbers

main
Trevor Slocum 2023-09-18 18:47:53 -07:00
parent e5b0a3470e
commit b7b0599a72
8 changed files with 61 additions and 53 deletions

View File

@ -1,8 +0,0 @@
//go:build js && wasm
// +build js,wasm
package main
func init() {
AutoWatch = true
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -6,11 +6,11 @@ import (
"image/color"
"log"
"os"
"strconv"
"time"
"github.com/hajimehoshi/ebiten/v2"
"code.rocket9labs.com/tslocum/bgammon"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
@ -73,7 +73,7 @@ func NewBoard() *board {
barWidth: 100,
triangleOffset: float64(50),
horizontalBorderSize: 20,
verticalBorderSize: 10,
verticalBorderSize: 20,
overlapSize: 97,
Sprites: &Sprites{
sprites: make([]*Sprite, 30),
@ -90,12 +90,12 @@ func NewBoard() *board {
{
label: "Roll",
selected: b.selectRoll,
}, {
label: "OK",
selected: b.selectOK,
}, {
label: "Reset",
selected: b.selectReset,
}, {
label: "OK",
selected: b.selectOK,
},
}
@ -113,15 +113,15 @@ func NewBoard() *board {
}
func (b *board) selectRoll() {
b.Client.Out <- []byte("roll")
}
func (b *board) selectOK() {
b.Client.Out <- []byte("ok")
}
func (b *board) selectReset() {
b.Client.Out <- []byte("reset")
}
func (b *board) handleDraw() {
@ -283,18 +283,29 @@ func (b *board) updateBackgroundImage() {
b.op.GeoM.Reset()
b.op.GeoM.Translate(b.horizontalBorderSize-borderSize, 0)
b.backgroundImage.DrawImage(img, b.op)
// Draw space numbers.
for space, r := range b.spaceRects {
if space < 1 || space > 24 {
continue
}
sp := strconv.Itoa(space)
if space < 10 {
sp = " " + sp
}
x := r[0] + r[2]/2 + int(b.horizontalBorderSize/2) + 4
y := r[1] + 2
if b.bottomRow(space) {
y = b.h - int(b.verticalBorderSize) + 2
}
ebitenutil.DebugPrintAt(b.backgroundImage, sp, x, y)
}
}
func (b *board) ScheduleFrame() {
b.drawFrame <- true
}
func (b *board) resetButtonRect() (int, int, int, int) {
w := 200
h := 75
return (b.w - w) / 2, (b.h - h) / 2, w, h
}
func (b *board) drawButtons(screen *ebiten.Image) {
for i, btn := range b.buttons {
if (i == 0 && b.gameState.MayRoll()) ||
@ -605,8 +616,8 @@ func (b *board) draw(screen *ebiten.Image) {
func (b *board) updateButtonRects() {
btnRoll := b.buttons[0]
btnOK := b.buttons[1]
btnReset := b.buttons[2]
btnReset := b.buttons[1]
btnOK := b.buttons[2]
w := 200
h := 75
@ -614,8 +625,15 @@ func (b *board) updateButtonRects() {
btnRoll.rect = image.Rect(x, y, x+w, y+h)
btnOK.rect = image.Rect(x, y, x+w/2, y+h)
btnReset.rect = image.Rect(x+w/2, y, x+w, y+h)
const padding = 20
if b.gameState.MayReset() && b.gameState.MayOK() {
log.Println("BOTH")
btnReset.rect = image.Rect(b.w/2-padding/2-w, y, b.w/2-padding/2, y+h)
btnOK.rect = image.Rect(b.w/2+padding/2, y, b.w/2+padding/2+w, y+h)
} else {
btnReset.rect = image.Rect(x, y, x+w, y+h)
btnOK.rect = image.Rect(x, y, x+w, y+h)
}
}
func (b *board) setRect(x, y, w, h int) {
@ -836,6 +854,7 @@ func (b *board) ProcessState() {
if b.lastPlayerNumber != b.gameState.PlayerNumber {
b.setSpaceRects()
}
b.updateButtonRects()
b.lastPlayerNumber = b.gameState.PlayerNumber
b.Sprites = &Sprites{}
@ -968,7 +987,22 @@ func (b *board) playingGame() bool {
}
func (b *board) playerTurn() bool {
return b.playingGame() && b.gameState.Turn == b.gameState.PlayerNumber
return b.playingGame() && (b.gameState.MayRoll() || b.gameState.Turn == b.gameState.PlayerNumber)
}
func (b *board) handleClick(x int, y int) bool {
p := image.Point{x, y}
for i, btn := range b.buttons {
if (i == 0 && b.gameState.MayRoll()) ||
(i == 1 && b.gameState.MayReset()) ||
(i == 2 && b.gameState.MayOK()) {
if p.In(btn.rect) {
btn.selected()
return true
}
}
}
return false
}
func (b *board) update() {
@ -979,21 +1013,10 @@ func (b *board) update() {
if b.dragging == nil && b.playerTurn() {
// TODO allow grabbing multiple pieces by grabbing further down the stack
handleReset := func(x, y int) bool {
if b.gameState.Turn == b.gameState.PlayerNumber && len(b.gameState.Moves) > 0 {
rx, ry, rw, rh := b.resetButtonRect()
if x >= rx && x <= rx+rw && y >= ry && y <= ry+rh {
b.Client.Out <- []byte("reset")
return true
}
}
return false
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
if b.dragging == nil {
x, y := ebiten.CursorPosition()
handled := handleReset(x, y)
handled := b.handleClick(x, y)
if !handled {
s := b.spriteAt(x, y)
if s != nil {
@ -1007,7 +1030,7 @@ func (b *board) update() {
b.touchIDs = inpututil.AppendJustPressedTouchIDs(b.touchIDs[:0])
for _, id := range b.touchIDs {
x, y := ebiten.TouchPosition(id)
handled := handleReset(x, y)
handled := b.handleClick(x, y)
if !handled {
b.dragX, b.dragY = x, y

2
go.mod
View File

@ -3,7 +3,7 @@ module code.rocket9labs.com/tslocum/boxcars
go 1.17
require (
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230918181620-3db3cacce39c
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230919010037-e6b46bf42cf4
code.rocketnine.space/tslocum/kibodo v1.0.0
code.rocketnine.space/tslocum/messeji v1.0.3
github.com/hajimehoshi/ebiten/v2 v2.5.9

4
go.sum
View File

@ -1,5 +1,5 @@
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230918181620-3db3cacce39c h1:CAI9t5uwDjKNulAXqDd6VnOGS6Jjx2SR1we9SVxtYLE=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230918181620-3db3cacce39c/go.mod h1:LS/m5Zq7/93dP8XJrLkL1T5ZTwtddkN8X9TyRrrdCkQ=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230919010037-e6b46bf42cf4 h1:sCTjSVNkOu0mv1JzVhmU0SkSWAuapuMZ9q5oyBLgd9U=
code.rocket9labs.com/tslocum/bgammon v0.0.0-20230919010037-e6b46bf42cf4/go.mod h1:LS/m5Zq7/93dP8XJrLkL1T5ZTwtddkN8X9TyRrrdCkQ=
code.rocketnine.space/tslocum/kibodo v1.0.0 h1:/xs59UCuo+NGs89ABilARlowQnmIsjbNVjss57W5O7k=
code.rocketnine.space/tslocum/kibodo v1.0.0/go.mod h1:xYmBfho98sIbB+Gtf8SU5GDQD9HOSqOtZ64eZnlHmRI=
code.rocketnine.space/tslocum/messeji v1.0.3 h1:o0HqUckStFUFE2SkYrkzRHoAY7QT7cTsuRQ7JEmfw6w=

View File

@ -3,15 +3,15 @@ project_name: boxcars
builds:
-
id: boxcars
# ldflags:
# - -s -w -X code.rocket9labs.com/tslocum/boxcars/main.Version={{.Version}}
goos:
- js
- linux
- windows
# - darwin
# - freebsd
goarch:
- amd64
- wasm
archives:
-
id: boxcars
@ -24,7 +24,6 @@ archives:
format: zip
files:
- ./*.md
# - CHANGELOG
- LICENSE
checksum:
name_template: 'checksums.txt'

View File

@ -18,8 +18,6 @@ const (
screenHeight = 768
)
var AutoWatch bool // WASM only
func main() {
ebiten.SetWindowTitle("bgammon.org - Free Online Backgammon")
ebiten.SetWindowSize(screenWidth, screenHeight)
@ -44,10 +42,6 @@ func main() {
}()
}
if AutoWatch {
g.Watch = true
}
// Auto-connect
if g.Username != "" && g.Password != "" {
g.Connect()