Real-time strategy (RTS) video game
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

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
}