Skip to content

Commit

Permalink
all: Move all the internal code to 'int'
Browse files Browse the repository at this point in the history
Before there was a combination between 'int' and 'float64' which was anoying to deal with.

Also ran 'go vet ./...' to fix few things and cleaned up some unused methods
  • Loading branch information
xescugc committed Mar 5, 2024
1 parent 8b785f2 commit 52ab7e5
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 203 deletions.
36 changes: 18 additions & 18 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ type Action struct {
TowerAttack *TowerAttackPayload `json:"tower_attack,omitempty"`
UnitKilled *UnitKilledPayload `json:"unit_killed,omitempty"`
WindowResizing *WindowResizingPayload `json:"window_resizing,omitempty"`
NavigateTo *NavigateToPayload `json:"navigate_to, omitempty"`
StartGame *StartGamePayload `json:"start_game, omitempty"`
GoHome *GoHomePayload `json:"go_home, omitempty"`
NavigateTo *NavigateToPayload `json:"navigate_to,omitempty"`
StartGame *StartGamePayload `json:"start_game,omitempty"`
GoHome *GoHomePayload `json:"go_home,omitempty"`
ToggleStats *ToggleStatsPayload `json:"toggle_stats,omitempty"`

OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu, omitempty"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu, omitempty"`

UserSignUp *UserSignUpPayload `json:"user_sign_up, omitempty"`
SignUpError *SignUpErrorPayload `json:"sign_in_error, omitempty"`
UserSignIn *UserSignInPayload `json:"user_sign_in, omitempty"`
UserSignOut *UserSignOutPayload `json:"user_sign_out, omitempty"`

AddPlayer *AddPlayerPayload `json:"add_player, omitempty"`
RemovePlayer *RemovePlayerPayload `json:"remove_player, omitempty"`
JoinWaitingRoom *JoinWaitingRoomPayload `json:"join_waiting_room, omitempty"`
ExitWaitingRoom *ExitWaitingRoomPayload `json:"exit_waiting_room, omitempty"`
SyncState *SyncStatePayload `json:"sync_state, omitempty"`
SyncUsers *SyncUsersPayload `json:"sync_users, omitempty"`
SyncWaitingRoom *SyncWaitingRoomPayload `json:"sync_waiting_room, omitempty"`
OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu,omitempty"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu,omitempty"`

UserSignUp *UserSignUpPayload `json:"user_sign_up,omitempty"`
SignUpError *SignUpErrorPayload `json:"sign_in_error,omitempty"`
UserSignIn *UserSignInPayload `json:"user_sign_in,omitempty"`
UserSignOut *UserSignOutPayload `json:"user_sign_out,omitempty"`

AddPlayer *AddPlayerPayload `json:"add_player,omitempty"`
RemovePlayer *RemovePlayerPayload `json:"remove_player,omitempty"`
JoinWaitingRoom *JoinWaitingRoomPayload `json:"join_waiting_room,omitempty"`
ExitWaitingRoom *ExitWaitingRoomPayload `json:"exit_waiting_room,omitempty"`
SyncState *SyncStatePayload `json:"sync_state,omitempty"`
SyncUsers *SyncUsersPayload `json:"sync_users,omitempty"`
SyncWaitingRoom *SyncWaitingRoomPayload `json:"sync_waiting_room,omitempty"`
}

