mirror of
https://github.com/maaslalani/gambit.git
synced 2025-01-16 02:33:26 +02:00
mouse
This commit is contained in:
parent
55caa3afbc
commit
35192ca66a
31
game/game.go
31
game/game.go
@ -9,25 +9,26 @@ import (
|
||||
|
||||
type model struct {
|
||||
board board.Board
|
||||
selected *position.Position
|
||||
selected position.Position
|
||||
moves []board.Move
|
||||
}
|
||||
|
||||
func Model() tea.Model {
|
||||
b, _ := board.FromFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||
return model{
|
||||
board: b,
|
||||
board: b,
|
||||
selected: position.NoPosition,
|
||||
}
|
||||
}
|
||||
|
||||
func (m model) Init() tea.Cmd { return nil }
|
||||
func (m model) View() string { return m.board.String() + m.selected.String() }
|
||||
func (m model) View() string { return m.board.String() }
|
||||
|
||||
const (
|
||||
cellHeight = 2
|
||||
cellWidth = 4
|
||||
marginLeft = 4
|
||||
marginTop = 2
|
||||
cellWidth = 4
|
||||
cellHeight = 2
|
||||
)
|
||||
|
||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
@ -40,15 +41,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
row = 7 - row
|
||||
}
|
||||
|
||||
if m.selected != nil {
|
||||
m.selected = &position.Position{Row: row, Col: col}
|
||||
if msg.Type == tea.MouseRelease {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
if m.selected == position.NoPosition {
|
||||
m.selected = position.Position{Row: row, Col: col}
|
||||
} else {
|
||||
from := m.selected
|
||||
to := &position.Position{Row: row, Col: col}
|
||||
m.moves = append(m.moves, board.Move{
|
||||
From: Square((*from).String()),
|
||||
To: Square((*to).String()),
|
||||
})
|
||||
from := Square(m.selected.String())
|
||||
to := Square(position.Position{Row: row, Col: col}.String())
|
||||
move := board.Move{From: from, To: to}
|
||||
m.moves = append(m.moves, move)
|
||||
m.board.Move(move)
|
||||
m.selected = position.NoPosition
|
||||
}
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
|
@ -49,3 +49,5 @@ func ColumnToFile(column int) string {
|
||||
func FileToColumn(file byte) int {
|
||||
return int(file - 'A')
|
||||
}
|
||||
|
||||
var NoPosition = Position{-1, -1}
|
||||
|
Loading…
Reference in New Issue
Block a user