Skip to content

Commit

Permalink
client/hud: Added support for Unit and Tower keybinds
Browse files Browse the repository at this point in the history
Those are defined on the assets files for units and towers and then loaded and validated
on init() so we know they are valid
  • Loading branch information
xescugc committed Jan 19, 2024
1 parent c7525cb commit 994f477
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 19 deletions.
6 changes: 4 additions & 2 deletions assets/towers.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"soldier": {
"range": 16,
"damage": 5,
"gold": 10
"gold": 10,
"keybind": "w"
},
"monk": {
"range": 80,
"damage": 1,
"gold": 10
"gold": 10,
"keybind": "q"
}
}
30 changes: 20 additions & 10 deletions assets/units.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,61 @@
"spirit":{
"health": 100,
"income": 1,
"gold": 10
"gold": 10,
"keybind":"1"
},
"spirit2":{
"health": 200,
"income": 2,
"gold": 20
"gold": 20,
"keybind":"2"
},
"flam2":{
"health": 300,
"income": 3,
"gold": 30
"gold": 30,
"keybind":"3"
},
"flam":{
"health": 400,
"income": 4,
"gold": 40
"gold": 40,
"keybind":"4"
},
"octopus":{
"health": 500,
"income": 5,
"gold": 50
"gold": 50,
"keybind":"5"
},
"octopus2":{
"health": 600,
"income": 6,
"gold": 60
"gold": 60,
"keybind":"6"
},
"raccon":{
"health": 700,
"income": 7,
"gold": 70
"gold": 70,
"keybind":"7"
},
"gold_racoon":{
"health": 800,
"income": 8,
"gold": 80
"gold": 80,
"keybind":"8"
},
"cyclope2":{
"health": 900,
"income": 9,
"gold": 90
"gold": 90,
"keybind":"9"
},
"cyclope":{
"health": 1000,
"income":10,
"gold": 100
"gold": 100,
"keybind":"0"
}
}
48 changes: 41 additions & 7 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ebitenui/ebitenui/image"
"github.com/ebitenui/ebitenui/widget"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/xescugc/go-flux"
"github.com/xescugc/maze-wars/action"
"github.com/xescugc/maze-wars/inputer"
Expand Down Expand Up @@ -57,6 +58,32 @@ type SelectedTower struct {
Invalid bool
}

var (
// The key value of this maps is the TYPE of the Unit|Tower
unitKeybinds = make(map[string]ebiten.Key)
towerKeybinds = make(map[string]ebiten.Key)
)

func init() {
for _, u := range unit.Units {
var k ebiten.Key
err := k.UnmarshalText([]byte(u.Keybind))
if err != nil {
panic(err)
}
unitKeybinds[u.Type.String()] = k
}

for _, t := range tower.Towers {
var k ebiten.Key
err := k.UnmarshalText([]byte(t.Keybind))
if err != nil {
panic(err)
}
towerKeybinds[t.Type.String()] = k
}
}

// 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) {
hs := &HUDStore{
Expand Down Expand Up @@ -150,11 +177,18 @@ func (hs *HUDStore) Update() error {
}
}

// TODO: https://github.com/xescugc/maze-wars/issues/60
//if cp.Gold >= tower.Towers[tower.Soldier.String()].Gold && hs.input.IsKeyJustPressed(ebiten.KeyT) {
//actionDispatcher.SelectTower(tower.Soldier.String(), x, y)
//return nil
//}
for ut, kb := range unitKeybinds {
if inpututil.IsKeyJustPressed(kb) {
actionDispatcher.SummonUnit(ut, cp.ID, cp.LineID, hs.game.Store.Map.GetNextLineID(cp.LineID))
return nil
}
}
for tt, kb := range towerKeybinds {
if inpututil.IsKeyJustPressed(kb) {
actionDispatcher.SelectTower(tt, x, y)
return nil
}
}
if hst.TowerOpenMenuID != "" {
if hs.input.IsKeyJustPressed(ebiten.KeyEscape) {
actionDispatcher.CloseTowerMenu()
Expand Down Expand Up @@ -607,7 +641,7 @@ func (hs *HUDStore) buildUI() {

toolTxt := widget.NewText(
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nHP: %.0f\nIncome: %d ", u.Gold, u.Health, u.Income), smallFont, color.White),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nHP: %.0f\nIncome: %d\nKeybind: %s", u.Gold, u.Health, u.Income, u.Keybind), smallFont, color.White),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.MinSize(100, 0)),
)
tooltipContainer.AddChild(toolTxt)
Expand Down Expand Up @@ -676,7 +710,7 @@ func (hs *HUDStore) buildUI() {

toolTxt := widget.NewText(
widget.TextOpts.Position(widget.TextPositionCenter, widget.TextPositionCenter),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nRange: %.0f\nDamage: %.0f", t.Gold, t.Range, t.Damage), smallFont, color.White),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nRange: %.0f\nDamage: %.0f\nKeybind: %s", t.Gold, t.Range, t.Damage, t.Keybind), smallFont, color.White),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.MinSize(100, 0)),
)
tooltipContainer.AddChild(toolTxt)
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions tower/tower.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Tower struct {
Damage float64 `json:"damage"`
Gold int `json:"gold"`

Keybind string

Faceset image.Image
}

Expand Down
2 changes: 2 additions & 0 deletions unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Unit struct {
Income int `json:"income"`
Gold int `json:"gold"`

Keybind string

Faceset image.Image
Sprite image.Image
}
Expand Down

0 comments on commit 994f477

Please sign in to comment.