Advertisement
Zeriab

(WIP) Combo script

Jan 19th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.57 KB | None | 0 0
  1. class Key_Combo
  2.   def initialize
  3.     @timeline = {}
  4.     @running = []
  5.     @actions = []
  6.     @ticks = 0
  7.     @total_ticks = 0
  8.   end
  9.  
  10.   ##
  11.   # Put an action into the timeline
  12.   #
  13.   def put(action, start)
  14.     @timeline[start] = [] if @timeline[start].nil?
  15.     @timeline[start] << action
  16.     @actions << action
  17.     # Update the total ticks if necessary
  18.     total_ticks = start + action.total_ticks
  19.     if total_ticks > @total_ticks
  20.       @total_ticks = total_ticks
  21.     end
  22.   end
  23.  
  24.   ##
  25.   # Resets the timeline and all the actions
  26.   #
  27.   def reset
  28.     @ticks = 0
  29.     @running.clear
  30.     for action in @actions
  31.       action.reset
  32.     end
  33.   end
  34.  
  35.   ##
  36.   # Updates the combo
  37.   #
  38.   # Returns
  39.   # nil - Not completed yet
  40.   # false - The action failed
  41.   # true - The action succeeded
  42.   #
  43.   def update
  44.     # Add new actions
  45.     new_actions = @timeline[@ticks]
  46.     unless new_actions.nil?
  47.       for action in new_actions
  48.         @running << action
  49.       end
  50.     end
  51.     # Check and update the running actions
  52.     for action in @running
  53.       # Update the action
  54.       result = action.update
  55.       # Check if the action still is running
  56.       unless result.nil?
  57.         if result
  58.           # It was successful
  59.           @running.delete(action)
  60.         else
  61.           # It was not successful
  62.           return false
  63.         end
  64.       end
  65.     end
  66.     # Tick
  67.     @ticks += 1
  68.     # Check if all ticks has been used
  69.     if @ticks >= @total_ticks
  70.       # No failures => success
  71.       return true
  72.     end
  73.     return nil
  74.   end
  75. end
  76.  
  77. class Key_Action
  78.   attr_reader :ticks
  79.   attr_reader :total_ticks
  80.   def reset
  81.     raise NotImplementedError.new(self.class.to_s + ".reset")
  82.   end
  83.   def update
  84.     raise NotImplementedError.new(self.class.to_s + ".update")
  85.   end
  86. end
  87.  
  88. class Conjunction_Key_Action < Key_Action
  89.   ##
  90.   # Initialization
  91.   # condition - The condition that must be fulfilled
  92.   # *actions - The actions to check if they in conjunction succeed
  93.   #            The first action entered is the first to be executed
  94.   #            The second action entered is the next and so on
  95.   #
  96.   #    action1 ^ action2 ^ action3 ^ ... ^ actionN
  97.   #
  98.   # Note: Automatically succeeds if no actions are entered unless condition is
  99.   #       false
  100.   #
  101.   def initialize(condition, *actions)
  102.     @total_ticks = 0
  103.     @condition = condition
  104.     @actions = []
  105.     for action in actions
  106.       if action.is_a?(Key_Action)
  107.         @actions << action
  108.         @total_ticks += action.total_ticks
  109.       else
  110.         raise IllegalArgumentError.new("On of the arguments in " + self.class.to_s +
  111.                 " is not a Key_Action, it is a " + condition.class.to_s)
  112.       end
  113.     end
  114.     @index = 0
  115.     @ticks = 0
  116.   end
  117.  
  118.   ##
  119.   # Reset the action so it can be used again
  120.   #
  121.   def reset
  122.     @index = 0
  123.     @ticks = 0
  124.     for action in @actions
  125.       action.reset
  126.     end
  127.   end
  128.  
  129.   ##
  130.   # Returns
  131.   # nil - Not completed yet
  132.   # false - The action failed
  133.   # true - The action succeeded
  134.   #
  135.   def update
  136.     # Check if the condition is fulfilled
  137.     unless @condition.checkCondition
  138.       return false
  139.     end
  140.     # Check if the correct keys have been triggered
  141.     action = @actions[@index]
  142.     if action.nil?
  143.       return true
  144.     end
  145.     # Get the result from the action update
  146.     @ticks += 1
  147.     result = action.update
  148.     # If the action has failed
  149.     if result == false
  150.       return false
  151.     # If the action has succeeded
  152.     elsif result == true
  153.       # Move to next action
  154.       @index += 1
  155.       # If no more actions
  156.       if @actions.size <= @index
  157.         return true
  158.       end
  159.     end
  160.     return nil
  161.   end
  162. end
  163.  
  164. class Disjunction_Key_Action < Key_Action
  165.   ##
  166.   # Initialization
  167.   # condition - The condition that must be fulfilled
  168.   # *actions - The actions to check if they in disjunction succeed
  169.   #            The first action entered is the first to be executed
  170.   #            The second action entered is the next and so on
  171.   #
  172.   #    action1 v action2 v action3 v ... v actionN
  173.   #
  174.   # Note: Automatically fails if no actions are entered.
  175.   #
  176.   def initialize(condition, *actions)
  177.     @total_ticks = 0
  178.     @condition = condition
  179.     @actions = []
  180.     for action in actions
  181.       if action.is_a?(Key_Action)
  182.         @actions << action
  183.         @total_ticks += action.total_ticks
  184.       else
  185.         raise IllegalArgumentError.new("On of the arguments in " + self.class.to_s +
  186.                 " is not a Key_Action, it is a " + condition.class.to_s)
  187.       end
  188.     end
  189.     @index = 0
  190.     @ticks = 0
  191.   end
  192.  
  193.   ##
  194.   # Reset the action so it can be used again
  195.   #
  196.   def reset
  197.     @index = 0
  198.     @ticks = 0
  199.     for action in @actions
  200.       action.reset
  201.     end
  202.   end
  203.  
  204.   ##
  205.   # Returns
  206.   # nil - Not completed yet
  207.   # false - The action failed
  208.   # true - The action succeeded
  209.   #
  210.   def update
  211.     # Check if the condition is fulfilled
  212.     unless @condition.checkCondition
  213.       return false
  214.     end
  215.     # Check if the correct keys have been triggered
  216.     action = @actions[@index]
  217.     if action.nil?
  218.       return false
  219.     end
  220.     # Get the result from the action update
  221.     @ticks += 1
  222.     result = action.update
  223.     # If the action has failed
  224.     if result == false
  225.       # Move to next action
  226.       @index += 1
  227.       # If no more actions
  228.       if @actions.size <= @index
  229.         return false
  230.       end
  231.     # If the action has succeeded
  232.     elsif result == true
  233.       return true
  234.     end
  235.     return nil
  236.   end
  237. end
  238.  
  239. class Trigger_Key_Action < Key_Action
  240.   ##
  241.   # Initialization
  242.   # key_press - The keys to press
  243.   # condition - The condition that must be fulfilled
  244.   # ticks - The number of ticks to press the key
  245.   #
  246.   def initialize(key_press, condition, ticks)
  247.     @key_press = key_press
  248.     @condition = condition
  249.     @total_ticks = ticks
  250.     @ticks = 0
  251.   end
  252.  
  253.   ##
  254.   # Reset the action so it can be used again
  255.   #
  256.   def reset
  257.     @ticks = @total_ticks
  258.   end
  259.  
  260.   ##
  261.   # Returns
  262.   # nil - Not completed yet
  263.   # false - The action failed
  264.   # true - The action succeeded
  265.   #
  266.   def update
  267.     # Check if the condition is fulfilled
  268.     unless @condition.checkCondition
  269.       return false
  270.     end
  271.     # Check if the correct keys have been triggered
  272.     if @key_press.trigger?
  273.       return true
  274.     end
  275.     # Removes the amount of ticks for the action
  276.     @ticks += 1
  277.     # Check if all ticks has been used
  278.     if @ticks >= @total_ticks
  279.       return false
  280.     end
  281.     return nil
  282.   end
  283. end
  284.  
  285. class Release_Key_Action < Key_Action
  286.   ##
  287.   # Initialization
  288.   # key_press - The keys to press
  289.   # condition - The condition that must be fulfilled
  290.   # ticks - The number of ticks to hold the key pressed
  291.   # delta - The amounts of ticks before and after the right tick which are
  292.   #         considered as being timely release
  293.   #
  294.   def initialize(key_press, condition, ticks, delta)
  295.     @key_press = key_press
  296.     @condition = condition
  297.     @total_ticks = ticks + delta
  298.     @ticks = 0
  299.     @delta = delta
  300.   end
  301.  
  302.   ##
  303.   # Reset the action so it can be used again
  304.   #
  305.   def reset
  306.     @ticks = 0
  307.   end
  308.  
  309.   ##
  310.   # Returns
  311.   # nil - Not completed yet
  312.   # false - The action failed
  313.   # true - The action succeeded
  314.   #
  315.   def update
  316.     # Check if the condition is fulfilled
  317.     unless @condition.checkCondition
  318.       return false
  319.     end
  320.     # Check if the correct keys is still pressed
  321.     unless @key_press.press?
  322.       if @total_ticks - @delta * 2 <= @ticks && @total_ticks >= @ticks
  323.         return true
  324.       else
  325.         return false
  326.       end
  327.     end
  328.     # Removes the amount of ticks for the action
  329.     @ticks += 1
  330.     # Check if all ticks has been used
  331.     if @ticks >= @total_ticks
  332.       return false
  333.     end
  334.     return nil
  335.   end
  336. end
  337.  
  338. ##
  339. # Manages a serie of keys and whether they are pressed, triggered and repeated
  340. # Basically the same as the Input module except that it can check an arbitrary
  341. # number of keys instead of 1 key
  342. #
  343. class Key_Press
  344.   @keys = []
  345.   ##
  346.   # Initialization
  347.   # *args - integer values of the keys to press. (Refer to Input constants)
  348.   def initialize(*args)
  349.     @keys = args.flatten.compact
  350.   end
  351.   ##
  352.   # Check if all keys are triggered
  353.   #
  354.   def trigger?
  355.     for key in @keys
  356.       unless Input.trigger?(key)
  357.         return false
  358.       end
  359.     end
  360.     return true
  361.   end
  362.   ##
  363.   # Check if all keys are pressed
  364.   #
  365.   def press?
  366.     for key in @keys
  367.       unless Input.press?(key)
  368.         return false
  369.       end
  370.     end
  371.     return true
  372.   end
  373.   ##
  374.   # Check if all keys are repeated
  375.   #
  376.   def repeat?
  377.     for key in @keys
  378.       unless Input.repeat?(key)
  379.         return false
  380.       end
  381.     end
  382.     return true
  383.   end
  384. end
  385.  
  386. class IllegalArgumentError < StandardError
  387. end
  388.  
  389. class StandardError < Exception
  390.   def to_s
  391.     b =  self.backtrace
  392.     b.delete_at(0)
  393.     return super + "\n\nBacktrace:\n" + b.join("\n")
  394.   end
  395. end
  396.  
  397.  
  398. class Condition
  399.   def checkCondition(*args)
  400.     raise NotImplementedError.new(self.class.to_s + ".checkCondition")
  401.   end
  402. end
  403.  
  404. class ConjunctionCondition < Condition
  405.   def initialize(*conditions)
  406.     @conditions = []
  407.     for condition in conditions
  408.       if condition.is_a?(Condition)
  409.         @conditions << condition
  410.       else
  411.         raise IllegalArgumentError.new("On of the arguments in " + self.class.to_s +
  412.                 " is not a Condition, it is a " + condition.class.to_s)
  413.       end
  414.     end
  415.   end
  416.   def checkCondition(*args)
  417.     for condition in @conditions
  418.       return false unless condition.checkCondition(*args)
  419.     end
  420.     return true
  421.   end
  422. end
  423.  
  424. class DisjunctionCondition < Condition
  425.   def initialize(*conditions)
  426.     @conditions = []
  427.     for condition in conditions
  428.       if condition.is_a?(Condition)
  429.         @conditions << condition
  430.       else
  431.         raise IllegalArgumentError.new("On of the arguments in " + self.class.to_s +
  432.                 " is not a Condition, it is a " + condition.class.to_s)
  433.       end
  434.     end
  435.   end
  436.   def checkCondition(*args)
  437.     for condition in @conditions
  438.       return true if condition.checkCondition(*args)
  439.     end
  440.     return false
  441.   end
  442. end
  443.  
  444. class NotCondition < Condition
  445.   def initialize(condition)
  446.     if condition.is_a?(Condition)
  447.       @condition = condition
  448.     else
  449.       raise IllegalArgumentError.new("Given argument in " + self.class.to_s +
  450.                 " is not a condition, it is a " + condition.class.to_s)
  451.     end
  452.   end
  453.   def checkCondition(*args)
  454.     return condition.checkCondition(*args)
  455.   end
  456. end
  457.  
  458. class TrueCondition < Condition
  459.   def checkCondition(*args)
  460.     return true
  461.   end
  462. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement