2024-12-10 18:55:23 -05:00
|
|
|
package elements
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"client/gamedata"
|
|
|
|
|
"image/color"
|
2024-12-12 07:12:46 -05:00
|
|
|
"math"
|
2024-12-10 18:55:23 -05:00
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Block struct {
|
|
|
|
|
Sprite *ebiten.Image
|
|
|
|
|
cycle int
|
|
|
|
|
position gamedata.Coordinates
|
2024-12-12 07:12:46 -05:00
|
|
|
target gamedata.Coordinates
|
2024-12-11 12:55:07 -05:00
|
|
|
hit bool
|
2024-12-14 06:36:45 -05:00
|
|
|
clr color.RGBA
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewBlock() *Block {
|
|
|
|
|
return &Block{
|
|
|
|
|
Sprite: ebiten.NewImage(20, 20),
|
|
|
|
|
cycle: 0,
|
2024-12-14 06:36:45 -05:00
|
|
|
clr: color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0x00},
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Block) Update() {
|
2024-12-12 07:12:46 -05:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-10 18:55:23 -05:00
|
|
|
b.cycle++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Block) Draw() {
|
|
|
|
|
b.Sprite.Clear()
|
2024-12-14 06:36:45 -05:00
|
|
|
b.Sprite.Fill(b.clr)
|
2024-12-10 18:55:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Block) SetPosition(pos gamedata.Coordinates) {
|
|
|
|
|
b.position = pos
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Block) GetPosition() gamedata.Coordinates {
|
|
|
|
|
return b.position
|
|
|
|
|
}
|
2024-12-11 12:55:07 -05:00
|
|
|
|
2024-12-12 07:12:46 -05:00
|
|
|
func (b *Block) SetTargetPosition(pos gamedata.Coordinates) {
|
|
|
|
|
b.target = pos
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Block) GetTargetosition() gamedata.Coordinates {
|
|
|
|
|
return b.target
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 12:55:07 -05:00
|
|
|
func (b *Block) GetHit() bool {
|
|
|
|
|
return b.hit
|
|
|
|
|
}
|
2024-12-14 06:36:45 -05:00
|
|
|
|
|
|
|
|
func (b *Block) SetColor(clr color.RGBA) {
|
|
|
|
|
b.clr = clr
|
|
|
|
|
}
|