package terrain import ( "bytes" "github.com/hajimehoshi/ebiten/v2" "gitlab.com/kbr4/9heroja/configuration" "gitlab.com/kbr4/9heroja/resources" "image" "log" ) var ( grassImage *ebiten.Image subimage *ebiten.Image ) type Terrain struct { PositionX float64 PositionY float64 direction configuration.Direction } func (t *Terrain) DrawTerrain(screen *ebiten.Image) { screenWidth := screen.Bounds().Max.X screenHeight := screen.Bounds().Max.Y offsetX := int(t.PositionX) % 64 offsetY := int(t.PositionY) % 64 op := &ebiten.DrawImageOptions{} for i := -1; i <= screenWidth/64+1; i++ { for j := -1; j <= screenHeight/64+1; j++ { // ground op.GeoM.Reset() op.GeoM.Translate(float64(i*64+offsetX), float64(j*64+offsetY)) screen.DrawImage(subimage, op) } } } func init() { img, _, err := image.Decode(bytes.NewReader(resources.Grass_png)) if err != nil { log.Fatal(err) } grassImage = ebiten.NewImageFromImage(img) subimage = grassImage.SubImage(image.Rect(32, 160, 32+64, 160+64)).(*ebiten.Image) } func NewTerrain() *Terrain { return &Terrain{ PositionX: 0, PositionY: 0, } } func (t *Terrain) ChangeDirection(direction configuration.Direction) { if direction != configuration.PreviouslyHeld { t.direction = direction } }