Improved cloud saves
This commit is contained in:
@@ -25,7 +25,7 @@ GameGlobals="*res://scripts/game_globals.gd"
|
||||
HelperFuncs="*res://scripts/HelperFuncs.gd"
|
||||
ColorSwatch="*res://scripts/ColorSwatch.gd"
|
||||
Firebase="*res://addons/godot-firebase/firebase/firebase.tscn"
|
||||
Leaderboard="*res://scripts/leaderboard.gd"
|
||||
Cloud="*res://scripts/cloud.gd"
|
||||
|
||||
[display]
|
||||
|
||||
|
||||
89
scripts/cloud.gd
Normal file
89
scripts/cloud.gd
Normal file
@@ -0,0 +1,89 @@
|
||||
extends Node
|
||||
|
||||
func save_cloud_data(data,collection_id):
|
||||
var auth = Firebase.Auth.auth
|
||||
if auth.localid:
|
||||
var collection: FirestoreCollection = Firebase.Firestore.collection(collection_id)
|
||||
var document = await collection.get_doc(auth.localid)
|
||||
|
||||
if document:
|
||||
for key in data.keys():
|
||||
document.add_or_update_field(key,data[key])
|
||||
var update_task = await collection.update(document)
|
||||
if update_task:
|
||||
print("Document updated successfully")
|
||||
else:
|
||||
print("Failed to update document")
|
||||
else:
|
||||
await collection.add(auth.localid,data)
|
||||
|
||||
func load_cloud_data(collection_id):
|
||||
var auth = Firebase.Auth.auth
|
||||
|
||||
if auth.localid:
|
||||
var collection : FirestoreCollection = Firebase.Firestore.collection(collection_id)
|
||||
|
||||
var document = await collection.get_doc(auth.localid)
|
||||
|
||||
if document:
|
||||
print(document)
|
||||
return document
|
||||
else:
|
||||
print("Failed to load document from Firebase")
|
||||
|
||||
func get_leaderboard_name():
|
||||
return str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id])
|
||||
|
||||
func save_leaderboard_data():
|
||||
var leaderboard_data = {
|
||||
"high_score" : GameGlobals.high_score,
|
||||
"money" : GameGlobals.money,
|
||||
"deposited_money" : GameGlobals.deposited_money
|
||||
}
|
||||
|
||||
var leaderboard_name = get_leaderboard_name()
|
||||
|
||||
var data = {
|
||||
leaderboard_name : leaderboard_data
|
||||
}
|
||||
|
||||
var collection = "leaderboard"
|
||||
|
||||
save_cloud_data(data,collection)
|
||||
|
||||
func load_leaderboard_data():
|
||||
var collection = get_leaderboard_name()
|
||||
load_cloud_data(collection)
|
||||
|
||||
func save_user_data():
|
||||
var leaderboard_data = {
|
||||
"last_hit_path" : GameGlobals.last_hit_path,
|
||||
"current_match_id" : GameGlobals.current_match_id,
|
||||
"current_round_id" : GameGlobals.current_round_id,
|
||||
"enemies_killed" : GameGlobals.enemies_killed,
|
||||
"held_guns" : SaveLoad.save_resource_path(GameGlobals.held_guns),
|
||||
"gun_ammo" : GameGlobals.gun_ammo,
|
||||
"ammo_reserve" : GameGlobals.ammo_reserve,
|
||||
"current_gun_index" : GameGlobals.current_gun_index
|
||||
}
|
||||
|
||||
var leaderboard_name = get_leaderboard_name()
|
||||
|
||||
var data ={
|
||||
leaderboard_name : leaderboard_data
|
||||
}
|
||||
|
||||
var collection = "user_data"
|
||||
|
||||
save_cloud_data(data,collection)
|
||||
|
||||
func save_persistent_data():
|
||||
var data = {
|
||||
"user_name" : GameGlobals.user_names[0],
|
||||
"last_leaderboard_id" : GameGlobals.last_leaderboard_id,
|
||||
"all_user_leaderboards" : GameGlobals.all_user_leaderboards[0]
|
||||
}
|
||||
|
||||
var collection = "persistent_data"
|
||||
|
||||
save_cloud_data(data,collection)
|
||||
@@ -17,4 +17,4 @@ func _process(delta: float) -> void:
|
||||
|
||||
func interact():
|
||||
SignalBus.emit_signal("money_deposited")
|
||||
Leaderboard.save_leaderboard_data()
|
||||
Cloud.save_leaderboard_data()
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
extends Node
|
||||
|
||||
func save_leaderboard_data():
|
||||
var auth = Firebase.Auth.auth
|
||||
var COLLECTION_ID = get_leaderboard_name()
|
||||
if auth.localid:
|
||||
var collection: FirestoreCollection = Firebase.Firestore.collection(COLLECTION_ID)
|
||||
var data : Dictionary = {
|
||||
"user_name" : GameGlobals.user_names[GameGlobals.user_id],
|
||||
"leaderboard" : get_leaderboard_name(),
|
||||
"high_score" : GameGlobals.high_score,
|
||||
"money" : GameGlobals.money,
|
||||
"deposited_money" : GameGlobals.deposited_money
|
||||
}
|
||||
print("DATA: ",data)
|
||||
var document = await collection.get_doc(auth.localid)
|
||||
if document:
|
||||
for key in data.keys():
|
||||
document.add_or_update_field(key,data[key])
|
||||
var task = await collection.update(document)
|
||||
if task:
|
||||
print("Document updated successfully")
|
||||
else:
|
||||
print("Failed to update document")
|
||||
else:
|
||||
await collection.add(auth.localid,data)
|
||||
|
||||
func load_leaderboard_data():
|
||||
var auth = Firebase.Auth.auth
|
||||
var COLLECTION_ID = get_leaderboard_name()
|
||||
|
||||
if auth.localid:
|
||||
var collection : FirestoreCollection = Firebase.Firestore.collection(COLLECTION_ID)
|
||||
|
||||
var document = await collection.get_doc(auth.localid)
|
||||
|
||||
if document:
|
||||
print(document)
|
||||
return document
|
||||
else:
|
||||
print("Failed to load document from Firebase")
|
||||
|
||||
func get_leaderboard_name():
|
||||
return str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id]) + "_leaderboard"
|
||||
@@ -48,6 +48,8 @@ func _on_continue_pressed() -> void:
|
||||
SaveLoad.save_persistent_data()
|
||||
SaveLoad.load_user_data()
|
||||
SaveLoad.save_user_data()
|
||||
Cloud.save_user_data()
|
||||
Cloud.save_persistent_data()
|
||||
get_tree().change_scene_to_file(level)
|
||||
print("LEVEL : ",GameGlobals.current_level)
|
||||
|
||||
|
||||
@@ -702,17 +702,16 @@ func weapon_sway(delta):
|
||||
gun.look_at(gun_look_target.global_position)
|
||||
|
||||
#apply sway
|
||||
if !ads:
|
||||
var joy_input = Vector2(Input.get_joy_axis(0,JOY_AXIS_RIGHT_X)*5,Input.get_joy_axis(0,JOY_AXIS_RIGHT_Y)*5)
|
||||
mouse_input = lerp(mouse_input, Vector2.ZERO, 10 * delta)
|
||||
joy_input = lerp(joy_input,Vector2.ZERO,10 * delta)
|
||||
weapon_holder.rotation.x = lerp(weapon_holder.rotation.x, (mouse_input.y + joy_input.y) * weapon_sway_amount, 5 * delta)
|
||||
weapon_holder.rotation.y = lerp(weapon_holder.rotation.y, (mouse_input.x + joy_input.x) * weapon_sway_amount, 5 * delta)
|
||||
|
||||
else:
|
||||
if gun != null:
|
||||
if gun.weapon_info.ads == true:
|
||||
weapon_holder.rotation = Vector3.ZERO
|
||||
#
|
||||
#else:
|
||||
#if gun != null:
|
||||
#if gun.weapon_info.ads == true:
|
||||
#weapon_holder.rotation = Vector3.ZERO
|
||||
|
||||
func weapon_bob(vel : float, delta):
|
||||
if weapon_holder:
|
||||
|
||||
@@ -47,7 +47,7 @@ func load_persistent_data():
|
||||
print("No file found...")
|
||||
|
||||
func save_user_data():
|
||||
Leaderboard.save_leaderboard_data()
|
||||
Cloud.save_leaderboard_data()
|
||||
var user_save_path = str("user://user_",str(GameGlobals.user_id),"_",str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id]),"_data.save")
|
||||
var file = FileAccess.open(user_save_path, FileAccess.WRITE)
|
||||
|
||||
@@ -149,7 +149,7 @@ func load_user_data():
|
||||
|
||||
|
||||
func save_game_data(level_name):
|
||||
Leaderboard.save_leaderboard_data()
|
||||
Cloud.save_leaderboard_data()
|
||||
var level_control = get_tree().current_scene
|
||||
var player = level_control.player
|
||||
var game_save_path = str("user://user_",str(GameGlobals.user_id),"_",str(GameGlobals.all_user_leaderboards[GameGlobals.user_id][GameGlobals.last_leaderboard_id]),"_last_level","_data.save")
|
||||
|
||||
@@ -10,7 +10,7 @@ extends Node3D
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
Leaderboard.load_leaderboard_data()
|
||||
Cloud.load_leaderboard_data()
|
||||
|
||||
|
||||
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
|
||||
Reference in New Issue
Block a user