Exemple #1
0
    public MutantCharacter SpawnRandomAnimalCharacter(string name, AISquad squad, Vector3 loc)
    {
        MutantCharacter character = GameObject.Instantiate(Resources.Load("MutantAnimal") as GameObject).GetComponent <MutantCharacter>();

        character.CharacterID = name;

        character.Initialize();
        character.CharacterType      = CharacterType.Animal;
        character.MyNavAgent.enabled = false;
        character.Faction            = squad.Faction;
        character.SquadID            = squad.ID;

        GameManager.Inst.ItemManager.LoadNPCInventory(character.Inventory, squad.Faction, 0);
        character.MyAI.WeaponSystem.LoadWeaponsFromInventory(false);

        character.transform.position = loc;

        character.MyNavAgent.enabled = true;

        character.gameObject.name = character.gameObject.name + _counter.ToString();
        _counter++;



        return(character);
    }
    public bool Load(string saveGameName)
    {
        string          fullPath = Application.persistentDataPath + "/" + saveGameName + ".dat";
        BinaryFormatter bf       = new BinaryFormatter();
        FileStream      file;

        if (File.Exists(fullPath))
        {
            file = File.Open(fullPath, FileMode.Open);
        }
        else
        {
            return(false);
        }

        CurrentSave = (SaveGame)bf.Deserialize(file);

        //load level
        GameManager.Inst.WorldManager.AllLevels = CurrentSave.Levels;
        foreach (Level level in GameManager.Inst.WorldManager.AllLevels)
        {
            Debug.Log("Loading levels " + level.Name);
            if (level.Name == CurrentSave.LevelToLoad)
            {
                GameManager.Inst.WorldManager.CurrentLevel = level;
                break;
            }
        }

        GameManager.Inst.WorldManager.CurrentDay         = CurrentSave.CurrentDay;
        GameManager.Inst.WorldManager.CurrentTime        = CurrentSave.CurrentTime;
        GameManager.Inst.WorldManager.CurrentWeather     = CurrentSave.CurrentWeather;
        GameManager.Inst.WorldManager.IsDayTime          = CurrentSave.IsDayTime;
        GameManager.Inst.WorldManager.DayNightTransition = CurrentSave.DayNightTransition;
        GameManager.Inst.WorldManager.NightDayTransition = CurrentSave.NightDayTransition;

        GameManager.Inst.WorldManager.ChangeEnvironment(CurrentSave.CurrentEnvironmentName);

        //load player basics
        GameManager.Inst.PlayerProgress.PlayerFirstName = CurrentSave.PlayerFirstName;
        GameManager.Inst.PlayerProgress.PlayerLastName  = CurrentSave.PlayerLastName;
        //load progress
        GameManager.Inst.PlayerProgress.DiscoveredTopics = new List <string>(CurrentSave.DiscoveredTopics);
        GameManager.Inst.PlayerProgress.JournalEntries   = new List <List <string> >(CurrentSave.JournalEntries);
        GameManager.Inst.PlayerProgress.IncompleteTasks  = new List <int>(CurrentSave.IncompleteTasks);
        GameManager.Inst.PlayerProgress.CompletedTasks   = new List <int>(CurrentSave.CompletedTasks);
        //load player status
        GameManager.Inst.PlayerControl.SelectedPC.MyStatus.Data = CurrentSave.PlayerStatus;
        //load player boosts
        GameManager.Inst.PlayerControl.Survival.SetStatBoosts(CurrentSave.PlayerBoosts);
        //load player inventory
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.ArmorSlot    = CurrentSave.PlayerInventory.ArmorSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.HeadSlot     = CurrentSave.PlayerInventory.HeadSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.RifleSlot    = CurrentSave.PlayerInventory.RifleSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.SideArmSlot  = CurrentSave.PlayerInventory.SideArmSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.ThrowSlot    = CurrentSave.PlayerInventory.ThrowSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.ToolSlot     = CurrentSave.PlayerInventory.ToolSlot;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.BackpackCols = CurrentSave.PlayerInventory.BackpackCols;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.BackpackRows = CurrentSave.PlayerInventory.BackpackRows;
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.Backpack     = new List <GridItemData>(CurrentSave.PlayerInventory.Backpack);
        GameManager.Inst.PlayerControl.SelectedPC.Inventory.PostLoad();

        GameManager.Inst.PlayerControl.SelectedPC.ArmorSystem.SwitchToArmor(GameManager.Inst.PlayerControl.SelectedPC.Inventory.ArmorSlot);
        GameManager.Inst.PlayerControl.SelectedPC.ArmorSystem.SwitchToHelmet(GameManager.Inst.PlayerControl.SelectedPC.Inventory.HeadSlot);
        GameManager.Inst.PlayerControl.SelectedPC.MyAI.WeaponSystem.LoadWeaponsFromInventory(false);

        //place player in last saved location
        GameManager.Inst.PlayerControl.SelectedPC.transform.position = new Vector3(CurrentSave.PlayerLocation[0], CurrentSave.PlayerLocation[1], CurrentSave.PlayerLocation[2]);

        //load story conditions
        GameManager.Inst.QuestManager.StoryConditions = new Dictionary <string, StoryCondition>();
        foreach (StoryConditionItem condition in CurrentSave.ItemConditions)
        {
            GameManager.Inst.QuestManager.StoryConditions.Add(condition.ID, condition);
        }
        foreach (StoryConditionTrigger condition in CurrentSave.TriggerConditions)
        {
            GameManager.Inst.QuestManager.StoryConditions.Add(condition.ID, condition);
        }

        //load story event handler
        LinkedList <StoryEventListener> [] storyListenerLists = StoryEventHandler.Instance.AllListenerLists;

        for (int i = 0; i < storyListenerLists.Length; i++)
        {
            storyListenerLists[i].Clear();
            for (int j = 0; j < CurrentSave.StoryListenerLists[i].Count; j++)
            {
                storyListenerLists[i].AddLast(CurrentSave.StoryListenerLists[i][j]);
            }
        }

        StoryEventHandler.Instance.StoryEventQueue    = StoryEventHandler.Instance.ConvertListStoryEventToQueue(CurrentSave.StoryEventList);
        StoryEventHandler.Instance.CurrentStoryEvent  = CurrentSave.CurrentStoryEvent;
        StoryEventHandler.Instance.IsCurrentEventDone = CurrentSave.IsCurrentEventDone;


        //load pickup items
        Level currentLevel = GameManager.Inst.WorldManager.CurrentLevel;

        //remove all existing pickup items
        GameObject [] objects = GameObject.FindGameObjectsWithTag("PickupItem");

        foreach (GameObject o in objects)
        {
            GameObject.Destroy(o);
        }
        //create new ones
        foreach (PickupItemData pickupItemData in currentLevel.PickupItemDatas)
        {
            //create an Item from ItemID
            Item item     = GameManager.Inst.ItemManager.LoadItem(pickupItemData.ItemID);
            var  resource = Resources.Load(item.PrefabName + "Pickup");
            if (resource != null)
            {
                GameObject pickup = GameObject.Instantiate(resource) as GameObject;
                pickup.transform.position         = pickupItemData.Pos.ConvertToVector3();
                pickup.transform.localEulerAngles = pickupItemData.EulerAngles.ConvertToVector3();
                Transform parent = GameManager.Inst.ItemManager.FindPickupItemParent(pickup.transform);
                if (parent != null)
                {
                    pickup.transform.parent = parent;
                }
                pickup.GetComponent <PickupItem>().Item       = item;
                pickup.GetComponent <PickupItem>().Quantity   = pickupItemData.Quantity;
                pickup.GetComponent <PickupItem>().Durability = pickupItemData.Durability;
            }
        }

        //load Chests
        //clear all existing chests in this level and then add new content
        objects = GameObject.FindGameObjectsWithTag("Chest");

        foreach (GameObject o in objects)
        {
            Chest chest = o.GetComponent <Chest>();
            chest.Items.Clear();
            foreach (ChestData chestData in currentLevel.ChestDatas)
            {
                if (chest.ChestID == chestData.ChestID)
                {
                    chest.Items    = chestData.Items;
                    chest.IsLocked = chestData.IsLocked;
                    chest.KeyID    = chestData.KeyID;
                    chest.PostLoad();
                }
            }
        }

        //load doors
        GameObject [] doors = GameObject.FindGameObjectsWithTag("Door");
        foreach (GameObject o in doors)
        {
            Door door = o.GetComponent <Door>();
            foreach (DoorSaveData doorData in currentLevel.Doors)
            {
                if (door.ID == "zsk_blockadedoor")
                {
                    Debug.Log("blocade door unlocked? " + doorData.ID + " - " + doorData.IsLocked);
                }
                if (door.ID == doorData.ID)
                {
                    door.IsLocked = doorData.IsLocked;
                    door.IsOpen   = doorData.IsOpen;
                }
            }
        }

        //load factions
        GameManager.Inst.NPCManager.AllFactions.Clear();
        foreach (KeyValuePair <Faction, FactionData> factionData in CurrentSave.Factions)
        {
            GameManager.Inst.NPCManager.AllFactions.Add(factionData.Key, factionData.Value);
            factionData.Value.PostLoad();
        }


        //load household
        GameManager.Inst.NPCManager.AllSquads.Clear();
        Household [] households = (Household[])GameObject.FindObjectsOfType <Household>();
        foreach (Household household in households)
        {
            foreach (HouseholdSaveData saveData in currentLevel.Households)
            {
                if (household.name == saveData.HouseholdName)
                {
                    if (saveData.CurrentSquadID != "")
                    {
                        //create a new squad
                        AISquad squad = new AISquad();
                        squad.ID        = saveData.CurrentSquadID;
                        squad.Tier      = saveData.CurrentSquadTier;
                        squad.Faction   = saveData.OwningFaction;
                        squad.Household = household;
                        squad.Household.CurrentSquad = squad;
                        if (!GameManager.Inst.NPCManager.AllSquads.ContainsKey(squad.ID))
                        {
                            GameManager.Inst.NPCManager.AllSquads.Add(squad.ID, squad);
                        }
                    }
                    household.IsMemberAlreadyAdded = true;
                    household.SetScheduleData(saveData.IsRefilledToday, saveData.Expedition1SentToday, saveData.Expedition2SentToday,
                                              saveData.ExpeditionTime1, saveData.ExpeditionTime2);
                }
            }
        }



        //load characters
        //first remove all existing characters
        GameObject [] npcs = GameObject.FindGameObjectsWithTag("NPC");
        foreach (GameObject npc in npcs)
        {
            npc.tag = "Untagged";
            GameObject.Destroy(npc.gameObject);
        }
        //add new characters from save
        //then reinitialize NPCManager

        npcs = GameObject.FindGameObjectsWithTag("NPC");
        Debug.Log("Number of loading NPC before loading " + npcs.Length);

        foreach (CharacterSaveData characterData in currentLevel.Characters)
        {
            if (characterData.CharacterType == CharacterType.Human)
            {
                HumanCharacter character = GameObject.Instantiate(Resources.Load("HumanCharacter") as GameObject).GetComponent <HumanCharacter>();

                character.CharacterID = characterData.CharacterID;
                character.GoapID      = characterData.GoapID;

                character.Initialize();
                character.MyNavAgent.enabled = false;
                character.CharacterType      = characterData.CharacterType;
                character.SquadID            = characterData.SquadID;
                character.Faction            = characterData.Faction;
                character.IsCommander        = characterData.IsCommander;
                character.IsEssential        = characterData.IsEssential;


                character.MyStatus.Data              = characterData.StatusData;
                character.transform.position         = characterData.Pos.ConvertToVector3();
                character.transform.localEulerAngles = characterData.Angles.ConvertToVector3();

                character.MyNavAgent.enabled = true;

                character.Name            = characterData.Name;
                character.Title           = characterData.Title;
                character.gameObject.name = characterData.GOName;

                //attach job components
                if (characterData.Jobs.Contains(NPCJobs.Trader))
                {
                    character.gameObject.AddComponent <Trader>();
                }

                character.Inventory              = new CharacterInventory();
                character.Inventory.ArmorSlot    = characterData.Inventory.ArmorSlot;
                character.Inventory.HeadSlot     = characterData.Inventory.HeadSlot;
                character.Inventory.RifleSlot    = characterData.Inventory.RifleSlot;
                character.Inventory.SideArmSlot  = characterData.Inventory.SideArmSlot;
                character.Inventory.ThrowSlot    = characterData.Inventory.ThrowSlot;
                character.Inventory.ToolSlot     = characterData.Inventory.ToolSlot;
                character.Inventory.BackpackCols = characterData.Inventory.BackpackCols;
                character.Inventory.BackpackRows = characterData.Inventory.BackpackRows;
                character.Inventory.Backpack     = new List <GridItemData>(characterData.Inventory.Backpack);
                character.Inventory.PostLoad();
            }
            else if (characterData.CharacterType == CharacterType.Mutant)
            {
                MutantCharacter character = GameObject.Instantiate(Resources.Load("MutantCharacter") as GameObject).GetComponent <MutantCharacter>();
                character.CharacterID = characterData.CharacterID;
                character.GoapID      = characterData.GoapID;

                character.Initialize();
                character.MyNavAgent.enabled = false;
                character.CharacterType      = characterData.CharacterType;
                character.SquadID            = characterData.SquadID;
                character.Faction            = characterData.Faction;
                GameManager.Inst.ItemManager.LoadNPCInventory(character.Inventory, character.Faction, 0);
                character.Inventory.PostLoad();

                character.MyStatus.Data      = characterData.StatusData;
                character.transform.position = characterData.Pos.ConvertToVector3();

                character.MyNavAgent.enabled = true;

                character.Name            = characterData.Name;
                character.Title           = characterData.Title;
                character.gameObject.name = characterData.GOName;
            }
            else if (characterData.CharacterType == CharacterType.Animal)
            {
                MutantCharacter character = GameObject.Instantiate(Resources.Load("MutantAnimal") as GameObject).GetComponent <MutantCharacter>();
                character.CharacterID = characterData.CharacterID;
                character.GoapID      = characterData.GoapID;

                character.Initialize();
                character.MyNavAgent.enabled = false;
                character.CharacterType      = characterData.CharacterType;
                character.SquadID            = characterData.SquadID;
                character.Faction            = characterData.Faction;
                GameManager.Inst.ItemManager.LoadNPCInventory(character.Inventory, character.Faction, 0);
                character.Inventory.PostLoad();

                character.MyStatus.Data      = characterData.StatusData;
                character.transform.position = characterData.Pos.ConvertToVector3();

                character.MyNavAgent.enabled = true;

                character.Name            = characterData.Name;
                character.Title           = characterData.Title;
                character.gameObject.name = characterData.GOName;
            }
        }


        GameManager.Inst.NPCManager.Initialize();

        //add player to NPC manager
        GameManager.Inst.NPCManager.AddHumanCharacter(GameManager.Inst.PlayerControl.SelectedPC);

        //load traders
        foreach (HumanCharacter character in GameManager.Inst.NPCManager.HumansInScene)
        {
            Trader trader = character.GetComponent <Trader>();
            if (trader != null)
            {
                Debug.Log("loading trader, on character " + character.CharacterID);
                foreach (TraderData traderData in currentLevel.Traders)
                {
                    if (traderData.CharacterID == character.CharacterID)
                    {
                        trader.Cash             = traderData.Cash;
                        trader.Tier             = traderData.Tier;
                        trader.TraderInventory  = traderData.TraderInventory;
                        trader.SupplyRenewTimer = traderData.SupplyRenewTimer;
                        trader.PostLoad();
                    }
                }
            }
        }



        return(true);
    }