Files
FueraDeEscala/Assets/Game Kit Controller/Scripts/Save System/Custom Class To Save/savePlayerWeaponsInfo.cs

335 lines
13 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public class savePlayerWeaponsInfo : saveGameInfo
{
2026-03-29 23:03:14 -07:00
public playerWeaponsManager mainPlayerWeaponsManager;
List<persistanceWeaponInfo> persistanceInfoList;
2026-03-29 23:03:14 -07:00
public override void saveGame (int saveNumber, int playerID, string currentSaveDataPath, bool showDebugInfo, bool savingGameToChangeScene)
{
saveGameContent (saveNumber, playerID, currentSaveDataPath, showDebugInfo, savingGameToChangeScene);
}
2026-03-29 23:03:14 -07:00
public override void loadGame (int saveNumberToLoad, int playerID, string currentSaveDataPath, bool showDebugInfo)
{
loadGameContent (saveNumberToLoad, playerID, currentSaveDataPath, showDebugInfo);
}
2026-03-29 23:03:14 -07:00
public override void initializeValuesOnComponent ()
{
initializeValues ();
}
public void saveGameContent (int currentSaveNumber, int playerID, string currentSaveDataPath, bool showDebugInfo, bool savingGameToChangeScene)
{
if (mainPlayerWeaponsManager == null) {
return;
}
2026-03-29 23:03:14 -07:00
if (!mainPlayerWeaponsManager.saveCurrentPlayerWeaponsToSaveFile) {
return;
}
2026-03-29 23:03:14 -07:00
if (showDebugInfo) {
print ("\n\n");
2026-03-29 23:03:14 -07:00
print ("Saving player weapons");
}
2026-03-29 23:03:14 -07:00
bool saveLocated = false;
bool playerLocated = false;
2026-03-29 23:03:14 -07:00
int saveSlotIndex = -1;
int listIndex = -1;
2026-03-29 23:03:14 -07:00
BinaryFormatter bf = new BinaryFormatter ();
FileStream file;
2026-03-29 23:03:14 -07:00
persistanceWeaponListByPlayerInfo weaponsToSave = getPersistanceList (playerID, showDebugInfo);
2026-03-29 23:03:14 -07:00
persistanceWeaponListBySaveSlotInfo newPersistanceWeaponListBySaveSlotInfo = new persistanceWeaponListBySaveSlotInfo ();
2026-03-29 23:03:14 -07:00
List<persistanceWeaponListBySaveSlotInfo> infoListToSave = new List<persistanceWeaponListBySaveSlotInfo> ();
2026-03-29 23:03:14 -07:00
if (File.Exists (currentSaveDataPath)) {
bf = new BinaryFormatter ();
file = File.Open (currentSaveDataPath, FileMode.Open);
object currentData = bf.Deserialize (file);
infoListToSave = currentData as List<persistanceWeaponListBySaveSlotInfo>;
2026-03-29 23:03:14 -07:00
file.Close ();
}
2026-03-29 23:03:14 -07:00
int infoListToSaveCount = infoListToSave.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < infoListToSaveCount; j++) {
if (infoListToSave [j].saveNumber == currentSaveNumber) {
newPersistanceWeaponListBySaveSlotInfo = infoListToSave [j];
saveLocated = true;
saveSlotIndex = j;
}
}
2026-03-29 23:03:14 -07:00
if (saveLocated) {
int playerWeaponListCount = newPersistanceWeaponListBySaveSlotInfo.playerWeaponList.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < playerWeaponListCount; j++) {
if (newPersistanceWeaponListBySaveSlotInfo.playerWeaponList [j].playerID == weaponsToSave.playerID) {
playerLocated = true;
listIndex = j;
}
}
}
2026-03-29 23:03:14 -07:00
if (showDebugInfo) {
print ("\n\n");
2026-03-29 23:03:14 -07:00
print ("Number of weapons: " + weaponsToSave.weaponList.Count);
2026-03-29 23:03:14 -07:00
print ("Current Save Number " + currentSaveNumber);
print ("Save Located " + saveLocated);
print ("Player Located " + playerLocated);
}
2026-03-29 23:03:14 -07:00
//if the save is located, check if the player id exists
if (saveLocated) {
//if player id exists, overwrite it
if (playerLocated) {
infoListToSave [saveSlotIndex].playerWeaponList [listIndex].weaponList = weaponsToSave.weaponList;
} else {
infoListToSave [saveSlotIndex].playerWeaponList.Add (weaponsToSave);
}
} else {
newPersistanceWeaponListBySaveSlotInfo.saveNumber = currentSaveNumber;
newPersistanceWeaponListBySaveSlotInfo.playerWeaponList.Add (weaponsToSave);
infoListToSave.Add (newPersistanceWeaponListBySaveSlotInfo);
}
2026-03-29 23:03:14 -07:00
bf = new BinaryFormatter ();
file = File.Open (currentSaveDataPath, FileMode.OpenOrCreate);
bf.Serialize (file, infoListToSave);
2026-03-29 23:03:14 -07:00
file.Close ();
}
2026-03-29 23:03:14 -07:00
public void loadGameContent (int saveNumberToLoad, int playerID, string currentSaveDataPath, bool showDebugInfo)
{
if (mainPlayerWeaponsManager == null) {
return;
}
2026-03-29 23:03:14 -07:00
if (!mainPlayerWeaponsManager.loadWeaponAttachmentsInfoFromSaveFile) {
initializeValues ();
2026-03-29 23:03:14 -07:00
return;
}
2026-03-29 23:03:14 -07:00
if (showDebugInfo) {
print ("\n\n");
2026-03-29 23:03:14 -07:00
print ("Loading player weapons");
}
2026-03-29 23:03:14 -07:00
persistanceInfoList = new List<persistanceWeaponInfo> ();
2026-03-29 23:03:14 -07:00
List<persistanceWeaponListBySaveSlotInfo> infoListToLoad = new List<persistanceWeaponListBySaveSlotInfo> ();
2026-03-29 23:03:14 -07:00
if (File.Exists (currentSaveDataPath)) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (currentSaveDataPath, FileMode.Open);
object currentData = bf.Deserialize (file);
infoListToLoad = currentData as List<persistanceWeaponListBySaveSlotInfo>;
2026-03-29 23:03:14 -07:00
file.Close ();
}
2026-03-29 23:03:14 -07:00
if (saveNumberToLoad > -1) {
persistanceWeaponListBySaveSlotInfo newPersistanceWeaponListBySaveSlotInfo = new persistanceWeaponListBySaveSlotInfo ();
2026-03-29 23:03:14 -07:00
int infoListToLoadCount = infoListToLoad.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < infoListToLoadCount; j++) {
if (infoListToLoad [j].saveNumber == saveNumberToLoad) {
newPersistanceWeaponListBySaveSlotInfo = infoListToLoad [j];
}
}
2026-03-29 23:03:14 -07:00
int listIndex = -1;
2026-03-29 23:03:14 -07:00
int playerWeaponListCount = newPersistanceWeaponListBySaveSlotInfo.playerWeaponList.Count;
2026-03-29 23:03:14 -07:00
for (int j = 0; j < playerWeaponListCount; j++) {
if (newPersistanceWeaponListBySaveSlotInfo.playerWeaponList [j].playerID == playerID) {
listIndex = j;
}
}
2026-03-29 23:03:14 -07:00
if (listIndex > -1) {
persistanceInfoList = newPersistanceWeaponListBySaveSlotInfo.playerWeaponList [listIndex].weaponList;
}
}
2026-03-29 23:03:14 -07:00
if (showDebugInfo) {
print ("\n\n");
2026-03-29 23:03:14 -07:00
print ("Weapons Loaded in Save Number " + saveNumberToLoad);
print ("Weapons amount: " + persistanceInfoList.Count);
2026-03-29 23:03:14 -07:00
for (int j = 0; j < persistanceInfoList.Count; j++) {
print ("Weapon Name: " + persistanceInfoList [j].Name + " Is Enabled: " + persistanceInfoList [j].isWeaponEnabled +
"Is Current Weapon: " + persistanceInfoList [j].isCurrentWeapon + " Remain Ammo: " + persistanceInfoList [j].remainingAmmo);
}
}
2026-03-29 23:03:14 -07:00
loadInfoOnMainComponent ();
}
2026-03-29 23:03:14 -07:00
public persistanceWeaponListByPlayerInfo getPersistanceList (int playerID, bool showDebugInfo)
{
persistanceWeaponListByPlayerInfo newPersistanceWeaponListByPlayerInfo = new persistanceWeaponListByPlayerInfo ();
2026-03-29 23:03:14 -07:00
newPersistanceWeaponListByPlayerInfo.playerID = playerID;
2026-03-29 23:03:14 -07:00
List<persistanceWeaponInfo> newPersistanceWeaponInfoList = new List<persistanceWeaponInfo> ();
2026-03-29 23:03:14 -07:00
List<IKWeaponSystem> weaponsList = mainPlayerWeaponsManager.weaponsList;
int weaponsListCount = weaponsList.Count;
for (int k = 0; k < weaponsListCount; k++) {
persistanceWeaponInfo newPersistanceWeaponInfo = new persistanceWeaponInfo ();
IKWeaponSystem currentWeapon = weaponsList [k];
newPersistanceWeaponInfo.Name = currentWeapon.getWeaponSystemName ();
newPersistanceWeaponInfo.index = k;
newPersistanceWeaponInfo.isWeaponEnabled = currentWeapon.isWeaponEnabled ();
newPersistanceWeaponInfo.isCurrentWeapon = currentWeapon.isCurrentWeapon ();
newPersistanceWeaponInfo.remainingAmmo = currentWeapon.getWeaponSystemManager ().weaponSettings.remainAmmo + currentWeapon.getWeaponSystemManager ().getWeaponClipSize ();
if (mainPlayerWeaponsManager.saveWeaponAttachmentsInfoToSaveFile) {
if (currentWeapon.checkIfWeaponUsesAttachment ()) {
newPersistanceWeaponInfo.weaponUsesAttachment = true;
weaponAttachmentSystem currentWeaponAttachmentSystem = currentWeapon.getWeaponAttachmentSystem ();
if (currentWeaponAttachmentSystem != null) {
newPersistanceWeaponInfo.weaponAttachmentPlaceList = currentWeaponAttachmentSystem.getPersistanceAttachmentWeaponInfoList ();
}
}
newPersistanceWeaponInfoList.Add (newPersistanceWeaponInfo);
}
}
newPersistanceWeaponListByPlayerInfo.weaponList = newPersistanceWeaponInfoList;
return newPersistanceWeaponListByPlayerInfo;
}
void loadInfoOnMainComponent ()
{
initializeValues ();
if (persistanceInfoList != null && persistanceInfoList.Count > 0) {
List<IKWeaponSystem> weaponsList = mainPlayerWeaponsManager.weaponsList;
if (mainPlayerWeaponsManager.storePickedWeaponsOnInventory) {
if (mainPlayerWeaponsManager.loadWeaponAttachmentsInfoFromSaveFile) {
if (mainPlayerWeaponsManager.loadCurrentPlayerWeaponsFromSaveFile) {
int persistanceInfoListCount = persistanceInfoList.Count;
for (int i = 0; i < persistanceInfoListCount; i++) {
persistanceWeaponInfo currentPersistanceWeaponInfo = persistanceInfoList [i];
int currentIndex = currentPersistanceWeaponInfo.index;
if (weaponsList.Count > currentIndex && currentIndex > -1) {
IKWeaponSystem currentIKWeaponToCheck = weaponsList [currentIndex];
if (currentPersistanceWeaponInfo.weaponUsesAttachment) {
weaponAttachmentSystem currentWeaponAttachmentSystem = currentIKWeaponToCheck.getWeaponAttachmentSystem ();
if (currentWeaponAttachmentSystem != null) {
currentWeaponAttachmentSystem.initializeAttachmentValues (currentPersistanceWeaponInfo.weaponAttachmentPlaceList);
}
}
}
}
}
}
return;
}
if (mainPlayerWeaponsManager.loadCurrentPlayerWeaponsFromSaveFile) {
bool thereIsCurrentWeapon = false;
int persistanceInfoListCount = persistanceInfoList.Count;
for (int i = 0; i < persistanceInfoListCount; i++) {
persistanceWeaponInfo currentPersistanceWeaponInfo = persistanceInfoList [i];
int currentIndex = currentPersistanceWeaponInfo.index;
if (weaponsList.Count > currentIndex && currentIndex > -1) {
IKWeaponSystem currentIKWeaponToCheck = weaponsList [currentIndex];
currentIKWeaponToCheck.setWeaponEnabledState (currentPersistanceWeaponInfo.isWeaponEnabled);
currentIKWeaponToCheck.setCurrentWeaponState (currentPersistanceWeaponInfo.isCurrentWeapon);
currentIKWeaponToCheck.weaponSystemManager.weaponSettings.remainAmmo = currentPersistanceWeaponInfo.remainingAmmo
- currentIKWeaponToCheck.weaponSystemManager.getWeaponClipSize ();
if (currentIKWeaponToCheck.isCurrentWeapon ()) {
thereIsCurrentWeapon = true;
}
if (mainPlayerWeaponsManager.loadWeaponAttachmentsInfoFromSaveFile) {
if (currentPersistanceWeaponInfo.weaponUsesAttachment) {
weaponAttachmentSystem currentWeaponAttachmentSystem = currentIKWeaponToCheck.getWeaponAttachmentSystem ();
if (currentWeaponAttachmentSystem != null) {
currentWeaponAttachmentSystem.initializeAttachmentValues (currentPersistanceWeaponInfo.weaponAttachmentPlaceList);
}
}
}
}
}
if (!thereIsCurrentWeapon) {
int weaponsListCount = weaponsList.Count;
for (int k = 0; k < weaponsListCount; k++) {
IKWeaponSystem currentIKWeaponToCheck = weaponsList [k];
if (!thereIsCurrentWeapon && currentIKWeaponToCheck.isWeaponEnabled ()) {
currentIKWeaponToCheck.setCurrentWeaponState (true);
thereIsCurrentWeapon = true;
}
}
}
}
}
}
void initializeValues ()
{
mainPlayerWeaponsManager.initializePlayerWeaponsValues ();
}
}