Skip to content

Commit

Permalink
client/game/: Moved all the game logic to it's own package
Browse files Browse the repository at this point in the history
So it makes development easy and it's also a nice separation of concepts
  • Loading branch information
xescugc committed Mar 5, 2024
1 parent 5688293 commit 8b785f2
Show file tree
Hide file tree
Showing 26 changed files with 494 additions and 401 deletions.
14 changes: 0 additions & 14 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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"`
ToggleStats *ToggleStatsPayload `json:"toggle_stats,omitempty"`

OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu, omitempty"`
Expand Down Expand Up @@ -351,19 +350,6 @@ func NewGoHome() *Action {
}
}

type CheckedPathPayload struct {
Checked bool
}

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

type ToggleStatsPayload struct {
}

Expand Down
1 change: 0 additions & 1 deletion action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const (
OpenTowerMenu
CloseTowerMenu
GoHome
CheckedPath
ChangeUnitLine
SignUpError
UserSignUp
Expand Down
122 changes: 59 additions & 63 deletions action/type_string.go

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

156 changes: 10 additions & 146 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/xescugc/go-flux"
"github.com/xescugc/maze-wars/action"
cutils "github.com/xescugc/maze-wars/client/utils"
"github.com/xescugc/maze-wars/store"
"github.com/xescugc/maze-wars/utils"
"nhooyr.io/websocket"
Expand Down Expand Up @@ -47,111 +48,6 @@ func (ac *ActionDispatcher) Dispatch(a *action.Action) {
ac.dispatcher.Dispatch(a)
}

// CursorMove dispatches an action of moving the Cursor
// to the new x,y coordinates
func (ac *ActionDispatcher) CursorMove(x, y int) {
cma := action.NewCursorMove(x, y)
ac.Dispatch(cma)
}

// SummonUnit summons the 'unit' from the player id 'pid' to the line
// 'plid' and with the current line id 'clid'
func (ac *ActionDispatcher) SummonUnit(unit, pid string, plid, clid int) {
sua := action.NewSummonUnit(unit, pid, plid, clid)
wsSend(sua)
//ac.Dispatch(sua)
}

// TPS is the call for every TPS event
func (ac *ActionDispatcher) TPS() {
tpsa := action.NewTPS()
ac.Dispatch(tpsa)
}

// RemoveUnit removes the unit with the id 'uid'
func (ac *ActionDispatcher) RemoveUnit(uid string) {
rua := action.NewRemoveUnit(uid)
wsSend(rua)
ac.Dispatch(rua)
}

// StealLive removes one live from the player with id 'fpid' and
// adds it to the player with id 'tpid'
func (ac *ActionDispatcher) StealLive(fpid, tpid string) {
sla := action.NewStealLive(fpid, tpid)
wsSend(sla)
ac.Dispatch(sla)
}

// CameraZoom zooms the camera the direction 'd'
func (ac *ActionDispatcher) CameraZoom(d int) {
cza := action.NewCameraZoom(d)
ac.Dispatch(cza)
}

// PlaceTower places the tower 't' on the position X and Y of the player pid
func (ac *ActionDispatcher) PlaceTower(t, pid string, x, y int) {
// TODO: Add the LineID in the action
p := ac.store.Players.FindByID(pid)
if l := ac.store.Lines.FindByID(p.LineID); l != nil && l.Graph.CanAddTower(x, y, 32, 32) {
pta := action.NewPlaceTower(t, pid, x, y)
wsSend(pta)
}
//ac.Dispatch(pta)
}

// RemoveTower removes the tower tid
func (ac *ActionDispatcher) RemoveTower(pid, tid, tt string) {
rta := action.NewRemoveTower(pid, tid, tt)
wsSend(rta)
//ac.Dispatch(rta)
}

// SelectTower selects the tower 't' on the position x, y
func (ac *ActionDispatcher) SelectTower(t string, x, y int) {
sta := action.NewSelectTower(t, x, y)
ac.Dispatch(sta)
}

// SelectTower selects the tower 't' on the position x, y
func (ac *ActionDispatcher) SelectedTowerInvalid(i bool) {
sta := action.NewSelectedTowerInvalid(i)
ac.Dispatch(sta)
}

// DeselectTower cleans the current selected tower
func (ac *ActionDispatcher) DeselectTower(t string) {
dsta := action.NewDeselectTower(t)
ac.Dispatch(dsta)
}

// IncomeTick a new tick for the income
func (ac *ActionDispatcher) IncomeTick() {
it := action.NewIncomeTick()
ac.Dispatch(it)
}

// TowerAttack issues a attack to the Unit with uid
func (ac *ActionDispatcher) TowerAttack(uid, tt string) {
ta := action.NewTowerAttack(uid, tt)
wsSend(ta)
ac.Dispatch(ta)
}

// UnitKilled adds gold to the user
func (ac *ActionDispatcher) UnitKilled(pid, ut string) {
uk := action.NewUnitKilled(pid, ut)
wsSend(uk)
ac.Dispatch(uk)
}

func (ac *ActionDispatcher) RemovePlayer(pid string) {
rpa := action.NewRemovePlayer(pid)
wsSend(rpa)
ac.Dispatch(rpa)
ac.Dispatch(action.NewNavigateTo(LobbyRoute))
}

// WindowResizing new sizes of the window
func (ac *ActionDispatcher) WindowResizing(w, h int) {
wr := action.NewWindowResizing(w, h)
Expand All @@ -164,38 +60,6 @@ func (ac *ActionDispatcher) NavigateTo(route string) {
ac.Dispatch(nt)
}

// OpenTowerMenu when a tower is clicked and the menu of
// the tower is displayed
func (ac *ActionDispatcher) OpenTowerMenu(tid string) {
otm := action.NewOpenTowerMenu(tid)
ac.Dispatch(otm)
}

// CloseTowerMenu when a tower menu needs to be closed
func (ac *ActionDispatcher) CloseTowerMenu() {
ctm := action.NewCloseTowerMenu()
ac.Dispatch(ctm)
}

// GoHome will move the camera to the current player home line
func (ac *ActionDispatcher) GoHome() {
gha := action.NewGoHome()
ac.Dispatch(gha)
}

// CheckedPath will set the value of the path checked
func (ac *ActionDispatcher) CheckedPath(cp bool) {
cpa := action.NewCheckedPath(cp)
ac.Dispatch(cpa)
}

// ChangeUnitLine will move the unit to the next line
func (ac *ActionDispatcher) ChangeUnitLine(uid string) {
cula := action.NewChangeUnitLine(uid)
wsSend(cula)
ac.Dispatch(cula)
}

func (ac *ActionDispatcher) SignUpSubmit(un string) {
httpu, _ := url.Parse(ac.opt.HostURL)
httpu.Path = "/users"
Expand Down Expand Up @@ -247,25 +111,25 @@ func (ac *ActionDispatcher) SignUpSubmit(un string) {

go wsHandler(ctx)

ac.Dispatch(action.NewNavigateTo(LobbyRoute))
ac.Dispatch(action.NewNavigateTo(cutils.LobbyRoute))
}

// GoHome will move the camera to the current player home line
func (ac *ActionDispatcher) GoHome() {
gha := action.NewGoHome()
ac.Dispatch(gha)
}

func (ac *ActionDispatcher) JoinWaitingRoom(un string) {
jwra := action.NewJoinWaitingRoom(un)
wsSend(jwra)

ac.Dispatch(action.NewNavigateTo(WaitingRoomRoute))
ac.Dispatch(action.NewNavigateTo(cutils.WaitingRoomRoute))
}

func (ac *ActionDispatcher) ExitWaitingRoom(un string) {
ewra := action.NewExitWaitingRoom(un)
wsSend(ewra)

ac.Dispatch(action.NewNavigateTo(LobbyRoute))
}

func (ac *ActionDispatcher) ToggleStats() {
tsa := action.NewToggleStats()

ac.Dispatch(tsa)
ac.Dispatch(action.NewNavigateTo(cutils.LobbyRoute))
}
8 changes: 0 additions & 8 deletions client/colors.go

This file was deleted.

Loading

0 comments on commit 8b785f2

Please sign in to comment.