class RubyMan::Ghost

Ghost that chases player

Public Instance Methods

change_direction(direction) click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 70
def change_direction(direction)
  @direction = direction
  return if @scared
  case direction
  when Direction.left then self.sprite = Sprite['ghost_left']
  when Direction.right then self.sprite = Sprite['ghost_right']
  when Direction.up then self.sprite = Sprite['ghost_up']
  when Direction.down then self.sprite = Sprite['ghost_down']
  end
end
chase_directions() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 39
def chase_directions
  directions = possible_directions
  directions_to_player.each do |dir|
    2.times { directions.push(dir) } if directions.include?(dir)
  end
  directions
end
created() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 7
def created
  self.sprite = Sprite['ghost_left']
  @spawn_x, @spawn_y = @grid_x, @grid_y
  stop_scared
end
die() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 114
def die
  ResourceManager['sfx_eatghost'].play
  @game.score += 500
  @game.add_object(GhostCorpse.new(@grid_x, @grid_y,
                                   @phase, @direction,
                                   @spawn_x, @spawn_y))
  destroy
end
directions_to_player() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 53
def directions_to_player
  return [] unless @game.player
  rel_x = @game.player.grid_x - @grid_x
  rel_y = @game.player.grid_y - @grid_y
  directions = []
  directions.push(Direction.right) if rel_x > 0
  directions.push(Direction.left) if rel_x < 0
  directions.push(Direction.down) if rel_y > 0
  directions.push(Direction.up) if rel_y < 0
  directions
end
grid_step() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 18
def grid_step
  select_direction!
end
possible_directions() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 22
def possible_directions
  directions = []
  Direction.each do |dir|
    next unless dir != @direction.opposite && direction_free(dir)
    directions.push(dir)
  end
  directions
end
scare() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 86
def scare
  @scared = 7.0
  @speed = 1.5
  update_scared(0.0)
end
scared?() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 92
def scared?
  @scared != nil
end
scared_directions() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 31
def scared_directions
  to_pl = directions_to_player
  possible = possible_directions
  away = possible - to_pl
  return possible if away.empty?
  away
end
select_direction!() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 65
def select_direction!
  directions = weighted_directions
  change_direction(directions[rand(directions.size)])
end
set_pos(x, y) click to toggle source
Calls superclass method
# File lib/ruby_man/GameObjects/ghost.rb, line 81
def set_pos(x, y)
  super
  select_direction! if @game
end
stop_scared() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 107
def stop_scared
  @speed = 2.5
  @scared = nil
  # reset sprite according to current direction
  change_direction(@direction)
end
update(delta) click to toggle source
Calls superclass method
# File lib/ruby_man/GameObjects/ghost.rb, line 13
def update(delta)
  super
  update_scared(delta) unless @dead
end
update_scared(delta) click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 96
def update_scared(delta)
  return unless @scared
  @scared -= delta
  return stop_scared if @scared <= 0.0
  if @scared > 2.0
    self.sprite = Sprite['ghost_scared']
  else
    self.sprite = Sprite['ghost_stop_scared']
  end
end
weighted_directions() click to toggle source
# File lib/ruby_man/GameObjects/ghost.rb, line 47
def weighted_directions
  directions = @scared ? scared_directions : chase_directions
  directions.push(Direction.none) if directions.empty?
  directions
end