A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.6 KiB

  1. # Adapted from: https://gist.github.com/bitwes/fe1e2aef8da0940104c8
  2. # Example of a script that can be run from the command line and parses out options. This is adapted
  3. # from the Gut command line interface. The first 2 classes are not to be used directly, they are used
  4. # by the Options class and can be ignored. Start reading after the Options class to get a feel for
  5. # how it is used, then work backwards.
  6. #
  7. # This could be easily extracted out into a class itself, but in the interest of how it is being used
  8. # I wanted it all in one file. It is yours to do with what you please, but if you make something out
  9. # of it, I'd love to hear about it. I'm bitwes on godot forums, github, and bitbucket.
  10. extends Control
  11. #-------------------------------------------------------------------------------
  12. # Parses the command line arguments supplied into an array that can then be
  13. # examined and parsed based on how the gut options work.
  14. #-------------------------------------------------------------------------------
  15. class CmdLineParser:
  16. var _opts = []
  17. func _init():
  18. for i in range(OS.get_cmdline_args().size()):
  19. _opts.append(OS.get_cmdline_args()[i])
  20. # Search _opts for an element that starts with the option name
  21. # specified.
  22. func find_option(name):
  23. var found = false
  24. var idx = 0
  25. while(idx < _opts.size() and !found):
  26. if(_opts[idx].find(name) == 0):
  27. found = true
  28. else:
  29. idx += 1
  30. if(found):
  31. return idx
  32. else:
  33. return -1
  34. # Parse out the value of an option. Values are seperated from
  35. # the option name with "="
  36. func get_option_value(full_option):
  37. var split = full_option.split('=')
  38. if(split.size() > 1):
  39. return split[1]
  40. else:
  41. return null
  42. # Parse out multiple comma delimited values from a command line
  43. # option. Values are separated from option name with "=" and
  44. # additional values are comma separated.
  45. func get_option_array_value(full_option):
  46. var value = get_option_value(full_option)
  47. var split = value.split(',')
  48. return split
  49. func get_array_value(option):
  50. var to_return = []
  51. var opt_loc = find_option(option)
  52. if(opt_loc != -1):
  53. to_return = get_option_array_value(_opts[opt_loc])
  54. _opts.remove(opt_loc)
  55. return to_return
  56. # returns the value of an option if it was specfied, otherwise
  57. # it returns the default.
  58. func get_value(option, default):
  59. var to_return = default
  60. var opt_loc = find_option(option)
  61. if(opt_loc != -1):
  62. to_return = get_option_value(_opts[opt_loc])
  63. _opts.remove(opt_loc)
  64. return to_return
  65. # returns true if it finds the option, false if not.
  66. func was_specified(option):
  67. var opt_loc = find_option(option)
  68. if(opt_loc != -1):
  69. _opts.remove(opt_loc)
  70. return opt_loc != -1
  71. #-------------------------------------------------------------------------------
  72. # Simple class to hold a command line option
  73. #-------------------------------------------------------------------------------
  74. class Option:
  75. var value = null
  76. var option_name = ''
  77. var default = null
  78. var description = ''
  79. func _init(name, default_value, desc=''):
  80. option_name = name
  81. default = default_value
  82. description = desc
  83. value = default_value
  84. func pad(value, size, pad_with=' '):
  85. var to_return = value
  86. for i in range(value.length(), size):
  87. to_return += pad_with
  88. return to_return
  89. func to_s(min_space=0):
  90. var subbed_desc = description
  91. if(subbed_desc.find('[default]') != -1):
  92. subbed_desc = subbed_desc.replace('[default]', str(default))
  93. return pad(option_name, min_space) + subbed_desc
  94. #-------------------------------------------------------------------------------
  95. # The high level interface between this script and the command line options
  96. # supplied. Uses Option class and CmdLineParser to extract information from
  97. # the command line and make it easily accessible.
  98. #-------------------------------------------------------------------------------
  99. class Options:
  100. var options = []
  101. var _opts = []
  102. var _banner = ''
  103. func add(name, default, desc):
  104. options.append(Option.new(name, default, desc))
  105. func get_value(name):
  106. var found = false
  107. var idx = 0
  108. while(idx < options.size() and !found):
  109. if(options[idx].option_name == name):
  110. found = true
  111. else:
  112. idx += 1
  113. if(found):
  114. return options[idx].value
  115. else:
  116. print("COULD NOT FIND OPTION " + name)
  117. return null
  118. func set_banner(banner):
  119. _banner = banner
  120. func print_help():
  121. var longest = 0
  122. for i in range(options.size()):
  123. if(options[i].option_name.length() > longest):
  124. longest = options[i].option_name.length()
  125. print('---------------------------------------------------------')
  126. print(_banner)
  127. print("\nOptions\n-------")
  128. for i in range(options.size()):
  129. print(' ' + options[i].to_s(longest + 2))
  130. print('---------------------------------------------------------')
  131. func print_options():
  132. for i in range(options.size()):
  133. print(options[i].option_name + '=' + str(options[i].value))
  134. func parse():
  135. var parser = CmdLineParser.new()
  136. for i in range(options.size()):
  137. var t = typeof(options[i].default)
  138. if(t == TYPE_INT):
  139. options[i].value = int(parser.get_value(options[i].option_name, options[i].default))
  140. elif(t == TYPE_STRING):
  141. options[i].value = parser.get_value(options[i].option_name, options[i].default)
  142. elif(t == TYPE_ARRAY):
  143. options[i].value = parser.get_array_value(options[i].option_name)
  144. elif(t == TYPE_BOOL):
  145. options[i].value = parser.was_specified(options[i].option_name)
  146. elif(t == TYPE_NIL):
  147. print(options[i].option_name + ' cannot be processed, it has a nil datatype')
  148. else:
  149. print(options[i].option_name + ' cannot be processsed, it has unknown datatype:' + str(t))