class Player

Attributes

moved[R]
pause[RW]
stop[RW]
x[R]
y[R]

Public Class Methods

new(window, coords) click to toggle source
# File lib/ruby-ai/player.rb, line 9
def initialize(window, coords)
  @image = Gosu::Image.new(window, "#{LIB}/ruby-ai/media/tiny-orange.png", false)
  @tiles = window.tiles
  @x = coords[:x]
  @y = coords[:y]
  @moved = false
  @pause = true
  @stop = false
end

Public Instance Methods

look(dir=nil, num=1) click to toggle source
# File lib/ruby-ai/player.rb, line 19
def look(dir=nil, num=1)
  return @tiles.find {|t| t.x == x && t.y == y} unless dir
  at = send(dir, num)
  tile = @tiles.find do |tile|
    tile.x == at[:x] && tile.y == at[:y]
  end
  tile
end
move(dir) click to toggle source
# File lib/ruby-ai/player.rb, line 28
def move(dir)
  if moved? || hit_wall?(dir)
    @stop = true
    return
  end

  to = send(dir)
  @x = to[:x]
  @y = to[:y]
  myputs "Moved #{dir}."
end
stop=(val) click to toggle source
# File lib/ruby-ai/player.rb, line 40
def stop=(val)
  if val
    puts 'Game Over.'
    @stop = true
  end
end

Private Instance Methods

down(num=1) click to toggle source
# File lib/ruby-ai/player.rb, line 53
def down(num=1)
  {:x => @x, :y => @y + num * 10}
end
draw() click to toggle source
# File lib/ruby-ai/player.rb, line 88
def draw
  @image.draw(@x, @y, 1)
end
hit_wall?(dir) click to toggle source
# File lib/ruby-ai/player.rb, line 79
def hit_wall?(dir)
  if look(dir).kind_of?(Wall)
    puts "You moved #{dir} and knocked yourself out against a wall."
    true
  else
    false
  end
end
left(num=1) click to toggle source
# File lib/ruby-ai/player.rb, line 57
def left(num=1)
  {:x => @x - num * 10, :y => @y}
end
moved=(val) click to toggle source
# File lib/ruby-ai/player.rb, line 75
def moved=(val)
  @moved = val
end
moved?() click to toggle source
# File lib/ruby-ai/player.rb, line 65
def moved?
  if @moved
    puts 'Already moved!'
    true
  else
    @moved = true
    false
  end
end
myputs(string) click to toggle source
# File lib/ruby-ai/player.rb, line 92
def myputs(string)
  @count ||= 0
  @count += 1
  puts "#{@count}. " + string
end
right(num=1) click to toggle source
# File lib/ruby-ai/player.rb, line 61
def right(num=1)
  {:x => @x + num * 10, :y => @y}
end
up(num=1) click to toggle source
# File lib/ruby-ai/player.rb, line 49
def up(num=1)
  {:x => @x, :y => @y - num * 10}
end