-
Notifications
You must be signed in to change notification settings - Fork 1
Add player dead state #80
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |||
"candy/game/square" | ||||
"candy/input" | ||||
"candy/pubsub" | ||||
"time" | ||||
) | ||||
|
||||
var _ state = (*standingState)(nil) | ||||
|
@@ -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 { | ||||
|
@@ -44,8 +49,8 @@ func newStandingStateOnSquare( | |||
regionOffset regionOffset, | ||||
character character, | ||||
pubSub *pubsub.PubSub, | ||||
) standingState { | ||||
return standingState{ | ||||
) *standingState { | ||||
return &standingState{ | ||||
&sharedState{ | ||||
dropCandyChecker: dropCandyChecker, | ||||
moveChecker: moveChecker, | ||||
|
@@ -62,6 +67,7 @@ func newStandingStateOnSquare( | |||
availableCandy: character.initialCandyLimit, | ||||
character: character, | ||||
pubSub: pubSub, | ||||
showMarker: true, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
}, | ||||
} | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -24,6 +24,7 @@ type state interface { | |||||
incrementAvailableCandy() | ||||||
increaseCandyLimit(amountIncrease int) | ||||||
isNormal() bool | ||||||
shouldShowMarker() bool | ||||||
} | ||||||
|
||||||
type sharedState struct { | ||||||
|
@@ -33,6 +34,7 @@ type sharedState struct { | |||||
playerHeight int | ||||||
x int | ||||||
y int | ||||||
remainingTime time.Duration | ||||||
dropCandyChecker DropCandyChecker | ||||||
moveChecker MoveChecker | ||||||
regionOffset regionOffset | ||||||
|
@@ -42,10 +44,7 @@ type sharedState struct { | |||||
candyLimit int | ||||||
character character | ||||||
pubSub *pubsub.PubSub | ||||||
} | ||||||
|
||||||
func (s sharedState) update(timeElapsed time.Duration) { | ||||||
return | ||||||
showMarker bool | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
func (s sharedState) isNormal() bool { | ||||||
|
@@ -99,6 +98,10 @@ func (s *sharedState) incrementAvailableCandy() { | |||||
} | ||||||
} | ||||||
|
||||||
func (s *sharedState) shouldShowMarker() bool { | ||||||
return s.showMarker | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
func (s *sharedState) dropCandy() { | ||||||
if s.availableCandy == 0 { | ||||||
return | ||||||
|
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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, | ||||||||
}, | ||||||||
} | ||||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
func (t trappedState) isNormal() bool { | ||||||
return false | ||||||
} | ||||||
|
||||||
func (t *trappedState) handleInput(in input.Input) state { | ||||||
func (t *trappedState) handleInput(_ input.Input) state { | ||||||
return t | ||||||
} | ||||||
|
||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
|
@@ -42,5 +51,6 @@ func newTrapState(state *sharedState) *trappedState { | |||||
sharedState: state, | ||||||
jelly: &jl, | ||||||
prevDirection: prevDirection, | ||||||
trappedLag: 0, | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this in update and create a new field called: shouldShow. Then use it here for guarding