class Core::Game::MapNPC

Interactive map object

Attributes

behaviour[RW]
children[R]
gamename[R]
goal[R]

Public Class Methods

new(x, y, props) click to toggle source
Calls superclass method Core::Game::MapObject::new
# File lib/game/npc/npc.rb, line 12
def initialize(x, y, props)
  super(x, y, props)
  @x, @y = x, y
  @behaviour = nil
  @goal = CompositeGoal.new(:retry)
  @speed = 2
  @name = "npc-#{props[:file].downcase}-#{rand(1000)}"
  @gamename = props[:name] ? props[:name] : @name
  @children = []
  @bubble = nil
end

Public Instance Methods

destroy_goal() click to toggle source
# File lib/game/npc/npc.rb, line 90
def destroy_goal
  @goal.reset
end
draw(x, y) click to toggle source
Calls superclass method Core::Game::MapObject#draw
# File lib/game/npc/npc.rb, line 79
def draw(x, y)
  super
  @bubble.draw(@x+x, @y+y) if @bubble
  @children.each { |child| child.draw(x, y) }
end
find_path_to(x, y) click to toggle source

Generates a path from the current position to the given screen coordinates by using A*

# File lib/game/npc/npc.rb, line 30
def find_path_to(x, y)
  #puts("from #{@x/32}|#{@y/32} to #{x}|#{y}")
  curr = Core.window.state.map.current.astar(AStar::Node.new(@x/32, @y/32), AStar::Node.new(x, y))
  @goal.reset
  while curr and curr.parent
    @goal.push(MotionGoal.new((curr.x - curr.parent.x) * 32, (curr.y - curr.parent.y) * 32, x, y))
    curr = curr.parent
  end
  @goal.reverse!
  @goal.start
  return @goal
end
setup() click to toggle source
Calls superclass method Core::Game::MapObject#setup
# File lib/game/npc/npc.rb, line 24
def setup
  super
  @behaviour.on_init if @behaviour
end
talk(text) click to toggle source

Displays a speech bubble with the given text

# File lib/game/npc/npc.rb, line 86
def talk(text)
  @bubble = Bubble.new(text)
end
update() click to toggle source
# File lib/game/npc/npc.rb, line 43
def update
  @children.each { |child|
    if child.follow
      child.x = @x + child.xoff
      child.y = @y + child.yoff
    end
    child.update
  }
  @behaviour.on_update if @behaviour
  moved = update_move(Core.window.state.map.current)
  @goal.update if @goal.size > 0
  if @goal.current.class == MotionGoal
    if @goal.current.state == :recalc
      @dx = @dy = 0
      find_path_to(@goal.last.x, @goal.last.y)
      @goal.update
    end
    if @dx == 0 and @dy == 0
      if @goal.current.state == :before
        @dx, @dy = @goal.current.dx, @goal.current.dy
        @goal.current.state = :progress
      elsif @goal.current.state == :progress
        if moved
          @goal.advance
        else
          @goal.current.state = :failed
        end
      end
    end
  end
  if @bubble
    @bubble.update
    @bubble = nil if @bubble.remove?
  end
end