Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- class_name Planet, "res://Assets/world.ico.png"
- signal new_production
- signal queue_changed
- #Name
- export var planet_name: String = "New Planet"
- #Base Resources
- export var food_base: float = 0
- export var production_base: float = 0
- export var energy_base: float = 0
- export var science_base: float = 0
- #Current rate of change
- export var food_rate: float setget set_rate, food_rate
- export var energy_rate: float setget set_rate, energy_rate
- export var production_rate: float setget set_rate, production_rate
- #Current Resources
- export var food_stocks: float = 0
- export var energy_stocks: float = 0
- export var production_stocks: float = 0
- export var population: int = 0
- export var happiness: int = 0
- #Max Resources
- export var population_max_base: int = 0
- export var population_max: int setget set_rate, population_max
- export var food_max: float setget set_rate, food_max
- export var energy_max: float setget set_rate, energy_max
- #Production
- var currently_producing = null
- export var production_cost: float = 0
- var buildings = []
- var queue = [] setget set_queue
- #Planetary Information
- var planet_type: String = "Terran" setget set_planet_type, get_planet_type
- var planet_size
- var texture: Texture setget set_rate, get_texture
- #Owner
- var planet_owner
- # Called when the node enters the scene tree for the first time.
- func _ready() -> void:
- set_planet_type(planet_type)
- pass # Replace with function body.
- # Called every frame. 'delta' is the elapsed time since the previous frame.
- func _process(delta: float) -> void:
- #Determine rate of change
- var food_rate = food_rate()
- var energy_rate = energy_rate()
- var production_rate = production_rate()
- #Modify resources
- food_stocks += (food_rate * delta)
- energy_stocks += (energy_rate * delta)
- if (energy_stocks < 0):
- production_rate = 0
- energy_stocks = 0
- #owner.science += (science_rate * delta) FIXME
- production_stocks += (production_rate * delta)
- #Upgrade population if needed
- if (food_max() > 0):
- while ((food_stocks > food_max()) && (population < population_max)):
- population += 1
- food_stocks -= food_max()
- pass
- if (food_stocks < 0):
- if (population > 1):
- population -= 1
- food_stocks = 0
- #Limit excess food
- if (food_stocks > food_max()):
- food_stocks = food_max()
- #Limit excess energy
- if (energy_stocks > energy_max()):
- energy_stocks = energy_max()
- if (currently_producing == null):
- begin_next_production()
- if (currently_producing != null):
- while ((production_stocks > production_cost) && (currently_producing != null)):
- production_stocks -= production_cost
- add_building(currently_producing)
- production_cost = 0
- currently_producing = null
- begin_next_production()
- pass
- if (production_stocks > production_cost):
- production_stocks = production_cost
- #Wants a building name
- func add_to_queue(building: String) -> void:
- #FIXME: This needs to check if player is even allowed to build this
- queue.push_back(building)
- emit_signal("queue_changed")
- pass
- func set_queue(new_queue: Array) -> void:
- queue = new_queue
- emit_signal("queue_changed")
- #Wants a building name
- func add_building(new_building: String) -> void:
- buildings.push_back(new_building)
- pass
- func begin_next_production() -> void:
- if (queue.size() > 0):
- currently_producing = queue.pop_front()
- var building = Database.Buildings.get_building(currently_producing)
- production_cost = building.production_cost
- emit_signal("new_production", currently_producing)
- emit_signal("queue_changed")
- pass
- func food_rate() -> float:
- var food_bonus = 0
- var food_population = 0
- var food_percent = 0
- var hunger = population * 2
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- food_bonus += building_instance.food_bonus
- food_population += building_instance.food_population
- food_percent += building_instance.food_percent
- pass
- return ((((food_base + food_population) * population) + food_bonus) * (1 + (food_percent/100)) - hunger)
- func production_rate() -> float:
- var production_bonus = 0
- var production_population = 0
- var production_percent = 0
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- production_bonus += building_instance.production_bonus
- production_population += building_instance.production_population
- production_percent += building_instance.production_percent
- pass
- return (((production_base + production_population) * population) + production_bonus) * (1 + (production_percent/100))
- func energy_rate() -> float:
- var energy_bonus = 0
- var energy_population = 0
- var energy_percent = 0
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- energy_bonus += building_instance.energy_bonus
- energy_population += building_instance.energy_population
- energy_percent += building_instance.energy_percent
- pass
- return (((energy_base + energy_population) * population) + energy_bonus) * (1 + (energy_percent/100))
- #Noop
- func set_rate(new_rate) -> void:
- pass
- func set_planet_type(new_type: String) -> void:
- planet_type = new_type
- var type_instance = Database.PlanetTypes.get_type(new_type)
- food_base = type_instance.food_base
- production_base = type_instance.production_base
- energy_base = type_instance.energy_base
- science_base = type_instance.science_base
- population_max_base = type_instance.population_max
- pass
- func get_planet_type() -> String:
- return planet_type
- func food_max() -> float:
- food_max = 0.0
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- food_max += building_instance.food_storage
- pass
- return food_max
- func energy_max() -> float:
- energy_max = 0.0
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- energy_max += building_instance.energy_storage
- pass
- return energy_max
- func population_max() -> int:
- var population_max = population_max_base
- for building in buildings:
- var building_instance = Database.Buildings.get_building(building)
- population_max += building_instance.population_storage
- pass
- return population_max
- func get_texture() -> Texture:
- var type_instance = Database.PlanetTypes.get_type(planet_type)
- return type_instance.texture
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement