136 lines
3.7 KiB
GDScript
136 lines
3.7 KiB
GDScript
extends Node
|
|
|
|
signal pickup_item
|
|
|
|
const ENTITIES = preload("res://assets/entities.png")
|
|
const SAVE_FILE = "user://misguided_data.tres"
|
|
const SPACE_POD = preload("res://scenes/space_pod.tscn")
|
|
|
|
const type_to_int ={
|
|
"star": 96,
|
|
"fuel": 64
|
|
}
|
|
|
|
var game_data: GameData
|
|
|
|
var current_rocket: Rocket
|
|
|
|
func get_current_rocket_display() -> Array[TextureRect]:
|
|
var rocket: Array[TextureRect] = []
|
|
return rocket
|
|
|
|
func ready_to_launch() -> bool:
|
|
return not game_data.modules[GameData.ModuleType.ENGINE].is_empty() and not game_data.modules[GameData.ModuleType.COCKPIT]
|
|
|
|
func launch_rocket() -> void:
|
|
game_data.cargo = {}
|
|
game_data.fuel = game_data.fuel_max
|
|
|
|
func pickup(type: String) -> void:
|
|
game_data.pickup(type)
|
|
pickup_item.emit()
|
|
|
|
func has_cargo() -> bool:
|
|
return game_data.has_cargo()
|
|
|
|
func type_to_texture(type: String) -> Texture:
|
|
var texture := AtlasTexture.new()
|
|
texture.atlas = ENTITIES
|
|
texture.region = Rect2(type_to_int[type], 0, 32, 32)
|
|
return texture
|
|
|
|
func move_cargo_to_inventory() -> void:
|
|
game_data.move_cargo_to_inventory()
|
|
|
|
func create_cargo_pod(pod_position: Vector2) -> void:
|
|
game_data.create_cargo_pod(pod_position)
|
|
|
|
func save_game_data() -> void:
|
|
ResourceSaver.save(game_data, SAVE_FILE)
|
|
|
|
func load_game_data() -> void:
|
|
if ResourceLoader.exists(SAVE_FILE):
|
|
game_data = load(SAVE_FILE)
|
|
else:
|
|
game_data = GameData.new()
|
|
|
|
func spawn_pods() -> Array[SpacePod]:
|
|
var pod_nodes: Array[SpacePod] = []
|
|
for pod: FloatingPodData in game_data.floating_pods:
|
|
var pod_spawn = SPACE_POD.instantiate()
|
|
pod_spawn.pod_data = pod
|
|
pod_spawn.position = pod.position
|
|
pod_nodes.append(pod_spawn)
|
|
return pod_nodes
|
|
|
|
func retrive_pod(pod_data: FloatingPodData) -> void:
|
|
for item in pod_data.cargo:
|
|
game_data.pickup(item, pod_data.cargo[item])
|
|
var index = game_data.floating_pods.find(pod_data)
|
|
if index >= 0:
|
|
game_data.floating_pods.remove_at(index)
|
|
pickup_item.emit()
|
|
|
|
func remove_module(location: GameData.ModuleType, part: String) -> void:
|
|
print("Module removed %s" % part)
|
|
|
|
func add_module(location: GameData.ModuleType, part: Module) -> void:
|
|
print("Module added %s" % part)
|
|
|
|
func initial_velocity() -> Vector2:
|
|
return Vector2.RIGHT * get_speed() * 0.011
|
|
|
|
func has_fuel() -> bool:
|
|
return game_data.fuel > 0
|
|
|
|
func add_fuel(amount: float, apply_efficiency: bool = false) -> void:
|
|
game_data.fuel += amount if not apply_efficiency else amount * game_data.fuel_efficiency
|
|
|
|
func get_fuel() -> float:
|
|
return game_data.fuel
|
|
|
|
func get_fuel_percent() -> float:
|
|
return game_data.fuel / game_data.fuel_max
|
|
|
|
func get_speed() -> float:
|
|
return game_data.SPEED
|
|
|
|
func get_maneuverability() -> float:
|
|
return 0.01
|
|
|
|
func has_fuel_needed_for_eject() -> bool:
|
|
return eject_fuel_percentage() >= 1.0
|
|
|
|
func eject_fuel_percentage() -> float:
|
|
return game_data.fuel / fuel_needed_for_eject()
|
|
|
|
func eject_fuel_max_percentage() -> float:
|
|
return fuel_needed_for_eject() / game_data.fuel_max
|
|
|
|
func fuel_needed_for_eject() -> float:
|
|
return max(current_rocket.position.length() / 160.0, 10.0)
|
|
|
|
func get_available_upgrades() -> Array[Upgrade]:
|
|
return game_data.upgrades.filter(func(upgd: Upgrade) -> bool: return not upgd.purchased)
|
|
|
|
func purchase_upgrade(upgrade: Upgrade) -> bool:
|
|
upgrade.purchased = not upgrade.purchased
|
|
if not has_upgrade_cost(upgrade): return false
|
|
var index = game_data.upgrades.find(upgrade)
|
|
if index < 0: return false
|
|
#TODO finsh this
|
|
print(game_data.upgrades[index].purchased)
|
|
return true
|
|
|
|
func has_upgrade_cost(upgrade: Upgrade) -> bool:
|
|
for item in upgrade.cost:
|
|
if not game_data.inventory.has(item) or game_data.inventory[item] < upgrade.cost[item]:
|
|
return false
|
|
return true
|
|
|
|
func get_modules_for_location(location: GameData.ModuleType) -> Array:
|
|
return game_data.modules[location]
|
|
|
|
func get_mid_count() -> int:
|
|
return game_data.mid_module_limit
|