class Pacman::Game

controller

Public Class Methods

new(rel_path = '') click to toggle source
Calls superclass method
# File lib/pacman/controller.rb, line 11
def initialize(rel_path = '')
  super(1280, 800, false)
  @last_milliseconds = 0

  self.caption = 'PacMan'
  @level = 1

  @renderer = LevelRenderer.new(self, rel_path)

  # affects pacman and creatures speed
  @timer_default = 20
  @rel_path = rel_path

  load_level

  @ghosts_controller = GhostsController.new(@level)

  @state = :running
end

Public Instance Methods

button_down(key) click to toggle source
# File lib/pacman/controller.rb, line 47
def button_down(key)
  case @state
  when :running
    @level.player.direction = :left if (key == Gosu::KbLeft)
    @level.player.direction = :right if (key == Gosu::KbRight)
    @level.player.direction = :up if (key == Gosu::KbUp)
    @level.player.direction = :down if (key == Gosu::KbDown)
  end
end
button_up(key) click to toggle source

this is a callback for key up events or equivalent (there are constants for gamepad buttons and mouse clicks)

# File lib/pacman/controller.rb, line 43
def button_up(key)
  close if key == Gosu::KbEscape
end
draw() click to toggle source
# File lib/pacman/controller.rb, line 57
def draw
  @renderer.render(@level, @state)
end
eat_power_pellet() click to toggle source
# File lib/pacman/controller.rb, line 114
def eat_power_pellet
  @level.ghosts_frozen = 10 # frozen for x steps
end
load_level() click to toggle source
# File lib/pacman/controller.rb, line 31
def load_level
  file = File.open(@rel_path + "levels/level#{@level}.lvl", 'r')
  dsl = file.read
  file.close

  @level = LevelBuilder.build(dsl)

  @timer = @timer_default
end
update() click to toggle source
# File lib/pacman/controller.rb, line 61
def update
  update_delta
  # with a delta we need to express the speed of our entities in
  # terms of pixels/second

  case @state
  when :running
    @timer -= 1

    if (@timer % 10 == 0)
      # update animation
      @level.player.animation_index = (@level.player.animation_index + 1)
    end

    if (@timer == 0)
      update_player
      @ghosts_controller.update
      @state = :win if @ghosts_controller.colission
      @timer = @timer_default
    end
  end
end
update_delta() click to toggle source
# File lib/pacman/controller.rb, line 118
def update_delta
  # Gosu::millisecodns returns the time since the game_window was created
  # Divide by 1000 since we want to work in seconds
  current_time = Gosu.milliseconds / 1000.0
  # clamping here is important to avoid strange behaviors
  @delta = [current_time - @last_milliseconds, 0.25].min
  @last_milliseconds = current_time
end
update_player() click to toggle source
# File lib/pacman/controller.rb, line 84
def update_player
  player = @level.player
  case @level.player.direction
  when :left
    next_obj = @level.get(player.x - 1, player.y)
    player.x -= 1 if player.x > 0 && (next_obj.nil? || next_obj.passable?)
  when :right
    next_obj = @level.get(player.x + 1, player.y)
    player.x += 1 if player.x < @level.width - 1 && (next_obj.nil? ||
        next_obj.passable?)
  when :up
    next_obj = @level.get(player.x, player.y - 1)
    player.y -= 1 if player.y > 0 && (next_obj.nil? || next_obj.passable?)
  when :down
    if player.y + 1 < @level.height - 1
      next_obj = @level.get(player.x, player.y + 1)
      player.y += 1 if next_obj.nil? || next_obj.passable?
    end
  end

  return unless next_obj != 0 && next_obj.class.ancestors.include?(Item)

  @level.pellets_left -= 1 if next_obj.instance_of?(Pellet)
  @state = :win if @level.pellets_left == 0

  eat_power_pellet if next_obj.instance_of?(PowerPellet)

  @level[player.y][player.x] = nil
end