Files
old-9heroja/terrain/terrain.go

78 lines
1.9 KiB
Go
Raw Normal View History

2023-11-19 08:06:40 +01:00
package terrain
import (
"bytes"
"github.com/hajimehoshi/ebiten/v2"
2023-11-21 20:12:56 +01:00
"gitlab.com/kbr4/9heroja/configuration"
2023-11-19 08:06:40 +01:00
"gitlab.com/kbr4/9heroja/resources"
"image"
"log"
2023-11-21 20:12:56 +01:00
"slices"
2023-11-19 08:06:40 +01:00
)
var (
grassImage *ebiten.Image
2024-01-04 05:57:21 +01:00
subimage *ebiten.Image
2023-11-19 08:06:40 +01:00
)
type Terrain struct {
2023-11-30 14:49:01 +01:00
PositionX float64
PositionY float64
2023-11-21 20:12:56 +01:00
direction configuration.Direction
2023-11-19 08:06:40 +01:00
}
func (t *Terrain) DrawTerrain(screen *ebiten.Image) {
screenWidth := screen.Bounds().Max.X
screenHeight := screen.Bounds().Max.Y
2023-11-30 14:49:01 +01:00
offsetX := int(t.PositionX) % 64
offsetY := int(t.PositionY) % 64
2023-11-19 08:06:40 +01:00
op := &ebiten.DrawImageOptions{}
2023-11-19 14:19:34 +01:00
for i := -1; i <= screenWidth/64+1; i++ {
for j := -1; j <= screenHeight/64+1; j++ {
2023-11-19 08:06:40 +01:00
// ground
op.GeoM.Reset()
2023-11-19 14:19:34 +01:00
op.GeoM.Translate(float64(i*64+offsetX), float64(j*64+offsetY))
2024-01-04 05:57:21 +01:00
screen.DrawImage(subimage, op)
2023-11-19 08:06:40 +01:00
}
}
}
func init() {
img, _, err := image.Decode(bytes.NewReader(resources.Grass_png))
if err != nil {
log.Fatal(err)
}
grassImage = ebiten.NewImageFromImage(img)
2024-01-04 05:57:21 +01:00
subimage = grassImage.SubImage(image.Rect(32, 160, 32+64, 160+64)).(*ebiten.Image)
2023-11-19 08:06:40 +01:00
}
2023-11-19 14:19:34 +01:00
func NewTerrain() *Terrain {
return &Terrain{
2023-11-30 14:49:01 +01:00
PositionX: 0,
PositionY: 0,
2023-11-19 14:19:34 +01:00
}
}
2023-11-21 20:12:56 +01:00
func (t *Terrain) Move() {
if slices.Contains([]configuration.Direction{configuration.North, configuration.NorthEast, configuration.NorthWest}, t.direction) {
2024-01-04 05:57:21 +01:00
t.PositionY += 3
2023-11-21 20:12:56 +01:00
}
if slices.Contains([]configuration.Direction{configuration.South, configuration.SouthEast, configuration.SouthWest}, t.direction) {
2024-01-04 05:57:21 +01:00
t.PositionY -= 3
2023-11-21 20:12:56 +01:00
}
if slices.Contains([]configuration.Direction{configuration.East, configuration.NorthEast, configuration.SouthEast}, t.direction) {
2024-01-04 05:57:21 +01:00
t.PositionX -= 3
2023-11-21 20:12:56 +01:00
}
if slices.Contains([]configuration.Direction{configuration.West, configuration.NorthWest, configuration.SouthWest}, t.direction) {
2024-01-04 05:57:21 +01:00
t.PositionX += 3
2023-11-21 20:12:56 +01:00
}
}
func (t *Terrain) ChangeDirection(direction configuration.Direction) {
2023-11-28 07:29:17 +01:00
if direction != configuration.PreviouslyHeld {
t.direction = direction
}
2023-11-19 14:19:34 +01:00
}