Skip to content

Commit

Permalink
client/hud: Added a flag to know if we already checked the path viabi…
Browse files Browse the repository at this point in the history
…lity

So we do not check it every refresh tick when we know it does not change
  • Loading branch information
xescugc committed Nov 8, 2023
1 parent d85c249 commit d58ea50
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
14 changes: 14 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Action struct {
NavigateTo *NavigateToPayload `json:"navigate_to, omitempty"`
StartGame *StartGamePayload `json:"start_game, omitempty"`
GoHome *GoHomePayload `json:"go_home, omitempty"`
CheckedPath *CheckedPathPayload `json:"checked_path,omitempty"`

OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu, omitempty"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu, omitempty"`
Expand Down Expand Up @@ -356,6 +357,19 @@ func NewGoHome() *Action {
}
}

type CheckedPathPayload struct {
Checked bool
}

func NewCheckedPath(cp bool) *Action {
return &Action{
Type: CheckedPath,
CheckedPath: &CheckedPathPayload{
Checked: cp,
},
}
}

type UpdateStatePayload struct {
Players *UpdateStatePlayersPayload
Towers *UpdateStateTowersPayload
Expand Down
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
OpenTowerMenu
CloseTowerMenu
GoHome
CheckedPath

// Specific to WS
JoinRoom
Expand Down
44 changes: 24 additions & 20 deletions action/type_string.go

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

6 changes: 6 additions & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,9 @@ func (ac *ActionDispatcher) GoHome() {
gha := action.NewGoHome()
ac.dispatcher.Dispatch(gha)
}

// CheckedPath will set the value of the path checked
func (ac *ActionDispatcher) CheckedPath(cp bool) {
cpa := action.NewCheckedPath(cp)
ac.dispatcher.Dispatch(cpa)
}
10 changes: 8 additions & 2 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type HUDState struct {
TowerOpenMenuID string

LastCursorPosition utils.Object
CheckedPath bool
}

type SelectedTower struct {
Expand Down Expand Up @@ -169,7 +170,6 @@ func (hs *HUDStore) Update() error {
return nil
}
for _, t := range tws {

if clickAbsolute.IsColliding(t.Object) && cp.ID == t.PlayerID {
if hst.TowerOpenMenuID != "" {
// When the user clicks 2 times on the same tower we remove it
Expand Down Expand Up @@ -236,7 +236,7 @@ func (hs *HUDStore) Update() error {
// Only check if the line is blocked when is still valid position and it has not moved.
// TODO: We can improve this by storing this result (if blocking or not) so we only validate
// this once and not when the mouse is static with a selected tower
if !invalid && (hst.LastCursorPosition.X == float64(x) && hst.LastCursorPosition.Y == float64(y)) {
if !invalid && (hst.LastCursorPosition.X == float64(x) && hst.LastCursorPosition.Y == float64(y) && !hst.CheckedPath) {
var fakex, fakey float64 = hs.game.Store.Map.GetRandomSpawnCoordinatesForLineID(cp.LineID)
utws = append(utws, utils.Object{
X: hst.SelectedTower.X + cs.X,
Expand All @@ -253,6 +253,7 @@ func (hs *HUDStore) Update() error {
if len(steps) == 0 {
invalid = true
}
actionDispatcher.CheckedPath(true)
}
if invalid != hst.SelectedTower.Invalid {
actionDispatcher.SelectedTowerInvalid(invalid)
Expand Down Expand Up @@ -399,6 +400,9 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
hstate.SelectedTower.Y = float64(closestMultiple(act.CursorMove.Y, multiple)) - (hstate.SoldierButton.H / 2)
}
}
// If it has moved we set the CheckedPath as not checked as it's only checked
// when the Cursor has not moved
hstate.CheckedPath = false
case action.PlaceTower, action.DeselectTower:
hstate.SelectedTower = nil
case action.SelectedTowerInvalid:
Expand All @@ -409,6 +413,8 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
hstate.TowerOpenMenuID = act.OpenTowerMenu.TowerID
case action.CloseTowerMenu:
hstate.TowerOpenMenuID = ""
case action.CheckedPath:
hstate.CheckedPath = act.CheckedPath.Checked
default:
}

Expand Down
4 changes: 2 additions & 2 deletions client/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func NewRouterStore(d *flux.Dispatcher, g *Game, l *Lobby) *RouterStore {
}

rs.ReduceStore = flux.NewReduceStore(d, rs.Reduce, RouterState{
//Route: LobbyRoute,
Route: GameRoute,
Route: LobbyRoute,
//Route: GameRoute,
})

return rs
Expand Down

0 comments on commit d58ea50

Please sign in to comment.