examples/2048/2048/tile.go
func (t *Tile) Draw(boardImage *ebiten.Image) {
	i, j := t.current.x, t.current.y
	ni, nj := t.next.x, t.next.y
	v := t.current.value
	if v == 0 {
		return
	}
	op := &ebiten.DrawImageOptions{}
	x := i*tileSize + (i+1)*tileMargin
	y := j*tileSize + (j+1)*tileMargin
	nx := ni*tileSize + (ni+1)*tileMargin
	ny := nj*tileSize + (nj+1)*tileMargin
	switch {
	case 0 < t.movingCount:
		rate := 1 - float64(t.movingCount)/maxMovingCount
		x = mean(x, nx, rate)
		y = mean(y, ny, rate)
	case 0 < t.startPoppingCount:
		rate := 1 - float64(t.startPoppingCount)/float64(maxPoppingCount)
		scale := meanF(0.0, 1.0, rate)
		op.GeoM.Translate(float64(-tileSize/2), float64(-tileSize/2))
		op.GeoM.Scale(scale, scale)
		op.GeoM.Translate(float64(tileSize/2), float64(tileSize/2))
	case 0 < t.poppingCount:
		const maxScale = 1.2
		rate := 0.0
		if maxPoppingCount*2/3 <= t.poppingCount {
			// 0 to 1
			rate = 1 - float64(t.poppingCount-2*maxPoppingCount/3)/float64(maxPoppingCount/3)
		} else {
			// 1 to 0
			rate = float64(t.poppingCount) / float64(maxPoppingCount*2/3)
		}
		scale := meanF(1.0, maxScale, rate)
		op.GeoM.Translate(float64(-tileSize/2), float64(-tileSize/2))
		op.GeoM.Scale(scale, scale)
		op.GeoM.Translate(float64(tileSize/2), float64(tileSize/2))
	}
	op.GeoM.Translate(float64(x), float64(y))
	r, g, b, a := colorToScale(tileBackgroundColor(v))
	op.ColorM.Scale(r, g, b, a)
	boardImage.DrawImage(tileImage, op)
	str := strconv.Itoa(v)

	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
	text.Draw(boardImage, str, f, x, y, tileColor(v))
}
func (t *Tile) Draw(boardImage *ebiten.Image) {
	i, j := t.current.x, t.current.y
	ni, nj := t.next.x, t.next.y
	v := t.current.value
	if v == 0 {
		return
	}
	op := &ebiten.DrawImageOptions{}
	x := i*tileSize + (i+1)*tileMargin
	y := j*tileSize + (j+1)*tileMargin
	nx := ni*tileSize + (ni+1)*tileMargin
	ny := nj*tileSize + (nj+1)*tileMargin
	switch {
	case 0 < t.movingCount:
		rate := 1 - float64(t.movingCount)/maxMovingCount
		x = mean(x, nx, rate)
		y = mean(y, ny, rate)
	case 0 < t.startPoppingCount:
		rate := 1 - float64(t.startPoppingCount)/float64(maxPoppingCount)
		scale := meanF(0.0, 1.0, rate)
		op.GeoM.Translate(float64(-tileSize/2), float64(-tileSize/2))
		op.GeoM.Scale(scale, scale)
		op.GeoM.Translate(float64(tileSize/2), float64(tileSize/2))
	case 0 < t.poppingCount:
		const maxScale = 1.2
		rate := 0.0
		if maxPoppingCount*2/3 <= t.poppingCount {
			// 0 to 1
			rate = 1 - float64(t.poppingCount-2*maxPoppingCount/3)/float64(maxPoppingCount/3)
		} else {
			// 1 to 0
			rate = float64(t.poppingCount) / float64(maxPoppingCount*2/3)
		}
		scale := meanF(1.0, maxScale, rate)
		op.GeoM.Translate(float64(-tileSize/2), float64(-tileSize/2))
		op.GeoM.Scale(scale, scale)
		op.GeoM.Translate(float64(tileSize/2), float64(tileSize/2))
	}
	op.GeoM.Translate(float64(x), float64(y))
	r, g, b, a := colorToScale(tileBackgroundColor(v))
	op.ColorM.Scale(r, g, b, a)
	boardImage.DrawImage(tileImage, op)
	str := strconv.Itoa(v)

	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
	text.Draw(boardImage, str, f, x, y, tileColor(v))
}
func ( t * Tile ) Draw ( boardImage * ebiten . Image ) { i , j := t . current . x , t . current . y ni , nj := t . next . x , t . next . y v := t . current . value if v == 0 { return } op := & ebiten . DrawImageOptions { } x := i * tileSize + ( i + 1 ) * tileMargin y := j * tileSize + ( j + 1 ) * tileMargin nx := ni * tileSize + ( ni + 1 ) * tileMargin ny := nj * tileSize + ( nj + 1 ) * tileMargin switch { case 0 < t . movingCount : rate := 1 - float64 ( t . movingCount ) / maxMovingCount x = mean ( x , nx , rate ) y = mean ( y , ny , rate ) case 0 < t . startPoppingCount : rate := 1 - float64 ( t . startPoppingCount ) / float64 ( maxPoppingCount ) scale := meanF ( 0.0 , 1.0 , rate ) op . GeoM . Translate ( float64 ( - tileSize / 2 ) , float64 ( - tileSize / 2 ) ) op . GeoM . Scale ( scale , scale ) op . GeoM . Translate ( float64 ( tileSize / 2 ) , float64 ( tileSize / 2 ) ) case 0 < t . poppingCount : const maxScale = 1.2 rate := 0.0 if maxPoppingCount * 2 / 3 <= t . poppingCount { // 0 to 1 rate = 1 - float64 ( t . poppingCount - 2 * maxPoppingCount / 3 ) / float64 ( maxPoppingCount / 3 ) } else { // 1 to 0 rate = float64 ( t . poppingCount ) / float64 ( maxPoppingCount * 2 / 3 ) } scale := meanF ( 1.0 , maxScale , rate ) op . GeoM . Translate ( float64 ( - tileSize / 2 ) , float64 ( - tileSize / 2 ) ) op . GeoM . Scale ( scale , scale ) op . GeoM . Translate ( float64 ( tileSize / 2 ) , float64 ( tileSize / 2 ) ) } op . GeoM . Translate ( float64 ( x ) , float64 ( y ) ) r , g , b , a := colorToScale ( tileBackgroundColor ( v ) ) op . ColorM . Scale ( r , g , b , a ) boardImage . DrawImage ( tileImage , op ) str := strconv . Itoa ( v ) 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 text . Draw ( boardImage , str , f , x , y , tileColor ( v ) ) }
// Draw draws the current tile to the given boardImage.
Draw draws the current tile to the given boardImage .