Skip to content

Commit

Permalink
Merge pull request #20 from xescugc/fg-4
Browse files Browse the repository at this point in the history
  • Loading branch information
xescugc committed Sep 7, 2023
2 parents 3edd066 + cf515ba commit 24c79eb
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 129 deletions.
16 changes: 16 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Action struct {
SelectedTowerInvalid *SelectedTowerInvalidPayload `json:"selected_tower_invalid",omitempty"`
TowerAttack *TowerAttackPayload `json:"tower_attack",omitempty`
UnitKilled *UnitKilledPayload `json:"unit_killed",omitempty`
WindowResizing *WindowResizingPayload `json:"window_resizing",omitempty"`
}

type CursorMovePayload struct {
Expand Down Expand Up @@ -184,3 +185,18 @@ func NewUnitKilled(pid int, ut string) *Action {
},
}
}

type WindowResizingPayload struct {
Width int
Height int
}

func NewWindowResizing(w, h int) *Action {
return &Action{
Type: WindowResizing,
WindowResizing: &WindowResizingPayload{
Width: w,
Height: h,
},
}
}
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ const (
IncomeTick
TowerAttack
UnitKilled
WindowResizing
)
12 changes: 8 additions & 4 deletions action/type_string.go

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

6 changes: 6 additions & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,9 @@ func (ac *ActionDispatcher) UnitKilled(pid int, ut string) {
uk := action.NewUnitKilled(pid, ut)
ac.dispatcher.Dispatch(uk)
}

// WindowResizing new sizes of the window
func (ac *ActionDispatcher) WindowResizing(w, h int) {
wr := action.NewWindowResizing(w, h)
ac.dispatcher.Dispatch(wr)
}
36 changes: 23 additions & 13 deletions client/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type CameraState struct {
Zoom float64
}

const (
zoomScale = 0.5
minZoom = 0
maxZoom = 2
)

// 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
Expand All @@ -46,9 +52,14 @@ func NewCameraStore(d *flux.Dispatcher, g *Game, w, h int) *CameraStore {
}