type CursorMovePayload struct {
Expand Down
30 changes: 15 additions & 15 deletions client/game/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type CameraStore struct {

logger *slog.Logger

cameraSpeed float64
cameraSpeed int
}

// CameraState is the store data on the Camera
Expand Down Expand Up @@ -51,8 +51,8 @@ func NewCameraStore(d *flux.Dispatcher, s *store.Store, l *slog.Logger, w, h int
cs.ReduceStore = flux.NewReduceStore(d, cs.Reduce, CameraState{
Object: utils.Object{
X: 0, Y: 0,
W: float64(w),
H: float64(h),
W: w,
H: h,
},
Zoom: 1,
})
Expand Down Expand Up @@ -94,20 +94,20 @@ 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)
cstate.LastCursorPosition.X = act.CursorMove.X
cstate.LastCursorPosition.Y = 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(cstate.LastCursorPosition.Y) >= (cstate.H - leeway) {
if cstate.LastCursorPosition.Y >= (cstate.H - leeway) {
cstate.Y += cs.cameraSpeed
} else if cstate.LastCursorPosition.Y <= (0 + leeway) {
cstate.Y -= cs.cameraSpeed
}

if float64(cstate.LastCursorPosition.X) >= (cstate.W - leeway) {
if cstate.LastCursorPosition.X >= (cstate.W - leeway) {
cstate.X += cs.cameraSpeed
} else if cstate.LastCursorPosition.X <= (0 + leeway) {
cstate.X -= cs.cameraSpeed
Expand All @@ -118,19 +118,19 @@ func (cs *CameraStore) Reduce(state, a interface{}) interface{} {
// values as we cannot go out of the map
if cstate.X <= 0 {
cstate.X = 0
} else if cstate.X >= float64(cs.Store.Map.GetX()) {
cstate.X = float64(cs.Store.Map.GetX())
} else if cstate.X >= cs.Store.Map.GetX() {
cstate.X = cs.Store.Map.GetX()
}
if cstate.Y <= 0 {
cstate.Y = 0
} else if cstate.Y >= float64(cs.Store.Map.GetY()) {
cstate.Y = float64(cs.Store.Map.GetY())
} else if cstate.Y >= cs.Store.Map.GetY() {
cstate.Y = cs.Store.Map.GetY()
}
case action.CameraZoom:
cstate.Zoom += float64(act.CameraZoom.Direction) * zoomScale
//case action.CameraZoom:
//cstate.Zoom += act.CameraZoom.Direction * zoomScale
case action.WindowResizing:
cstate.W = float64(act.WindowResizing.Width)
cstate.H = float64(act.WindowResizing.Height)
cstate.W = act.WindowResizing.Width
cstate.H = act.WindowResizing.Height
case action.GoHome:
cp := cs.Store.Players.FindCurrent()
x, y := cs.Store.Map.GetHomeCoordinates(cp.LineID)
Expand Down
30 changes: 15 additions & 15 deletions client/game/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (hs *HUDStore) Update() error {
cl := hs.game.Store.Lines.FindByID(cp.LineID)
tws := cl.Towers
// Only send a CursorMove when the curso has actually moved
if hst.LastCursorPosition.X != float64(x) || hst.LastCursorPosition.Y != float64(y) {
if hst.LastCursorPosition.X != x || hst.LastCursorPosition.Y != y {
actionDispatcher.CursorMove(x, y)
}
// If the Current player is dead or has no more lives there are no
Expand All @@ -118,13 +118,13 @@ func (hs *HUDStore) Update() error {
}
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
clickAbsolute := utils.Object{
X: float64(x) + cs.X,
Y: float64(y) + cs.Y,
X: x + cs.X,
Y: y + cs.Y,
W: 1, H: 1,
}

if hst.SelectedTower != nil && !hst.SelectedTower.Invalid {
actionDispatcher.PlaceTower(hst.SelectedTower.Type, cp.ID, int(hst.SelectedTower.X+cs.X), int(hst.SelectedTower.Y+cs.Y))
actionDispatcher.PlaceTower(hst.SelectedTower.Type, cp.ID, hst.SelectedTower.X+cs.X, hst.SelectedTower.Y+cs.Y)
return nil
}
for _, t := range tws {
Expand Down Expand Up @@ -181,7 +181,7 @@ func (hs *HUDStore) Update() error {
neo.Y += cs.Y

if !invalid {
invalid = !cl.Graph.CanAddTower(int(neo.X), int(neo.Y), int(neo.W), int(neo.H))
invalid = !cl.Graph.CanAddTower(neo.X, neo.Y, neo.W, neo.H)
}

if !invalid {
Expand Down Expand Up @@ -272,7 +272,7 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {

if hst.SelectedTower != nil {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.SelectedTower.X/cs.Zoom, hst.SelectedTower.Y/cs.Zoom)
op.GeoM.Translate(float64(hst.SelectedTower.X)/cs.Zoom, float64(hst.SelectedTower.Y)/cs.Zoom)
op.GeoM.Scale(cs.Zoom, cs.Zoom)

if hst.SelectedTower != nil && hst.SelectedTower.Invalid {
Expand Down Expand Up @@ -319,7 +319,7 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
hs.GetDispatcher().WaitFor(hs.game.Store.Players.GetDispatcherToken())
cp := hs.game.Store.Players.FindCurrent()
cs := hs.game.Camera.GetState().(CameraState)
x, y := fixPosition(cs, float64(act.SelectTower.X), float64(act.SelectTower.Y))
x, y := fixPosition(cs, act.SelectTower.X, act.SelectTower.Y)
hstate.SelectedTower = &SelectedTower{
Tower: store.Tower{
Object: utils.Object{
Expand All @@ -335,8 +335,8 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
}
case action.CursorMove:
// We update the last seen cursor position to not resend unnecessary events
nx := float64(act.CursorMove.X)
ny := float64(act.CursorMove.Y)
nx := act.CursorMove.X
ny := act.CursorMove.Y

hstate.LastCursorPosition.X = nx
hstate.LastCursorPosition.Y = ny
Expand Down Expand Up @@ -364,7 +364,7 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
return hstate
}

func fixPosition(cs CameraState, x, y float64) (float64, float64) {
func fixPosition(cs CameraState, x, y int) (int, int) {
absnx := x + cs.X
absny := y + cs.Y
// We find the closes multiple in case the cursor moves too fast, between FPS reloads,
Expand All @@ -374,15 +374,15 @@ func fixPosition(cs CameraState, x, y float64) (float64, float64) {
var multiple int = 16
// If it's == 0 means it's exact but as we want to center it we remove 16 (towers are 32)
// If it's !=0 then we find what's the remaning for
if int(absnx)%multiple == 0 {
if absnx%multiple == 0 {
x -= 16
} else {
x = float64(utils.ClosestMultiple(int(absnx), multiple)) - 16 - cs.X
x = utils.ClosestMultiple(absnx, multiple) - 16 - cs.X
}
if int(absny)%multiple == 0 {
if absny%multiple == 0 {
y -= 16
} else {
y = float64(utils.ClosestMultiple(int(absny), multiple)) - 16 - cs.Y
y = utils.ClosestMultiple(absny, multiple) - 16 - cs.Y
}

return x, y
Expand Down Expand Up @@ -700,7 +700,7 @@ func (hs *HUDStore) buildUI() {
widget.ButtonOpts.ClickedHandler(func(t *tower.Tower) func(args *widget.ButtonClickedEventArgs) {
return func(args *widget.ButtonClickedEventArgs) {
hst := hs.GetState().(HUDState)
actionDispatcher.SelectTower(t.Type.String(), int(hst.LastCursorPosition.X), int(hst.LastCursorPosition.Y))
actionDispatcher.SelectTower(t.Type.String(), hst.LastCursorPosition.X, hst.LastCursorPosition.Y)
}
}(t)),
)
Expand Down
22 changes: 11 additions & 11 deletions client/game/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (ls *Lines) Update() error {
actionDispatcher.RemoveUnit(u.ID)
continue
}
if n := l.Graph.GetNodeOf(int(u.X), int(u.Y)); n != nil && n.IsDeathZone {
if n := l.Graph.GetNodeOf(u.X, u.Y); n != nil && n.IsDeathZone {
p := ls.game.Store.Players.FindByLineID(u.CurrentLineID)
nlid := ls.game.Store.Map.GetNextLineID(u.CurrentLineID)
actionDispatcher.StealLive(p.ID, u.PlayerID)
Expand Down Expand Up @@ -131,13 +131,13 @@ func (ls *Lines) DrawTower(screen *ebiten.Image, c *CameraStore, t *store.Tower)
return
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(t.X-cs.X, t.Y-cs.Y)
op.GeoM.Translate(float64(t.X-cs.X), float64(t.Y-cs.Y))
op.GeoM.Scale(cs.Zoom, cs.Zoom)
screen.DrawImage(imagesCache.Get(t.FacetKey()), op)

if t.ID == hst.TowerOpenMenuID {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(t.X-cs.X+8, t.Y-cs.Y+8)
op.GeoM.Translate(float64(t.X-cs.X+8), float64(t.Y-cs.Y+8))
op.GeoM.Scale(cs.Zoom, cs.Zoom)
screen.DrawImage(ls.tilesetLogicImage.(*ebiten.Image), op)
}
Expand All @@ -148,28 +148,28 @@ func (ls *Lines) DrawUnit(screen *ebiten.Image, c *CameraStore, u *store.Unit) {
// This is to display the full unit calculated path as a line
// used for testing visually the path
//for _, s := range u.Path {
//screen.Set(s.X-int(cs.X), s.Y-int(cs.Y), color.Black)
//screen.Set(s.X-cs.X, s.Y-cs.Y, color.Black)
//}
if !u.IsColliding(cs.Object) {
return
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y)
op.GeoM.Translate(float64(u.X-cs.X), float64(u.Y-cs.Y))
op.GeoM.Scale(cs.Zoom, cs.Zoom)
sx := directionToTile[u.Facing] * int(u.W)
sx := directionToTile[u.Facing] * u.W
i := (u.MovingCount / 5) % 4
sy := i * int(u.H)
screen.DrawImage(imagesCache.Get(u.SpriteKey()).SubImage(image.Rect(sx, sy, sx+int(u.W), sy+int(u.H))).(*ebiten.Image), op)
sy := i * u.H
screen.DrawImage(imagesCache.Get(u.SpriteKey()).SubImage(image.Rect(sx, sy, sx+u.W, sy+u.H)).(*ebiten.Image), op)

// Only draw the Health bar if the unit has been hit
h := unit.Units[u.Type].Health
if unit.Units[u.Type].Health != u.Health {
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(ls.lifeBarUnder.Bounds().Dy()))
op.GeoM.Translate(float64(u.X-cs.X), float64(u.Y-cs.Y-ls.lifeBarUnder.Bounds().Dy()))
screen.DrawImage(ls.lifeBarUnder.(*ebiten.Image), op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y-float64(ls.lifeBarProgress.Bounds().Dy()))
screen.DrawImage(ls.lifeBarProgress.(*ebiten.Image).SubImage(image.Rect(0, 0, int(float64(ls.lifeBarProgress.Bounds().Dx())*(u.Health/h)), ls.lifeBarProgress.Bounds().Dy())).(*ebiten.Image), op)
op.GeoM.Translate(float64(u.X-cs.X), float64(u.Y-cs.Y-ls.lifeBarProgress.Bounds().Dy()))
screen.DrawImage(ls.lifeBarProgress.(*ebiten.Image).SubImage(image.Rect(0, 0, ls.lifeBarProgress.Bounds().Dx()*int((u.Health/h)), ls.lifeBarProgress.Bounds().Dy())).(*ebiten.Image), op)
}
}
26 changes: 13 additions & 13 deletions client/game/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (m *Map) Draw(screen *ebiten.Image) {
s := m.game.Camera.GetState().(CameraState)
op.GeoM.Scale(s.Zoom, s.Zoom)
inverseZoom := maxZoom - s.Zoom + zoomScale
screen.DrawImage(imagesCache.Get(m.game.Store.Map.GetImageKey()).SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)
screen.DrawImage(imagesCache.Get(m.game.Store.Map.GetImageKey()).SubImage(image.Rect(s.X, s.Y, int(float64((s.X+s.W))*inverseZoom), int(float64((s.Y+s.H))*inverseZoom))).(*ebiten.Image), op)

cs := m.game.Camera.GetState().(CameraState)
cp := m.game.Store.Players.FindCurrent()
Expand All @@ -50,23 +50,23 @@ func (m *Map) Draw(screen *ebiten.Image) {
for i := x - 4; i <= x+(18*16)+3; i++ {
// We draw 3 lines so it's kind of **bold**
// and it's easier to see
screen.Set(int(i-cs.X), int(y-cs.Y-4), cutils.Green)
screen.Set(int(i-cs.X), int(y-cs.Y-3), cutils.Green)
screen.Set(int(i-cs.X), int(y-cs.Y-2), cutils.Green)
screen.Set(i-cs.X, y-cs.Y-4, cutils.Green)
screen.Set(i-cs.X, y-cs.Y-3, cutils.Green)
screen.Set(i-cs.X, y-cs.Y-2, cutils.Green)

screen.Set(int(i-cs.X), int((y+86*16)-cs.Y+3), cutils.Green)
screen.Set(int(i-cs.X), int((y+86*16)-cs.Y+2), cutils.Green)
screen.Set(int(i-cs.X), int((y+86*16)-cs.Y+1), cutils.Green)
screen.Set(i-cs.X, (y+86*16)-cs.Y+3, cutils.Green)
screen.Set(i-cs.X, (y+86*16)-cs.Y+2, cutils.Green)
screen.Set(i-cs.X, (y+86*16)-cs.Y+1, cutils.Green)
}

// Color Left and Right
for i := y - 1; i <= y+(86*16); i++ {
screen.Set(int(x-cs.X-4), int(i-cs.Y), cutils.Green)
screen.Set(int(x-cs.X-3), int(i-cs.Y), cutils.Green)
screen.Set(int(x-cs.X-2), int(i-cs.Y), cutils.Green)
screen.Set(x-cs.X-4, i-cs.Y, cutils.Green)
screen.Set(x-cs.X-3, i-cs.Y, cutils.Green)
screen.Set(x-cs.X-2, i-cs.Y, cutils.Green)

screen.Set(int((x+18*16)-cs.X+3), int(i-cs.Y), cutils.Green)
screen.Set(int((x+18*16)-cs.X+2), int(i-cs.Y), cutils.Green)
screen.Set(int((x+18*16)-cs.X+1), int(i-cs.Y), cutils.Green)
screen.Set((x+18*16)-cs.X+3, i-cs.Y, cutils.Green)
screen.Set((x+18*16)-cs.X+2, i-cs.Y, cutils.Green)
screen.Set((x+18*16)-cs.X+1, i-cs.Y, cutils.Green)
}
}
2 changes: 1 addition & 1 deletion client/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (rs *RouterStore) Draw(screen *ebiten.Image) {

func (rs *RouterStore) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
cs := rs.game.Game.Camera.GetState().(game.CameraState)
if cs.W != float64(outsideWidth) || cs.H != float64(outsideHeight) {
if cs.W != outsideWidth || cs.H != outsideHeight {
actionDispatcher.WindowResizing(outsideWidth, outsideHeight)
}
return outsideWidth, outsideHeight
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion server/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (us *UsersStore) FindByRemoteAddress(ra string) (User, bool) {
for _, u := range us.GetState().(UsersState).Users {
if u.RemoteAddr == ra {
return *u, true
break
}
}
return User{}, false
Expand Down
2 changes: 1 addition & 1 deletion store/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func startGame(t *testing.T, s *store.Store) (store.MapState, store.LinesState)
ls := linesInitialState()
for _, p := range s.Players.List() {
x, y := s.Map.GetHomeCoordinates(p.LineID)
g, err := graph.New(int(x+16), int(y+16), 16, 84, 16, 7, 74, 3)
g, err := graph.New(x+16, y+16, 16, 84, 16, 7, 74, 3)
require.NoError(t, err)
ls.Lines[p.LineID] = &store.Line{
Towers: make(map[string]*store.Tower),
Expand Down
Loading

0 comments on commit 52ab7e5

Please sign in to comment.