Added randomly generated background.
This commit is contained in:
49
game.go
49
game.go
@@ -1,13 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"mover/fonts"
|
||||
|
||||
_ "embed"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
"github.com/hajimehoshi/ebiten/v2/text"
|
||||
@@ -19,7 +23,15 @@ const (
|
||||
MOVER_HEIGHT = 48
|
||||
)
|
||||
|
||||
var (
|
||||
tilesetImage *ebiten.Image
|
||||
|
||||
//go:embed grasstile.png
|
||||
tileset_img []byte
|
||||
)
|
||||
|
||||
type Game struct {
|
||||
background *ebiten.Image
|
||||
collisionMask *ebiten.Image
|
||||
projectileMask *ebiten.Image
|
||||
heroCollisionMask *ebiten.Image
|
||||
@@ -46,10 +58,19 @@ type Game struct {
|
||||
//pressedButtons map[ebiten.GamepadID][]string
|
||||
}
|
||||
|
||||
func init() {
|
||||
img, _, err := image.Decode(bytes.NewReader(tileset_img))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
tilesetImage = ebiten.NewImageFromImage(img)
|
||||
}
|
||||
|
||||
func (g *Game) Initialize() {
|
||||
|
||||
origin := Coordinates{X: 640 / 2, Y: 480 / 2}
|
||||
|
||||
g.ConstructBackground()
|
||||
g.hero = NewHero()
|
||||
g.hero.SetOrigin(origin)
|
||||
g.hero.ToggleRotate()
|
||||
@@ -113,6 +134,9 @@ func (g *Game) Update() error {
|
||||
|
||||
func (g *Game) Draw(screen *ebiten.Image) {
|
||||
|
||||
screen.Clear()
|
||||
screen.DrawImage(g.background, nil)
|
||||
|
||||
g.hero.Draw()
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
@@ -269,6 +293,8 @@ func (g *Game) UpdateProjectiles() {
|
||||
if p.Pos.X >= target.Pos.X-MOVER_WIDTH/2 && p.Pos.X <= target.Pos.X+MOVER_WIDTH/2 && p.Pos.Y >= target.Pos.Y-MOVER_HEIGHT/2 && p.Pos.Y <= target.Pos.Y+MOVER_HEIGHT/2 && target.Action == MoverActionDamaged {
|
||||
//fmt.Println("potential collision")
|
||||
|
||||
//the following computes total collisions in the image using a projectile mask that is a duplicate of what is on screen
|
||||
//there's definitely room for optimization here
|
||||
g.collisionMask.Clear()
|
||||
g.collisionMask.DrawImage(g.projectileMask, nil)
|
||||
|
||||
@@ -398,3 +424,26 @@ func (g *Game) UpdateHeroPosition() {
|
||||
g.hero.Pos.Y += ebiten.GamepadAxisValue(0, 1) * 5
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) ConstructBackground() {
|
||||
g.background = ebiten.NewImage(screenWidth, screenHeight)
|
||||
|
||||
for i := 0; i < 640/16; i++ {
|
||||
for j := 0; j < 480/16; j++ {
|
||||
|
||||
//select random tile in x and y from tileset
|
||||
idx_x := rand.IntN(256 / 16)
|
||||
idx_y := rand.IntN(256 / 16)
|
||||
|
||||
x0 := 16 * idx_x
|
||||
y0 := 16 * idx_y
|
||||
x1 := x0 + 16
|
||||
y1 := y0 + 16
|
||||
|
||||
//translate for grid element we're painting
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(i)*16, float64(j)*16)
|
||||
g.background.DrawImage(tilesetImage.SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user