Skip to content

Commit

Permalink
unit: Added more units
Browse files Browse the repository at this point in the history
And also abstracted the way units are invoked and displayed
  • Loading branch information
xescugc committed Nov 22, 2023
1 parent 018fa0a commit 5aca532
Show file tree
Hide file tree
Showing 28 changed files with 302 additions and 61 deletions.
66 changes: 62 additions & 4 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,70 @@ import (
_ "image/png"
)

//go:embed cyclope/Faceset.png
// UNITS

//go:embed units/cyclope/Faceset.png
var CyclopeFaceset_png []byte

//go:embed units/cyclope/Cyclopes.png
var Cyclope_png []byte

//go:embed units/cyclope2/Faceset.png
var Cyclope2Faceset_png []byte

//go:embed units/cyclope2/SpriteSheet.png
var Cyclope2_png []byte

//go:embed units/flam/Faceset19.png
var FlamFaceset_png []byte

//go:embed units/flam/Flam.png
var Flam_png []byte

//go:embed units/flam2/Faceset14.png
var Flam2Faceset_png []byte

//go:embed units/flam2/Flam2.png
var Flam2_png []byte

//go:embed units/gold_racoon/Faceset23.png
var GoldRacoonFaceset_png []byte

//go:embed units/gold_racoon/Tanuki2.png
var GoldRacoon_png []byte

//go:embed units/octopus/Faceset21.png
var OctopusFaceset_png []byte

//go:embed units/octopus/Octopus.png
var Octopus_png []byte

//go:embed units/octopus2/Faceset16.png
var Octopus2Faceset_png []byte

//go:embed units/octopus2/Octopus2.png
var Octopus2_png []byte

//go:embed units/racoon/Faceset18.png
var RacoonFaceset_png []byte

//go:embed units/racoon/Tanuki.png
var Racoon_png []byte

//go:embed units/spirit/Faceset20.png
var SpiritFaceset_png []byte

//go:embed units/spirit/Spirit.png
var Spirit_png []byte

//go:embed units/spirit2/Faceset15.png
var Spirit2Faceset_png []byte

//go:embed units/spirit2/Spirit2.png
var Spirit2_png []byte

// END UNITS

//go:embed TilesetHouse.png
var TilesetHouse_png []byte

Expand All @@ -17,9 +78,6 @@ var TilesetLogic_png []byte
//go:embed TilesetElement.png
var TilesetElement_png []byte

//go:embed cyclope/Cyclopes.png
var Cyclopes_png []byte

//go:embed maps/2.png
var Map_2_png []byte

