Commit 7c0e5ecf authored by ceddral's avatar ceddral
Browse files

board: pass place to render calls

coordinates rotations and pieces are now taken from
the place or state parameters. this fixes an issue of
tokens on a board place being shown on the currently
to-be-placed piece.
parent 4f8f178e
......@@ -76,6 +76,8 @@ type Place struct {
rot Rotation
fieldIdToPlayer map[byte]*GamePlayer
fieldIdToToken map[byte]Token
x int
y int
}
type Board struct {
......@@ -89,7 +91,7 @@ type Board struct {
}
func NewPlace(p *Piece, rot Rotation) *Place {
return &Place{p, rot, make(map[byte]*GamePlayer), make(map[byte]Token)}
return &Place{p, rot, make(map[byte]*GamePlayer), make(map[byte]Token), 0, 0}
}
func NewBoard(g *Game) (board *Board) {
......@@ -108,6 +110,8 @@ func (b *Board) Set(x, y int, p *Place) {
if y <= b.miny { b.miny = y - 1}
if b.maxx <= x + 1 { b.maxx = x + 2 }
if b.maxy <= y + 1 { b.maxy = y + 2 }
p.x = x
p.y = y
b.m.Store(Coord{x, y}, p)
b.free.Delete(Coord{x, y})
cs := getAdjacentCoords(x, y)
......@@ -121,7 +125,7 @@ func (b *Board) Set(x, y int, p *Place) {
func (b *Board) Get(x, y int) (*Place, bool) {
ip, ok := b.m.Load(Coord{x, y})
p := &Place{}
p := &Place{nil, 0, nil, nil, x, y}
if ok {
p = ip.(*Place)
}
......@@ -138,6 +142,8 @@ func (b *Board) GetWritable(x, y int) (*Place, bool) {
rot: pro.rot,
fieldIdToPlayer: make(map[byte]*GamePlayer),
fieldIdToToken: make(map[byte]Token),
x: pro.x,
y: pro.y,
}
copyMapByteGamePlayer(p.fieldIdToPlayer, pro.fieldIdToPlayer)
copyMapByteToken(p.fieldIdToToken, pro.fieldIdToToken)
......@@ -442,7 +448,33 @@ const (
PMMax
)
func (p *Piece) render(v *tg.View, viewx, viewy int, board *Board, boardx, boardy int, rot Rotation, mode PieceMode, s GameState) {
func render(v *tg.View, viewx, viewy int, board *Board, mode PieceMode, place *Place, s GameState) {
var p *Piece
var rot Rotation
if place != nil {
p = place.piece
rot = place.rot
} else if s != nil {
if swp, ok := s.(GameStateWithPiece); ok {
p = swp.GetPiece()
rot = swp.GetRotation()
} else {
panic ("render: invalid parameters. either place or state must contain the piece to render")
}
} else {
panic("render: invalid parameters. either place or state must be given")
}
var boardx int
var boardy int
if place != nil {
boardx = place.x
boardy = place.y
} else if spp, ok := s.(*StatePlacePiece); s != nil && ok {
boardx = spp.boardx
boardy = spp.boardy
} else if board != nil {
panic("render: invalid parameters. if board is given, either place or state must provide coordinates")
}
for y := 0; y < pieceHeight; y++ {
for x := 0; x < pieceWidth; x++ {
if p == nil {
......@@ -463,10 +495,10 @@ func (p *Piece) render(v *tg.View, viewx, viewy int, board *Board, boardx, board
downp = spp.p
downpRot = spp.rot
} else {
place, ok := board.Get(boardx, boardy+1)
downPlace, ok := board.Get(boardx, boardy+1)
if ok {
downp = place.piece
downpRot = place.rot
downp = downPlace.piece
downpRot = downPlace.rot
}
}
// Compare the fields ABCD in this and the downward neighbor piece
......@@ -508,7 +540,7 @@ func (p *Piece) render(v *tg.View, viewx, viewy int, board *Board, boardx, board
switch mode {
case PMPlayers:
if f.placeable && board != nil {
if place, ok := board.Get(boardx, boardy); ok {
if place != nil {
if p, ok := place.fieldIdToPlayer[f.id]; ok {
c = byte(p.id) + '0'
}
......@@ -528,7 +560,7 @@ func (p *Piece) render(v *tg.View, viewx, viewy int, board *Board, boardx, board
}
case PMTokens:
if f.placeable && board != nil {
if place, ok := board.Get(boardx, boardy); ok {
if place != nil {
if t, ok := place.fieldIdToToken[f.id]; ok {
c = t.toByte()
}
......@@ -583,7 +615,7 @@ func boardToView(x, y int, p *GamePlayer) (int, int) {
func (b *Board) renderCurrentPiece(p *GamePlayer, s *StatePlacePiece) {
viewx, viewy := boardToView(s.boardx, s.boardy, p)
s.p.render(p.boardv, viewx, viewy, b, s.boardx, s.boardy, s.rot, p.boardMode, nil)
render(p.boardv, viewx, viewy, b, p.boardMode, nil, s)
// draw markers around piece
viewx += pieceWidth
viewy += pieceHeight
......@@ -635,7 +667,7 @@ func (b *Board) Redraw(p *GamePlayer, s GameState) {
for y := miny; y < maxy + 1; y++ {
place, _ := b.Get(x, y)
viewx, viewy := boardToView(x, y, p)
place.piece.render(p.boardv, viewx, viewy, b, x, y, place.rot, p.boardMode, s)
render(p.boardv, viewx, viewy, b, p.boardMode, place, s)
}
}
if spp, ok := s.(*StatePlacePiece); ok {
......
......@@ -18,6 +18,11 @@ type GameState interface {
patientAction(*GamePlayer, Key) bool
}
type GameStateWithPiece interface {
GetPiece() *Piece
GetRotation() Rotation
}
func (g *Game) broadcastState(state GameState) {
for _, p := range g.players {
select {
......@@ -39,11 +44,11 @@ func (m *GameMenu) Redraw(s GameState) {
line := 0
v.DrawTextClearLine(fg, bg, []byte(fmt.Sprintf("Turn: %s", s.getAgent().name)), 0, line)
line++
if spp, ok := s.(*StatePlacePiece); ok {
spp.p.render(v, 0, line, nil, 0, 0, spp.rot, PMPlaces, nil)
if _, ok := s.(*StatePlacePiece); ok {
render(v, 0, line, nil, PMPlaces, nil, s)
}
if sip, ok := s.(*StateImpossiblePiece); ok {
sip.p.render(v, 0, line, nil, 0, 0, 0, PMPlayers, nil)
if _, ok := s.(*StateImpossiblePiece); ok {
render(v, 0, line, nil, PMPlayers, nil, s)
}
g := s.getGame()
g.Lock()
......@@ -571,12 +576,15 @@ func (s *StatePlacePiece) patientAction(p *GamePlayer, key Key) bool {
p.boardv.DrawTextClearLine(tg.ColorDefault, tg.ColorDefault, []byte(fmt.Sprintf("It is %s turn. PLEASE WAIT!!!!!", s.agent.name)), 0, 1)
return false
}
func (s *StatePlacePiece) GetPiece() *Piece { return s.p }
func (s *StatePlacePiece) GetRotation() Rotation { return s.rot }
type StateImpossiblePiece struct {
agent *GamePlayer
g *Game
p *Piece
rot Rotation
}
func NewStateImpossiblePiece(g *Game, a *GamePlayer, p *Piece) *StateImpossiblePiece {
......@@ -584,6 +592,7 @@ func NewStateImpossiblePiece(g *Game, a *GamePlayer, p *Piece) *StateImpossibleP
agent: a,
g: g,
p: p,
rot: 0,
}
}
......@@ -600,6 +609,8 @@ func (s *StateImpossiblePiece) agentAction(p *GamePlayer, key Key) bool {
return false
}
func (s *StateImpossiblePiece) patientAction(p *GamePlayer, key Key) bool { return false }
func (s *StateImpossiblePiece) GetPiece() *Piece { return s.p }
func (s *StateImpossiblePiece) GetRotation() Rotation { return s.rot }
type StatePlaceFollower struct {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment