Minor tweaks to flygoblin.
This commit is contained in:
178
elements/flygoblin.go
Normal file
178
elements/flygoblin.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"mover/assets"
|
||||
"mover/gamedata"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
)
|
||||
|
||||
type FlyGoblin struct {
|
||||
Sprite *ebiten.Image
|
||||
Maks *ebiten.Image
|
||||
MaksDest *ebiten.Image
|
||||
position gamedata.Coordinates
|
||||
target gamedata.Coordinates
|
||||
state gamedata.EnemyState
|
||||
cycle int
|
||||
health int
|
||||
dyingcount int
|
||||
damageduration int
|
||||
right bool
|
||||
hit bool
|
||||
touched bool
|
||||
toggle bool
|
||||
sploding bool
|
||||
damage bool
|
||||
}
|
||||
|
||||
func NewFlyGoblin() *FlyGoblin {
|
||||
fg := &FlyGoblin{
|
||||
Sprite: ebiten.NewImage(96, 96),
|
||||
Maks: ebiten.NewImage(96, 96),
|
||||
MaksDest: ebiten.NewImage(96, 96),
|
||||
health: 100,
|
||||
damageduration: 0,
|
||||
}
|
||||
fg.Maks.Fill(color.White)
|
||||
return fg
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) Update() error {
|
||||
|
||||
if f.damage {
|
||||
f.damageduration++
|
||||
if f.damageduration > 30 {
|
||||
f.damage = false
|
||||
f.damageduration = 0
|
||||
}
|
||||
}
|
||||
|
||||
if f.state < gamedata.EnemyStateDying {
|
||||
dx := f.target.X - f.position.X
|
||||
dy := f.target.Y - f.position.Y
|
||||
|
||||
f.right = dx/48 > 0
|
||||
|
||||
f.position.X += dx / 48
|
||||
f.position.Y += dy / 48
|
||||
|
||||
}
|
||||
|
||||
f.cycle++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) Draw() {
|
||||
f.Sprite.Clear()
|
||||
f.MaksDest.Clear()
|
||||
|
||||
idx := (f.cycle / 8) % 4
|
||||
x0 := 96 * idx
|
||||
x1 := x0 + 96
|
||||
y0 := 0
|
||||
y1 := 96
|
||||
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
if f.right {
|
||||
op.GeoM.Scale(-1, 1)
|
||||
op.GeoM.Translate(MOVER_WIDTH*2, 0)
|
||||
}
|
||||
|
||||
switch f.state {
|
||||
case gamedata.EnemyStateDefault:
|
||||
if !f.toggle {
|
||||
f.Sprite.DrawImage(assets.ImageBank[assets.Worm].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
} else {
|
||||
f.Sprite.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
}
|
||||
case gamedata.EnemyStateHit:
|
||||
if (f.cycle/5)%2 == 0 && f.damage {
|
||||
f.MaksDest.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Reset()
|
||||
op.Blend = ebiten.BlendSourceAtop
|
||||
f.MaksDest.DrawImage(f.Maks, op)
|
||||
f.Sprite.DrawImage(f.MaksDest, nil)
|
||||
} else {
|
||||
f.Sprite.DrawImage(assets.ImageBank[assets.WormDamaged].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
}
|
||||
case gamedata.EnemyStateExploding:
|
||||
op.GeoM.Scale(2, 2)
|
||||
//op.GeoM.Translate(-48, -48)
|
||||
f.Sprite.DrawImage(assets.ImageBank[assets.FlyEyeDying].SubImage(image.Rect(x0, y0, x1, y1)).(*ebiten.Image), op)
|
||||
if idx == 3 {
|
||||
f.state = gamedata.EnemyStateDead
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) GetPosition() gamedata.Coordinates {
|
||||
return f.position
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetPosition(pos gamedata.Coordinates) {
|
||||
f.position = pos
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetTarget(target gamedata.Coordinates) {
|
||||
f.target = target
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) GetSprite() *ebiten.Image {
|
||||
return f.Sprite
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) GetEnemyState() gamedata.EnemyState {
|
||||
return f.state
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetHit() {
|
||||
f.health--
|
||||
f.damage = true
|
||||
f.state = gamedata.EnemyStateHit
|
||||
|
||||
if f.health <= 0 {
|
||||
f.state = gamedata.EnemyStateExploding
|
||||
f.cycle = 0
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetToggle() {
|
||||
f.toggle = !f.toggle
|
||||
|
||||
if !f.toggle {
|
||||
f.state = gamedata.EnemyStateDefault
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) IsToggled() bool {
|
||||
return f.toggle
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetTouched() {
|
||||
f.touched = true
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) ClearTouched() {
|
||||
f.touched = false
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) IsTouched() bool {
|
||||
return f.touched
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) ExplosionInitiated() bool {
|
||||
return f.sploding
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) SetExplosionInitiated() {
|
||||
f.sploding = true
|
||||
}
|
||||
|
||||
func (f *FlyGoblin) Health() int {
|
||||
return f.health
|
||||
}
|
||||
Reference in New Issue
Block a user