Skip to content

Commit

Permalink
action: Changed the 'MoveUnit' to 'TPS'
Browse files Browse the repository at this point in the history
So it's more coherent to what it actually is and changed how the off camera
cursor moves the camera around so it does not longer require a movement
but just the positon of the Cursor
  • Loading branch information
xescugc committed Dec 9, 2023
1 parent 5b2fded commit 198e9f0
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 100 deletions.
4 changes: 2 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func NewSummonUnit(t, pid string, plid, clid int) *Action {
}
}

func NewMoveUnit() *Action {
func NewTPS() *Action {
return &Action{
Type: MoveUnit,
Type: TPS,
}
}

Expand Down
2 changes: 1 addition & 1 deletion action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const (
CursorMove Type = iota
CameraZoom
SummonUnit
MoveUnit
TPS
RemoveUnit
StealLive
PlaceTower
Expand Down
154 changes: 77 additions & 77 deletions action/type_string.go

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

8 changes: 4 additions & 4 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (ac *ActionDispatcher) SummonUnit(unit, pid string, plid, clid int) {
//ac.dispatcher.Dispatch(sua)
}

// MoveUnit moves all the units
func (ac *ActionDispatcher) MoveUnit() {
mua := action.NewMoveUnit()
ac.dispatcher.Dispatch(mua)
// TPS is the call for every TPS event
func (ac *ActionDispatcher) TPS() {
tpsa := action.NewTPS()
ac.dispatcher.Dispatch(tpsa)
}

// RemoveUnit removes the unit with the id 'uid'
Expand Down
15 changes: 10 additions & 5 deletions client/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ type CameraStore struct {
// CameraState is the store data on the Camera
type CameraState struct {
utils.Object
Zoom float64
Zoom float64
LastCursorPosition utils.Object
}

const (
Expand Down Expand Up @@ -80,19 +81,23 @@ func (cs *CameraStore) Reduce(state, a interface{}) interface{} {

switch act.Type {
case action.CursorMove:
// We update the last seen cursor position to not resend unnecessary events
cstate.LastCursorPosition.X = float64(act.CursorMove.X)
cstate.LastCursorPosition.Y = float64(act.CursorMove.Y)
case action.TPS:
// If the X or Y exceed the current Height or Width then
// it means the cursor is moving out of boundaries so we
// increase the camera X/Y at a ratio of the cameraSpeed
// so we move it around on the map
if float64(act.CursorMove.Y) >= (cstate.H - leeway) {
if float64(cstate.LastCursorPosition.Y) >= (cstate.H - leeway) {
cstate.Y += cs.cameraSpeed
} else if act.CursorMove.Y <= (0 + leeway) {
} else if cstate.LastCursorPosition.Y <= (0 + leeway) {
cstate.Y -= cs.cameraSpeed
}

if float64(act.CursorMove.X) >= (cstate.W - leeway) {
if float64(cstate.LastCursorPosition.X) >= (cstate.W - leeway) {
cstate.X += cs.cameraSpeed
} else if act.CursorMove.X <= (0 + leeway) {
} else if cstate.LastCursorPosition.X <= (0 + leeway) {
cstate.X -= cs.cameraSpeed
}

Expand Down
2 changes: 2 additions & 0 deletions client/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (g *Game) Update() error {
g.Units.Update()
g.Towers.Update()

actionDispatcher.TPS()

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions client/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (ss *LoggerStore) Reduce(cstate, a interface{}) interface{} {
return cstate
}

// As the MoveUnit is called on every TPS we can
// As the TPS is called on every TPS we can
// ignore it
if act.Type == action.MoveUnit {
if act.Type == action.TPS {
return cstate
}
// Prints all the action types
Expand Down
1 change: 0 additions & 1 deletion client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func NewUnits(g *Game) (*Units, error) {
}

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

for _, u := range us.game.Store.Units.List() {
Expand Down
2 changes: 1 addition & 1 deletion integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestRun(t *testing.T) {
// Start the Server
go func() {
err := server.New(sad, rooms, server.Options{
Port: ":5555",
Port: "5555",
})
require.NoError(t, err)
}()
Expand Down
6 changes: 3 additions & 3 deletions server/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func (ac *ActionDispatcher) IncomeTick(rooms *RoomsStore) {
ac.dispatcher.Dispatch(ita)
}

func (ac *ActionDispatcher) MoveUnit(rooms *RoomsStore) {
mua := action.NewMoveUnit()
ac.dispatcher.Dispatch(mua)
func (ac *ActionDispatcher) TPS(rooms *RoomsStore) {
tpsa := action.NewTPS()
ac.dispatcher.Dispatch(tpsa)
}

func (ac *ActionDispatcher) UpdateState(rooms *RoomsStore) {
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion server/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func startRoomsLoop(ctx context.Context, rooms *RoomsStore) {
case <-incomeTicker.C:
actionDispatcher.IncomeTick(rooms)
case <-moveTicker.C:
actionDispatcher.MoveUnit(rooms)
actionDispatcher.TPS(rooms)
case <-ctx.Done():
stateTicker.Stop()
incomeTicker.Stop()
Expand Down
4 changes: 2 additions & 2 deletions store/reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ func TestSummonUnit(t *testing.T) {
})
}

func TestMoveUnit(t *testing.T) {
func TestTPS(t *testing.T) {
t.Run("Success", func(t *testing.T) {
s := initStore()
p := addPlayer(s)
p, u := summonUnit(s, p)

s.Dispatch(action.NewMoveUnit())
s.Dispatch(action.NewTPS())

ps := playersInitialState()
ps.Players[p.ID] = &p
Expand Down
2 changes: 1 addition & 1 deletion store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
u.Path = us.Astar(us.store.Map, u.CurrentLineID, u.MovingObject, tws)
u.HashPath = utils.HashSteps(u.Path)
ustate.Units[uid.String()] = u
case action.MoveUnit:
case action.TPS:
us.mxUnits.Lock()
defer us.mxUnits.Unlock()

Expand Down

0 comments on commit 198e9f0

Please sign in to comment.