107 lines
4.1 KiB
GDScript
107 lines
4.1 KiB
GDScript
extends Node
|
|
|
|
class_name Game
|
|
|
|
enum ITEM_TYPE {VALUABLE, WEAPON, MODULE, AMMUNITION}
|
|
enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS}
|
|
enum BASE_TYPE {POWER, MINING, FOOD, TRADING, MODULE}
|
|
|
|
const DEFAULT_ITEM = preload("res://items/test_item.tres")
|
|
const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn")
|
|
|
|
const salt = "2ndbeam"
|
|
const gameversion = "Ictar 1.1"
|
|
|
|
static var profile : Profile = Profile.new()
|
|
|
|
## Creates a new profile if it doesn't exist
|
|
static func profile_create(profile_name : String) -> void:
|
|
profile = Profile.new()
|
|
profile.profile_meta['meta']['profile_name'] = profile_name
|
|
profile_save_from_meta(profile.profile_meta)
|
|
|
|
## Saves profile meta and includes game data if there is an active session.
|
|
static func profile_save(scene : Node) -> void:
|
|
if scene.name != "MainMenu":
|
|
profile.profile_meta['game'] = {
|
|
"ammo" : scene.ship.hull.ammunition,
|
|
"hp" : scene.ship.hull.hp,
|
|
"fuel" : scene.ship.hull.fuel,
|
|
"money" : scene.ship.money
|
|
}
|
|
if scene.ship.primary_slot.get_child_count() > 0:
|
|
profile.profile_meta['game']['primaryweapon'] = scene.ship.primary_slot.get_child(0).id
|
|
if scene.ship.secondary_slot.get_child_count() > 0:
|
|
profile.profile_meta['game']['secondaryweapon'] = scene.ship.secondary_slot.get_child(0).id
|
|
profile_save_from_meta(profile.profile_meta)
|
|
|
|
## Saves profile with provided profile meta
|
|
static func profile_save_from_meta(profile_meta : Dictionary) -> void:
|
|
var path = "user://"+profile_meta['meta']['profile_name']+".cosmic"
|
|
profile_meta['meta']['last_version'] = gameversion
|
|
profile_meta['meta']['last_updated'] = Time.get_datetime_string_from_system()
|
|
for key in profile_meta['meta']:
|
|
profile_meta['hash'][key] = (str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode()
|
|
if 'game' in profile_meta:
|
|
for key in profile_meta['game']:
|
|
profile_meta['hash'][key] = (str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode()
|
|
var json_string = JSON.stringify(profile_meta, "\t")
|
|
var file = FileAccess.open(path, FileAccess.WRITE)
|
|
file.store_string(json_string)
|
|
|
|
## Returns profile meta of profile if it exists
|
|
static func profile_load(profile_name : String) -> Dictionary:
|
|
var path = "user://"+profile_name+".cosmic"
|
|
if not FileAccess.file_exists(path):
|
|
return {}
|
|
var file = FileAccess.open(path, FileAccess.READ)
|
|
var string = file.get_as_text()
|
|
var profile_meta = JSON.parse_string(string)
|
|
return profile_meta
|
|
|
|
## Deletes profile file if it exists
|
|
static func profile_delete(profile_name : String) -> void:
|
|
var path = "user://{profile}.cosmic".format({"profile": profile_name})
|
|
if FileAccess.file_exists(path):
|
|
var dir = DirAccess.open("user://")
|
|
dir.remove(path)
|
|
|
|
## Compares existing hash and calculated hash of file and if they are not equal resaves file with legit = false. Either way it returns profile meta.
|
|
static func profile_legit_check(profile_meta : Dictionary) -> Dictionary:
|
|
var old_hash = []
|
|
var new_hash = []
|
|
if profile_meta == {}:
|
|
return {}
|
|
for key in profile_meta['meta']:
|
|
old_hash.append(profile_meta['hash'][key])
|
|
new_hash.append((str(profile_meta['meta'][key]) + salt).sha256_buffer().hex_encode())
|
|
if 'game' in profile_meta:
|
|
for key in profile_meta['game']:
|
|
old_hash.append(profile_meta['hash'][key])
|
|
new_hash.append((str(profile_meta['game'][key]) + salt).sha256_buffer().hex_encode())
|
|
old_hash.sort()
|
|
new_hash.sort()
|
|
if old_hash == new_hash and profile_meta['meta']['legit']:
|
|
return profile_meta
|
|
profile_meta['meta']['legit'] = false
|
|
profile_save_from_meta(profile_meta)
|
|
return profile_meta
|
|
|
|
## Returns copy of loaded item resource in directory if it exists or DEFAULT_ITEM instead.
|
|
static func get_item(id : String) -> Item:
|
|
var path_name = id.to_lower().replace(" ", "_")
|
|
var path = "res://items/{name}.tres".format({"name": path_name})
|
|
var res = load(path)
|
|
if res != null:
|
|
return res.duplicate()
|
|
else:
|
|
return DEFAULT_ITEM.duplicate()
|
|
|
|
## Returns weapon packed scene if it exists or DEFAULT_WEAPON instead
|
|
static func get_weapon(id : String) -> PackedScene:
|
|
var path = "res://scenes/weapons/presets/{name}.tscn".format({"name": id})
|
|
var res = load(path)
|
|
if res != null:
|
|
return res
|
|
else:
|
|
return DEFAULT_WEAPON
|