Class: Pacman::Arcade::Ghost

Inherits:
Object
  • Object
show all
Defined in:
lib/pacman/arcade/ghost.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spawn:, brain:, direction: Direction.up) ⇒ Ghost

Returns a new instance of Ghost.



8
9
10
11
12
13
14
# File 'lib/pacman/arcade/ghost.rb', line 8

def initialize(spawn:, brain:, direction: Direction.up)
  @spawn = spawn
  @brain = brain
  @direction = direction
  @position = spawn.start
  @state = :normal
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



6
7
8
# File 'lib/pacman/arcade/ghost.rb', line 6

def direction
  @direction
end

#positionObject (readonly)

Returns the value of attribute position.



6
7
8
# File 'lib/pacman/arcade/ghost.rb', line 6

def position
  @position
end

Instance Method Details

#advance(world) ⇒ Object



16
17
18
19
20
# File 'lib/pacman/arcade/ghost.rb', line 16

def advance(world)
  @direction = choose_direction(world)
  @position = world.maze.wrap(position.step(@direction))
  revive if eaten? && position == spawn.start
end

#calmObject



26
27
28
# File 'lib/pacman/arcade/ghost.rb', line 26

def calm
  @state = :normal unless eaten?
end

#devourObject



30
31
32
# File 'lib/pacman/arcade/ghost.rb', line 30

def devour
  @state = :eaten
end

#eaten?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pacman/arcade/ghost.rb', line 38

def eaten?
  @state == :eaten
end

#edible?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pacman/arcade/ghost.rb', line 34

def edible?
  @state == :frightened
end

#frightenObject



22
23
24
# File 'lib/pacman/arcade/ghost.rb', line 22

def frighten
  @state = :frightened unless eaten?
end

#respawnObject



42
43
44
45
46
# File 'lib/pacman/arcade/ghost.rb', line 42

def respawn
  @position = spawn.start
  @direction = Direction.up
  @state = :normal
end