commandeuropa/entity/unit.go

55 lines
1.2 KiB
Go

package entity
import (
"image/color"
"code.rocketnine.space/tslocum/commandeuropa/component"
"code.rocketnine.space/tslocum/gohan"
"github.com/hajimehoshi/ebiten/v2"
)
func isBuildingUnit(unitType component.UnitType) bool {
return unitType == component.UnitTypeBuildingBarracks
}
func unitSprite(unitType component.UnitType) *ebiten.Image {
var img *ebiten.Image
if isBuildingUnit(unitType) {
img = ebiten.NewImage(16, 16)
img.Fill(color.RGBA{0, 255, 0, 255})
} else {
img = ebiten.NewImage(4, 4)
img.Fill(color.RGBA{0, 0, 255, 255})
}
return img
}
func NewUnit(unitType component.UnitType, x float64, y float64) gohan.Entity {
hitPoints := 10
e := gohan.NewEntity()
e.AddComponent(&component.Unit{
Selected: true,
})
e.AddComponent(&component.Position{
X: x,
Y: y,
})
if isBuildingUnit(unitType) {
e.AddComponent(&component.Building{})
} else {
e.AddComponent(&component.Movable{})
}
img := unitSprite(unitType)
e.AddComponent(&component.Sprite{
Image: img,
Width: float64(img.Bounds().Dx()),
Height: float64(img.Bounds().Dy()),
})
e.AddComponent(&component.Health{
Current: hitPoints,
Max: hitPoints,
})
return e
}