Skip to content

Commit

Permalink
client: Made the tower/units cost gold
Browse files Browse the repository at this point in the history
  • Loading branch information
xescugc committed Sep 8, 2023
1 parent 24c79eb commit 9b98717
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
17 changes: 11 additions & 6 deletions client/hud.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewHUDStore(d *flux.Dispatcher, g *Game) (*HUDStore, error) {
func (hs *HUDStore) Update() error {
hst := hs.GetState().(HUDState)
x, y := ebiten.CursorPosition()
cp := hs.game.Players.GetCurrentPlayer()
// Only send a CursorMove when the curso has actually moved
if hst.LastCursorPosition.X != float64(x) || hst.LastCursorPosition.Y != float64(y) {
actionDispatcher.CursorMove(x, y)
Expand All @@ -99,13 +100,12 @@ func (hs *HUDStore) Update() error {
W: 1, H: 1,
}
// Check what the user has just clicked
if hst.CyclopeButton.IsColliding(obj) {
cp := hs.game.Players.GetCurrentPlayer()
if cp.Gold >= unitGold && hst.CyclopeButton.IsColliding(obj) {
//actionDispatcher.SummonUnit("cyclope", cp.ID, cp.LineID, hs.game.Map.GetNextLineID(cp.LineID))
actionDispatcher.SummonUnit("cyclope", cp.ID, cp.LineID, cp.LineID)
return nil
}
if hst.SoldierButton.IsColliding(obj) {
if cp.Gold >= towerGold && hst.SoldierButton.IsColliding(obj) {
actionDispatcher.SelectTower("soldier", x, y)
return nil
}
Expand All @@ -115,7 +115,7 @@ func (hs *HUDStore) Update() error {
actionDispatcher.PlaceTower(hst.SelectedTower.Type, int(hst.SelectedTower.X+cs.X), int(hst.SelectedTower.Y+cs.Y), hst.SelectedTower.LineID)
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyT) {
if cp.Gold >= towerGold && inpututil.IsKeyJustPressed(ebiten.KeyT) {
actionDispatcher.SelectTower("soldier", x, y)
return nil
}
Expand Down Expand Up @@ -157,14 +157,20 @@ func (hs *HUDStore) Update() error {
func (hs *HUDStore) Draw(screen *ebiten.Image) {
hst := hs.GetState().(HUDState)
cs := hs.game.Camera.GetState().(CameraState)
cp := hs.game.Players.GetCurrentPlayer()

op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.CyclopeButton.X, hst.CyclopeButton.Y)
if cp.Gold < unitGold {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
}
screen.DrawImage(hs.cyclopeFacesetImage.(*ebiten.Image), op)

op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(hst.SoldierButton.X, hst.SoldierButton.Y)
if hst.SelectedTower != nil && hst.SelectedTower.Type == "soldier" {
if cp.Gold < towerGold {
op.ColorM.Scale(2, 0.5, 0.5, 0.9)
} else if hst.SelectedTower != nil && hst.SelectedTower.Type == "soldier" {
// Once the tower is selected we gray it out
op.ColorM.Scale(0.5, 0.5, 0.5, 0.5)
}
Expand All @@ -182,7 +188,6 @@ func (hs *HUDStore) Draw(screen *ebiten.Image) {
screen.DrawImage(hst.SelectedTower.Image.(*ebiten.Image), op)
}

cp := hs.game.Players.GetCurrentPlayer()
ps := hs.game.Players.GetState().(PlayersState)
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 Down
3 changes: 3 additions & 0 deletions client/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (ps *PlayersStore) Reduce(state, a interface{}) interface{} {
tp.Lives += 1
case action.SummonUnit:
pstate.Players[act.SummonUnit.PlayerID].Income += unitIncome[act.SummonUnit.Type]
pstate.Players[act.SummonUnit.PlayerID].Gold -= unitGold
case action.IncomeTick:
pstate.IncomeTimer -= 1
if pstate.IncomeTimer == 0 {
Expand All @@ -126,6 +127,8 @@ func (ps *PlayersStore) Reduce(state, a interface{}) interface{} {
p.Gold += p.Income
}
}
case action.PlaceTower:
pstate.Players[act.PlaceTower.LineID].Gold -= towerGold
case action.UnitKilled:
pstate.Players[act.UnitKilled.PlayerID].Gold += unitIncome[act.UnitKilled.UnitType]
default:
Expand Down
1 change: 1 addition & 0 deletions client/towers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
towerImages = make(map[string]image.Image)
towerRange float64 = 16 * 2
towerDamage = 1
towerGold = 10
)

func init() {
Expand Down
5 changes: 4 additions & 1 deletion client/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import (
//go:embed assets/cyclope/Cyclopes.png
var Cyclopes_png []byte

var unitImages = make(map[string]image.Image)
var (
unitImages = make(map[string]image.Image)
unitGold = 10
)

func init() {
ci, _, err := image.Decode(bytes.NewReader(Cyclopes_png))
Expand Down

0 comments on commit 9b98717

Please sign in to comment.