Class: Window

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/zombie-chaser/ui.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWindow

Returns a new instance of Window.



102
103
104
105
106
107
108
109
110
# File 'lib/zombie-chaser/ui.rb', line 102

def initialize
  super(self.class.width, self.class.height, false)

  self.caption = 'Zombie-chaser'
  self.grid = 1

  @grass     = Gosu::Image.new(self, File.join(File.dirname(__FILE__),'tiles/grass.png'), true)
  @shrubbery = Gosu::Image.new(self, File.join(File.dirname(__FILE__),'tiles/shrubbery.png'), true)
end

Instance Attribute Details

#gridObject

Returns the value of attribute grid.



99
100
101
# File 'lib/zombie-chaser/ui.rb', line 99

def grid
  @grid
end

#human=(value) ⇒ Object (writeonly)

Sets the attribute human

Parameters:

  • value

    the value to set the attribute human to.



100
101
102
# File 'lib/zombie-chaser/ui.rb', line 100

def human=(value)
  @human = value
end

#zombie_list=(value) ⇒ Object (writeonly)

Sets the attribute zombie_list

Parameters:

  • value

    the value to set the attribute zombie_list to.



100
101
102
# File 'lib/zombie-chaser/ui.rb', line 100

def zombie_list=(value)
  @zombie_list = value
end

Class Method Details

.heightObject



97
# File 'lib/zombie-chaser/ui.rb', line 97

def self.height; @height end

.height=(height) ⇒ Object



96
# File 'lib/zombie-chaser/ui.rb', line 96

def self.height=(height); @height = height end

.widthObject



94
# File 'lib/zombie-chaser/ui.rb', line 94

def self.width; @width end

.width=(width) ⇒ Object



93
# File 'lib/zombie-chaser/ui.rb', line 93

def self.width=(width); @width = width end

Instance Method Details

#button_down(id) ⇒ Object



118
119
120
# File 'lib/zombie-chaser/ui.rb', line 118

def button_down(id)
  close if id == Gosu::Button::KbEscape
end

#drawObject



112
113
114
115
116
# File 'lib/zombie-chaser/ui.rb', line 112

def draw
  draw_scenery
  draw_human
  draw_zombies
end

#draw_humanObject



152
153
154
# File 'lib/zombie-chaser/ui.rb', line 152

def draw_human
  @human.draw if defined?(@human)
end

#draw_sceneryObject



144
145
146
147
148
149
150
# File 'lib/zombie-chaser/ui.rb', line 144

def draw_scenery
  map.each do |row|
    row.each do |col|
      col[:tile].draw(col[:x], col[:y], ZIndex.for(:world))
    end
  end
end

#draw_zombiesObject



156
157
158
# File 'lib/zombie-chaser/ui.rb', line 156

def draw_zombies
  @zombie_list.draw_zombies if defined?(@zombie_list)
end

#mapObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/zombie-chaser/ui.rb', line 132

def map
  @map ||= tile_positions[:y].map do |y|
    tile_positions[:x].map do |x|
      {
        :x => x,
        :y => y,
        :tile => (rand(32) % 32 == 0) ? @shrubbery : @grass
      }
    end
  end
end

#no_living_zombies_apart_from_me?(desired_step_count, actor) ⇒ Boolean

No longer a major issue now zombies approach from all directions in GUI mode

Returns:

  • (Boolean)


161
162
163
# File 'lib/zombie-chaser/ui.rb', line 161

def no_living_zombies_apart_from_me?(desired_step_count, actor)
  true
end

#tile_positionsObject

private



124
125
126
127
128
129
130
# File 'lib/zombie-chaser/ui.rb', line 124

def tile_positions
  w, h = @grass.width, @grass.height
  @tile_positions ||= {
    :x => (0...width).to_a.inject([]) {|a,x| a << x if x % w == 0; a},
    :y => (0...height).to_a.inject([]) {|a,y| a << y if y % h == 0; a}
  }
end