Skip to content

Commit

Permalink
client/hud: Added a Home button
Browse files Browse the repository at this point in the history
Which will oprt you back to your line
  • Loading branch information
xescugc committed Oct 26, 2023
1 parent 6872ef3 commit fb2af9c
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ Line Tower Wars

Each line 18t wide, 16t of those are usable and the other 2t are the left and right corners

The spawn area is 7tx16t and the end area is 16tx3t the building area is 16tx74t
The spawn area is 7tx16t and the end area is 3tx16t the building area is 74tx16t

The separation between maps (if they are side by side) is of 10t
The separation between maps (if they are side by side) is of 10t and the vertical separation is also of 10t
13 changes: 11 additions & 2 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Action struct {
PlayerReady *PlayerReadyPayload `json:"player_ready, omitempty"`
NavigateTo *NavigateToPayload `json:"navigate_to, omitempty"`
StartGame *StartGamePayload `json:"start_game, omitempty"`
GoHome *GoHomePayload `json:"go_home, omitempty"`

OpenTowerMenu *OpenTowerMenuPayload `json:"open_tower_menu, omitempty"`
CloseTowerMenu *CloseTowerMenuPayload `json:"close_tower_menu, omitempty"`
Expand Down Expand Up @@ -337,8 +338,7 @@ func NewOpenTowerMenu(tid string) *Action {
}
}

type CloseTowerMenuPayload struct {
}
type CloseTowerMenuPayload struct{}