Expand Down
55 changes: 50 additions & 5 deletions assets/units.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,52 @@
{
"cyclope": {
"health": 100,
"income": 1,
"gold": 10
}
"spirit":{
"health": 100,
"income": 1,
"gold": 10
},
"spirit2":{
"health": 200,
"income": 2,
"gold": 20
},
"flam2":{
"health": 300,
"income": 3,
"gold": 30
},
"flam":{
"health": 400,
"income": 4,
"gold": 40
},
"octopus":{
"health": 500,
"income": 5,
"gold": 50
},
"octopus2":{
"health": 600,
"income": 6,
"gold": 60
},
"raccon":{
"health": 700,
"income": 7,
"gold": 70
},
"gold_racoon":{
"health": 800,
"income": 8,
"gold": 80
},
"cyclope2":{
"health": 900,
"income": 9,
"gold": 90
},
"cyclope":{
"health": 1000,
"income":10,
"gold": 100
}
}
File renamed without changes
File renamed without changes
Binary file added assets/units/cyclope2/Faceset.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/cyclope2/SpriteSheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/flam/Faceset19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/flam/Flam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/flam2/Faceset14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/flam2/Flam2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/gold_racoon/Faceset23.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/gold_racoon/Tanuki2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/octopus/Faceset21.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/octopus/Octopus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/octopus2/Faceset16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/octopus2/Octopus2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/racoon/Faceset18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/racoon/Tanuki.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/spirit/Faceset20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/spirit/Spirit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/spirit2/Faceset15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/units/spirit2/Spirit2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 76 additions & 27 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type HUDStore struct {

// HUDState stores the HUD state
type HUDState struct {
Facesets []facesetButton

CyclopeButton utils.Object
SoldierButton utils.Object
HouseButton utils.Object
Expand All @@ -47,6 +49,11 @@ type HUDState struct {
CheckedPath bool
}

type facesetButton struct {
Unit *unit.Unit
Object utils.Object
}

type SelectedTower struct {
store.Tower

Expand All @@ -55,9 +62,32 @@ type SelectedTower struct {

// NewHUDStore creates a new HUDStore with the Dispatcher d and the Game g
func NewHUDStore(d *flux.Dispatcher, i inputer.Inputer, g *Game) (*HUDStore, error) {
fi, _, err := image.Decode(bytes.NewReader(assets.CyclopeFaceset_png))
if err != nil {
return nil, err
us := make([]*unit.Unit, 0, 0)
for _, u := range unit.Units {
us = append(us, u)
}
sort.Slice(us, func(i, j int) bool {
return us[i].Gold < us[j].Gold
})

cs := g.Camera.GetState().(CameraState)
// We want to create rows of 5
fs := make([]facesetButton, 0, 0)
nrows := len(us) / 5

// As all the Faceset are equal squares
// we just need to take one
fhw := float64(us[0].Faceset.Bounds().Dx())
for i, u := range us {
fs = append(fs, facesetButton{
Unit: u,
Object: utils.Object{
X: cs.W - (fhw * float64(5-(i%5))),
Y: cs.H - (fhw * float64(nrows-(i/5))),
W: fhw,
H: fhw,
},
})
}

thi, _, err := image.Decode(bytes.NewReader(assets.TilesetHouse_png))
Expand All @@ -73,20 +103,13 @@ func NewHUDStore(d *flux.Dispatcher, i inputer.Inputer, g *Game) (*HUDStore, err
hs := &HUDStore{
game: g,

cyclopeFacesetImage: ebiten.NewImageFromImage(fi),
tilesetHouseImage: ebiten.NewImageFromImage(thi).SubImage(image.Rect(5*16, 17*16, 5*16+16*2, 17*16+16*2)),
houseIcon: ebiten.NewImageFromImage(hi).SubImage(image.Rect(12*16, 0*16, 12*16+16, 0*16+16)),
tilesetHouseImage: ebiten.NewImageFromImage(thi).SubImage(image.Rect(5*16, 17*16, 5*16+16*2, 17*16+16*2)),
houseIcon: ebiten.NewImageFromImage(hi).SubImage(image.Rect(12*16, 0*16, 12*16+16, 0*16+16)),

input: i,
}
cs := g.Camera.GetState().(CameraState)
hs.ReduceStore = flux.NewReduceStore(d, hs.Reduce, HUDState{
CyclopeButton: utils.Object{
X: float64(cs.W - float64(hs.cyclopeFacesetImage.Bounds().Dx())),
Y: float64(cs.H - float64(hs.cyclopeFacesetImage.Bounds().Dy())),
W: float64(hs.cyclopeFacesetImage.Bounds().Dx()),
H: float64(hs.cyclopeFacesetImage.Bounds().Dy()),
},
Facesets: fs,
SoldierButton: utils.Object{
X: 0,
Y: float64(cs.H - 16*2),
Expand Down Expand Up @@ -132,9 +155,11 @@ func (hs *HUDStore) Update() error {
W: 1, H: 1,
}
// Check what the user has just clicked
if cp.Gold >= unit.Units[unit.Cyclope.String()].Gold && hst.CyclopeButton.IsColliding(click) {
actionDispatcher.SummonUnit(unit.Cyclope.String(), cp.ID, cp.LineID, hs.game.Store.Map.GetNextLineID(cp.LineID))
return nil
for _, f := range hst.Facesets {
if cp.Gold >= f.Unit.Gold && f.Object.IsColliding(click) {
actionDispatcher.SummonUnit(f.Unit.Type.String(), cp.ID, cp.LineID, hs.game.Store.Map.GetNextLineID(cp.LineID))
return nil
}
}
if cp.Gold >= tower.Towers[tower.Soldier.String()].Gold && hst.SoldierButton.IsColliding(click) {
actionDispatcher.SelectTower(tower.Soldier.String(), x, y)
Expand Down Expand Up @@ -282,14 +307,16 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
text.Draw(screen, "YOU WON!", smallFont, int(cs.W/2), int(cs.H/2), color.White)
}

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.CyclopeButton.X, hst.CyclopeButton.Y)
if cp.Gold < unit.Units[unit.Cyclope.String()].Gold {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
for _, f := range hst.Facesets {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(f.Object.X, f.Object.Y)
if cp.Gold < f.Unit.Gold {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
}
screen.DrawImage(f.Unit.Faceset.(*ebiten.Image), op)
}
screen.DrawImage(hs.cyclopeFacesetImage.(*ebiten.Image), op)

op = &ebiten.DrawImageOptions{}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.SoldierButton.X, hst.SoldierButton.Y)
if cp.Gold < tower.Towers[tower.Soldier.String()].Gold {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
Expand Down Expand Up @@ -345,12 +372,34 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
case action.WindowResizing:
hs.GetDispatcher().WaitFor(hs.game.Camera.GetDispatcherToken())
cs := hs.game.Camera.GetState().(CameraState)
hstate.CyclopeButton = utils.Object{
X: float64(cs.W - float64(hs.cyclopeFacesetImage.Bounds().Dx())),
Y: float64(cs.H - float64(hs.cyclopeFacesetImage.Bounds().Dy())),
W: float64(hs.cyclopeFacesetImage.Bounds().Dx()),
H: float64(hs.cyclopeFacesetImage.Bounds().Dy()),

us := make([]*unit.Unit, 0, 0)
for _, u := range unit.Units {
us = append(us, u)
}
sort.Slice(us, func(i, j int) bool {
return us[i].Gold < us[j].Gold
})

// We want to create rows of 5
fs := make([]facesetButton, 0, 0)
nrows := len(us) / 5

// As all the Faceset are equal squares
// we just need to take one
fhw := float64(us[0].Faceset.Bounds().Dx())
for i, u := range us {
fs = append(fs, facesetButton{
Unit: u,
Object: utils.Object{
X: cs.W - (fhw * float64(5-(i%5))),
Y: cs.H - (fhw * float64(nrows-(i/5))),
W: fhw,
H: fhw,
},
})
}
hstate.Facesets = fs
hstate.SoldierButton = utils.Object{
X: 0,
Y: float64(cs.H - 16*2),
Expand Down
2 changes: 1 addition & 1 deletion client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (us *Units) DrawUnit(screen *ebiten.Image, c *CameraStore, u *store.Unit) {
sx := facingToTile[u.Facing] * int(u.W)
i := (u.MovingCount / 5) % 4
sy := i * int(u.H)
screen.DrawImage(u.Image().(*ebiten.Image).SubImage(image.Rect(sx, sy, sx+int(u.W), sy+int(u.H))).(*ebiten.Image), op)
screen.DrawImage(u.Sprite().(*ebiten.Image).SubImage(image.Rect(sx, sy, sx+int(u.W), sy+int(u.H))).(*ebiten.Image), op)

// Only draw the Health bar if the unit has been hit
h := unit.Units[u.Type].Health
Expand Down
8 changes: 6 additions & 2 deletions store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ type Unit struct {
Path []utils.Step
}

func (u *Unit) Image() image.Image {
return unit.Units[u.Type].Image
func (u *Unit) Faceset() image.Image {
return unit.Units[u.Type].Faceset
}

func (u *Unit) Sprite() image.Image {
return unit.Units[u.Type].Sprite
}

func NewUnits(d *flux.Dispatcher, s *Store) *Units {
Expand Down
13 changes: 11 additions & 2 deletions unit/type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package unit

//go:generate enumer -type=Type -transform=lower -output=type_string.go
//go:generate enumer -type=Type -transform=lower -transform=snake -output=type_string.go

type Type int

const (
Cyclope Type = iota
Spirit Type = iota
Spirit2
Flam
Flam2
Octopus
Octopus2
Raccon
GoldRacoon
Cyclope
Cyclope2
)
Loading

0 comments on commit 5aca532

Please sign in to comment.