1
0
mirror of https://gitee.com/freeyz/godot-mota.git synced 2024-12-23 12:29:22 +08:00
godot-mota/scripts/GameArchiveManager.gd

152 lines
4.0 KiB
GDScript3
Raw Normal View History

2020-07-23 15:47:32 +08:00
extends Node
const archive_image_path_prefix = "user://"
const archive_file_path = "user://mota.save"
# 当前玩家信息
var player_info = {
#英雄名称
"username" : "hero",
#英雄当前位置
"position" : Vector2.ZERO,
#英雄血量
"hp" : 1000,
# 英雄攻击力
"atk" : 1,
# 英雄防御力
"def" : 1,
#默认金钱
"money" : 0,
# 英雄等级
"level" : 1,
# 英雄经验
"experience" : 0,
# 当前楼层
"now_floor" : 1,
# 去过的最大楼层
"max_floor" : 1,
# 去过的最小楼层
"min_floor" : 1,
# 黄钥匙数量
"yellow_key" : 0,
# 蓝钥匙数量
"blue_key" : 0,
# 红钥匙数量
"red_key" : 0,
# 绿色钥匙数量
"green_key" : 0,
# 被使用的道具
"used_items" : {}
}
# 增加已经使用的对象
func add_used_item(path:String) -> void:
if not player_info.has("used_items"):
player_info.used_items = {}
var k = player_info.now_floor as String
if not player_info.used_items.has(k):
player_info.used_items[k] = [path]
else:
player_info.used_items[k].append(path)
# 根据楼层查询被使用的道具
func get_used_item(f:int) -> Array:
if not player_info.has("used_items"):
return []
var k = f as String
if not player_info.used_items.has(k):
return []
else:
return player_info.used_items[k]
return []
# 保存存档
func save_to_index(index:int):
update_player_position()
var archives := get_archives() as Array
if index >= 0 and index < archives.size():
var archive = archives[index] as Dictionary
archive.update_time = OS.get_unix_time()
archive.screenshot_file_path = save_screenshot_to_file(index)
archive.player_info = self.player_info
else:
var archive = {}
archive.create_time = OS.get_unix_time()
archive.update_time = archive.create_time
archive.screenshot_file_path = save_screenshot_to_file(index)
archive.player_info = self.player_info
archives.append(archive)
save_archives(archives)
# 获取玩家对象
func get_player() -> PlayerKinematicBody2D:
return get_tree().get_nodes_in_group("player")[0] as PlayerKinematicBody2D
# 更新玩家位置
func update_player_position() -> void:
var player = get_player()
player_info.position = player.position
# 根据索引加载存档
func load_by_index(index:int) -> void:
var archive = get_archives()[index]
self.player_info = archive.player_info
#加载楼层
GameFloorManager.load_by_floor(self.player_info.now_floor)
#设置人物位置
var player = get_player()
player.position = self.player_info.position
#更新楼层UI显示
$"/root/Main/UI/LevelBackground/LevelLabel".text = "魔塔 第%s" % player_info.now_floor
#更新人物血量
$"/root/Main/UI/HpSprite/HpBackground/HpLabel".text = player_info.hp as String
#更新钥匙数量显示
$"/root/Main/UI/KeyBackground/YellowKeyCountLabel".text = player_info.yellow_key as String
$"/root/Main/UI/KeyBackground/BlueKeyCountLabel".text = player_info.blue_key as String
$"/root/Main/UI/KeyBackground/RedKeyCountLabel".text = player_info.red_key as String
$"/root/Main/UI/KeyBackground/GreenKeyCountLabel".text = player_info.green_key as String
# 保存屏幕截图到本地
func save_screenshot_to_file(index:int) -> String:
var image = get_viewport().get_texture().get_data()
image.flip_y()
var image_path = "%s%s%s" % [archive_image_path_prefix,index,".png"]
var result = image.save_png(image_path)
if result != OK:
print("保存存档图片错误:",result)
return image_path
# 得到所有存档文件对象
func get_archives() -> Array:
var archive_file = File.new()
if not archive_file.file_exists(archive_file_path):
archive_file.close()
return []
var result = archive_file.open(archive_file_path,File.READ)
if result == OK:
return archive_file.get_var(true)
else:
archive_file.close()
return []
# 保存所有存档文件对象
func save_archives(archives:Array) -> void:
var archive_file = File.new()
var result = archive_file.open(archive_file_path,File.WRITE)
if result == OK:
archive_file.store_var(archives,true)
else:
print("保存游戏文件失败:",result)
archive_file.close()