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

client: Validate that a tower cannot be placed on top of a unit #142

Merged
merged 1 commit into from
Jan 30, 2024
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
25 changes: 20 additions & 5 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/xescugc/go-flux"
"github.com/xescugc/maze-wars/action"
"github.com/xescugc/maze-wars/store"
"github.com/xescugc/maze-wars/utils"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
Expand All @@ -19,14 +21,16 @@ import (
type ActionDispatcher struct {
dispatcher *flux.Dispatcher
opt Options
store *store.Store
}

// NewActionDispatcher initializes the action dispatcher
// with the give dispatcher
func NewActionDispatcher(d *flux.Dispatcher, opt Options) *ActionDispatcher {
func NewActionDispatcher(d *flux.Dispatcher, s *store.Store, opt Options) *ActionDispatcher {
return &ActionDispatcher{
dispatcher: d,
opt: opt,
store: s,
}
}

Expand Down Expand Up @@ -80,16 +84,27 @@ func (ac *ActionDispatcher) CameraZoom(d int) {

// 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) {
pta := action.NewPlaceTower(t, pid, x, y)
wsSend(pta)
ac.dispatcher.Dispatch(pta)
units := ac.store.Units.List()
to := utils.Object{X: float64(x), Y: float64(y), H: 32, W: 32}
canPlace := true
for _, u := range units {
if u.IsColliding(to) {
canPlace = false
break
}
}
if canPlace {
pta := action.NewPlaceTower(t, pid, x, y)
wsSend(pta)
}
//ac.dispatcher.Dispatch(pta)
}

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

// SelectTower selects the tower 't' on the position x, y
Expand Down
13 changes: 13 additions & 0 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ func (hs *HUDStore) Update() error {
invalid = true
}

if !invalid {
units := hs.game.Store.Units.List()
for _, u := range units {
if u.PlayerID != cp.ID {
continue
}
if u.IsColliding(neo) {
invalid = true
break
}
}
}

// Only check if the line is blocked when is still valid position and it has not moved.
// TODO: We can improve this by storing this result (if blocking or not) so we only validate
// this once and not when the mouse is static with a selected tower
Expand Down
4 changes: 2 additions & 2 deletions client/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ func NewClient() js.Func {
)

d := flux.NewDispatcher()
ad := client.NewActionDispatcher(d, opt)

s := store.NewStore(d)

ad := client.NewActionDispatcher(d, s, opt)

g := &client.Game{
Store: s,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ var (
}

d := flux.NewDispatcher()
ad := client.NewActionDispatcher(d, opt)

s := store.NewStore(d)

ad := client.NewActionDispatcher(d, s, opt)

g := &client.Game{
Store: s,
}
Expand Down
7 changes: 2 additions & 5 deletions integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import (
"github.com/xescugc/maze-wars/store"
)

//"github.com/stretchr/testify/assert"

var (

// The actual one is 4
serverGameTick = time.Second / 2

Expand Down Expand Up @@ -66,10 +63,10 @@ func TestRun(t *testing.T) {
ScreenH: screenH,
}
cd := flux.NewDispatcher()
cad := client.NewActionDispatcher(cd, copt)

s := store.NewStore(cd)

cad := client.NewActionDispatcher(cd, s, copt)

g := &client.Game{
Store: s,
}
Expand Down
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
Loading