func NewCloseTowerMenu() *Action {
return &Action{
Expand All @@ -347,6 +347,15 @@ func NewCloseTowerMenu() *Action {
}
}

type GoHomePayload struct{}

func NewGoHome() *Action {
return &Action{
Type: GoHome,
GoHome: &GoHomePayload{},
}
}

type UpdateStatePayload struct {
Players *UpdateStatePlayersPayload
Towers *UpdateStateTowersPayload
Expand Down
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
StartGame
OpenTowerMenu
CloseTowerMenu
GoHome

// Specific to WS
JoinRoom
Expand Down
44 changes: 24 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/TilesetElement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var TilesetHouse_png []byte
//go:embed TilesetLogic.png
var TilesetLogic_png []byte

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

//go:embed cyclope/Cyclopes.png
var Cyclopes_png []byte

Expand Down
6 changes: 6 additions & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,9 @@ func (ac *ActionDispatcher) CloseTowerMenu() {
ctm := action.NewCloseTowerMenu()
ac.dispatcher.Dispatch(ctm)
}

// GoHome will move the camera to the current player home line
func (ac *ActionDispatcher) GoHome() {
gha := action.NewGoHome()
ac.dispatcher.Dispatch(gha)
}
17 changes: 10 additions & 7 deletions client/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type CameraStore struct {
*flux.ReduceStore

Map *store.Map
Store *store.Store

cameraSpeed float64
}
Expand All @@ -34,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, m *store.Map, w, h int) *CameraStore {
func NewCameraStore(d *flux.Dispatcher, s *store.Store, w, h int) *CameraStore {
cs := &CameraStore{
Map: m,
Store: s,
cameraSpeed: 10,
}

Expand Down Expand Up @@ -100,19 +100,22 @@ 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.Map.GetX()) {
cstate.X = float64(cs.Map.GetX())
} else if cstate.X >= float64(cs.Store.Map.GetX()) {
cstate.X = float64(cs.Store.Map.GetX())
}
if cstate.Y <= 0 {
cstate.Y = 0
} else if cstate.Y >= float64(cs.Map.GetY()) {
cstate.Y = float64(cs.Map.GetY())
} else if cstate.Y >= float64(cs.Store.Map.GetY()) {
cstate.Y = float64(cs.Store.Map.GetY())
}
case action.CameraZoom:
cstate.Zoom += float64(act.CameraZoom.Direction) * zoomScale
case action.WindowResizing:
cstate.W = float64(act.WindowResizing.Width)
cstate.H = float64(act.WindowResizing.Height)
case action.GoHome:
cp := cs.Store.Players.GetCurrentPlayer()
cstate.X, cstate.Y = cs.Store.Map.GetHomeCoordinates(cp.LineID)
}

return cstate
Expand Down
28 changes: 28 additions & 0 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ type HUDStore struct {

cyclopeFacesetImage image.Image
tilesetHouseImage image.Image
houseIcon image.Image
}

// HUDState stores the HUD state
type HUDState struct {
CyclopeButton utils.Object
SoldierButton utils.Object
HouseButton utils.Object

SelectedTower *SelectedTower
TowerOpenMenuID string
Expand All @@ -59,11 +61,17 @@ func NewHUDStore(d *flux.Dispatcher, g *Game) (*HUDStore, error) {
return nil, err
}

hi, _, err := image.Decode(bytes.NewReader(assets.TilesetElement_png))
if err != nil {
return nil, err
}

hs := &HUDStore{
game: g,

cyclopeFacesetImage: ebiten.NewImageFromImage(fi),
tilesetHouseImage: ebiten.NewImageFromImage(thi).SubImage(image.Rect(5*16, 17*16, 5*16+16*2, 17*16+16*2)),
houseIcon: ebiten.NewImageFromImage(hi).SubImage(image.Rect(12*16, 0*16, 12*16+16, 0*16+16)),
}
cs := g.Camera.GetState().(CameraState)
hs.ReduceStore = flux.NewReduceStore(d, hs.Reduce, HUDState{
Expand All @@ -79,6 +87,12 @@ func NewHUDStore(d *flux.Dispatcher, g *Game) (*HUDStore, error) {
W: float64(16 * 2),
H: float64(16 * 2),
},
HouseButton: utils.Object{
X: float64(cs.W - 16),
Y: 0,
W: float64(16),
H: float64(16),
},
})

return hs, nil
Expand Down Expand Up @@ -120,6 +134,10 @@ func (hs *HUDStore) Update() error {
actionDispatcher.SelectTower(tower.Soldier.String(), x, y)
return nil
}
if hst.HouseButton.IsColliding(click) {
actionDispatcher.GoHome()
return nil
}

if hst.SelectedTower != nil && !hst.SelectedTower.Invalid {
// We double check that placing the tower would not block the path
Expand Down Expand Up @@ -277,6 +295,10 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
}
screen.DrawImage(hs.tilesetHouseImage.(*ebiten.Image), op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.HouseButton.X, hst.HouseButton.Y)
screen.DrawImage(hs.houseIcon.(*ebiten.Image), op)

if hst.SelectedTower != nil {
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.SelectedTower.X/cs.Zoom, hst.SelectedTower.Y/cs.Zoom)
Expand Down Expand Up @@ -333,6 +355,12 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
W: float64(16 * 2),
H: float64(16 * 2),
}
hstate.HouseButton = utils.Object{
X: float64(cs.W - 16),
Y: 0,
W: float64(16),
H: float64(16),
}
case action.SelectTower:
hs.GetDispatcher().WaitFor(hs.game.Store.Players.GetDispatcherToken())
cp := hs.game.Store.Players.GetCurrentPlayer()
Expand Down
2 changes: 1 addition & 1 deletion client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func main() {
}

// TODO: Change this to pass the specific store needed instead of all the game object
cs := NewCameraStore(dispatcher, m, screenW, screenH)
cs := NewCameraStore(dispatcher, s, screenW, screenH)
g.Camera = cs
g.Units = NewUnits(g)
g.Towers, err = NewTowers(g)
Expand Down
3 changes: 1 addition & 2 deletions client/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func NewRouterStore(d *flux.Dispatcher, g *Game, l *Lobby) *RouterStore {
}

rs.ReduceStore = flux.NewReduceStore(d, rs.Reduce, RouterState{
//Route: LobbyRoute,
Route: GameRoute,
Route: LobbyRoute,
})

return rs
Expand Down
16 changes: 12 additions & 4 deletions store/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,21 @@ func (m *Map) GetRandomSpawnCoordinatesForLineID(lid int) (float64, float64) {
yy := (p%7)*16 + 16
xx := ((p%16)*16 + 16) + (lid * 16 * (16 + 1 + 10 + 1))

//p := rand.Intn(15 * 16 * 6 * 16)
//yy := (p % (6 * 16)) + 16
//xx := (p % (15 * 16)) + 16

return float64(xx), float64(yy)
}

func (m *Map) GetHomeCoordinates(lid int) (float64, float64) {
var y float64
if lid > 3 {
// 86 is the total length of the map including borders
// and 10 is the vertical separation between the maps
y = 86 + 10
lid = -3
}

return float64(lid * 16 * (16 + 1 + 10 + 1)), y
}

func (m *Map) EndZone(lid int) utils.Object {
return utils.Object{
X: float64(16 + (lid * 16 * (16 + 1 + 10 + 1))),
Expand Down

0 comments on commit fb2af9c

Please sign in to comment.