73 lines
1.2 KiB
Go
73 lines
1.2 KiB
Go
package elements
|
|
|
|
import (
|
|
"client/gamedata"
|
|
"image/color"
|
|
"math"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type Block struct {
|
|
Sprite *ebiten.Image
|
|
cycle int
|
|
position gamedata.Coordinates
|
|
target gamedata.Coordinates
|
|
hit bool
|
|
}
|
|
|
|
func NewBlock() *Block {
|
|
return &Block{
|
|
Sprite: ebiten.NewImage(20, 20),
|
|
cycle: 0,
|
|
}
|
|
}
|
|
|
|
func (b *Block) Update() {
|
|
|
|
dx := b.target.X - b.position.X
|
|
dy := b.target.Y - b.position.Y
|
|
|
|
delta := math.Sqrt(dx*dx + dy*dy)
|
|
angle := math.Atan2(dy, dx)
|
|
if delta > 0.5 {
|
|
b.position.X += delta * math.Cos(angle) / 2
|
|
b.position.Y += delta * math.Sin(angle) / 2
|
|
}
|
|
|
|
b.cycle++
|
|
}
|
|
|
|
func (b *Block) Draw() {
|
|
b.Sprite.Clear()
|
|
if !b.hit {
|
|
b.Sprite.Fill(color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0x00})
|
|
} else {
|
|
b.Sprite.Fill(color.RGBA{R: 0x00, G: 0xff, B: 0x00, A: 0xff})
|
|
}
|
|
}
|
|
|
|
func (b *Block) SetPosition(pos gamedata.Coordinates) {
|
|
b.position = pos
|
|
}
|
|
|
|
func (b *Block) GetPosition() gamedata.Coordinates {
|
|
return b.position
|
|
}
|
|
|
|
func (b *Block) SetTargetPosition(pos gamedata.Coordinates) {
|
|
b.target = pos
|
|
}
|
|
|
|
func (b *Block) GetTargetosition() gamedata.Coordinates {
|
|
return b.target
|
|
}
|
|
|
|
func (b *Block) SetHit(hit bool) {
|
|
b.hit = hit
|
|
}
|
|
|
|
func (b *Block) GetHit() bool {
|
|
return b.hit
|
|
}
|