Class: MonkeyMusic::Level

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_cooldownObject

Returns the value of attribute boost_cooldown.



5
6
7
# File 'lib/monkey_music/level.rb', line 5

def boost_cooldown
  @boost_cooldown
end

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/monkey_music/level.rb', line 5

def height
  @height
end

#playersObject (readonly)

Returns the value of attribute players.



6
7
8
# File 'lib/monkey_music/level.rb', line 6

def players
  @players
end

#time_limitObject

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_limitObject

Returns the value of attribute turn_limit.



5
6
7
# File 'lib/monkey_music/level.rb', line 5

def turn_limit
  @turn_limit
end

#unitsObject (readonly)

Returns the value of attribute units.



6
7
8
# File 'lib/monkey_music/level.rb', line 6

def units
  @units
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/monkey_music/level.rb', line 6

def user
  @user
end

#widthObject

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

Returns:

  • (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

Returns:

  • (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.carrying.count > 0 }).nil?
end

#empty?(x, y) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

#serializeObject



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_sObject



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

#tracksObject



36
37
38
# File 'lib/monkey_music/level.rb', line 36

def tracks
  @units.select {|u| u.kind_of? Track }
end