Skip to content

Commit

Permalink
client/game/hud: Fixed the stats widget
Browse files Browse the repository at this point in the history
The way it was done it was causing a lot of CPU issues
  • Loading branch information
xescugc committed May 20, 2024
1 parent 2f12b44 commit aacab22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
29 changes: 18 additions & 11 deletions client/game/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,14 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
cp := hs.game.Store.Lines.FindCurrentPlayer()

psit := hs.game.Store.Lines.GetState().(store.LinesState).IncomeTimer
entries := make([]any, 0, 0)
entries := make([]any, 0, len(hs.game.Store.Lines.ListPlayers())+1)
entries = append(entries,
fmt.Sprintf("%s %s %s",
cutils.FillIn("Name", 10),
cutils.FillIn("Lives", 8),
cutils.FillIn("Income", 8)),
cutils.ListEntry{
Text: fmt.Sprintf("%s %s %s",
cutils.FillIn("Name", 10),
cutils.FillIn("Lives", 8),
cutils.FillIn("Income", 8)),
},
)

var sortedPlayers = make([]*store.Player, 0, 0)
Expand All @@ -266,13 +268,18 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
})
for _, p := range sortedPlayers {
entries = append(entries,
fmt.Sprintf("%s %s %s",
cutils.FillIn(p.Name, 10),
cutils.FillIn(strconv.Itoa(p.Lives), 8),
cutils.FillIn(strconv.Itoa(p.Income), 8)),
cutils.ListEntry{
ID: p.ID,
Text: fmt.Sprintf("%s %s %s",
cutils.FillIn(p.Name, 10),
cutils.FillIn(strconv.Itoa(p.Lives), 8),
cutils.FillIn(strconv.Itoa(p.Income), 8)),
},
)
}
hs.statsListW.SetEntries(entries)
if !cutils.EqualListEntries(entries, hs.statsListW.Entries().([]any)) {
hs.statsListW.SetEntries(entries)
}

visibility := widget.Visibility_Show
if !hst.ShowStats {
Expand Down Expand Up @@ -607,7 +614,7 @@ func (hs *HUDStore) buildUI() {
}),
// This required function returns the string displayed in the list
widget.ListOpts.EntryLabelFunc(func(e interface{}) string {
return e.(string)
return e.(cutils.ListEntry).Text
}),
// Padding for each entry
widget.ListOpts.EntryTextPadding(widget.NewInsetsSimple(5)),
Expand Down
28 changes: 5 additions & 23 deletions client/lobbies.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ type LobbiesView struct {
lobbiesListW *widget.List
}

func EqualListEntries(le, nle []any) bool {
if len(le) != len(nle) {
return false
} else {
for i, e := range nle {
if le[i].(ListEntry) != e.(ListEntry) {
return false
}
}
}
return true
}

type ListEntry struct {
ID string
Text string
}

func NewLobbiesView(s *Store, l *slog.Logger) *LobbiesView {
lv := &LobbiesView{
Store: s,
Expand Down Expand Up @@ -72,7 +54,7 @@ func (lv *LobbiesView) Draw(screen *ebiten.Image) {

entries := make([]any, 0, len(lbs))
for _, l := range lbs {
le := ListEntry{
le := cutils.ListEntry{
ID: l.ID,
Text: fmt.Sprintf("%s %s %s",
cutils.FillIn(l.Name, 10),
Expand All @@ -83,9 +65,9 @@ func (lv *LobbiesView) Draw(screen *ebiten.Image) {
entries = append(entries, le)
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].(ListEntry).ID > entries[j].(ListEntry).ID
return entries[i].(cutils.ListEntry).ID > entries[j].(cutils.ListEntry).ID
})
if !EqualListEntries(entries, lv.lobbiesListW.Entries().([]any)) {
if !cutils.EqualListEntries(entries, lv.lobbiesListW.Entries().([]any)) {
lv.lobbiesListW.SetEntries(entries)
}

Expand Down Expand Up @@ -271,7 +253,7 @@ func (lv *LobbiesView) buildUI() {
}),
// This required function returns the string displayed in the list
widget.ListOpts.EntryLabelFunc(func(e interface{}) string {
l := e.(ListEntry)
l := e.(cutils.ListEntry)
return l.Text
}),
// Padding for each entry
Expand All @@ -280,7 +262,7 @@ func (lv *LobbiesView) buildUI() {
widget.ListOpts.EntryTextPosition(widget.TextPositionStart, widget.TextPositionCenter),
// This handler defines what function to run when a list item is selected.
widget.ListOpts.EntrySelectedHandler(func(args *widget.ListEntrySelectedEventArgs) {
l := args.Entry.(ListEntry)
l := args.Entry.(cutils.ListEntry)
un := lv.Store.Users.Username()
lb := lv.Store.Lobbies.FindByID(l.ID)
if len(lb.Players)+1 > lb.MaxPlayers {
Expand Down
8 changes: 4 additions & 4 deletions client/show_lobby.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func (sl *ShowLobbyView) Draw(screen *ebiten.Image) {

entries := make([]any, 0, len(cl.Players))
for p := range cl.Players {
entries = append(entries, ListEntry{
entries = append(entries, cutils.ListEntry{
ID: p,
Text: p,
})
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].(ListEntry).ID > entries[j].(ListEntry).ID
return entries[i].(cutils.ListEntry).ID > entries[j].(cutils.ListEntry).ID
})
if !EqualListEntries(entries, sl.playersListW.Entries().([]any)) {
if !cutils.EqualListEntries(entries, sl.playersListW.Entries().([]any)) {
sl.playersListW.SetEntries(entries)
}

Expand Down Expand Up @@ -304,7 +304,7 @@ func (sl *ShowLobbyView) buildUI() {
}),
// This required function returns the string displayed in the list
widget.ListOpts.EntryLabelFunc(func(e interface{}) string {
return e.(ListEntry).Text
return e.(cutils.ListEntry).Text
}),
// Padding for each entry
widget.ListOpts.EntryTextPadding(widget.NewInsetsSimple(5)),
Expand Down
19 changes: 19 additions & 0 deletions client/utils/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package utils

func EqualListEntries(le, nle []any) bool {
if len(le) != len(nle) {
return false
} else {
for i, e := range nle {
if le[i].(ListEntry) != e.(ListEntry) {
return false
}
}
}
return true
}

type ListEntry struct {
ID string
Text string
}
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.

0 comments on commit aacab22

Please sign in to comment.