Class: Pacman::Arcade::Ghost
- Inherits:
-
Object
- Object
- Pacman::Arcade::Ghost
- Defined in:
- lib/pacman/arcade/ghost.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #advance(world) ⇒ Object
- #calm ⇒ Object
- #devour ⇒ Object
- #eaten? ⇒ Boolean
- #edible? ⇒ Boolean
- #frighten ⇒ Object
-
#initialize(spawn:, brain:, direction: Direction.up) ⇒ Ghost
constructor
A new instance of Ghost.
- #respawn ⇒ Object
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
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
6 7 8 |
# File 'lib/pacman/arcade/ghost.rb', line 6 def direction @direction end |
#position ⇒ Object (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 |
#calm ⇒ Object
26 27 28 |
# File 'lib/pacman/arcade/ghost.rb', line 26 def calm @state = :normal unless eaten? end |
#devour ⇒ Object
30 31 32 |
# File 'lib/pacman/arcade/ghost.rb', line 30 def devour @state = :eaten end |
#eaten? ⇒ Boolean
38 39 40 |
# File 'lib/pacman/arcade/ghost.rb', line 38 def eaten? @state == :eaten end |
#edible? ⇒ Boolean
34 35 36 |
# File 'lib/pacman/arcade/ghost.rb', line 34 def edible? @state == :frightened end |
#frighten ⇒ Object
22 23 24 |
# File 'lib/pacman/arcade/ghost.rb', line 22 def frighten @state = :frightened unless eaten? end |