~kota/sl8

99bcfd22c0ebeae00d96c3dd7c452c5832ae4e03 — Dakota Walsh 3 months ago 791f9db main
Store grabbed state in Stack

I think this is a bit cleaner than passing the grabbed value through
to the stack especially with wanting to segment the stacks into areas.
3 files changed, 12 insertions(+), 12 deletions(-)

M area/area.go
M game.go
M stack/stack.go
M area/area.go => area/area.go +2 -6
@@ 10,12 10,8 @@ type Area struct {
}

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

M game.go => game.go +6 -3
@@ 61,7 61,10 @@ func (g *game) update() {
		g.click(mouse)
	}
	if rl.IsMouseButtonReleased(rl.MouseLeftButton) {
		g.grabbed = nil
		if g.grabbed != nil {
			g.grabbed.Grabbed = false
			g.grabbed = nil
		}
	}

	if g.grabbed != nil {


@@ 79,17 82,17 @@ func (g *game) click(mouse rl.Vector2) {
		if rl.CheckCollisionPointRec(mouse, s.Boundary()) {
			g.offset = rl.Vector2Subtract(mouse, s.Position())
			g.grabbed = s
			g.grabbed.Grabbed = true
			return
		}
	}
	g.grabbed = nil
}

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

	g.root.Draw(g.grabbed)
	g.root.Draw()

	rl.EndDrawing()
}

M stack/stack.go => stack/stack.go +4 -3
@@ 9,7 9,8 @@ import rl "github.com/gen2brain/raylib-go/raylib"
// used for selections. A Stack is also used to encapsulate a single piece, like
// a chess piece.
type Stack struct {
	pieces []*piece
	Grabbed bool
	pieces  []*piece
}

// A piece represents a single item on the table such as a playing card in a


@@ 39,13 40,13 @@ func NewStack(paths []string, position rl.Vector2) *Stack {
}

// Draw a [Stack] using the image for its first piece at it's current position.
func (s *Stack) Draw(grabbed bool) {
func (s *Stack) Draw() {
	if len(s.pieces) == 0 {
		return
	}
	top := s.pieces[0]

	if grabbed {
	if s.Grabbed {
		rl.DrawTextureV(
			*top.texture,
			top.position,