Class: MonkeyMusic::Level
- Inherits:
-
Object
- Object
- MonkeyMusic::Level
- Defined in:
- lib/monkey_music/level.rb
Instance Attribute Summary collapse
-
#boost_cooldown ⇒ Object
Returns the value of attribute boost_cooldown.
-
#height ⇒ Object
Returns the value of attribute height.
-
#players ⇒ Object
readonly
Returns the value of attribute players.
-
#time_limit ⇒ Object
Returns the value of attribute time_limit.
-
#turn_limit ⇒ Object
Returns the value of attribute turn_limit.
-
#units ⇒ Object
readonly
Returns the value of attribute units.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #accessible?(x, y) ⇒ Boolean
- #add(unit, x, y) ⇒ Object
- #at(x, y) ⇒ Object
- #complete? ⇒ Boolean
- #empty?(x, y) ⇒ Boolean
-
#initialize(players, user) ⇒ Level
constructor
A new instance of Level.
- #load_from_file(file) ⇒ Object
- #out_of_bounds?(x, y) ⇒ Boolean
- #remove(unit) ⇒ Object
- #serialize ⇒ Object
- #to_s ⇒ Object
- #tracks ⇒ Object
Constructor Details
#initialize(players, user) ⇒ Level
Returns a new instance of Level.
8 9 10 11 12 13 14 15 |
# File 'lib/monkey_music/level.rb', line 8 def initialize(players, user) @players = players @user = user @width = 0 @height = 0 @boost_cooldown = 0 @units = [] end |
Instance Attribute Details
#boost_cooldown ⇒ Object
Returns the value of attribute boost_cooldown.
5 6 7 |
# File 'lib/monkey_music/level.rb', line 5 def boost_cooldown @boost_cooldown end |
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/monkey_music/level.rb', line 5 def height @height end |
#players ⇒ Object (readonly)
Returns the value of attribute players.
6 7 8 |
# File 'lib/monkey_music/level.rb', line 6 def players @players end |
#time_limit ⇒ Object
Returns the value of attribute time_limit.
5 6 7 |
# File 'lib/monkey_music/level.rb', line 5 def time_limit @time_limit end |
#turn_limit ⇒ Object
Returns the value of attribute turn_limit.
5 6 7 |
# File 'lib/monkey_music/level.rb', line 5 def turn_limit @turn_limit end |
#units ⇒ Object (readonly)
Returns the value of attribute units.
6 7 8 |
# File 'lib/monkey_music/level.rb', line 6 def units @units end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
6 7 8 |
# File 'lib/monkey_music/level.rb', line 6 def user @user end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/monkey_music/level.rb', line 5 def width @width end |
Instance Method Details
#accessible?(x, y) ⇒ Boolean
48 49 50 |
# File 'lib/monkey_music/level.rb', line 48 def accessible?(x, y) not out_of_bounds?(x, y) && empty?(x, y) end |
#add(unit, x, y) ⇒ Object
17 18 19 20 21 |
# File 'lib/monkey_music/level.rb', line 17 def add(unit, x, y) unit.place!(self, x, y) unit.assign_id @units << unit end |
#at(x, y) ⇒ Object
23 24 25 |
# File 'lib/monkey_music/level.rb', line 23 def at(x, y) @units.detect { |u| u.at?(x, y) } end |
#complete? ⇒ Boolean
31 32 33 34 |
# File 'lib/monkey_music/level.rb', line 31 def complete? (@units.detect { |u| u.kind_of? Track }).nil? && (@players.detect { |p| p.monkey..count > 0 }).nil? end |
#empty?(x, y) ⇒ Boolean
27 28 29 |
# File 'lib/monkey_music/level.rb', line 27 def empty?(x, y) at(x, y).nil? end |
#load_from_file(file) ⇒ Object
52 53 54 55 |
# File 'lib/monkey_music/level.rb', line 52 def load_from_file(file) LevelLoader.new(self).instance_eval(IO.read(file)) self end |
#out_of_bounds?(x, y) ⇒ Boolean
44 45 46 |
# File 'lib/monkey_music/level.rb', line 44 def out_of_bounds?(x, y) x < 0 || y < 0 || x > @width-1 || y > @height-1 end |
#remove(unit) ⇒ Object
40 41 42 |
# File 'lib/monkey_music/level.rb', line 40 def remove(unit) @units.reject! { |u| u == unit } end |
#serialize ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/monkey_music/level.rb', line 73 def serialize rows = [] @height.times do |y| row = [] @width.times do |x| unit = at(x, y) row << if unit then unit.serialize else '_' end end rows << row.join(",") end rows.join("\n") end |
#to_s ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/monkey_music/level.rb', line 57 def to_s rows = [] rows << " " + ("=" * @width) @height.times do |y| row = ["|"] @width.times do |x| unit = at(x, y) row << if unit then unit.to_s else ' ' end end row << "|" rows << row.join end rows << " " + ("=" * @width) rows.join("\n") end |
#tracks ⇒ Object
36 37 38 |
# File 'lib/monkey_music/level.rb', line 36 def tracks @units.select {|u| u.kind_of? Track } end |