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

all: Refactored the game interface to use 'ebitenui' #101

Merged
merged 1 commit into from
Jan 10, 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
41 changes: 16 additions & 25 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type Action struct {
TowerAttack *TowerAttackPayload `json:"tower_attack,omitempty"`
UnitKilled *UnitKilledPayload `json:"unit_killed,omitempty"`
WindowResizing *WindowResizingPayload `json:"window_resizing,omitempty"`
PlayerReady *PlayerReadyPayload `json:"player_ready, omitempty"`
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"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu, omitempty"`
Expand Down Expand Up @@ -284,28 +284,13 @@ func NewAddPlayer(id, name string, lid int) *Action {
}

type RemovePlayerPayload struct {
ID string
Room string
ID string
}

func NewRemovePlayer(r, id string) *Action {
func NewRemovePlayer(id string) *Action {
return &Action{
Type: RemovePlayer,
RemovePlayer: &RemovePlayerPayload{
ID: id,
Room: r,
},
}
}

type PlayerReadyPayload struct {
ID string
}

func NewPlayerReady(id string) *Action {
return &Action{
Type: PlayerReady,
PlayerReady: &PlayerReadyPayload{
ID: id,
},
}
Expand All @@ -325,15 +310,12 @@ func NewNavigateTo(route string) *Action {
}

type StartGamePayload struct {
Room string
}

func NewStartGame(r string) *Action {
func NewStartGame() *Action {
return &Action{
Type: StartGame,
StartGame: &StartGamePayload{
Room: r,
},
Type: StartGame,
StartGame: &StartGamePayload{},
}
}

Expand Down Expand Up @@ -381,6 +363,16 @@ func NewCheckedPath(cp bool) *Action {
}
}

type ToggleStatsPayload struct {
}

func NewToggleStats() *Action {
return &Action{
Type: ToggleStats,
ToggleStats: &ToggleStatsPayload{},
}
}

type SignUpErrorPayload struct {
Error string
}
Expand Down Expand Up @@ -500,7 +492,6 @@ type UpdateStatePlayerPayload struct {
Gold int
Current bool
Winner bool
Ready bool
}

type UpdateStateTowersPayload struct {
Expand Down
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
UserSignOut
JoinWaitingRoom
ExitWaitingRoom
ToggleStats

// Specific to WS
AddPlayer
Expand Down
58 changes: 31 additions & 27 deletions action/type_string.go

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

Binary file removed assets/TilesetElement.png
Binary file not shown.
3 changes: 0 additions & 3 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ var TilesetHouse_png []byte
//go:embed TilesetLogic.png
var TilesetLogic_png []byte

//go:embed TilesetElement.png
var TilesetElement_png []byte

//go:embed maps/2.png
var Map_2_png []byte

Expand Down
24 changes: 11 additions & 13 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ func (ac *ActionDispatcher) UnitKilled(pid, ut string) {
ac.dispatcher.Dispatch(uk)
}

// PlayerReady marks the player as ready to start the game
func (ac *ActionDispatcher) PlayerReady(pid string) {
pr := action.NewPlayerReady(pid)
wsSend(pr)
ac.dispatcher.Dispatch(pr)
func (ac *ActionDispatcher) RemovePlayer(pid string) {
rpa := action.NewRemovePlayer(pid)
wsSend(rpa)
ac.dispatcher.Dispatch(rpa)
ac.dispatcher.Dispatch(action.NewNavigateTo(LobbyRoute))
}

// WindowResizing new sizes of the window
Expand All @@ -149,14 +149,6 @@ func (ac *ActionDispatcher) NavigateTo(route string) {
ac.dispatcher.Dispatch(nt)
}

// StartGame notifies that the game will start,
// used to update any store before that
func (ac *ActionDispatcher) StartGame(r string) {
sg := action.NewStartGame(r)
wsSend(sg)
ac.dispatcher.Dispatch(sg)
}

// OpenTowerMenu when a tower is clicked and the menu of
// the tower is displayed
func (ac *ActionDispatcher) OpenTowerMenu(tid string) {
Expand Down Expand Up @@ -256,3 +248,9 @@ func (ac *ActionDispatcher) ExitWaitingRoom(un string) {

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

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

ac.dispatcher.Dispatch(tsa)
}
29 changes: 12 additions & 17 deletions client/game.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package client

import (
"image"

"github.com/hajimehoshi/ebiten/v2"
"github.com/xescugc/maze-wars/action"
"github.com/xescugc/maze-wars/store"
)

Expand All @@ -19,35 +18,31 @@ type Game struct {
Units *Units
Towers *Towers

SessionID string
Map *Map
}

func (g *Game) Update() error {
g.Map.Update()
g.Camera.Update()
g.HUD.Update()
g.Units.Update()
g.Towers.Update()

if len(g.Store.Players.List()) == 0 {
actionDispatcher.Dispatch(action.NewAddPlayer("1", "test1", 0))
actionDispatcher.Dispatch(action.NewAddPlayer("2", "test2", 1))
actionDispatcher.Dispatch(action.NewAddPlayer("3", "test3", 2))
actionDispatcher.Dispatch(action.NewAddPlayer("4", "test4", 3))
actionDispatcher.Dispatch(action.NewAddPlayer("5", "test5", 4))
actionDispatcher.Dispatch(action.NewAddPlayer("6", "test6", 5))
}
actionDispatcher.TPS()

return nil
}

func (g *Game) Draw(screen *ebiten.Image) {

// Draw will draw just a partial image of the map based on the viewport, so it does not render everything but just the
// part that it's seen by the user
// If we want to render everything and just move the viewport around we need o render the full image and change the
// opt.GeoM.Transport to the Map.X/Y and change the Update function to do the opposite in terms of -+
//
// TODO: Maybe create a self Map entity with Update/Draw
op := &ebiten.DrawImageOptions{}
s := g.Camera.GetState().(CameraState)
op.GeoM.Scale(s.Zoom, s.Zoom)
inverseZoom := maxZoom - s.Zoom + zoomScale
mi := ebiten.NewImageFromImage(g.Store.Map.GetState().(store.MapState).Image)
screen.DrawImage(mi.SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)

g.Map.Draw(screen)
g.Camera.Draw(screen)
g.HUD.Draw(screen)
g.Units.Draw(screen)
Expand Down
Loading
Loading