Cloud layer.
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"mover/assets"
|
||||
"mover/elements"
|
||||
"mover/fonts"
|
||||
@@ -50,6 +51,7 @@ func NewCanvas(a gamedata.Area) *Canvas {
|
||||
goblinspawned: false,
|
||||
score: 0,
|
||||
runtime: 0.,
|
||||
counter: 0,
|
||||
}
|
||||
c.eventmap = make(map[gamedata.GameEvent]func())
|
||||
return c
|
||||
@@ -70,9 +72,9 @@ func (c *Canvas) Update() error {
|
||||
c.UpdateCharge()
|
||||
c.UpdateEnemies()
|
||||
c.CleanupTargets()
|
||||
|
||||
c.counter++
|
||||
}
|
||||
c.counter++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -105,7 +107,7 @@ func (c *Canvas) Draw(drawimg *ebiten.Image) {
|
||||
if e.Health() > 0 {
|
||||
x0 := e.GetPosition().X - float64(e.GetSprite().Bounds().Dx())
|
||||
y0 := e.GetPosition().Y - 2/3.*float64(e.GetSprite().Bounds().Dy())
|
||||
vector.DrawFilledRect(c.Sprite, float32(x0), float32(y0), 204, 12, color.Black, true)
|
||||
vector.DrawFilledRect(c.Sprite, float32(x0), float32(y0), float32(e.MaxHealth())*2+4, 12, color.Black, true)
|
||||
vector.DrawFilledRect(c.Sprite, float32(x0+2), float32(y0+2), float32(e.Health())*2, 8, color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff}, true)
|
||||
}
|
||||
}
|
||||
@@ -351,7 +353,8 @@ func (c *Canvas) UpdateEnemies() {
|
||||
e.Update()
|
||||
}
|
||||
if !c.gameover {
|
||||
/*
|
||||
|
||||
if !c.goblinspawned {
|
||||
//spawn new enemies
|
||||
f := 40000 / (c.counter + 1)
|
||||
|
||||
@@ -376,13 +379,15 @@ func (c *Canvas) UpdateEnemies() {
|
||||
newenemy.SetTarget(c.hero.Pos)
|
||||
|
||||
c.enemies = append(c.enemies, newenemy)
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
if !c.goblinspawned {
|
||||
if !c.goblinspawned && c.counter > 1200 {
|
||||
newfg := elements.NewFlyGoblin()
|
||||
c.enemies = append(c.enemies, newfg)
|
||||
c.goblinspawned = true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
109
gameelement/cloudlayer.go
Normal file
109
gameelement/cloudlayer.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package gameelement
|
||||
|
||||
import (
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"mover/elements"
|
||||
"mover/gamedata"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type CloudLayer struct {
|
||||
Sprite *ebiten.Image
|
||||
clouds []*elements.Cloud
|
||||
dimensions gamedata.Area
|
||||
cycle int
|
||||
}
|
||||
|
||||
func NewCloudLayer(a gamedata.Area) *CloudLayer {
|
||||
c := &CloudLayer{
|
||||
Sprite: ebiten.NewImage(a.Width, a.Height),
|
||||
cycle: 0,
|
||||
dimensions: a,
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *CloudLayer) SetInputs(gamedata.GameInputs) {
|
||||
}
|
||||
|
||||
func (c *CloudLayer) Update() error {
|
||||
c.cycle++
|
||||
|
||||
for _, cloud := range c.clouds {
|
||||
cloud.Update()
|
||||
|
||||
cpos := cloud.GetPosition()
|
||||
if cpos.X > float64(c.dimensions.Width)+float64(cloud.Sprite.Bounds().Dx()) {
|
||||
dx := -float64(cloud.Sprite.Bounds().Dx())
|
||||
cloud.SetPosition(gamedata.Coordinates{X: dx, Y: cloud.GetPosition().Y})
|
||||
}
|
||||
if cpos.X < -float64(cloud.Sprite.Bounds().Dx()) {
|
||||
dx := float64(c.dimensions.Width + cloud.Sprite.Bounds().Dx())
|
||||
cloud.SetPosition(gamedata.Coordinates{X: dx, Y: cloud.GetPosition().Y})
|
||||
}
|
||||
if cpos.Y > float64(c.dimensions.Height)+float64(cloud.Sprite.Bounds().Dy()) {
|
||||
dy := -float64(cloud.Sprite.Bounds().Dy())
|
||||
cloud.SetPosition(gamedata.Coordinates{X: cloud.GetPosition().X, Y: dy})
|
||||
}
|
||||
if cpos.Y < -float64(cloud.Sprite.Bounds().Dy()) {
|
||||
dy := float64(c.dimensions.Height + cloud.Sprite.Bounds().Dy())
|
||||
cloud.SetPosition(gamedata.Coordinates{X: cloud.GetPosition().X, Y: dy})
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *CloudLayer) Draw(drawimg *ebiten.Image) {
|
||||
c.Sprite.Clear()
|
||||
|
||||
for _, cloud := range c.clouds {
|
||||
cloud.Draw()
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(cloud.GetPosition().X, cloud.GetPosition().Y)
|
||||
op.ColorScale.ScaleAlpha(float32(cloud.Alpha))
|
||||
c.Sprite.DrawImage(cloud.Sprite, op)
|
||||
}
|
||||
|
||||
drawimg.DrawImage(c.Sprite, nil)
|
||||
}
|
||||
|
||||
func (c *CloudLayer) Initialize() {
|
||||
|
||||
//cull previous cloud layer
|
||||
for i := 0; i < len(c.clouds); i++ {
|
||||
c.clouds[i] = nil
|
||||
}
|
||||
c.clouds = c.clouds[:0]
|
||||
|
||||
numclouds := rand.IntN(20)
|
||||
angle := rand.Float64() * math.Pi * 2
|
||||
|
||||
for i := 0; i < numclouds; i++ {
|
||||
|
||||
a := gamedata.Area{
|
||||
Height: rand.IntN(c.dimensions.Width/2) + 1,
|
||||
Width: rand.IntN(c.dimensions.Height/2) + 1,
|
||||
}
|
||||
|
||||
velocity := rand.Float64() * 3
|
||||
//velocity := 0.
|
||||
|
||||
newcloud := elements.NewCloud(a, angle, velocity)
|
||||
|
||||
newcloud.Alpha = rand.Float64() / 2
|
||||
|
||||
newcloud.SetPosition(gamedata.Coordinates{
|
||||
X: rand.Float64() * float64(c.dimensions.Width),
|
||||
Y: rand.Float64() * float64(c.dimensions.Height),
|
||||
})
|
||||
|
||||
c.clouds = append(c.clouds, newcloud)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CloudLayer) RegisterEvents(e gamedata.GameEvent, f func()) {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user