Class: Pacman::Game

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/pacman/controller.rb

Overview

controller

Instance Method Summary collapse

Constructor Details

#initialize(rel_path = '') ⇒ Game

Returns a new instance of Game.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#button_down(key) ⇒ Object



47
48
49
50
51
52
53
54
55
# 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) ⇒ Object

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



43
44
45
# File 'lib/pacman/controller.rb', line 43

def button_up(key)
  close if key == Gosu::KbEscape
end

#drawObject



57
58
59
# File 'lib/pacman/controller.rb', line 57

def draw
  @renderer.render(@level, @state)
end

#eat_power_pelletObject



114
115
116
# File 'lib/pacman/controller.rb', line 114

def eat_power_pellet
  @level.ghosts_frozen = 10 # frozen for x steps
end

#load_levelObject



31
32
33
34
35
36
37
38
39
# 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

#updateObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_deltaObject



118
119
120
121
122
123
124
125
# 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_playerObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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