class MonkeyMusic::Base

Attributes

character[RW]
id[R]
level[R]
x[RW]
y[RW]

Public Instance Methods

assign_id() click to toggle source
# File lib/monkey_music/units/base.rb, line 8
def assign_id
  @id = @@curr_id
  @@curr_id += 1
end
at?(x, y) click to toggle source
# File lib/monkey_music/units/base.rb, line 22
def at?(x, y)
  @x == x && @y == y
end
direction_of(unit) click to toggle source
# 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) click to toggle source
# File lib/monkey_music/units/base.rb, line 26
def distance_to(unit)
  (@x - unit.x).abs + (@y - unit.y).abs
end
move!() click to toggle source
# File lib/monkey_music/units/base.rb, line 18
def move!
  # To be overriden
end
place!(level, x, y) click to toggle source
# File lib/monkey_music/units/base.rb, line 13
def place!(level, x, y)
  @level = level
  @x, @y = x, y
end
pos() click to toggle source
# File lib/monkey_music/units/base.rb, line 30
def pos
  [@x, @y]
end
serialize() click to toggle source

As shown to player

# File lib/monkey_music/units/base.rb, line 47
def serialize
  to_s
end
to_json(options = {}) click to toggle source
# 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_s() click to toggle source
# File lib/monkey_music/units/base.rb, line 42
def to_s
  @character
end

Private Instance Methods

translate(x, y, direction) click to toggle source
# File lib/monkey_music/units/base.rb, line 61
def translate(x, y, direction)
  case direction
  when :north then [x,     y - 1]
  when :south then [x,     y + 1]
  when :east  then [x + 1, y    ]
  when :west  then [x - 1, y    ]
  end
end