Class: World

Inherits:
Object
  • Object
show all
Defined in:
lib/rofltim.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(horizon) ⇒ World

Returns a new instance of World.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rofltim.rb', line 160

def initialize horizon
  @ticks = 0
  @horizon = horizon
  @building_generator = BuildingGenerator.new(self, WindowColor.new)
  @background = Background.new(self)
  @player = Player.new(25, @background)
  @buildings = [ @building_generator.build(-10, 30, 120) ]
  @misc = [ Scoreboard.new(self), RoflCopter.new(50, 4, @background) ]
  @speed = 4
  @distance = 0
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



171
172
173
# File 'lib/rofltim.rb', line 171

def background
  @background
end

#buildingsObject (readonly)

Returns the value of attribute buildings.



171
172
173
# File 'lib/rofltim.rb', line 171

def buildings
  @buildings
end

#distanceObject (readonly)

Returns the value of attribute distance.



171
172
173
# File 'lib/rofltim.rb', line 171

def distance
  @distance
end

#horizonObject (readonly)

Returns the value of attribute horizon.



171
172
173
# File 'lib/rofltim.rb', line 171

def horizon
  @horizon
end

#miscObject (readonly)

Returns the value of attribute misc.



171
172
173
# File 'lib/rofltim.rb', line 171

def misc
  @misc
end

#playerObject (readonly)

Returns the value of attribute player.



171
172
173
# File 'lib/rofltim.rb', line 171

def player
  @player
end

#speedObject (readonly)

Returns the value of attribute speed.



171
172
173
# File 'lib/rofltim.rb', line 171

def speed
  @speed
end

#ticksObject (readonly)

Returns the value of attribute ticks.



171
172
173
# File 'lib/rofltim.rb', line 171

def ticks
  @ticks
end

Instance Method Details

#building_under_playerObject



214
215
216
217
218
# File 'lib/rofltim.rb', line 214

def building_under_player
  buildings.detect do |b|
    b.x <= player.x && b.right_x >= player.right_x
  end
end

#tickObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rofltim.rb', line 172

def tick
  # TODO: this, but less often.
  if @ticks % 20 == 0
    @building_generator.generate_if_necessary
    @building_generator.destroy_if_necessary
  end

  @distance += speed

  buildings.each do |b|
    b.move_left speed
  end

  if b = building_under_player
    if player.bottom_y > b.y
      b.move_left(-speed)
      @speed = 0
      @misc << Blood.new(player.x, player.y)
      @misc << GameOverBanner.new
      player.die!
    end
  end

  begin
    if STDIN.read_nonblock(1)
      if player.dead?
        return false
      else
        player.jump
      end
    end
  rescue Errno::EAGAIN
  end

  player.tick

  if b = building_under_player
    player.walk_on_building b if player.bottom_y >= b.y
  end

  @ticks += 1
end