Advertisement
Guest User

Dialogue Tree Parser

a guest
Oct 26th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.56 KB | None | 0 0
  1.  
  2. module DialogTree
  3.   @@conversations = {}
  4.   @@debug = false
  5.   @@eventid = 0
  6.  
  7.   def self.debug=(val)
  8.     @@debug = val
  9.   end
  10.  
  11.   def self.conversations
  12.     @@conversations
  13.   end
  14.  
  15.   def self.run_conversation(id, eventid = 0)
  16.     @@eventid = eventid
  17.     @@conversations[id].execute
  18.   end
  19.  
  20.   class Actor
  21.     attr_accessor :id
  22.     attr_accessor :name
  23.     attr_accessor :picture
  24.    
  25.     def initialize(id, name, picture)
  26.         @id = id
  27.         @name = name
  28.         @picture = picture
  29.     end
  30.    
  31.   end
  32.  
  33.   class DialogNode
  34.     attr_accessor :id
  35.     attr_accessor :mood
  36.     attr_accessor :script
  37.     attr_accessor :children
  38.     attr_accessor :dialog
  39.     attr_accessor :actor
  40.    
  41.     def initialize(id, dialog, script, mood, actor)
  42.       @id = id
  43.       @dialog = dialog
  44.       @script = script
  45.       @mood = mood
  46.       @actor = actor
  47.       @children = []
  48.     end
  49.    
  50.     def execute
  51.       eval(@script)
  52.       if @script != nil && @script != ''
  53.         puts "Running "+@script
  54.       end
  55.       DialogTree::put_message(@dialog, @actor, @mood)
  56.       if(@children.length > 0)
  57.         puts @id.to_s + " -> " + @children[0].id.to_s
  58.         @children[0].execute
  59.       end
  60.     end
  61.    
  62.   end
  63.  
  64.   class DecisionNode
  65.     attr_accessor :id
  66.     attr_accessor :condition
  67.     attr_accessor :children
  68.    
  69.     def initialize(id, condition)
  70.       @id = id
  71.       @condition = condition
  72.       @children = []
  73.     end
  74.    
  75.     def execute
  76.       val = eval(@condition)
  77.       puts @condition+ "==" + val.to_s
  78.       if val
  79.         @children[1].execute
  80.       else
  81.         @children[0].execute
  82.       end
  83.     end
  84.    
  85.   end
  86.  
  87.   class Conversation
  88.     attr_accessor :id
  89.     attr_accessor :title
  90.     attr_accessor :root
  91.  
  92.     def initialize(id, root, title)
  93.       @id = id
  94.       @title = title
  95.       @root = root
  96.     end
  97.    
  98.     def execute
  99.       @root.children[0].execute()
  100.     end
  101.  
  102.   end
  103.    
  104.   def self.load_dialog_file(filename)
  105.     File.open(filename,'r') do|file|
  106.       @@conversations = Marshal.load(file)
  107.     end
  108.   end
  109.  
  110.  
  111.  
  112.   def self.put_message(str, actor, mood)
  113.     strings = str.split("\r\n")
  114.       script_line = -1
  115.       strings.each_with_index { |text, index|
  116.          t = text.strip
  117.          if t == '\\script'
  118.            puts index.to_s
  119.            script_line = index
  120.          end
  121.         }
  122.     puts script_line.to_s
  123.     if @@debug
  124.      
  125.       if script_line > -1
  126.         strings[0..(script_line-1)].each { |text|
  127.           if actor != nil
  128.           puts(actor.name + "("+mood.to_s+")" ": "+text)
  129.         else
  130.           puts(text)
  131.         end
  132.         }
  133.        
  134.         eval(strings[(script_line+1)..strings.length].join("\r\n"))
  135.       else
  136.         strings.each { |text|
  137.           if actor != nil
  138.           puts(actor.name + "("+mood.to_s+")" ": "+text)
  139.         else
  140.           puts(text)
  141.         end
  142.         }
  143.       end
  144.        
  145.      
  146.     else
  147.       if actor != nil && actor.name != ''
  148.         $game_message.face_name = actor.name
  149.       else
  150.         $game_message.face_name = ''
  151.       end  
  152.       if script_line == 0
  153.         eval(strings[(script_line+1)..strings.length].join("\r\n"))
  154.       elsif script_line > -1
  155.         strings[0..(script_line-1)].each { |text|
  156.           $game_message.add(text)
  157.         }
  158.         eval(strings[(script_line+1)..strings.length].join("\r\n"))
  159.       else
  160.         strings.each { |text|
  161.           $game_message.add(text)
  162.         }
  163.       end
  164.       while $game_message.has_text?
  165.         $gi.wait(1)
  166.       end
  167.     end
  168.   end
  169.  
  170.  
  171.  
  172.  
  173. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement