Skip to content

Commit

Permalink
e.g. add an enemy type
Browse files Browse the repository at this point in the history
  • Loading branch information
mskasa committed Aug 27, 2023
1 parent ab3afdd commit 6c5f88a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
5 changes: 4 additions & 1 deletion buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ func isCharHunter(x, y int) bool {
func isCharGhost(x, y int) bool {
return isChar(x, y, chGhost)
}
func isCharAssassin(x, y int) bool {
return isChar(x, y, chAssassin)
}
func isCharEnemy(x, y int) bool {
return isCharHunter(x, y) || isCharGhost(x, y)
return isCharHunter(x, y) || isCharGhost(x, y) || isCharAssassin(x, y)
}
func isCharSpace(x, y int) bool {
return isChar(x, y, chSpace)
Expand Down
12 changes: 12 additions & 0 deletions enemy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type hunter struct {
type ghost struct {
enemy
}
type assassin struct {
enemy
}

type strategy interface {
eval(p *player, x, y int) float64
Expand Down Expand Up @@ -148,6 +151,7 @@ type iEnemyBuilder interface {
movable(func(int, int) bool) iEnemyBuilder
defaultHunter() iEnemyBuilder
defaultGhost() iEnemyBuilder
defaultAssassin() iEnemyBuilder
build() iEnemy
}
type enemyBuilder struct {
Expand All @@ -169,6 +173,8 @@ func (eb *enemyBuilder) displayFormat(r rune, s string) iEnemyBuilder {
eb.color = termbox.ColorRed
case "CYAN":
eb.color = termbox.ColorCyan
case "DARK_GRAY":
eb.color = termbox.ColorDarkGray
}
return eb
}
Expand Down Expand Up @@ -198,6 +204,12 @@ func (eb *enemyBuilder) defaultGhost() iEnemyBuilder {
}
return eb.displayFormat(chGhost, "CYAN").speed(2).movable(fn).strategize(&assault{})
}
func (eb *enemyBuilder) defaultAssassin() iEnemyBuilder {
fn := func(x, y int) bool {
return !isCharWall(x, y) && !isCharEnemy(x, y)
}
return eb.displayFormat(chAssassin, "DARK_GRAY").speed(1).movable(fn).strategize(&assault{})
}

func newEnemyBuilder() iEnemyBuilder {
return &enemyBuilder{}
Expand Down
2 changes: 1 addition & 1 deletion files/stage/map01.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
+ oooo!ooooooooo!oooo +
+ oooooo!!!oooooo +
+ ooooooooooo +
+ oooo ooooooo Hoooo +
+ oooo ooooooo Aoooo +
+ oooo ooo oooo +
+ oooo o oooo +
+++++++++++++++++++++++++++++
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
chPlayer = 'P'
chHunter = 'H'
chGhost = 'G'
chAssassin = 'A'
chApple = 'o'
chSpace = ' '
chPoison = 'X'
Expand Down
32 changes: 20 additions & 12 deletions stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,25 @@ import (
)

type stage struct {
level int
mapPath string
hunterBuilder iEnemyBuilder
ghostBuilder iEnemyBuilder
enemies []iEnemy
gameSpeed time.Duration
width int
height int
level int
mapPath string
hunterBuilder iEnemyBuilder
ghostBuilder iEnemyBuilder
assassinBuilder iEnemyBuilder // The only change is here.
enemies []iEnemy
gameSpeed time.Duration
width int
height int
}

func initStages() []stage {
return []stage{
{
level: 1,
mapPath: "files/stage/map01.txt",
hunterBuilder: newEnemyBuilder().defaultHunter(),
gameSpeed: 1250 * time.Millisecond,
level: 1,
mapPath: "files/stage/map01.txt",
hunterBuilder: newEnemyBuilder().defaultHunter(),
assassinBuilder: newEnemyBuilder().defaultAssassin(), // The only change is here.
gameSpeed: 1250 * time.Millisecond,
},
{
level: 2,
Expand Down Expand Up @@ -121,6 +123,12 @@ func (s *stage) plot(b *buffer, p *player) {
char, color := g.getDisplayFormat()
termbox.SetCell(x, y, char, color, color)
s.enemies = append(s.enemies, g)
} else if isCharAssassin(x, y) {
a := s.assassinBuilder.build()
a.setPosition(x, y)
char, color := a.getDisplayFormat()
termbox.SetCell(x, y, char, color, color)
s.enemies = append(s.enemies, a)
}
}
}
Expand Down

0 comments on commit 6c5f88a

Please sign in to comment.