Class: Core::Game::MapNPC

Inherits:
MapObject show all
Defined in:
lib/game/npc/npc.rb

Overview

Interactive map object

Direct Known Subclasses

Player

Constant Summary

Constants inherited from MapObject

Core::Game::MapObject::DELIM

Instance Attribute Summary collapse

Attributes inherited from MapObject

#dx, #dy, #follow, #name, #properties, #through, #tx, #ty, #x, #y, #z

Instance Method Summary collapse

Methods inherited from MapObject

#dead?, #direction, #do_setup?, #teleport, #to_save, #trigger, #update_move, #update_trigger

Constructor Details

#initialize(x, y, props) ⇒ MapNPC

Returns a new instance of MapNPC.



12
13
14
15
16
17
18
19
20
21
22
# 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

Instance Attribute Details

#behaviourObject

Returns the value of attribute behaviour.



10
11
12
# File 'lib/game/npc/npc.rb', line 10

def behaviour
  @behaviour
end

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/game/npc/npc.rb', line 11

def children
  @children
end

#gamenameObject (readonly)

Returns the value of attribute gamename.



11
12
13
# File 'lib/game/npc/npc.rb', line 11

def gamename
  @gamename
end

#goalObject (readonly)

Returns the value of attribute goal.



11
12
13
# File 'lib/game/npc/npc.rb', line 11

def goal
  @goal
end

Instance Method Details

#destroy_goalObject



90
91
92
# File 'lib/game/npc/npc.rb', line 90

def destroy_goal
  @goal.reset
end

#draw(x, y) ⇒ Object



79
80
81
82
83
# 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) ⇒ Object

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



30
31
32
33
34
35
36
37
38
39
40
41
# 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

#setupObject



24
25
26
27
# File 'lib/game/npc/npc.rb', line 24

def setup
  super
  @behaviour.on_init if @behaviour
end

#talk(text) ⇒ Object

Displays a speech bubble with the given text



86
87
88
# File 'lib/game/npc/npc.rb', line 86

def talk(text)
  @bubble = Bubble.new(text)
end

#updateObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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