Skip to content

Commit

Permalink
client/hub: Improved the movement of the selecte tower
Browse files Browse the repository at this point in the history
Now it's only moving in chunks which makes it easier to positionate the towers
  • Loading branch information
xescugc committed Aug 17, 2023
1 parent 4733086 commit 1a30775
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 29 deletions.
39 changes: 17 additions & 22 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package action
type Action struct {
Type Type `json:"type"`

CursorMove CursorMovePayload `json:"cursor_move,omitempty"`
SummonUnit SummonUnitPayload `json:"summon_unit,omitempty"`
MoveUnit MoveUnitPayload `json:"move_unit,omitempty"`
RemoveUnit RemoveUnitPayload `json:"remove_unit,omitempty"`
StealLive StealLivePayload `json:"steal_live,omitempty"`
CameraZoom CameraZoomPayload `json:"camera_zoom,omitempty"`
SelectTower SelectTowerPayload `json:"select_tower,omitempty"`
PlaceTower PlaceTowerPayload `json:"place_tower,omitempty"`
SelectedTowerInvalid SelectedTowerInvalidPayload `json:"selected_tower_invalid",omitempty"`
CursorMove *CursorMovePayload `json:"cursor_move,omitempty"`
SummonUnit *SummonUnitPayload `json:"summon_unit,omitempty"`
RemoveUnit *RemoveUnitPayload `json:"remove_unit,omitempty"`
StealLive *StealLivePayload `json:"steal_live,omitempty"`
CameraZoom *CameraZoomPayload `json:"camera_zoom,omitempty"`
SelectTower *SelectTowerPayload `json:"select_tower,omitempty"`
PlaceTower *PlaceTowerPayload `json:"place_tower,omitempty"`
SelectedTowerInvalid *SelectedTowerInvalidPayload `json:"selected_tower_invalid",omitempty"`
}

