Compare commits
3 Commits
e454d649a5
...
element-re
| Author | SHA1 | Date | |
|---|---|---|---|
|
8e7db331a7
|
|||
| 4e6d720a91 | |||
|
5f47a811aa
|
@@ -3,51 +3,36 @@ package blocks
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/vector"
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) *Block {
|
||||||
return func() elements.Element {
|
b := &Block{}
|
||||||
b := Block{}
|
for i := range ops {
|
||||||
for i := range ops {
|
ops[i](b)
|
||||||
ops[i](&b)
|
|
||||||
}
|
|
||||||
return &b
|
|
||||||
}
|
}
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
type Block struct {
|
||||||
anchor elements.Point
|
core.Element
|
||||||
|
|
||||||
backgroundColor *color.Color
|
backgroundColor *color.Color
|
||||||
mouse.NopHandler
|
width, height int
|
||||||
width, height int
|
name string
|
||||||
name string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) SetAnchor(a elements.Point) {
|
func (b *Block) Bounds() geo.Bounds {
|
||||||
d := b.anchor.Delta(a)
|
return geo.Bounds{
|
||||||
for i := range b.Children() {
|
Min: b.Anchor(),
|
||||||
b.Children()[i].SetAnchor(b.Children()[i].Anchor().Add(d))
|
|
||||||
}
|
|
||||||
b.anchor = a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) Bounds() elements.Bounds {
|
|
||||||
return elements.Bounds{
|
|
||||||
Min: b.anchor,
|
|
||||||
Width: b.width,
|
Width: b.width,
|
||||||
Height: b.height,
|
Height: b.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Anchor() elements.Point {
|
|
||||||
return b.anchor
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Block) Size() (w, h int) {
|
func (b *Block) Size() (w, h int) {
|
||||||
return b.width, b.height
|
return b.width, b.height
|
||||||
}
|
}
|
||||||
@@ -57,8 +42,8 @@ func (b *Block) Draw(image *ebiten.Image) {
|
|||||||
if b.backgroundColor != nil {
|
if b.backgroundColor != nil {
|
||||||
vector.DrawFilledRect(
|
vector.DrawFilledRect(
|
||||||
image,
|
image,
|
||||||
float32(b.anchor.X),
|
float32(b.Anchor().X),
|
||||||
float32(b.anchor.Y),
|
float32(b.Anchor().Y),
|
||||||
float32(bnd.Width),
|
float32(bnd.Width),
|
||||||
float32(bnd.Height),
|
float32(bnd.Height),
|
||||||
*b.backgroundColor,
|
*b.backgroundColor,
|
||||||
@@ -67,7 +52,3 @@ func (b *Block) Draw(image *ebiten.Image) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Block) Children() []elements.Element {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,9 +1,21 @@
|
|||||||
package blocks
|
package blocks
|
||||||
|
|
||||||
import "image/color"
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
|
)
|
||||||
|
|
||||||
type OptFunc func(*Block)
|
type OptFunc func(*Block)
|
||||||
|
|
||||||
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
|
return func(b *Block) {
|
||||||
|
for _, f := range fs {
|
||||||
|
f(&b.Element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Size(w, h int) OptFunc {
|
func Size(w, h int) OptFunc {
|
||||||
return func(b *Block) {
|
return func(b *Block) {
|
||||||
b.width, b.height = w, h
|
b.width, b.height = w, h
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package buttons
|
|||||||
import (
|
import (
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
"git.vezzani.net/ben/games/common/ux/v1"
|
"git.vezzani.net/ben/games/common/ux/v1"
|
||||||
@@ -13,19 +12,17 @@ import (
|
|||||||
"golang.org/x/image/font"
|
"golang.org/x/image/font"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) *Button {
|
||||||
return func() elements.Element {
|
b := Button{
|
||||||
b := Button{
|
Block: blocks.Block{},
|
||||||
Block: blocks.Block{},
|
font: ux.FontFace,
|
||||||
font: ux.FontFace,
|
color: ux.FontColor,
|
||||||
color: ux.FontColor,
|
|
||||||
}
|
|
||||||
for op := range ops {
|
|
||||||
ops[op](&b)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &b
|
|
||||||
}
|
}
|
||||||
|
for op := range ops {
|
||||||
|
ops[op](&b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &b
|
||||||
}
|
}
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
@@ -67,7 +64,7 @@ func (b *Button) HandleMouseEvent(ms mouse.State) bool {
|
|||||||
if !bnd.Contains(ms.Point()) {
|
if !bnd.Contains(ms.Point()) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
case b.onClick != nil && (ms.RightClicked || ms.LeftClicked):
|
||||||
b.onClick(ms)
|
b.onClick(ms)
|
||||||
|
|||||||
@@ -4,13 +4,18 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptFunc func(button *Button)
|
type OptFunc func(button *Button)
|
||||||
|
|
||||||
type Option interface {
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
OptFunc | blocks.OptFunc
|
return func(b *Button) {
|
||||||
|
for _, f := range fs {
|
||||||
|
f(&b.Element)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func OnClick(f func(ms mouse.State)) OptFunc {
|
func OnClick(f func(ms mouse.State)) OptFunc {
|
||||||
|
|||||||
17
common/elements/v1/core/config.go
Normal file
17
common/elements/v1/core/config.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import "git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
|
||||||
|
type OptFunc func(*Element)
|
||||||
|
|
||||||
|
func Children(children ...elements.Element) OptFunc {
|
||||||
|
return func(c *Element) {
|
||||||
|
c.children = children
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Name(name string) OptFunc {
|
||||||
|
return func(s *Element) {
|
||||||
|
s.name = name
|
||||||
|
}
|
||||||
|
}
|
||||||
32
common/elements/v1/core/core.go
Normal file
32
common/elements/v1/core/core.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Element struct {
|
||||||
|
mouse.NopHandler
|
||||||
|
|
||||||
|
anchor geo.Point
|
||||||
|
children []elements.Element
|
||||||
|
|
||||||
|
name string // For debugging
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) Anchor() geo.Point {
|
||||||
|
return c.anchor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) SetAnchor(a geo.Point) {
|
||||||
|
d := c.Anchor().Delta(a)
|
||||||
|
for _, ch := range c.Children() {
|
||||||
|
ch.SetAnchor(ch.Anchor().Add(d))
|
||||||
|
}
|
||||||
|
c.anchor = a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Element) Children() []elements.Element {
|
||||||
|
return c.children
|
||||||
|
}
|
||||||
@@ -1,42 +1,14 @@
|
|||||||
package elements
|
package elements
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InitFunc func() Element
|
|
||||||
|
|
||||||
type Element interface {
|
type Element interface {
|
||||||
Draw(*ebiten.Image)
|
Draw(*ebiten.Image)
|
||||||
SetAnchor(Point)
|
SetAnchor(geo.Point)
|
||||||
Anchor() Point
|
Anchor() geo.Point
|
||||||
Size() (w, h int)
|
Size() (w, h int)
|
||||||
Children() []Element
|
Children() []Element
|
||||||
}
|
}
|
||||||
|
|
||||||
type Point struct {
|
|
||||||
X, Y int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Point) Delta(p2 Point) Point {
|
|
||||||
return Point{
|
|
||||||
X: p2.X - p.X,
|
|
||||||
Y: p2.Y - p.Y,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Point) Add(p2 Point) Point {
|
|
||||||
return Point{
|
|
||||||
X: p.X + p2.X,
|
|
||||||
Y: p.Y + p2.Y,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Bounds struct {
|
|
||||||
Min Point
|
|
||||||
Width, Height int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bounds) Contains(p Point) bool {
|
|
||||||
return p.X >= b.Min.X && p.X <= b.Min.X+b.Width && p.Y >= b.Min.Y && p.Y <= b.Min.Y+b.Height
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package mouse
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
"git.vezzani.net/ben/games/common/elements/v1"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
)
|
)
|
||||||
@@ -24,8 +25,8 @@ type State struct {
|
|||||||
Clicked bool
|
Clicked bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *State) Point() elements.Point {
|
func (s *State) Point() geo.Point {
|
||||||
return elements.Point{
|
return geo.Point{
|
||||||
X: s.X,
|
X: s.X,
|
||||||
Y: s.Y,
|
Y: s.Y,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,16 @@
|
|||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OptFunc func(*Stack)
|
type OptFunc func(*Stack)
|
||||||
|
|
||||||
func Children(children ...elements.InitFunc) OptFunc {
|
func Core(fs ...core.OptFunc) OptFunc {
|
||||||
return func(s *Stack) {
|
return func(s *Stack) {
|
||||||
s.childrenInit = children
|
for _, f := range fs {
|
||||||
|
f(&s.Element)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,61 +1,43 @@
|
|||||||
package stacks
|
package stacks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New(ops ...OptFunc) elements.InitFunc {
|
func New(ops ...OptFunc) *Stack {
|
||||||
return func() elements.Element {
|
s := Stack{}
|
||||||
s := Stack{}
|
for op := range ops {
|
||||||
for op := range ops {
|
ops[op](&s)
|
||||||
ops[op](&s)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.children = make([]elements.Element, len(s.childrenInit))
|
|
||||||
var cw, ch int
|
|
||||||
anchor := s.anchor
|
|
||||||
for i := range s.childrenInit {
|
|
||||||
s.children[i] = s.childrenInit[i]()
|
|
||||||
s.children[i].SetAnchor(anchor)
|
|
||||||
cw, ch = s.children[i].Size()
|
|
||||||
if s.horizontal {
|
|
||||||
anchor.X += cw
|
|
||||||
} else {
|
|
||||||
anchor.Y += ch
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cw, ch int
|
||||||
|
anchor := s.Anchor()
|
||||||
|
for _, c := range s.Children() {
|
||||||
|
c.SetAnchor(anchor)
|
||||||
|
cw, ch = c.Size()
|
||||||
|
if s.horizontal {
|
||||||
|
anchor.X += cw
|
||||||
|
} else {
|
||||||
|
anchor.Y += ch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
type Stack struct {
|
type Stack struct {
|
||||||
|
core.Element
|
||||||
mouse.NopHandler
|
mouse.NopHandler
|
||||||
anchor elements.Point
|
horizontal bool
|
||||||
horizontal bool
|
|
||||||
childrenInit []elements.InitFunc
|
|
||||||
children []elements.Element
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stack) SetAnchor(a elements.Point) {
|
|
||||||
d := s.anchor.Delta(a)
|
|
||||||
for i := range s.Children() {
|
|
||||||
s.Children()[i].SetAnchor(s.Children()[i].Anchor().Add(d))
|
|
||||||
}
|
|
||||||
s.anchor = a
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Stack) Anchor() elements.Point {
|
|
||||||
return s.anchor
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Size() (w, h int) {
|
func (s *Stack) Size() (w, h int) {
|
||||||
var cw, ch int
|
var cw, ch int
|
||||||
for i := range s.children {
|
for _, c := range s.Children() {
|
||||||
cw, ch = s.children[i].Size()
|
cw, ch = c.Size()
|
||||||
if s.horizontal {
|
if s.horizontal {
|
||||||
w += cw
|
w += cw
|
||||||
if h < ch {
|
if h < ch {
|
||||||
@@ -73,11 +55,7 @@ func (s *Stack) Size() (w, h int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Draw(image *ebiten.Image) {
|
func (s *Stack) Draw(image *ebiten.Image) {
|
||||||
for i := range s.children {
|
for _, c := range s.Children() {
|
||||||
s.children[i].Draw(image)
|
c.Draw(image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stack) Children() []elements.Element {
|
|
||||||
return s.children
|
|
||||||
}
|
|
||||||
|
|||||||
10
common/geo/bounds.go
Normal file
10
common/geo/bounds.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
type Bounds struct {
|
||||||
|
Min Point
|
||||||
|
Width, Height int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bounds) Contains(p Point) bool {
|
||||||
|
return p.X >= b.Min.X && p.X <= b.Min.X+b.Width && p.Y >= b.Min.Y && p.Y <= b.Min.Y+b.Height
|
||||||
|
}
|
||||||
19
common/geo/point.go
Normal file
19
common/geo/point.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package geo
|
||||||
|
|
||||||
|
type Point struct {
|
||||||
|
X, Y int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Point) Delta(p2 Point) Point {
|
||||||
|
return Point{
|
||||||
|
X: p2.X - p.X,
|
||||||
|
Y: p2.Y - p.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Point) Add(p2 Point) Point {
|
||||||
|
return Point{
|
||||||
|
X: p.X + p2.X,
|
||||||
|
Y: p.Y + p2.Y,
|
||||||
|
}
|
||||||
|
}
|
||||||
229
games/minesweeper/v2/game/game.go
Normal file
229
games/minesweeper/v2/game/game.go
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
package game
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"git.vezzani.net/ben/games/common/colors"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/text"
|
||||||
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
||||||
|
"golang.org/x/image/font/basicfont"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mouseButton int
|
||||||
|
|
||||||
|
const (
|
||||||
|
cellSize = 32
|
||||||
|
borderThickness = 1
|
||||||
|
|
||||||
|
sideMargin = cellSize
|
||||||
|
topMargin = cellSize * 2
|
||||||
|
bottomMargin = cellSize
|
||||||
|
|
||||||
|
left mouseButton = iota
|
||||||
|
right
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
backgroundColor = colors.FromInt(0xffffff)
|
||||||
|
borderColor = colors.FromInt(0x999999)
|
||||||
|
revealedColor = colors.FromInt(0xdddddd)
|
||||||
|
downColor = colors.FromInt(0xaaaaaa)
|
||||||
|
|
||||||
|
dangerColors = [8]color.Color{
|
||||||
|
colors.FromInt(0x000000),
|
||||||
|
colors.FromInt(0x440000),
|
||||||
|
colors.FromInt(0x880000),
|
||||||
|
colors.FromInt(0xaa0000),
|
||||||
|
colors.FromInt(0xcc0000),
|
||||||
|
colors.FromInt(0xdd0000),
|
||||||
|
colors.FromInt(0xee0000),
|
||||||
|
colors.FromInt(0xff0000),
|
||||||
|
}
|
||||||
|
|
||||||
|
cols, rows int32
|
||||||
|
|
||||||
|
board []square
|
||||||
|
|
||||||
|
leftDown, rightDown bool
|
||||||
|
leftClicked, rightClicked bool
|
||||||
|
|
||||||
|
mouseX, mouseY int
|
||||||
|
|
||||||
|
over bool
|
||||||
|
)
|
||||||
|
|
||||||
|
type square struct {
|
||||||
|
mined bool
|
||||||
|
revealed bool
|
||||||
|
danger uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(colCount, rowCount, mineCount int32) *Core {
|
||||||
|
cols = colCount
|
||||||
|
rows = rowCount
|
||||||
|
board = make([]square, cols*rows)
|
||||||
|
for range mineCount {
|
||||||
|
addr := rand.Int31n(cols * rows)
|
||||||
|
for board[addr].mined {
|
||||||
|
addr = rand.Int31n(cols * rows)
|
||||||
|
}
|
||||||
|
board[addr].mined = true
|
||||||
|
}
|
||||||
|
|
||||||
|
initDanger()
|
||||||
|
|
||||||
|
return &Core{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Core struct{}
|
||||||
|
|
||||||
|
func (c Core) Update() error {
|
||||||
|
mouseX, mouseY = ebiten.CursorPosition()
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
|
||||||
|
leftDown = true
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonRight) {
|
||||||
|
rightDown = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonLeft) {
|
||||||
|
leftDown = false
|
||||||
|
if !rightDown {
|
||||||
|
handleClick(left)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if inpututil.IsMouseButtonJustReleased(ebiten.MouseButtonRight) {
|
||||||
|
rightDown = false
|
||||||
|
if !leftDown {
|
||||||
|
handleClick(right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Core) Draw(screen *ebiten.Image) {
|
||||||
|
maxX, maxY := c.Layout(0, 0)
|
||||||
|
vector.DrawFilledRect(screen, 0, 0, float32(maxX), float32(maxY), backgroundColor, false)
|
||||||
|
|
||||||
|
drawGrid(screen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Core) Layout(_, _ int) (screenWidth, screenHeight int) {
|
||||||
|
return int(cellSize*cols + sideMargin*2), int(cellSize*rows + topMargin + bottomMargin)
|
||||||
|
}
|
||||||
|
|
||||||
|
func drawGrid(screen *ebiten.Image) {
|
||||||
|
vector.StrokeRect(screen, sideMargin, topMargin, float32(cellSize*cols), float32(cellSize*rows), borderThickness, borderColor, false)
|
||||||
|
|
||||||
|
mouseSquare := int32(-1)
|
||||||
|
|
||||||
|
if leftDown || rightDown {
|
||||||
|
mouseSquare = mouseOverSquare()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range rows * cols {
|
||||||
|
clr := backgroundColor
|
||||||
|
if board[i].revealed {
|
||||||
|
clr = revealedColor
|
||||||
|
} else {
|
||||||
|
if mouseSquare == i {
|
||||||
|
clr = downColor
|
||||||
|
}
|
||||||
|
}
|
||||||
|
x, y, w, h := getCellSquare(i)
|
||||||
|
vector.DrawFilledRect(screen, x, y, w, h, clr, false)
|
||||||
|
vector.StrokeRect(screen, x, y, w, h, borderThickness, borderColor, false)
|
||||||
|
if board[i].revealed && board[i].danger > 0 {
|
||||||
|
txt := strconv.Itoa(int(board[i].danger))
|
||||||
|
|
||||||
|
text.Draw(screen, txt, basicfont.Face7x13, int(x)+12, int(y)+20, dangerColors[board[i].danger])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mouseOverSquare() int32 {
|
||||||
|
if int32(mouseX) < sideMargin ||
|
||||||
|
int32(mouseY) < topMargin ||
|
||||||
|
int32(mouseX) > sideMargin+cellSize*cols ||
|
||||||
|
int32(mouseX) > topMargin+cellSize*rows {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
baseMouseX := (int32(mouseX) - sideMargin) / cellSize
|
||||||
|
baseMouseY := (int32(mouseY) - topMargin) / cellSize
|
||||||
|
|
||||||
|
return baseMouseY*rows + baseMouseX
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCellSquare(addr int32) (float32, float32, float32, float32) {
|
||||||
|
c, r := float32(addr%cols), float32(addr/rows)
|
||||||
|
return sideMargin + c*cellSize, topMargin + r*cellSize, cellSize, cellSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleClick(side mouseButton) {
|
||||||
|
sq := mouseOverSquare()
|
||||||
|
if sq == -1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if side == left {
|
||||||
|
if board[sq].mined {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
reveal(sq)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func reveal(addr int32) {
|
||||||
|
if board[addr].revealed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
board[addr].revealed = true
|
||||||
|
for _, nAddr := range getNeighbors(addr) {
|
||||||
|
if board[nAddr].revealed || board[nAddr].mined {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
reveal(nAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNeighbors(addr int32) []int32 {
|
||||||
|
ns := make([]int32, 0)
|
||||||
|
sides := make([]int32, 0)
|
||||||
|
c, r := addr%cols, addr/rows
|
||||||
|
if c > 0 {
|
||||||
|
sides = append(sides, addr-1)
|
||||||
|
}
|
||||||
|
if c < cols-1 {
|
||||||
|
sides = append(sides, addr+1)
|
||||||
|
}
|
||||||
|
if r > 0 {
|
||||||
|
ns = append(ns, addr-cols)
|
||||||
|
for _, s := range sides {
|
||||||
|
ns = append(ns, s-cols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if r < rows-1 {
|
||||||
|
ns = append(ns, addr+cols)
|
||||||
|
for _, s := range sides {
|
||||||
|
ns = append(ns, s+cols)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(ns, sides...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func initDanger() {
|
||||||
|
for i := range board {
|
||||||
|
for _, n := range getNeighbors(int32(i)) {
|
||||||
|
if board[n].mined {
|
||||||
|
board[i].danger++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1
games/minesweeper/v2/main.go
Normal file
1
games/minesweeper/v2/main.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package v2
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.vezzani.net/ben/games/common/elements/v1"
|
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
"git.vezzani.net/ben/games/common/elements/v1/blocks"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/buttons"
|
"git.vezzani.net/ben/games/common/elements/v1/buttons"
|
||||||
|
"git.vezzani.net/ben/games/common/elements/v1/core"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
"git.vezzani.net/ben/games/common/elements/v1/mouse"
|
||||||
"git.vezzani.net/ben/games/common/elements/v1/stacks"
|
"git.vezzani.net/ben/games/common/elements/v1/stacks"
|
||||||
|
"git.vezzani.net/ben/games/common/geo"
|
||||||
"git.vezzani.net/ben/games/common/sprites/v1"
|
"git.vezzani.net/ben/games/common/sprites/v1"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2"
|
"github.com/hajimehoshi/ebiten/v2"
|
||||||
@@ -14,29 +15,30 @@ import (
|
|||||||
|
|
||||||
var root = stacks.New(
|
var root = stacks.New(
|
||||||
stacks.Horizontal(),
|
stacks.Horizontal(),
|
||||||
//stacks.BlockOpt(blocks.BackgroundColor(color.White)),
|
stacks.Core(core.Name("parent")),
|
||||||
//stacks.BlockOpt(blocks.Name("parent")),
|
stacks.Core(core.Children(
|
||||||
stacks.Children(
|
|
||||||
stacks.New(
|
stacks.New(
|
||||||
//stacks.BlockOpt(blocks.Name("left")),
|
stacks.Core(
|
||||||
stacks.Children(
|
core.Name("left"),
|
||||||
buttons.New(
|
core.Children(
|
||||||
buttons.BlockOpt(blocks.Size(100, 100)),
|
buttons.New(
|
||||||
buttons.Label("hello"),
|
buttons.BlockOpt(blocks.Size(100, 100)),
|
||||||
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
buttons.Label("hello"),
|
||||||
buttons.OnClick(func(ms mouse.State) {
|
buttons.BlockOpt(blocks.BackgroundColor(colornames.Green)),
|
||||||
println("green")
|
buttons.OnClick(func(ms mouse.State) {
|
||||||
}),
|
println("green")
|
||||||
),
|
}),
|
||||||
blocks.New(
|
),
|
||||||
blocks.Size(100, 100),
|
blocks.New(
|
||||||
blocks.BackgroundColor(colornames.Yellow),
|
blocks.Size(100, 100),
|
||||||
|
blocks.BackgroundColor(colornames.Yellow),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
stacks.New(
|
stacks.New(
|
||||||
//stacks.BlockOpt(blocks.Name("right")),
|
stacks.Core(core.Name("right")),
|
||||||
stacks.Children(
|
stacks.Core(core.Children(
|
||||||
blocks.New(
|
blocks.New(
|
||||||
blocks.Size(100, 100),
|
blocks.Size(100, 100),
|
||||||
blocks.BackgroundColor(colornames.Blue),
|
blocks.BackgroundColor(colornames.Blue),
|
||||||
@@ -45,9 +47,9 @@ var root = stacks.New(
|
|||||||
blocks.Size(100, 100),
|
blocks.Size(100, 100),
|
||||||
blocks.BackgroundColor(colornames.Red),
|
blocks.BackgroundColor(colornames.Red),
|
||||||
),
|
),
|
||||||
),
|
)),
|
||||||
),
|
),
|
||||||
),
|
)),
|
||||||
)
|
)
|
||||||
|
|
||||||
func newEditor() *editor {
|
func newEditor() *editor {
|
||||||
@@ -55,8 +57,7 @@ func newEditor() *editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type editor struct {
|
type editor struct {
|
||||||
root elements.Element
|
bounds geo.Bounds
|
||||||
bounds elements.Bounds
|
|
||||||
handleMouse func() bool
|
handleMouse func() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,18 +68,17 @@ func (e *editor) Update() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Draw(screen *ebiten.Image) {
|
func (e *editor) Draw(screen *ebiten.Image) {
|
||||||
e.root.Draw(screen)
|
root.Draw(screen)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
func (e *editor) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
||||||
if e.bounds.Width != outsideWidth || e.bounds.Height != outsideHeight {
|
if e.bounds.Width != outsideWidth || e.bounds.Height != outsideHeight {
|
||||||
e.bounds = elements.Bounds{
|
e.bounds = geo.Bounds{
|
||||||
Min: elements.Point{},
|
Min: geo.Point{},
|
||||||
Width: outsideWidth,
|
Width: outsideWidth,
|
||||||
Height: outsideHeight,
|
Height: outsideHeight,
|
||||||
}
|
}
|
||||||
e.root = root()
|
e.handleMouse = mouse.Handler(root)
|
||||||
e.handleMouse = mouse.Handler(e.root)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return outsideWidth, outsideHeight
|
return outsideWidth, outsideHeight
|
||||||
|
|||||||
Reference in New Issue
Block a user