Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/players: Added logic to detect if a user Won or Lost #29

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ type UpdateStatePlayerPayload struct {
Income int
Gold int
Current bool
Winner bool
}

type UpdateStateTowersPayload struct {
Expand Down
1 change: 1 addition & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (ac *ActionDispatcher) MoveUnit() {
// RemoveUnit removes the unit with the id 'uid'
func (ac *ActionDispatcher) RemoveUnit(uid string) {
rua := action.NewRemoveUnit(uid)
wsSend(rua)
ac.dispatcher.Dispatch(rua)
}

Expand Down
14 changes: 14 additions & 0 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func (hs *HUDStore) Update() error {
if hst.LastCursorPosition.X != float64(x) || hst.LastCursorPosition.Y != float64(y) {
actionDispatcher.CursorMove(x, y)
}
// If the Current player is dead or has no more lives there are no
// mo actions that can be done
// TODO Be able to move the camera when won or lose
if cp.Lives == 0 || cp.Winner {
return nil
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
obj := utils.Object{
X: float64(x),
Expand Down Expand Up @@ -157,6 +163,14 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
cs := hs.game.Camera.GetState().(CameraState)
cp := hs.game.Players.GetCurrentPlayer()

if cp.Lives == 0 {
ebitenutil.DebugPrintAt(screen, "YOU LOST", int(cs.W/2), int(cs.H/2))
}

if cp.Winner {
ebitenutil.DebugPrintAt(screen, "YOU WON!", int(cs.W/2), int(cs.H/2))
}

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.CyclopeButton.X, hst.CyclopeButton.Y)
if cp.Gold < unit.Units[unit.Cyclope.String()].Gold {
Expand Down
36 changes: 21 additions & 15 deletions client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,30 @@ func NewUnits(g *Game) *Units {

func (us *Units) Update() error {
actionDispatcher.MoveUnit()
cp := us.game.Players.GetCurrentPlayer()

for id, u := range us.game.Store.Units.GetState().(store.UnitsState).Units {
if u.Health == 0 {
p := us.game.Store.Players.GetByLineID(u.CurrentLineID)
actionDispatcher.UnitKilled(p.ID, u.Type)
actionDispatcher.RemoveUnit(id)
continue
}
if us.game.Map.IsAtTheEnd(u.Object, u.CurrentLineID) {
p := us.game.Store.Players.GetByLineID(u.CurrentLineID)
actionDispatcher.StealLive(p.ID, u.PlayerID)
nlid := us.game.Map.GetNextLineID(u.CurrentLineID)
if nlid == u.PlayerLineID {
// Only do the events as the owern of the unit if not the actionDispatcher
// will also dispatch it to the server and the event will be done len(players)
// amount of times
if cp.ID == u.PlayerID {
if u.Health == 0 {
p := us.game.Store.Players.GetByLineID(u.CurrentLineID)
actionDispatcher.UnitKilled(p.ID, u.Type)
actionDispatcher.RemoveUnit(id)
} else {
// TODO: Send to next line
// this will need to be done once
// we add more than 2 players
continue
}
if us.game.Map.IsAtTheEnd(u.Object, u.CurrentLineID) {
p := us.game.Store.Players.GetByLineID(u.CurrentLineID)
actionDispatcher.StealLive(p.ID, u.PlayerID)
nlid := us.game.Map.GetNextLineID(u.CurrentLineID)
if nlid == u.PlayerLineID {
actionDispatcher.RemoveUnit(id)
} else {
// TODO: Send to next line
// this will need to be done once
// we add more than 2 players
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions store/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Player struct {
Income int
Gold int
Current bool
Winner bool
}

func NewPlayers(d *flux.Dispatcher) *Players {
Expand Down Expand Up @@ -82,9 +83,26 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
case action.StealLive:
fp := pstate.Players[act.StealLive.FromPlayerID]
fp.Lives -= 1
if fp.Lives < 0 {
fp.Lives = 0
}

tp := pstate.Players[act.StealLive.ToPlayerID]
tp.Lives += 1

var stillPlayersLeft bool
for _, p := range pstate.Players {
if stillPlayersLeft {
continue
}
if p.Lives != 0 && p.ID != tp.ID {
stillPlayersLeft = true
}
}

if !stillPlayersLeft {
tp.Winner = true
}
case action.SummonUnit:
pstate.Players[act.SummonUnit.PlayerID].Income += unit.Units[act.SummonUnit.Type].Income
pstate.Players[act.SummonUnit.PlayerID].Gold -= unit.Units[act.SummonUnit.Type].Gold
Expand Down