type CursorMovePayload struct {
Expand All @@ -22,7 +21,7 @@ type CursorMovePayload struct {
func NewCursorMove(x, y int) *Action {
return &Action{
Type: CursorMove,
CursorMove: CursorMovePayload{
CursorMove: &CursorMovePayload{
X: x,
Y: y,
},
Expand All @@ -39,7 +38,7 @@ type SummonUnitPayload struct {
func NewSummonUnit(t string, pid, plid, clid int) *Action {
return &Action{
Type: SummonUnit,
SummonUnit: SummonUnitPayload{
SummonUnit: &SummonUnitPayload{
Type: t,
PlayerID: pid,
PlayerLineID: plid,
Expand All @@ -48,13 +47,9 @@ func NewSummonUnit(t string, pid, plid, clid int) *Action {
}
}

type MoveUnitPayload struct {
}

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

Expand All @@ -65,7 +60,7 @@ type RemoveUnitPayload struct {
func NewRemoveUnit(uid int) *Action {
return &Action{
Type: RemoveUnit,
RemoveUnit: RemoveUnitPayload{
RemoveUnit: &RemoveUnitPayload{
UnitID: uid,
},
}
Expand All @@ -79,7 +74,7 @@ type StealLivePayload struct {
func NewStealLive(fpid, tpid int) *Action {
return &Action{
Type: StealLive,
StealLive: StealLivePayload{
StealLive: &StealLivePayload{
FromPlayerID: fpid,
ToPlayerID: tpid,
},
Expand All @@ -93,7 +88,7 @@ type CameraZoomPayload struct {
func NewCameraZoom(d int) *Action {
return &Action{
Type: CameraZoom,
CameraZoom: CameraZoomPayload{
CameraZoom: &CameraZoomPayload{
Direction: d,
},
}
Expand All @@ -109,7 +104,7 @@ type PlaceTowerPayload struct {
func NewPlaceTower(t string, x, y, lid int) *Action {
return &Action{
Type: PlaceTower,
PlaceTower: PlaceTowerPayload{
PlaceTower: &PlaceTowerPayload{
Type: t,
LineID: lid,
X: x,
Expand All @@ -127,7 +122,7 @@ type SelectTowerPayload struct {
func NewSelectTower(t string, x, y int) *Action {
return &Action{
Type: SelectTower,
SelectTower: SelectTowerPayload{
SelectTower: &SelectTowerPayload{
Type: t,
X: x,
Y: y,
Expand All @@ -142,7 +137,7 @@ type SelectedTowerInvalidPayload struct {
func NewSelectedTowerInvalid(i bool) *Action {
return &Action{
Type: SelectedTowerInvalid,
SelectedTowerInvalid: SelectedTowerInvalidPayload{
SelectedTowerInvalid: &SelectedTowerInvalidPayload{
Invalid: i,
},
}
Expand Down
40 changes: 36 additions & 4 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"fmt"
"image"
_ "image/png"
"math"

"github.com/davecgh/go-spew/spew"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
Expand Down Expand Up @@ -37,6 +39,8 @@ type HUDState struct {
SoldierButton Object

SelectedTower *SelectedTower

LastCursorPosition Object
}

type SelectedTower struct {
Expand Down Expand Up @@ -82,9 +86,12 @@ func NewHUDStore(d *flux.Dispatcher, g *Game) (*HUDStore, error) {
}

func (hs *HUDStore) Update() error {
x, y := ebiten.CursorPosition()
actionDispatcher.CursorMove(x, y)
hst := hs.GetState().(HUDState)
x, y := ebiten.CursorPosition()
// Only send a CursorMove when the curso has actually moved
if hst.LastCursorPosition.X != float64(x) || hst.LastCursorPosition.Y != float64(y) {
actionDispatcher.CursorMove(x, y)
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
obj := Object{
X: float64(x),
Expand Down Expand Up @@ -219,9 +226,27 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
},
}
case action.CursorMove:
// We update the last seen cursor position to not resend unnecessary events
hstate.LastCursorPosition.X = float64(act.CursorMove.X)
hstate.LastCursorPosition.Y = float64(act.CursorMove.Y)

if hstate.SelectedTower != nil {
hstate.SelectedTower.X = float64(act.CursorMove.X) - (hstate.SoldierButton.W / 2)
hstate.SelectedTower.Y = float64(act.CursorMove.Y) - (hstate.SoldierButton.H / 2)
// We find the closes multiple in case the cursor moves too fast, between FPS reloads,
// and lands in a position not 'multiple' which means the position of the SelectedTower
// is not updated and the result is the cursor far away from the Drawing of the SelectedTower
// as it has stayed on the previous position
var multiple int = 8
if act.CursorMove.X%multiple == 0 {
hstate.SelectedTower.X = float64(act.CursorMove.X) - (hstate.SoldierButton.W / 2)
} else if math.Abs(float64(act.CursorMove.X)-hstate.SelectedTower.X) > float64(multiple) {
hstate.SelectedTower.X = float64(closestMultiple(act.CursorMove.X, multiple)) - (hstate.SoldierButton.W / 2)
}
if act.CursorMove.Y%multiple == 0 {
hstate.SelectedTower.Y = float64(act.CursorMove.Y) - (hstate.SoldierButton.H / 2)
} else if math.Abs(float64(act.CursorMove.Y)-hstate.SelectedTower.Y) > float64(multiple) {
hstate.SelectedTower.Y = float64(closestMultiple(act.CursorMove.Y, multiple)) - (hstate.SoldierButton.H / 2)
}
spew.Dump(hstate.SelectedTower.Object)
}
case action.PlaceTower, action.DeselectTower:
hstate.SelectedTower = nil
Expand All @@ -234,3 +259,10 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {

return hstate
}

// closestMultiple finds the coses multiple of 'b' for the number 'a'
func closestMultiple(a, b int) int {
a = a + b/2
a = a - (a % b)
return a
}
10 changes: 7 additions & 3 deletions client/logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/xescugc/go-flux"
"github.com/xescugc/ltw/action"
)
Expand All @@ -26,8 +25,13 @@ func (ss *LoggerStore) Reduce(cstate, a interface{}) interface{} {
return cstate
}

// As the MoveUnit is called on every TPS we can
// ignore it
if act.Type == action.MoveUnit {
return cstate
}
// Prints all the action types
fmt.Println(act.Type)
spew.Dump(act)

return cstate
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/xescugc/ltw
go 1.17

require (
github.com/davecgh/go-spew v1.1.0
github.com/hajimehoshi/ebiten/v2 v2.2.5
github.com/xescugc/go-flux v0.0.0-20220312003507-8d5ac35e55d7
)
Expand Down

0 comments on commit 1a30775

Please sign in to comment.