Skip to content

Commit

Permalink
Merge pull request #36 from xescugc/fg-23
Browse files Browse the repository at this point in the history
  • Loading branch information
xescugc committed Oct 21, 2023
2 parents cfaf4ab + f674341 commit 5a87942
Show file tree
Hide file tree
Showing 34 changed files with 3,776 additions and 156 deletions.
39 changes: 39 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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"`

AddPlayer *AddPlayerPayload `json:"add_player, omitempty"`
RemovePlayer *RemovePlayerPayload `json:"remove_player, omitempty"`
Expand Down Expand Up @@ -265,6 +268,41 @@ func NewRemovePlayer(r, id string) *Action {
}
}

type PlayerReadyPayload struct {
ID string
}

func NewPlayerReady(id string) *Action {
return &Action{
Type: PlayerReady,
PlayerReady: &PlayerReadyPayload{
ID: id,
},
}
}

type NavigateToPayload struct {
Route string
}

func NewNavigateTo(route string) *Action {
return &Action{
Type: NavigateTo,
NavigateTo: &NavigateToPayload{
Route: route,
},
}
}

type StartGamePayload struct{}

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

type UpdateStatePayload struct {
Players *UpdateStatePlayersPayload
Towers *UpdateStateTowersPayload
Expand All @@ -285,6 +323,7 @@ type UpdateStatePlayerPayload struct {
Gold int
Current bool
Winner bool
Ready bool
}

type UpdateStateTowersPayload struct {
Expand Down
3 changes: 3 additions & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const (
TowerAttack
UnitKilled
WindowResizing
PlayerReady
NavigateTo
StartGame

// Specific to WS
JoinRoom
Expand Down
52 changes: 32 additions & 20 deletions action/type_string.go

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

Binary file added assets/NoButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/NormalFont.ttf
Binary file not shown.
Binary file added assets/YesButton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 23 additions & 2 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@ var TilesetHouse_png []byte
//go:embed cyclope/Cyclopes.png
var Cyclopes_png []byte

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

//go:embed maps/3.png
var Map_3_png []byte

//go:embed maps/4.png
var Map_4_png []byte

//go:embed maps/5.png
var Map_5_png []byte

//go:embed maps/6.png
var Map_6_png []byte

//go:embed YesButton.png
var YesButton_png []byte

//go:embed NoButton.png
var NoButton_png []byte

//go:embed towers.json
var Towers_json []byte

//go:embed units.json
var Units_json []byte

//go:embed NormalFont.ttf
var NormalFont_ttf []byte
36 changes: 0 additions & 36 deletions assets/maps/1.json

This file was deleted.

Binary file removed assets/maps/1.png
Binary file not shown.
File renamed without changes
Binary file added assets/maps/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/maps/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/maps/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/maps/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ 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)
}

// WindowResizing new sizes of the window
func (ac *ActionDispatcher) WindowResizing(w, h int) {
wr := action.NewWindowResizing(w, h)
Expand All @@ -123,3 +130,16 @@ func (ac *ActionDispatcher) JoinRoom(room, name string) {
wsSend(jr)
ac.dispatcher.Dispatch(jr)
}

// NavigateTo navigates to the given route
func (ac *ActionDispatcher) NavigateTo(route string) {
nt := action.NewNavigateTo(route)
ac.dispatcher.Dispatch(nt)
}

// StartGame notifies that the game will start,
// used to update any store before that
func (ac *ActionDispatcher) StartGame() {
sg := action.NewStartGame()
ac.dispatcher.Dispatch(sg)
}
29 changes: 9 additions & 20 deletions client/camera.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package main

import (
"image"

"github.com/hajimehoshi/ebiten/v2"
"github.com/xescugc/go-flux"
"github.com/xescugc/ltw/action"
"github.com/xescugc/ltw/store"
"github.com/xescugc/ltw/utils"
)

Expand All @@ -15,7 +14,7 @@ import (
type CameraStore struct {
*flux.ReduceStore

game *Game
Map *store.Map

cameraSpeed float64
}
Expand All @@ -35,9 +34,9 @@ const (
// NewCameraStore creates a new CameraState linked to the Dispatcher d
// with the Game g and with width w and height h which is the size of
// the viewport
func NewCameraStore(d *flux.Dispatcher, g *Game, w, h int) *CameraStore {
func NewCameraStore(d *flux.Dispatcher, m *store.Map, w, h int) *CameraStore {
cs := &CameraStore{
game: g,
Map: m,
cameraSpeed: 10,
}

Expand Down Expand Up @@ -65,17 +64,7 @@ func (cs *CameraStore) Update() error {
return nil
}

// 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 -+
func (cs *CameraStore) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
s := cs.GetState().(CameraState)
op.GeoM.Scale(s.Zoom, s.Zoom)
inverseZoom := maxZoom - s.Zoom + zoomScale
screen.DrawImage(cs.game.Map.Image.(*ebiten.Image).SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)
}
func (cs *CameraStore) Draw(screen *ebiten.Image) {}

func (cs *CameraStore) Reduce(state, a interface{}) interface{} {
act, ok := a.(*action.Action)
Expand Down Expand Up @@ -111,13 +100,13 @@ 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.game.Map.GetX()) {
cstate.X = float64(cs.game.Map.GetX())
} else if cstate.X >= float64(cs.Map.GetX()) {
cstate.X = float64(cs.Map.GetX())
}
if cstate.Y <= 0 {
cstate.Y = 0
} else if cstate.Y >= float64(cs.game.Map.GetY()) {
cstate.Y = float64(cs.game.Map.GetY())
} else if cstate.Y >= float64(cs.Map.GetY()) {
cstate.Y = float64(cs.Map.GetY())
}
case action.CameraZoom:
cstate.Zoom += float64(act.CameraZoom.Direction) * zoomScale
Expand Down
7 changes: 7 additions & 0 deletions client/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "image/color"

var (
green = color.RGBA{0x00, 0x80, 0x00, 0xff}
)
28 changes: 16 additions & 12 deletions client/game.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"image"

"github.com/hajimehoshi/ebiten/v2"
"github.com/xescugc/ltw/store"
)
Expand All @@ -11,11 +13,11 @@ import (
type Game struct {
Store *store.Store

Camera *CameraStore
HUD *HUDStore
Players *Players
Units *Units
Towers *Towers
Camera *CameraStore
HUD *HUDStore

Units *Units
Towers *Towers

Map *store.Map

Expand All @@ -35,12 +37,14 @@ func (g *Game) Draw(screen *ebiten.Image) {
g.HUD.Draw(screen)
g.Units.Draw(screen)
g.Towers.Draw(screen)
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
cs := g.Camera.GetState().(CameraState)
if cs.W != float64(outsideWidth) || cs.H != float64(outsideHeight) {
actionDispatcher.WindowResizing(outsideWidth, outsideHeight)
}
return outsideWidth, outsideHeight
// 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 -+
op := &ebiten.DrawImageOptions{}
s := g.Camera.GetState().(CameraState)
op.GeoM.Scale(s.Zoom, s.Zoom)
inverseZoom := maxZoom - s.Zoom + zoomScale
screen.DrawImage(g.Map.GetState().(store.MapState).Image.(*ebiten.Image).SubImage(image.Rect(int(s.X), int(s.Y), int((s.X+s.W)*inverseZoom), int((s.Y+s.H)*inverseZoom))).(*ebiten.Image), op)
}
Loading

0 comments on commit 5a87942

Please sign in to comment.