func (cs *CameraStore) Update() error {
if _, wy := ebiten.Wheel(); wy != 0 {
actionDispatcher.CameraZoom(int(wy) * 3)
}
// TODO: https://github.com/xescugc/ltw/issues/4
//s := cs.GetState().(CameraState)
//if _, wy := ebiten.Wheel(); wy != 0 {
//fmt.Println(s.Zoom)
//if s.Zoom+(wy*zoomScale) <= maxZoom && s.Zoom+(wy*zoomScale) > minZoom {
//actionDispatcher.CameraZoom(int(wy))
//}
//}

return nil
}
Expand All @@ -60,7 +71,9 @@ func (cs *CameraStore) Update() error {
func (cs *CameraStore) Draw(screen *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
s := cs.GetState().(CameraState)
screen.DrawImage(cs.game.Map.Image.(*ebiten.Image).SubImage(image.Rect(int(s.X), int(s.Y), int(s.X+s.W+s.Zoom), int(s.Y+s.H+s.Zoom))).(*ebiten.Image), op)
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) Reduce(state, a interface{}) interface{} {
Expand All @@ -76,21 +89,17 @@ func (cs *CameraStore) Reduce(state, a interface{}) interface{} {

switch act.Type {
case action.CursorMove:
cs.GetDispatcher().WaitFor(cs.game.Screen.GetDispatcherToken())

ss := cs.game.Screen

// 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 act.CursorMove.Y >= ss.GetHeight() {
if float64(act.CursorMove.Y) >= cstate.H {
cstate.Y += cs.cameraSpeed
} else if act.CursorMove.Y <= 0 {
cstate.Y -= cs.cameraSpeed
}

if act.CursorMove.X >= ss.GetWidth() {
if float64(act.CursorMove.X) >= cstate.W {
cstate.X += cs.cameraSpeed
} else if act.CursorMove.X <= 0 {
cstate.X -= cs.cameraSpeed
Expand All @@ -110,9 +119,10 @@ func (cs *CameraStore) Reduce(state, a interface{}) interface{} {
cstate.Y = float64(cs.game.Map.GetY())
}
case action.CameraZoom:
cstate.Zoom += float64(act.CameraZoom.Direction)
cstate.W += float64(act.CameraZoom.Direction)
cstate.H += float64(act.CameraZoom.Direction)
cstate.Zoom += float64(act.CameraZoom.Direction) * zoomScale
case action.WindowResizing:
cstate.W = float64(act.WindowResizing.Width)
cstate.H = float64(act.WindowResizing.Height)
}

return cstate
Expand Down
7 changes: 5 additions & 2 deletions client/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
// It holds all the other Stores and the Map
type Game struct {
Camera *CameraStore
Screen *ScreenStore
HUD *HUDStore
Players *PlayersStore
Units *UnitsStore
Expand All @@ -34,5 +33,9 @@ func (g *Game) Draw(screen *ebiten.Image) {
}

func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return g.Screen.GetWidth(), g.Screen.GetHeight()
cs := g.Camera.GetState().(CameraState)
if cs.W != float64(outsideWidth) || cs.H != float64(outsideHeight) {
actionDispatcher.WindowResizing(outsideWidth, outsideHeight)
}
return outsideWidth, outsideHeight
}
23 changes: 13 additions & 10 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ func NewHUDStore(d *flux.Dispatcher, g *Game) (*HUDStore, error) {
cyclopeFacesetImage: ebiten.NewImageFromImage(fi),
tilesetHouseImage: ebiten.NewImageFromImage(thi).SubImage(image.Rect(5*16, 17*16, 5*16+16*2, 17*16+16*2)),
}
cs := g.Camera.GetState().(CameraState)
hs.ReduceStore = flux.NewReduceStore(d, hs.Reduce, HUDState{
CyclopeButton: Object{
X: float64(g.Screen.GetWidth() - hs.cyclopeFacesetImage.Bounds().Dx()),
Y: float64(g.Screen.GetHeight() - hs.cyclopeFacesetImage.Bounds().Dy()),
X: float64(cs.W - float64(hs.cyclopeFacesetImage.Bounds().Dx())),
Y: float64(cs.H - float64(hs.cyclopeFacesetImage.Bounds().Dy())),
W: float64(hs.cyclopeFacesetImage.Bounds().Dx()),
H: float64(hs.cyclopeFacesetImage.Bounds().Dy()),
},
SoldierButton: Object{
X: 0,
Y: float64(g.Screen.GetHeight() - 16*2),
Y: float64(cs.H - 16*2),
W: float64(16 * 2),
H: float64(16 * 2),
},
Expand Down Expand Up @@ -155,6 +156,7 @@ func (hs *HUDStore) Update() error {

func (hs *HUDStore) Draw(screen *ebiten.Image) {
hst := hs.GetState().(HUDState)
cs := hs.game.Camera.GetState().(CameraState)

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.CyclopeButton.X, hst.CyclopeButton.Y)
Expand All @@ -170,7 +172,8 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {

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

if hst.SelectedTower != nil && hst.SelectedTower.Invalid {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
Expand All @@ -181,7 +184,6 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {

cp := hs.game.Players.GetCurrentPlayer()
ps := hs.game.Players.GetState().(PlayersState)
cs := hs.game.Camera.GetState().(CameraState)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Lives: %d, Gold: %d, Income: %d (%ds)", cp.Lives, cp.Gold, cp.Income, ps.IncomeTimer), 0, 0)
ebitenutil.DebugPrintAt(screen, fmt.Sprintf("(X: %d, Y: %d)", int(hst.LastCursorPosition.X+cs.X), int(hst.LastCursorPosition.Y+cs.Y)), 0, 15)
}
Expand All @@ -198,17 +200,18 @@ func (hs *HUDStore) Reduce(state, a interface{}) interface{} {
}

switch act.Type {
case action.CameraZoom:
hs.GetDispatcher().WaitFor(hs.game.Screen.GetDispatcherToken())
case action.WindowResizing:
hs.GetDispatcher().WaitFor(hs.game.Camera.GetDispatcherToken())
cs := hs.game.Camera.GetState().(CameraState)
hstate.CyclopeButton = Object{
X: float64(hs.game.Screen.GetWidth() - hs.cyclopeFacesetImage.Bounds().Dx()),
Y: float64(hs.game.Screen.GetHeight() - hs.cyclopeFacesetImage.Bounds().Dy()),
X: float64(cs.W - float64(hs.cyclopeFacesetImage.Bounds().Dx())),
Y: float64(cs.H - float64(hs.cyclopeFacesetImage.Bounds().Dy())),
W: float64(hs.cyclopeFacesetImage.Bounds().Dx()),
H: float64(hs.cyclopeFacesetImage.Bounds().Dy()),
}
hstate.SoldierButton = Object{
X: 0,
Y: float64(hs.game.Screen.GetHeight() - 16*2),
Y: float64(cs.H - 16*2),
W: float64(16 * 2),
H: float64(16 * 2),
}
Expand Down
2 changes: 1 addition & 1 deletion client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func main() {

ebiten.SetWindowTitle("LTW")
ebiten.SetWindowSize(screenW*2, screenH*2)
ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
dispatcher := flux.NewDispatcher()

actionDispatcher = NewActionDispatcher(dispatcher)
Expand All @@ -47,7 +48,6 @@ func main() {
}

g := &Game{
Screen: NewScreenStore(dispatcher, screenW, screenH),
Players: NewPlayersStore(dispatcher),
Map: m,
}
Expand Down
54 changes: 0 additions & 54 deletions client/screen.go

This file was deleted.

1 change: 1 addition & 0 deletions client/towers.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ func (t *Tower) Draw(screen *ebiten.Image, c *CameraStore) {
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(t.X-cs.X, t.Y-cs.Y)
op.GeoM.Scale(cs.Zoom, cs.Zoom)
screen.DrawImage(t.Image.(*ebiten.Image), op)
}
1 change: 1 addition & 0 deletions client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func (u *Unit) Draw(screen *ebiten.Image, c *CameraStore) {
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(u.X-cs.X, u.Y-cs.Y)
op.GeoM.Scale(cs.Zoom, cs.Zoom)
sx := facingToTile[u.Facing] * int(u.W)
i := (u.MovingCount / 5) % 4
sy := i * int(u.H)
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ go 1.17

require (
github.com/davecgh/go-spew v1.1.0
github.com/hajimehoshi/ebiten/v2 v2.2.5
github.com/hajimehoshi/ebiten/v2 v2.5.9
github.com/xescugc/go-flux v0.0.0-20220312003507-8d5ac35e55d7
)

require (
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect
github.com/jezek/xgb v0.0.0-20210312150743-0e0f116e1240 // indirect
github.com/ebitengine/purego v0.4.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/jezek/xgb v1.1.0 // indirect
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/mobile v0.0.0-20210902104108-5d9a33257ab5 // indirect
golang.org/x/image v0.10.0 // indirect
golang.org/x/mobile v0.0.0-20230301163155-e0f57694e12c // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
)
Loading

0 comments on commit 24c79eb

Please sign in to comment.