Skip to content

Commit

Permalink
unit/environment: Added 'environment' to Units with values 'terrestri…
Browse files Browse the repository at this point in the history
…al' or 'aerial'

Which will affect on the pathing and also to which Towers can attack them
  • Loading branch information
xescugc committed Mar 26, 2024
1 parent b14ec58 commit 969360b
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 39 deletions.
6 changes: 4 additions & 2 deletions assets/towers.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"range": 40,
"damage": 5,
"gold": 10,
"keybind": "w"
"keybind": "w",
"targets":["terrestrial"]
},
"monk": {
"range": 80,
"damage": 1,
"gold": 10,
"keybind": "q"
"keybind": "q",
"targets":["terrestrial","aerial"]
}
}
34 changes: 22 additions & 12 deletions assets/units.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,70 @@
"health": 100,
"income": 1,
"gold": 10,
"keybind":"1"
"keybind":"1",
"environment":"terrestrial"
},
"eye":{
"health": 200,
"income": 2,
"gold": 20,
"keybind":"2"
"keybind":"2",
"environment":"terrestrial"
},
"flam":{
"health": 300,
"income": 3,
"gold": 30,
"keybind":"3"
"keybind":"3",
"environment":"terrestrial"
},
"skull":{
"health": 400,
"income": 4,
"gold": 40,
"keybind":"4"
"keybind":"4",
"environment":"terrestrial"
},
"mole":{
"health": 500,
"income": 5,
"gold": 50,
"keybind":"5"
"keybind":"5",
"environment":"terrestrial"
},
"snake":{
"health": 600,
"income": 6,
"gold": 60,
"keybind":"6"
"keybind":"6",
"environment":"terrestrial"
},
"raccon":{
"health": 700,
"income": 7,
"gold": 70,
"keybind":"7"
"keybind":"7",
"environment":"terrestrial"
},
"beast":{
"health": 800,
"income": 8,
"gold": 80,
"keybind":"8"
"keybind":"8",
"environment":"terrestrial"
},
"cyclope":{
"health": 900,
"income": 9,
"gold": 90,
"keybind":"9"
"keybind":"9",
"environment":"terrestrial"
},
"butterfly":{
"health": 1000,
"income":10,
"health": 300,
"income":3,
"gold": 100,
"keybind":"0"
"keybind":"0",
"environment":"aerial"
}
}
4 changes: 2 additions & 2 deletions client/game/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,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\nKeybind: %s", u.Gold, u.Health, u.Income, u.Keybind), cutils.SmallFont, color.White),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nHP: %.0f\nIncome: %d\nEnv: %s\nKeybind: %s", u.Gold, u.Health, u.Income, u.Environment, u.Keybind), cutils.SmallFont, color.White),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.MinSize(100, 0)),
)
tooltipContainer.AddChild(toolTxt)
Expand Down Expand Up @@ -674,7 +674,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\nKeybind: %s", t.Gold, t.Range, t.Damage, t.Keybind), cutils.SmallFont, color.White),
widget.TextOpts.Text(fmt.Sprintf("Gold: %d\nRange: %.0f\nDamage: %.0f\nTargets: %s\nKeybind: %s", t.Gold, t.Range, t.Damage, t.Targets, t.Keybind), cutils.SmallFont, color.White),
widget.TextOpts.WidgetOpts(widget.WidgetOpts.MinSize(100, 0)),
)
tooltipContainer.AddChild(toolTxt)
Expand Down
2 changes: 1 addition & 1 deletion client/game/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ls *Lines) Update() error {
minDistUnit string
)
for _, u := range l.Units {
if u.CurrentLineID != cp.LineID {
if u.CurrentLineID != cp.LineID || !t.CanTarget(unit.Units[u.Type].Environment) {
continue
}
d := t.PDistance(u.Object)
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
10 changes: 7 additions & 3 deletions store/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/xescugc/maze-wars/action"
"github.com/xescugc/maze-wars/tower"
"github.com/xescugc/maze-wars/unit"
"github.com/xescugc/maze-wars/unit/environment"
"github.com/xescugc/maze-wars/utils"
"github.com/xescugc/maze-wars/utils/graph"
)
Expand Down Expand Up @@ -62,6 +63,9 @@ type Tower struct {
}

func (t *Tower) FacetKey() string { return tower.Towers[t.Type].FacesetKey() }
func (t *Tower) CanTarget(env environment.Environment) bool {
return tower.Towers[t.Type].CanTarget(env)
}

type Unit struct {
utils.MovingObject
Expand Down Expand Up @@ -246,7 +250,7 @@ func (ls *Lines) Reduce(state, a interface{}) interface{} {
CreatedAt: time.Now(),
}

u.Path = l.Graph.AStar(u.X, u.Y, u.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, atScale)
u.Path = l.Graph.AStar(u.X, u.Y, u.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, unit.Units[act.SummonUnit.Type].Environment, atScale)
u.HashPath = graph.HashSteps(u.Path)
l.Units[u.ID] = u
case action.ChangeUnitLine:
Expand All @@ -268,7 +272,7 @@ func (ls *Lines) Reduce(state, a interface{}) interface{} {
u.X = n.X
u.Y = n.Y

u.Path = nl.Graph.AStar(u.X, u.Y, u.Facing, nl.Graph.DeathNode.X, nl.Graph.DeathNode.Y, atScale)
u.Path = nl.Graph.AStar(u.X, u.Y, u.Facing, nl.Graph.DeathNode.X, nl.Graph.DeathNode.Y, unit.Units[u.Type].Environment, atScale)
u.HashPath = graph.HashSteps(u.Path)

u.CreatedAt = time.Now()
Expand Down Expand Up @@ -381,7 +385,7 @@ func recalculateLineUnitSteps(l *Line) {
moveLineUnitsTo(l, t)

for _, u := range l.Units {
u.Path = l.Graph.AStar(u.X, u.Y, u.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, atScale)
u.Path = l.Graph.AStar(u.X, u.Y, u.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, unit.Units[u.Type].Environment, atScale)
u.HashPath = graph.HashSteps(u.Path)
}
}
Expand Down
6 changes: 3 additions & 3 deletions store/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestSummonUnit(t *testing.T) {
eu.ID, eu.X, eu.Y = units[uid].ID, units[uid].X, units[uid].Y

// We need to set the path after the X, Y are set
eu.Path = l.Graph.AStar(eu.X, eu.Y, eu.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, atScale)
eu.Path = l.Graph.AStar(eu.X, eu.Y, eu.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, unit.Units[eu.Type].Environment, atScale)
eu.HashPath = graph.HashSteps(eu.Path)

// As the Unit is created we remove it from the gold
Expand Down Expand Up @@ -455,7 +455,7 @@ func TestPlaceTower(t *testing.T) {
l.Towers[tw.ID] = &tw

l2 := ls.Lines[p2.LineID]
u.Path = l2.Graph.AStar(u.X, u.Y, u.Facing, l2.Graph.DeathNode.X, l2.Graph.DeathNode.Y, atScale)
u.Path = l2.Graph.AStar(u.X, u.Y, u.Facing, l2.Graph.DeathNode.X, l2.Graph.DeathNode.Y, unit.Units[u.Type].Environment, atScale)
u.HashPath = graph.HashSteps(u.Path)

l2.Units[u.ID] = &u
Expand Down Expand Up @@ -674,7 +674,7 @@ func TestChangeUnitLine(t *testing.T) {
u1.CreatedAt = units[uid].CreatedAt

// We need to set the path after the X, Y are set
u1.Path = l.Graph.AStar(u1.X, u1.Y, u1.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, atScale)
u1.Path = l.Graph.AStar(u1.X, u1.Y, u1.Facing, l.Graph.DeathNode.X, l.Graph.DeathNode.Y, unit.Units[u1.Type].Environment, atScale)
u1.HashPath = graph.HashSteps(u1.Path)

ls.Lines[p3.LineID].Units[u1.ID] = &u1
Expand Down
18 changes: 18 additions & 0 deletions tower/tower.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"

"github.com/xescugc/maze-wars/assets"
"github.com/xescugc/maze-wars/unit/environment"
)

type Tower struct {
Expand All @@ -17,13 +18,29 @@ type Tower struct {
Damage float64 `json:"damage"`
Gold int `json:"gold"`

Targets []environment.Environment `json:"targets"`
targets map[environment.Environment]struct{}

Keybind string

Faceset image.Image
}

func (t *Tower) FacesetKey() string { return fmt.Sprintf("t-f-%s", t.Type) }

func (t *Tower) CanTarget(env environment.Environment) bool {
_, ok := t.targets[env]
return ok
}

// initTargets will map the Targets to a map for easy access
func (t *Tower) initTargets() {
t.targets = make(map[environment.Environment]struct{})
for _, tg := range t.Targets {
t.targets[tg] = struct{}{}
}
}

var (
Towers map[string]*Tower
)
Expand Down Expand Up @@ -53,6 +70,7 @@ func init() {
log.Fatalf("failed to load tower %q\n", t)
}
tw.Type = ty
tw.initTargets()
}
}

Expand Down
10 changes: 10 additions & 0 deletions unit/environment/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package environment

//go:generate enumer -type=Environment -transform=lower -json -transform=snake -output=environment_string.go

type Environment int

const (
Terrestrial Environment = iota
Aerial
)
96 changes: 96 additions & 0 deletions unit/environment/environment_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions unit/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"

"github.com/xescugc/maze-wars/assets"
"github.com/xescugc/maze-wars/unit/environment"
)

type Unit struct {
Expand All @@ -17,6 +18,8 @@ type Unit struct {
Income int `json:"income"`
Gold int `json:"gold"`

Environment environment.Environment `json:"environment"`

Keybind string

Faceset image.Image
Expand Down
Loading

0 comments on commit 969360b

Please sign in to comment.