133 lines
3.9 KiB
C#
133 lines
3.9 KiB
C#
using UnityEngine;
|
|
using Invector;
|
|
using Invector.vCharacterController;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using PixelCrushers;
|
|
|
|
namespace Beyond
|
|
{
|
|
/// <summary>
|
|
/// Save System saver for Invector character's stats.
|
|
/// </summary>
|
|
[AddComponentMenu("Pixel Crushers/Save System/Savers/Invector/Invector Stats Saver")]
|
|
public class InvectorStatsSaver : Saver
|
|
{
|
|
public static bool isResettingScene = false;
|
|
|
|
[Tooltip("Save health value in vHealthController. If unticked, save health value in vThirdPersonMotor.")]
|
|
public bool saveHealthInHealthController = true;
|
|
|
|
[Serializable]
|
|
public class Data
|
|
{
|
|
// vHealthController:
|
|
public float health;
|
|
|
|
public float maxHealth;
|
|
|
|
// vThirdPersonMotorx
|
|
public float maxStamina;
|
|
|
|
public float staminaRecovery;
|
|
public float currentStaminaRecovery;
|
|
public float currentStamina;
|
|
public float sprintStamina;
|
|
public float jumpStamina;
|
|
public float rollStamina;
|
|
public PlayerAttribute[] attributes;
|
|
}
|
|
|
|
private vHealthController m_controller = null;
|
|
|
|
private vHealthController controller
|
|
{
|
|
get
|
|
{
|
|
if (m_controller == null) m_controller = GetComponent<vHealthController>();
|
|
return m_controller;
|
|
}
|
|
}
|
|
|
|
private Player m_player = null;
|
|
|
|
private Player Player
|
|
{
|
|
get
|
|
{
|
|
if (m_player == null) m_player = GetComponent<Player>();
|
|
return m_player;
|
|
}
|
|
}
|
|
|
|
private vThirdPersonMotor m_motor = null;
|
|
|
|
private vThirdPersonMotor motor
|
|
{
|
|
get
|
|
{
|
|
if (m_motor == null) m_motor = GetComponent<vThirdPersonMotor>();
|
|
return m_motor;
|
|
}
|
|
}
|
|
|
|
public override string RecordData()
|
|
{
|
|
var data = new Data();
|
|
if (saveHealthInHealthController && controller == null) return string.Empty;
|
|
if (!saveHealthInHealthController && motor == null) return string.Empty;
|
|
if (motor != null)
|
|
{
|
|
if (!saveHealthInHealthController)
|
|
{
|
|
data.health = motor.currentHealth;
|
|
}
|
|
data.currentStamina = motor.currentStamina;
|
|
}
|
|
if (saveHealthInHealthController)
|
|
{
|
|
data.health = controller.currentHealth;
|
|
}
|
|
//attributes
|
|
data.attributes = Player.Attributes.ToArray();
|
|
return SaveSystem.Serialize(data);
|
|
}
|
|
|
|
public override void ApplyData(string s)
|
|
{
|
|
if (isResettingScene)
|
|
{
|
|
isResettingScene = false;
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(s)) return;
|
|
if (saveHealthInHealthController && controller == null) return;
|
|
if (!saveHealthInHealthController && motor == null) return;
|
|
|
|
var data = SaveSystem.Deserialize<Data>(s);
|
|
if (data == null) return;
|
|
|
|
// Note inconsistency: ChangeMaxHealth/Stamina adds (+=), ChangeHealth/Stamina sets (=).
|
|
|
|
//attributes
|
|
for (int i = 0; i < data.attributes.Length; i++)
|
|
{
|
|
Player.SetAttribute(data.attributes[i]);
|
|
}
|
|
|
|
if (saveHealthInHealthController)
|
|
{
|
|
// controller.ChangeMaxHealth((int)(data.maxHealth - controller.maxHealth));
|
|
controller.ChangeHealth((int)data.health);
|
|
}
|
|
|
|
if (motor != null)
|
|
{
|
|
if (!saveHealthInHealthController)
|
|
{
|
|
motor.ChangeHealth((int)data.health);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |