~kota/sl8

791f9db3a4d0d7b58d616140f1be818d3391aafa — Dakota Walsh 3 months ago 51c773a
Add areas
2 files changed, 41 insertions(+), 19 deletions(-)

A area/area.go
M game.go
A area/area.go => area/area.go +21 -0
@@ 0,0 1,21 @@
package area

import "git.sr.ht/~kota/sl8/stack"

// An Area holds a list of Stacks and or a Child Area. This encapsulation allows
// "opening" a collection of playing cards.
type Area struct {
	Child  *Area
	Stacks []*stack.Stack
}

// Draw the Area's Stacks and Child.
func (a *Area) Draw(grabbed *stack.Stack) {
	for _, s := range a.Stacks {
		if grabbed == s {
			s.Draw(true)
			continue
		}
		s.Draw(false)
	}
}

M game.go => game.go +20 -19
@@ 3,30 3,35 @@ package main
import (
	"image/color"

	"git.sr.ht/~kota/sl8/area"
	"git.sr.ht/~kota/sl8/stack"
	rl "github.com/gen2brain/raylib-go/raylib"
)

type game struct {
	board []*stack.Stack
	root *area.Area

	// grabbed points to a specific stack. This stack could be part of the root
	// [Area] or a child Area.
	grabbed *stack.Stack
	offset  rl.Vector2

	// offset of the cursor from the grabbed Stack's position.
	offset rl.Vector2
}

func newGame() *game {
	pieces := make([]*stack.Stack, 0)
	pieces = append(pieces, stack.NewStack([]string{"img/chess_knight.png"},
	stacks := make([]*stack.Stack, 0)
	stacks = append(stacks, stack.NewStack([]string{"img/chess_knight.png"},
		rl.Vector2{
			X: 20,
			Y: 20,
		}))
	pieces = append(pieces, stack.NewStack([]string{"img/chess_rook.png"},
	stacks = append(stacks, stack.NewStack([]string{"img/chess_rook.png"},
		rl.Vector2{
			X: 140,
			Y: 20,
		}))
	pieces = append(pieces, stack.NewStack(
	stacks = append(stacks, stack.NewStack(
		[]string{
			"img/d6_1.png",
			"img/d6_2.png",


@@ 40,7 45,9 @@ func newGame() *game {
		}))

	return &game{
		board: pieces,
		root: &area.Area{
			Stacks: stacks,
		},
	}
}



@@ 64,14 71,14 @@ func (g *game) update() {
}

func (g *game) click(mouse rl.Vector2) {
	for _, p := range g.board {
		if g.grabbed == p {
	for _, s := range g.root.Stacks {
		if g.grabbed == s {
			continue
		}

		if rl.CheckCollisionPointRec(mouse, p.Boundary()) {
			g.offset = rl.Vector2Subtract(mouse, p.Position())
			g.grabbed = p
		if rl.CheckCollisionPointRec(mouse, s.Boundary()) {
			g.offset = rl.Vector2Subtract(mouse, s.Position())
			g.grabbed = s
			return
		}
	}


@@ 82,13 89,7 @@ func (g *game) draw() {
	rl.BeginDrawing()
	rl.ClearBackground(color.RGBA{68, 136, 102, 255})

	for _, p := range g.board {
		if g.grabbed == p {
			p.Draw(true)
			continue
		}
		p.Draw(false)
	}
	g.root.Draw(g.grabbed)

	rl.EndDrawing()
}