diff --git a/view_board.go b/view_board.go deleted file mode 100644 index 9798c95..0000000 --- a/view_board.go +++ /dev/null @@ -1,91 +0,0 @@ -package main - -import ( - "errors" - "image/color" - "log" - - "github.com/hajimehoshi/ebiten/v2" -) - -var errorTaskTerminated = errors.New("freego: task terminated") - -type task func() error - -// ViewBoard represents the game board. -type ViewBoard struct { - size int - tiles map[*ViewTile]struct{} - tasks []task -} - -// NewViewBoard generates a new ViewBoard with giving a size. -func NewViewBoard(g *Game) (*ViewBoard, error) { - size := g.board.size - b := &ViewBoard{ - size: size, - tiles: map[*ViewTile]struct{}{}, - } - for i := 0; i < size; i++ { - for j := 0; j < size; j++ { - b.tiles[NewViewTile(i, j, g.board.board[i][j])] = struct{}{} - } - } - - return b, nil -} - -//func (b *ViewBoard) tileAt(x, y int) *Tile { -// return tileAt(b.tiles, x, y) -//} - -// Update updates the board state. -func (b *ViewBoard) Update(input *Input) error { - for t := range b.tiles { - if err := t.Update(); err != nil { - return err - } - } - if 0 < len(b.tasks) { - t := b.tasks[0] - if err := t(); err == errorTaskTerminated { - b.tasks = b.tasks[1:] - } else if err != nil { - return err - } - return nil - } - if dir, ok := input.Dir(); ok { - //if err := b.Move(dir); err != nil { - // return err - //} - log.Println(dir) - } - return nil -} - -// Draw draws the board to the given boardImage. -func (b *ViewBoard) Draw(boardImage *ebiten.Image) { - boardImage.Fill(color.RGBA{0xbb, 0xad, 0xa0, 0xff}) - for j := 0; j < b.size; j++ { - for i := 0; i < b.size; i++ { - //v := 0 - op := &ebiten.DrawImageOptions{} - x := i*tileSize + (i+1)*tileMargin - y := j*tileSize + (j+1)*tileMargin - op.GeoM.Translate(float64(x), float64(y)) - //op.ColorM.ScaleWithColor(tileBackgroundColor(v)) - boardImage.DrawImage(tileImage, op) - } - } - for t := range b.tiles { - t.Draw(boardImage) - } -} - -// Size returns the board size. -func (b *ViewBoard) Size() (int, int) { - x := b.size*tileSize + (b.size+1)*tileMargin - y := x - return x, y -} diff --git a/view_tile.go b/view_tile.go deleted file mode 100644 index aed8fb9..0000000 --- a/view_tile.go +++ /dev/null @@ -1,129 +0,0 @@ -package main - -import ( - "image/color" - "log" - - "github.com/hajimehoshi/ebiten/examples/resources/fonts" - "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/text" - "golang.org/x/image/font" - "golang.org/x/image/font/opentype" -) - -//ViewTile is a tile in the viewer -type ViewTile struct { - gameTile *Tile - - // next represents a next tile information after moving. - // next is empty when the tile is not about to move. - //next TileData - - //movingCount int - //startPoppingCount int - //poppingCount int -} - -var ( - tileImage = ebiten.NewImage(tileSize, tileSize) -) - -var ( - mplusSmallFont font.Face - mplusNormalFont font.Face - mplusBigFont font.Face -) - -const ( - tileSize = 80 - tileMargin = 4 -) - -func init() { - tt, err := opentype.Parse(fonts.MPlus1pRegular_ttf) - if err != nil { - log.Fatal(err) - } - - const dpi = 72 - mplusSmallFont, err = opentype.NewFace(tt, &opentype.FaceOptions{ - Size: 24, - DPI: dpi, - Hinting: font.HintingFull, - }) - if err != nil { - log.Fatal(err) - } - mplusNormalFont, err = opentype.NewFace(tt, &opentype.FaceOptions{ - Size: 32, - DPI: dpi, - Hinting: font.HintingFull, - }) - if err != nil { - log.Fatal(err) - } - mplusBigFont, err = opentype.NewFace(tt, &opentype.FaceOptions{ - Size: 48, - DPI: dpi, - Hinting: font.HintingFull, - }) - if err != nil { - log.Fatal(err) - } -} - -//NewViewTile creates a new view tile -func NewViewTile(x, y int, t *Tile) *ViewTile { - return &ViewTile{ - gameTile: t, - } -} - -// Update updates the tile's animation states. -func (t *ViewTile) Update() error { - return nil -} - -// Draw draws the current tile to the given boardImage. -func (t *ViewTile) Draw(boardImage *ebiten.Image) { - j, i := t.gameTile.x, t.gameTile.y - v := t.gameTile.entity - if v == nil && t.gameTile.Passable() { - return - } - op := &ebiten.DrawImageOptions{} - x := i*tileSize + (i+1)*tileMargin - y := j*tileSize + (j+1)*tileMargin - op.GeoM.Translate(float64(x), float64(y)) - //op.ColorM.ScaleWithColor(tileBackgroundColor(v)) - boardImage.DrawImage(tileImage, op) - str := "NA" - if v == nil { - str = "river" - } else { - str = v.Rank.String() - } - - f := mplusBigFont - switch { - case 3 < len(str): - f = mplusSmallFont - case 2 < len(str): - f = mplusNormalFont - } - - bound, _ := font.BoundString(f, str) - w := (bound.Max.X - bound.Min.X).Ceil() - h := (bound.Max.Y - bound.Min.Y).Ceil() - x = x + (tileSize-w)/2 - y = y + (tileSize-h)/2 + h - pieceColor := color.RGBA{0xf9, 0xf6, 0xf2, 0xff} - if v != nil { - if t.gameTile.entity.Owner == Red { - pieceColor = color.RGBA{0xff, 0x0, 0x0, 0xff} - } else if t.gameTile.entity.Owner == Blue { - pieceColor = color.RGBA{0x0, 0x0, 0xff, 0xff} - } - } - text.Draw(boardImage, str, f, x, y, pieceColor) -} diff --git a/viewer.go b/viewer.go deleted file mode 100644 index df6c3ee..0000000 --- a/viewer.go +++ /dev/null @@ -1,60 +0,0 @@ -package main - -import ( - "image/color" - - "github.com/hajimehoshi/ebiten/v2" -) - -//Viewer is a graphical representation of a freego game -type Viewer struct { - input *Input - board *ViewBoard - boardImage *ebiten.Image - gameState *Game -} - -//NewViewer creates a new viewer -func NewViewer(g *Game) (*Viewer, error) { - v := &Viewer{ - input: NewInput(), - gameState: g, - } - var err error - v.board, err = NewViewBoard(g) - if err != nil { - panic(err) - } - return v, nil -} - -// Layout implements ebiten.Game's Layout. -func (v *Viewer) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { - return 800, 640 -} - -// Update updates the current game state. -func (v *Viewer) Update() error { - v.input.Update() - if err := v.board.Update(v.input); err != nil { - return err - } - return nil -} - -// Draw draws the current game to the given screen. -func (v *Viewer) Draw(screen *ebiten.Image) { - if v.boardImage == nil { - w, h := v.board.Size() - v.boardImage = ebiten.NewImage(w, h) - } - screen.Fill(color.RGBA{0xfa, 0xf8, 0xef, 0xff}) - v.board.Draw(v.boardImage) - op := &ebiten.DrawImageOptions{} - sw, sh := screen.Size() - bw, bh := v.boardImage.Size() - x := (sw - bw) / 2 - y := (sh - bh) / 2 - op.GeoM.Translate(float64(x), float64(y)) - screen.DrawImage(v.boardImage, op) -}