Class: MonkeyMusic::Base

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

Direct Known Subclasses

Monkey, Track, User, Wall

Constant Summary collapse

@@curr_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#characterObject

Returns the value of attribute character.



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

def character
  @character
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#assign_idObject



8
9
10
11
# File 'lib/monkey_music/units/base.rb', line 8

def assign_id
  @id = @@curr_id
  @@curr_id += 1
end

#at?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/monkey_music/units/base.rb', line 22

def at?(x, y)
  @x == x && @y == y
end

#direction_of(unit) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/monkey_music/units/base.rb', line 34

def direction_of(unit)
  if (@x - unit.x).abs > (@y - unit.y).abs
    unit.x > @x ? :east : :west
  else
    unit.y > @y ? :south : :north
  end
end

#distance_to(unit) ⇒ Object



26
27
28
# File 'lib/monkey_music/units/base.rb', line 26

def distance_to(unit)
  (@x - unit.x).abs + (@y - unit.y).abs
end

#move!Object



18
19
20
# File 'lib/monkey_music/units/base.rb', line 18

def move!
  # To be overriden
end

#place!(level, x, y) ⇒ Object



13
14
15
16
# File 'lib/monkey_music/units/base.rb', line 13

def place!(level, x, y)
  @level = level
  @x, @y = x, y
end

#posObject



30
31
32
# File 'lib/monkey_music/units/base.rb', line 30

def pos
  [@x, @y]
end

#serializeObject

As shown to player



47
48
49
# File 'lib/monkey_music/units/base.rb', line 47

def serialize
  to_s
end

#to_json(options = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/monkey_music/units/base.rb', line 51

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

#to_sObject



42
43
44
# File 'lib/monkey_music/units/base.rb', line 42

def to_s
  @character
end