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

Commit 2ab5fd4

Browse files
yijia-ccYijia Chenmagicoder10
authored
Support speed game item automatically (#57)
* Support speed game item automatically * Support using speed game item * Fix dependency issue Co-authored-by: Yijia Chen <[email protected]> Co-authored-by: Harry Liu <[email protected]>
1 parent 69a9678 commit 2ab5fd4

File tree

9 files changed

+56
-24
lines changed

9 files changed

+56
-24
lines changed

game/gameitem/gameitem.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ func (p Power) GetType() Type {
8888
var _ GameItem = (*Speed)(nil)
8989

9090
type Speed struct {
91+
pubSub *pubsub.PubSub
9192
}
9293

9394
func (p Speed) Use() {
95+
p.pubSub.Publish(pubsub.IncreasePlayerSpeed, 2)
9496
}
9597

9698
func (p Speed) GetType() Type {
@@ -141,7 +143,9 @@ func WithPubSub(itemType Type, pubSub *pubsub.PubSub) GameItem {
141143
pubSub: pubSub,
142144
}
143145
case SpeedType:
144-
return Speed{}
146+
return Speed{
147+
pubSub: pubSub,
148+
}
145149
case CandyType:
146150
return Candy{}
147151
case FirstAidKitType:

game/player/character.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,77 @@ package player
33
type character struct {
44
walkCycleOffset walkCycleOffset
55
initialPower int
6+
initialStepSize int
67
}
78

89
var BlackBoy = character{
910
walkCycleOffset: walkCycleOffset{
1011
x: 0,
1112
y: spriteColHeight,
1213
},
13-
initialPower: 1,
14+
initialStepSize: 4,
15+
initialPower: 1,
1416
}
17+
1518
var BlackGirl = character{
1619
walkCycleOffset: walkCycleOffset{
1720
x: 0,
1821
y: 0,
1922
},
20-
initialPower: 1,
23+
initialStepSize: 4,
24+
initialPower: 1,
2125
}
26+
2227
var BrownBoy = character{
2328
walkCycleOffset: walkCycleOffset{
2429
x: spriteRowWidth,
2530
y: spriteColHeight,
26-
}}
31+
},
32+
initialStepSize: 6,
33+
initialPower: 1,
34+
}
35+
2736
var BrownGirl = character{
2837
walkCycleOffset: walkCycleOffset{
2938
x: spriteRowWidth,
3039
y: 0,
3140
},
32-
initialPower: 1,
41+
initialStepSize: 6,
42+
initialPower: 1,
3343
}
44+
3445
var YellowBoy = character{
3546
walkCycleOffset: walkCycleOffset{
3647
x: spriteRowWidth * 2,
3748
y: spriteColHeight,
3849
},
39-
initialPower: 1,
50+
initialStepSize: 6,
51+
initialPower: 1,
4052
}
53+
4154
var YellowGirl = character{
4255
walkCycleOffset: walkCycleOffset{
4356
x: spriteRowWidth * 2,
4457
y: 0,
4558
},
46-
initialPower: 1,
59+
initialStepSize: 6,
60+
initialPower: 1,
4761
}
62+
4863
var OrangeBoy = character{
4964
walkCycleOffset: walkCycleOffset{
5065
x: spriteRowWidth * 3,
5166
y: spriteColHeight,
5267
},
53-
initialPower: 1,
68+
initialStepSize: 6,
69+
initialPower: 1,
5470
}
71+
5572
var OrangeGirl = character{
5673
walkCycleOffset: walkCycleOffset{
5774
x: spriteRowWidth * 3,
5875
y: 0,
5976
},
60-
initialPower: 1,
77+
initialStepSize: 6,
78+
initialPower: 1,
6179
}

game/player/player.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ func (p *Player) IncreasePowerLevel(amountIncrease int) {
9898
p.state.increasePowerLevel(amountIncrease)
9999
}
100100

101+
func (p *Player) IncreaseStepSize(amountIncrease int) {
102+
p.state.increaseStepSize(amountIncrease)
103+
}
104+
101105
func NewPlayer(
102106
moveChecker MoveChecker,
103107
character character,

game/player/standing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func newStandingStateOnSquare(
5555
y: gridY + row*square.Width,
5656
regionOffset: regionOffset,
5757
powerLevel: character.initialPower,
58+
stepSize: character.initialStepSize,
5859
character: character,
5960
pubSub: pubSub,
6061
},

game/player/state.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type state interface {
2020
getWidth() int
2121
getHeight() int
2222
increasePowerLevel(amountIncrease int)
23+
increaseStepSize(amountIncrease int)
2324
isNormal() bool
2425
}
2526

@@ -33,6 +34,7 @@ type sharedState struct {
3334
moveChecker MoveChecker
3435
regionOffset regionOffset
3536
powerLevel int
37+
stepSize int
3638
character character
3739
pubSub *pubsub.PubSub
3840
}
@@ -77,6 +79,10 @@ func (s *sharedState) increasePowerLevel(amountIncrease int) {
7779
s.powerLevel += amountIncrease
7880
}
7981

82+
func (s *sharedState) increaseStepSize(amountIncrease int) {
83+
s.stepSize += amountIncrease
84+
}
85+
8086
func (s sharedState) dropCandy() {
8187
s.pubSub.Publish(pubsub.OnDropCandy, pubsub.OnDropCandyPayload{
8288
X: s.x,

game/player/walking.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"time"
55

66
"candy/game/direction"
7-
"candy/game/square"
87
"candy/input"
98
"candy/pubsub"
109
)
@@ -17,8 +16,7 @@ var _ state = (*walkingState)(nil)
1716

1817
type walkingState struct {
1918
*sharedState
20-
stepSize int
21-
lag int64
19+
lag int64
2220
}
2321

2422
func (w *walkingState) update(timeElapsed time.Duration) {
@@ -102,7 +100,6 @@ func newWalkingState(shared *sharedState, lag int64, direction direction.Directi
102100
return &walkingState{
103101
sharedState: shared,
104102
lag: lag,
105-
stepSize: square.Width / 10,
106103
}
107104
}
108105

graphics/window.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@ import (
44
"time"
55

66
"candy/input"
7-
87
"github.com/hajimehoshi/ebiten/v2"
98
"github.com/hajimehoshi/ebiten/v2/inpututil"
109
)
1110

12-
type Window interface {
13-
IsClosed() bool
14-
PollEvents() []input.Input
15-
Redraw()
16-
}
17-
1811
type WindowConfig struct {
1912
Width int
2013
Height int
@@ -92,9 +85,9 @@ func (e EbitenWindow) pollEvents() []input.Input {
9285
Device: input.DownArrowKey,
9386
})
9487
}
95-
if ebiten.IsKeyPressed(ebiten.KeyR) {
88+
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
9689
inputs = append(inputs, input.Input{
97-
Action: input.Press,
90+
Action: input.SinglePress,
9891
Device: input.RKey,
9992
})
10093
}

pubsub/topics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
OnCandyExploding topic = iota
77
OnPlayerWalking
88
IncreasePlayerPower
9+
IncreasePlayerSpeed
910
OnDropCandy
1011
)
1112

screen/game.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package screen
22

33
import (
4-
"candy/game/candy"
54
"time"
65

76
"candy/assets"
87
"candy/game"
8+
"candy/game/candy"
99
"candy/game/cell"
1010
"candy/game/gameitem"
1111
"candy/game/gamemap"
@@ -57,7 +57,7 @@ func (g Game) HandleInput(in input.Input) {
5757
case input.RKey:
5858
g.gameMap.HideItems()
5959
}
60-
case input.Press:
60+
case input.SinglePress:
6161
switch in.Device {
6262
case input.RKey:
6363
g.gameMap.RevealItems()
@@ -117,6 +117,10 @@ func (g Game) increasePlayerPower(amountIncrease int) {
117117
g.players[g.currPlayerIndex].IncreasePowerLevel(amountIncrease)
118118
}
119119

120+
func (g Game) increaseStepSize(amountIncrease int) {
121+
g.players[g.currPlayerIndex].IncreaseStepSize(amountIncrease)
122+
}
123+
120124
func NewGame(
121125
logger *observability.Logger,
122126
assets assets.Assets, g graphics.Graphics,
@@ -167,5 +171,9 @@ func NewGame(
167171
powerLevel := payload.(int)
168172
gm.increasePlayerPower(powerLevel)
169173
})
174+
pubSub.Subscribe(pubsub.IncreasePlayerSpeed, func(payload interface{}) {
175+
stepSizeDelta := payload.(int)
176+
gm.increaseStepSize(stepSizeDelta)
177+
})
170178
return &gm
171179
}

0 commit comments

Comments
 (0)