Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. extends Node
  2. class_name Planet, "res://Assets/world.ico.png"
  3. signal new_production
  4. signal queue_changed
  5.  
  6. #Name
  7. export var planet_name: String = "New Planet"
  8.  
  9. #Base Resources
  10. export var food_base: float = 0
  11. export var production_base: float = 0
  12. export var energy_base: float = 0
  13. export var science_base: float = 0
  14.  
  15. #Current rate of change
  16. export var food_rate: float setget set_rate, food_rate
  17. export var energy_rate: float setget set_rate, energy_rate
  18. export var production_rate: float setget set_rate, production_rate
  19.  
  20. #Current Resources
  21. export var food_stocks: float = 0
  22. export var energy_stocks: float = 0
  23. export var production_stocks: float = 0
  24. export var population: int = 0
  25. export var happiness: int = 0
  26.  
  27. #Max Resources
  28. export var population_max_base: int = 0
  29. export var population_max: int setget set_rate, population_max
  30. export var food_max: float setget set_rate, food_max
  31. export var energy_max: float setget set_rate, energy_max
  32.  
  33. #Production
  34. var currently_producing = null
  35. export var production_cost: float = 0
  36. var buildings = []
  37. var queue = [] setget set_queue
  38.  
  39. #Planetary Information
  40. var planet_type: String = "Terran" setget set_planet_type, get_planet_type
  41. var planet_size
  42. var texture: Texture setget set_rate, get_texture
  43.  
  44. #Owner
  45. var planet_owner
  46.  
  47.  
  48. # Called when the node enters the scene tree for the first time.
  49. func _ready() -> void:
  50. set_planet_type(planet_type)
  51. pass # Replace with function body.
  52.  
  53. # Called every frame. 'delta' is the elapsed time since the previous frame.
  54. func _process(delta: float) -> void:
  55. #Determine rate of change
  56. var food_rate = food_rate()
  57. var energy_rate = energy_rate()
  58. var production_rate = production_rate()
  59.  
  60. #Modify resources
  61. food_stocks += (food_rate * delta)
  62. energy_stocks += (energy_rate * delta)
  63.  
  64. if (energy_stocks < 0):
  65. production_rate = 0
  66. energy_stocks = 0
  67.  
  68. #owner.science += (science_rate * delta) FIXME
  69. production_stocks += (production_rate * delta)
  70.  
  71. #Upgrade population if needed
  72. if (food_max() > 0):
  73. while ((food_stocks > food_max()) && (population < population_max)):
  74. population += 1
  75. food_stocks -= food_max()
  76. pass
  77.  
  78. if (food_stocks < 0):
  79. if (population > 1):
  80. population -= 1
  81. food_stocks = 0
  82.  
  83. #Limit excess food
  84. if (food_stocks > food_max()):
  85. food_stocks = food_max()
  86.  
  87. #Limit excess energy
  88. if (energy_stocks > energy_max()):
  89. energy_stocks = energy_max()
  90.  
  91. if (currently_producing == null):
  92. begin_next_production()
  93.  
  94. if (currently_producing != null):
  95. while ((production_stocks > production_cost) && (currently_producing != null)):
  96. production_stocks -= production_cost
  97. add_building(currently_producing)
  98. production_cost = 0
  99. currently_producing = null
  100.  
  101. begin_next_production()
  102. pass
  103.  
  104. if (production_stocks > production_cost):
  105. production_stocks = production_cost
  106.  
  107. #Wants a building name
  108. func add_to_queue(building: String) -> void:
  109. #FIXME: This needs to check if player is even allowed to build this
  110. queue.push_back(building)
  111. emit_signal("queue_changed")
  112. pass
  113.  
  114. func set_queue(new_queue: Array) -> void:
  115. queue = new_queue
  116. emit_signal("queue_changed")
  117.  
  118. #Wants a building name
  119. func add_building(new_building: String) -> void:
  120. buildings.push_back(new_building)
  121. pass
  122.  
  123. func begin_next_production() -> void:
  124. if (queue.size() > 0):
  125. currently_producing = queue.pop_front()
  126. var building = Database.Buildings.get_building(currently_producing)
  127. production_cost = building.production_cost
  128. emit_signal("new_production", currently_producing)
  129. emit_signal("queue_changed")
  130. pass
  131.  
  132. func food_rate() -> float:
  133. var food_bonus = 0
  134. var food_population = 0
  135. var food_percent = 0
  136.  
  137. var hunger = population * 2
  138.  
  139. for building in buildings:
  140. var building_instance = Database.Buildings.get_building(building)
  141. food_bonus += building_instance.food_bonus
  142. food_population += building_instance.food_population
  143. food_percent += building_instance.food_percent
  144. pass
  145.  
  146. return ((((food_base + food_population) * population) + food_bonus) * (1 + (food_percent/100)) - hunger)
  147.  
  148. func production_rate() -> float:
  149. var production_bonus = 0
  150. var production_population = 0
  151. var production_percent = 0
  152.  
  153. for building in buildings:
  154. var building_instance = Database.Buildings.get_building(building)
  155. production_bonus += building_instance.production_bonus
  156. production_population += building_instance.production_population
  157. production_percent += building_instance.production_percent
  158. pass
  159.  
  160. return (((production_base + production_population) * population) + production_bonus) * (1 + (production_percent/100))
  161.  
  162. func energy_rate() -> float:
  163. var energy_bonus = 0
  164. var energy_population = 0
  165. var energy_percent = 0
  166.  
  167. for building in buildings:
  168. var building_instance = Database.Buildings.get_building(building)
  169. energy_bonus += building_instance.energy_bonus
  170. energy_population += building_instance.energy_population
  171. energy_percent += building_instance.energy_percent
  172. pass
  173.  
  174. return (((energy_base + energy_population) * population) + energy_bonus) * (1 + (energy_percent/100))
  175.  
  176. #Noop
  177. func set_rate(new_rate) -> void:
  178. pass
  179.  
  180. func set_planet_type(new_type: String) -> void:
  181. planet_type = new_type
  182.  
  183. var type_instance = Database.PlanetTypes.get_type(new_type)
  184. food_base = type_instance.food_base
  185. production_base = type_instance.production_base
  186. energy_base = type_instance.energy_base
  187. science_base = type_instance.science_base
  188. population_max_base = type_instance.population_max
  189. pass
  190.  
  191. func get_planet_type() -> String:
  192. return planet_type
  193.  
  194. func food_max() -> float:
  195. food_max = 0.0
  196. for building in buildings:
  197. var building_instance = Database.Buildings.get_building(building)
  198. food_max += building_instance.food_storage
  199. pass
  200.  
  201. return food_max
  202.  
  203. func energy_max() -> float:
  204. energy_max = 0.0
  205. for building in buildings:
  206. var building_instance = Database.Buildings.get_building(building)
  207. energy_max += building_instance.energy_storage
  208. pass
  209.  
  210. return energy_max
  211.  
  212. func population_max() -> int:
  213. var population_max = population_max_base
  214. for building in buildings:
  215. var building_instance = Database.Buildings.get_building(building)
  216. population_max += building_instance.population_storage
  217. pass
  218.  
  219. return population_max
  220.  
  221. func get_texture() -> Texture:
  222. var type_instance = Database.PlanetTypes.get_type(planet_type)
  223. return type_instance.texture
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement