Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Add player dead state #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions game/player/dead.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package player

import (
"candy/game/direction"
"candy/graphics"
"candy/input"
"time"
)

var _ state = (*deadState)(nil)
var TombstoneImageDuration = (3 * time.Second).Nanoseconds()

type deadState struct {
*sharedState
tombstone *Tombstone
lag int64
}

func (d *deadState) handleInput(in input.Input) state {
return d
}

func (d deadState) draw(batch graphics.Batch) {
if d.lag <= TombstoneImageDuration {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if d.lag <= TombstoneImageDuration {
if d.lag <= TombstoneImageDuration {

Let's put this in update and create a new field called: shouldShow. Then use it here for guarding

d.tombstone.draw(batch, d.x+d.playerWidth/2-d.tombstone.width/2, d.y, d.y)
}
}

func (d *deadState) update(timeElapsed time.Duration) state {
d.lag += timeElapsed.Nanoseconds()
return d
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just override shouldShowMarker function and return false.


func newDeadState(state *sharedState) *deadState {
ts := newTombstone()
state.direction = direction.Down
return &deadState{
sharedState: state,
tombstone: &ts,
lag: 0,
}
}
8 changes: 5 additions & 3 deletions game/player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (p Player) Draw(batch graphics.Batch) {
// TODO: player.state.getHeight() should rename to getDepth()
// TODO: square.Width need to be replaced with new getHeight()
markerY := p.state.getY() + square.Width + marker.YOffset
p.marker.Draw(batch, markerX, markerY, p.state.getY()+jellyZOffset-1)
if p.state.shouldShowMarker() {
p.marker.Draw(batch, markerX, markerY, p.state.getY()+jellyZOffset-1)
}
}
}

Expand All @@ -58,8 +60,8 @@ func (p *Player) HandleInput(in input.Input) {
p.state = p.state.handleInput(in)
}

func (p Player) Update(timeElapsed time.Duration) {
p.state.update(timeElapsed)
func (p *Player) Update(timeElapsed time.Duration) {
p.state = p.state.update(timeElapsed)
if p.marker != nil {
p.marker.Update(timeElapsed)
}
Expand Down
12 changes: 9 additions & 3 deletions game/player/standing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"candy/game/square"
"candy/input"
"candy/pubsub"
"time"
)

var _ state = (*standingState)(nil)
Expand All @@ -13,7 +14,11 @@ type standingState struct {
*sharedState
}

func (s standingState) handleInput(in input.Input) state {
func (s *standingState) update(timeElapsed time.Duration) state {
return s
}

func (s *standingState) handleInput(in input.Input) state {
switch in.Action {
case input.Press:
switch in.Device {
Expand Down Expand Up @@ -44,8 +49,8 @@ func newStandingStateOnSquare(
regionOffset regionOffset,
character character,
pubSub *pubsub.PubSub,
) standingState {
return standingState{
) *standingState {
return &standingState{
&sharedState{
dropCandyChecker: dropCandyChecker,
moveChecker: moveChecker,
Expand All @@ -62,6 +67,7 @@ func newStandingStateOnSquare(
availableCandy: character.initialCandyLimit,
character: character,
pubSub: pubSub,
showMarker: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
showMarker: true,

},
}
}
Expand Down
13 changes: 8 additions & 5 deletions game/player/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

type state interface {
handleInput(in input.Input) state
update(timeElapsed time.Duration)
update(timeElapsed time.Duration) state
draw(batch graphics.Batch)
trapped() state
getX() int
Expand All @@ -24,6 +24,7 @@ type state interface {
incrementAvailableCandy()
increaseCandyLimit(amountIncrease int)
isNormal() bool
shouldShowMarker() bool
}

type sharedState struct {
Expand All @@ -33,6 +34,7 @@ type sharedState struct {
playerHeight int
x int
y int
remainingTime time.Duration
dropCandyChecker DropCandyChecker
moveChecker MoveChecker
regionOffset regionOffset
Expand All @@ -42,10 +44,7 @@ type sharedState struct {
candyLimit int
character character
pubSub *pubsub.PubSub
}

func (s sharedState) update(timeElapsed time.Duration) {
return
showMarker bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
showMarker bool

}

func (s sharedState) isNormal() bool {
Expand Down Expand Up @@ -99,6 +98,10 @@ func (s *sharedState) incrementAvailableCandy() {
}
}

func (s *sharedState) shouldShowMarker() bool {
return s.showMarker
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return s.showMarker
return true

}

func (s *sharedState) dropCandy() {
if s.availableCandy == 0 {
return
Expand Down
32 changes: 32 additions & 0 deletions game/player/tombstone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package player

import (
"candy/graphics"
)

type Tombstone struct {
lag int64
width int
imageBound graphics.Bound
}

func (ts Tombstone) getWidth() int {
return ts.width
}
Comment on lines +13 to +15
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (ts Tombstone) getWidth() int {
return ts.width
}

We can directly access width in the same package

func (ts Tombstone) draw(batch graphics.Batch, x int, y int, z int) {
bound := ts.imageBound
batch.DrawSprite(x, y, z, bound, 1)
}

func newTombstone() Tombstone {
return Tombstone{
lag: 0,
width: 60,
imageBound: graphics.Bound{
X: 960,
Y: 933,
Width: 54,
Height: 77,
},
}
}
16 changes: 13 additions & 3 deletions game/player/trapped.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (
"candy/input"
)

var trapTimeOut = (5 * time.Second).Nanoseconds()

var _ state = (*trappedState)(nil)

type trappedState struct {
*sharedState
jelly *Jelly
prevDirection direction.Direction
trappedLag int64
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trappedLag int64
lag int64

}

func (t trappedState) isNormal() bool {
return false
}

func (t *trappedState) handleInput(in input.Input) state {
func (t *trappedState) handleInput(_ input.Input) state {
return t
}

Expand All @@ -29,9 +32,15 @@ func (t trappedState) draw(batch graphics.Batch) {
t.jelly.draw(batch, t.x+t.playerWidth/2-t.jelly.width/2, t.y, t.y+jellyZOffset)
}

func (t trappedState) update(timeElapsed time.Duration) {
t.sharedState.update(timeElapsed)
func (t *trappedState) update(timeElapsed time.Duration) state {
t.trappedLag += timeElapsed.Nanoseconds()
if t.trappedLag >= trapTimeOut {
t.showMarker = false
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean you already entered dead state. Dead state will take care of the marker.

return newDeadState(t.sharedState)
}

t.jelly.update(timeElapsed)
return t
}

func newTrapState(state *sharedState) *trappedState {
Expand All @@ -42,5 +51,6 @@ func newTrapState(state *sharedState) *trappedState {
sharedState: state,
jelly: &jl,
prevDirection: prevDirection,
trappedLag: 0,
}
}
3 changes: 2 additions & 1 deletion game/player/walking.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ type walkingState struct {
lag int64
}

func (w *walkingState) update(timeElapsed time.Duration) {
func (w *walkingState) update(timeElapsed time.Duration) state {
w.lag += timeElapsed.Nanoseconds()
steps := int(w.lag / nanoPerStep)
w.sharedState.currStep = nextStep(w.sharedState.currStep, steps)

w.lag %= nanoPerStep
return w
}

func (w walkingState) handleInput(in input.Input) state {
Expand Down
2 changes: 1 addition & 1 deletion screen/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func NewGame(
pubSub.Subscribe(pubsub.OnCandyStartExploding, func(payload interface{}) {
playerID := payload.(int)
gm.incrementAvailableCandy(playerID)

})

return &gm
}