Class: MonkeyMusic::Monkey

Inherits:
Base
  • Object
show all
Defined in:
lib/monkey_music/units/monkey.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#id, #level, #x, #y

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assign_id, #at?, #direction_of, #distance_to, #place!, #pos, #to_s

Constructor Details

#initializeMonkey

Returns a new instance of Monkey.



16
17
18
19
20
21
22
# File 'lib/monkey_music/units/monkey.rb', line 16

def initialize
  @score = 0
  @capacity = 1
  @carrying = []
  @delivered = []
  @facing = [:west, :east].sample
end

Instance Attribute Details

#capacityObject

Returns the value of attribute capacity.



3
4
5
# File 'lib/monkey_music/units/monkey.rb', line 3

def capacity
  @capacity
end

#carryingObject (readonly)

Returns the value of attribute carrying.



4
5
6
# File 'lib/monkey_music/units/monkey.rb', line 4

def carrying
  @carrying
end

#characterObject

Returns the value of attribute character.



3
4
5
# File 'lib/monkey_music/units/monkey.rb', line 3

def character
  @character
end

#facingObject (readonly)

Returns the value of attribute facing.



4
5
6
# File 'lib/monkey_music/units/monkey.rb', line 4

def facing
  @facing
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/monkey_music/units/monkey.rb', line 3

def name
  @name
end

#scoreObject

Returns the value of attribute score.



3
4
5
# File 'lib/monkey_music/units/monkey.rb', line 3

def score
  @score
end

Class Method Details

.from_players(players) ⇒ Object



10
11
12
13
14
# File 'lib/monkey_music/units/monkey.rb', line 10

def self.from_players(players)
  @player_index &&
    players[@player_index] &&
    players[@player_index].monkey
end

.player(number) ⇒ Object



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

def self.player(number)
  Class.new Monkey do @player_index = number - 1 end
end

Instance Method Details

#deliver!Object



55
56
57
58
59
# File 'lib/monkey_music/units/monkey.rb', line 55

def deliver!
  @delivered += @carrying
  @score += tally(@carrying)
  @carrying = []
end

#interact_with!(unit) ⇒ Object



41
42
43
44
45
46
# File 'lib/monkey_music/units/monkey.rb', line 41

def interact_with!(unit)
  case unit
  when Track then pick_up!(unit)
  when User then deliver!
  end
end

#move!(direction) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/monkey_music/units/monkey.rb', line 24

def move!(direction)
  target = translate(@x, @y, direction)
  if target_unit = @level.at(*target)
    # Interact with unit
    interact_with!(target_unit)
  else
    # Face the right direction
    @facing = case direction
      when :west then :west
      when :east then :east
      else @facing
    end
    # Perform move
    @x, @y = *target if @level.accessible?(*target)
  end
end

#pick_up!(unit) ⇒ Object



48
49
50
51
52
53
# File 'lib/monkey_music/units/monkey.rb', line 48

def pick_up!(unit)
  if @carrying.count < @capacity
    @level.remove(unit)
    @carrying << unit
  end
end

#remaining_capacityObject



69
70
71
# File 'lib/monkey_music/units/monkey.rb', line 69

def remaining_capacity
  (@capacity - carrying.count) || 0
end

#serializeObject



73
74
75
# File 'lib/monkey_music/units/monkey.rb', line 73

def serialize
  "M#{@id}"
end

#tally(tracks) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/monkey_music/units/monkey.rb', line 61

def tally(tracks)
  score = 0
  tracks.each do |track|
    score += track.value
  end
  score
end

#to_json(options = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/monkey_music/units/monkey.rb', line 81

def to_json(options = {})
  { :id => @id,
    :x => @x,
    :y => @y,
    :facing => @facing,
    :type => self.class.name.split('::').last,
    :name => @name,
    :score => @score,
  }.to_json
end