Skip to content

Commit

Permalink
client: Added logic for 'Income' and 'Gold'
Browse files Browse the repository at this point in the history
Every 15s the income is converted to gold and the income increases
based on the units send
  • Loading branch information
xescugc committed Sep 4, 2023
1 parent 38dbcc9 commit a077954
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
8 changes: 7 additions & 1 deletion action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ func NewSelectedTowerInvalid(i bool) *Action {
}
}

func NewDeselectTower(t string) *Action {
func NewDeselectTower(_ string) *Action {
return &Action{
Type: DeselectTower,
}
}

func NewIncomeTick() *Action {
return &Action{
Type: IncomeTick,
}
}
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ const (
SelectedTower
SelectedTowerInvalid
DeselectTower
IncomeTick
)
12 changes: 8 additions & 4 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 @@ -81,3 +81,9 @@ func (ac *ActionDispatcher) DeselectTower(t string) {
dsta := action.NewDeselectTower(t)
ac.dispatcher.Dispatch(dsta)
}

// IncomeTick a new tick for the income
func (ac *ActionDispatcher) IncomeTick() {
it := action.NewIncomeTick()
ac.dispatcher.Dispatch(it)
}
4 changes: 3 additions & 1 deletion client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
}

cp := hs.game.Players.GetCurrentPlayer()
ps := hs.game.Players.GetState().(PlayersState)
cs := hs.game.Camera.GetState().(CameraState)
ebitenutil.DebugPrint(screen, fmt.Sprintf("Lives: %d, (X: %d, Y: %d)", cp.Lives, int(hst.LastCursorPosition.X+cs.X), int(hst.LastCursorPosition.Y+cs.Y)))
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Lives: %d, Gold: %d, Income: %d (%ds)", cp.Lives, cp.Gold, cp.Income, ps.IncomeTimer), 0, 0)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("(X: %d, Y: %d)", int(hst.LastCursorPosition.X+cs.X), int(hst.LastCursorPosition.Y+cs.Y)), 0, 15)
}

func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
Expand Down
39 changes: 39 additions & 0 deletions client/players.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
package main

import (
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/xescugc/go-flux"
"github.com/xescugc/ltw/action"
)

var (
unitIncome = map[string]int{
"cyclope": 1,
}
incomeTimer = 15
)

type PlayersStore struct {
*flux.ReduceStore
CurrentPlayerID int
}

type PlayersState struct {
Players map[int]*PlayerState

// IncomeTimer is the internal counter that goes from 15 to 0
IncomeTimer int
}

type PlayerState struct {
ID int
Lives int
LineID int
Income int
Gold int
}

func NewPlayersStore(d *flux.Dispatcher) *PlayersStore {
Expand All @@ -27,20 +41,35 @@ func NewPlayersStore(d *flux.Dispatcher) *PlayersStore {
CurrentPlayerID: 0,
}
ps.ReduceStore = flux.NewReduceStore(d, ps.Reduce, PlayersState{
IncomeTimer: incomeTimer,
Players: map[int]*PlayerState{
0: &PlayerState{
ID: 0,
Lives: 20,
LineID: 0,
Income: 25,
Gold: 40,
},
1: &PlayerState{
ID: 1,
Lives: 20,
LineID: 1,
Income: 25,
Gold: 40,
},
},
})

go func() {
t := time.NewTicker(time.Second)
for {
select {
case <-t.C:
actionDispatcher.IncomeTick()
}
}
}()

return ps
}

Expand Down Expand Up @@ -87,6 +116,16 @@ func (ps *PlayersStore) Reduce(state, a interface{}) interface{} {

tp := pstate.Players[act.StealLive.ToPlayerID]
tp.Lives += 1
case action.SummonUnit:
pstate.Players[act.SummonUnit.PlayerID].Income += unitIncome[act.SummonUnit.Type]
case action.IncomeTick:
pstate.IncomeTimer -= 1
if pstate.IncomeTimer == 0 {
pstate.IncomeTimer = incomeTimer
for _, p := range pstate.Players {
p.Gold += p.Income
}
}
default:
}
return pstate
Expand Down

0 comments on commit a077954

Please sign in to comment.