/**
         * <summary>Serialises appropriate GameObject values into a string.</summary>
         * <returns>The data, serialised as a string</returns>
         */
        public override string SaveData()
        {
            NPCData npcData = new NPCData();

            npcData.objectID      = constantID;
            npcData.savePrevented = savePrevented;

            if (gameObject.layer == LayerMask.NameToLayer(KickStarter.settingsManager.hotspotLayer))
            {
                npcData.isOn = true;
            }
            else
            {
                npcData.isOn = false;
            }

            npcData.LocX = transform.position.x;
            npcData.LocY = transform.position.y;
            npcData.LocZ = transform.position.z;

            npcData.RotX = transform.eulerAngles.x;
            npcData.RotY = transform.eulerAngles.y;
            npcData.RotZ = transform.eulerAngles.z;

            npcData.ScaleX = transform.localScale.x;
            npcData.ScaleY = transform.localScale.y;
            npcData.ScaleZ = transform.localScale.z;

            if (GetComponent <NPC>())
            {
                NPC npc = GetComponent <NPC>();
                npcData = npc.SaveData(npcData);
            }

            return(Serializer.SaveScriptData <NPCData> (npcData));
        }
		public NPCData SaveData ()
		{
			NPCData npcData = new NPCData();
			
			npcData.objectID = constantID;
			
			if (gameObject.layer == LayerMask.NameToLayer (AdvGame.GetReferences ().settingsManager.hotspotLayer))
			{
				npcData.isOn = true;
			}
			else
			{
				npcData.isOn = false;
			}
			
			npcData.LocX = transform.position.x;
			npcData.LocY = transform.position.y;
			npcData.LocZ = transform.position.z;
			
			npcData.RotX = transform.eulerAngles.x;
			npcData.RotY = transform.eulerAngles.y;
			npcData.RotZ = transform.eulerAngles.z;
			
			npcData.ScaleX = transform.localScale.x;
			npcData.ScaleY = transform.localScale.y;
			npcData.ScaleZ = transform.localScale.z;
			
			if (GetComponent <NPC>())
			{
				NPC npc = GetComponent <NPC>();

				if (npc.animationEngine == AnimationEngine.Sprites2DToolkit || npc.animationEngine == AnimationEngine.SpritesUnity)
				{
					npcData.idleAnim = npc.idleAnimSprite;
					npcData.walkAnim = npc.walkAnimSprite;
					npcData.talkAnim = npc.talkAnimSprite;
					npcData.runAnim = npc.runAnimSprite;
				}
				else if (npc.animationEngine == AnimationEngine.Legacy)
				{
					npcData.idleAnim = AssetLoader.GetAssetInstanceID (npc.idleAnim).ToString ();
					npcData.walkAnim = AssetLoader.GetAssetInstanceID (npc.walkAnim).ToString ();
					npcData.runAnim = AssetLoader.GetAssetInstanceID (npc.runAnim).ToString ();
					npcData.talkAnim = AssetLoader.GetAssetInstanceID (npc.talkAnim).ToString ();
				}

				npcData.walkSound = AssetLoader.GetAssetInstanceID (npc.walkSound);
				npcData.runSound = AssetLoader.GetAssetInstanceID (npc.runSound);

				npcData.speechLabel = npc.speechLabel;
				npcData.portraitGraphic = AssetLoader.GetAssetInstanceID (npc.portraitIcon.texture);

				npcData.walkSpeed = npc.walkSpeedScale;
				npcData.runSpeed = npc.runSpeedScale;

				// Rendering
				npcData.lockDirection = npc.lockDirection;
				npcData.lockScale = npc.lockScale;
				if (npc.spriteChild && npc.spriteChild.GetComponent <FollowSortingMap>())
				{
					npcData.lockSorting = npc.spriteChild.GetComponent <FollowSortingMap>().lockSorting;
				}
				else if (npc.GetComponent <FollowSortingMap>())
				{
					npcData.lockSorting = npc.GetComponent <FollowSortingMap>().lockSorting;
				}
				else
				{
					npcData.lockSorting = false;
				}
				npcData.spriteDirection = npc.spriteDirection;
				npcData.spriteScale = npc.spriteScale;
				if (npc.spriteChild && npc.spriteChild.GetComponent<Renderer>())
				{
					npcData.sortingOrder = npc.spriteChild.GetComponent<Renderer>().sortingOrder;
					npcData.sortingLayer = npc.spriteChild.GetComponent<Renderer>().sortingLayerName;
				}
				else if (npc.GetComponent<Renderer>())
				{
					npcData.sortingOrder = npc.GetComponent<Renderer>().sortingOrder;
					npcData.sortingLayer = npc.GetComponent<Renderer>().sortingLayerName;
				}

				npcData.pathID = 0;
				npcData.lastPathID = 0;
				if (npc.GetPath (true))
				{
					npcData.targetNode = npc.GetTargetNode (true);
					npcData.prevNode = npc.GetPrevNode (true);
					npcData.isRunning = npc.isRunning;
					
					if (npc.GetPath (true) == GetComponent <Paths>())
					{
						npcData.pathData = Serializer.CreatePathData (GetComponent <Paths>());
					}
					else
					{
						if (npc.GetPath (true).GetComponent <ConstantID>())
						{
							npcData.pathID = npc.GetPath (true).GetComponent <ConstantID>().constantID;
						}
						else
						{
							Debug.LogWarning ("Want to save path data for " + name + " but path has no ID!");
						}
					}
				}

				if (npc.GetPath (false))
				{
					npcData.lastTargetNode = npc.GetTargetNode (false);
					npcData.lastPrevNode = npc.GetPrevNode (false);

					if (npc.GetPath (false).GetComponent <ConstantID>())
					{
						npcData.lastPathID = npc.GetPath (false).GetComponent <ConstantID>().constantID;
					}
					else
					{
						Debug.LogWarning ("Want to save previous path data for " + name + " but path has no ID!");
					}
				}
		
				if (npc.followTarget)
				{
					if (!npc.followTargetIsPlayer)
					{
						if (npc.followTarget.GetComponent <ConstantID>())
						{
							npcData.followTargetID = npc.followTarget.GetComponent <ConstantID>().constantID;
							npcData.followTargetIsPlayer = npc.followTargetIsPlayer;
							npcData.followFrequency = npc.followFrequency;
							npcData.followDistance = npc.followDistance;
							npcData.followDistanceMax= npc.followDistanceMax;
						}
						else
						{
							Debug.LogWarning ("Want to save follow data for " + name + " but " + npc.followTarget.name + " has no ID!");
						}
					}
					else
					{
						npcData.followTargetID = 0;
						npcData.followTargetIsPlayer = npc.followTargetIsPlayer;
						npcData.followFrequency = npc.followFrequency;
						npcData.followDistance = npc.followDistance;
						npcData.followDistanceMax = npc.followDistanceMax;
					}
				}
				else
				{
					npcData.followTargetID = 0;
					npcData.followTargetIsPlayer = false;
					npcData.followFrequency = 0f;
					npcData.followDistance = 0f;
					npcData.followDistanceMax = 0f;
				}

				if (npc.headFacing == HeadFacing.Manual)
				{
					npcData.isHeadTurning = true;
					npcData.headTargetX = npc.headTurnTarget.x;
					npcData.headTargetY = npc.headTurnTarget.y;
					npcData.headTargetZ = npc.headTurnTarget.z;
				}
				else
				{
					npcData.isHeadTurning = false;
					npcData.headTargetX = 0f;
					npcData.headTargetY = 0f;
					npcData.headTargetZ = 0f;
				}
			}
			
			return npcData;
		}
		public void LoadData (NPCData data)
		{
			if (data.isOn)
			{
				gameObject.layer = LayerMask.NameToLayer (AdvGame.GetReferences ().settingsManager.hotspotLayer);
			}
			else
			{
				gameObject.layer = LayerMask.NameToLayer (AdvGame.GetReferences ().settingsManager.deactivatedLayer);
			}
			
			transform.position = new Vector3 (data.LocX, data.LocY, data.LocZ);
			transform.eulerAngles = new Vector3 (data.RotX, data.RotY, data.RotZ);
			transform.localScale = new Vector3 (data.ScaleX, data.ScaleY, data.ScaleZ);
			
			if (GetComponent <NPC>())
			{
				NPC npc = GetComponent <NPC>();

				npc.EndPath ();
				
				if (npc.animationEngine == AnimationEngine.Sprites2DToolkit || npc.animationEngine == AnimationEngine.SpritesUnity)
				{
					npc.idleAnimSprite = data.idleAnim;
					npc.walkAnimSprite = data.walkAnim;
					npc.talkAnimSprite = data.talkAnim;
					npc.runAnimSprite = data.runAnim;
				}
				else if (npc.animationEngine == AnimationEngine.Legacy)
				{
					int _instanceID = 0;
					int.TryParse (data.idleAnim, out _instanceID);
					npc.idleAnim = AssetLoader.RetrieveAsset (npc.idleAnim, _instanceID);

					int.TryParse (data.walkAnim, out _instanceID);
					npc.walkAnim = AssetLoader.RetrieveAsset (npc.walkAnim, _instanceID);

					int.TryParse (data.talkAnim, out _instanceID);
					npc.runAnim = AssetLoader.RetrieveAsset (npc.runAnim, _instanceID);

					int.TryParse (data.runAnim, out _instanceID);
					npc.talkAnim = AssetLoader.RetrieveAsset (npc.talkAnim, _instanceID);
				}

				npc.walkSound = AssetLoader.RetrieveAsset (npc.walkSound, data.walkSound);
				npc.runSound = AssetLoader.RetrieveAsset (npc.runSound, data.runSound);
				npc.speechLabel = data.speechLabel;
				npc.portraitIcon.texture = AssetLoader.RetrieveAsset (npc.portraitIcon.texture, data.portraitGraphic);
					
				npc.walkSpeedScale = data.walkSpeed;
				npc.runSpeedScale = data.runSpeed;

				// Rendering
				npc.lockDirection = data.lockDirection;
				npc.lockScale = data.lockScale;
				if (npc.spriteChild && npc.spriteChild.GetComponent <FollowSortingMap>())
				{
					npc.spriteChild.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
				}
				else if (npc.GetComponent <FollowSortingMap>())
				{
					npc.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
				}
				else
				{
					npc.ReleaseSorting ();
				}
				
				if (data.lockDirection)
				{
					npc.spriteDirection = data.spriteDirection;
				}
				if (data.lockScale)
				{
					npc.spriteScale = data.spriteScale;
				}
				if (data.lockSorting)
				{
					if (npc.spriteChild && npc.spriteChild.GetComponent<Renderer>())
					{
						npc.spriteChild.GetComponent<Renderer>().sortingOrder = data.sortingOrder;
						npc.spriteChild.GetComponent<Renderer>().sortingLayerName = data.sortingLayer;
					}
					else if (npc.GetComponent<Renderer>())
					{
						npc.GetComponent<Renderer>().sortingOrder = data.sortingOrder;
						npc.GetComponent<Renderer>().sortingLayerName = data.sortingLayer;
					}
				}
			
				AC.Char charToFollow = null;
				if (data.followTargetID != 0)
				{
					RememberNPC followNPC = Serializer.returnComponent <RememberNPC> (data.followTargetID);
					if (followNPC.GetComponent <AC.Char>())
					{
						charToFollow = followNPC.GetComponent <AC.Char>();
					}
				}
				
				npc.FollowAssign (charToFollow, data.followTargetIsPlayer, data.followFrequency, data.followDistance, data.followDistanceMax);
				npc.Halt ();
				
				if (data.pathData != null && data.pathData != "" && GetComponent <Paths>())
				{
					Paths savedPath = GetComponent <Paths>();
					savedPath = Serializer.RestorePathData (savedPath, data.pathData);
					npc.SetPath (savedPath, data.targetNode, data.prevNode);
					npc.isRunning = data.isRunning;
				}
				else if (data.pathID != 0)
				{
					Paths pathObject = Serializer.returnComponent <Paths> (data.pathID);
					
					if (pathObject != null)
					{
						npc.SetPath (pathObject, data.targetNode, data.prevNode);
					}
					else
					{
						Debug.LogWarning ("Trying to assign a path for NPC " + this.name + ", but the path was not found - was it deleted?");
					}
				}

				if (data.lastPathID != 0)
				{
					Paths pathObject = Serializer.returnComponent <Paths> (data.lastPathID);
					
					if (pathObject != null)
					{
						npc.SetLastPath (pathObject, data.lastTargetNode, data.lastPrevNode);
					}
					else
					{
						Debug.LogWarning ("Trying to assign the previous path for NPC " + this.name + ", but the path was not found - was it deleted?");
					}
				}

				// Head target
				if (data.isHeadTurning)
				{
					npc.SetHeadTurnTarget (new Vector3 (data.headTargetX, data.headTargetY, data.headTargetZ), true);
				}
				else
				{
					npc.ClearHeadTurnTarget (true);
				}
			}
		}
        override public float Run()
        {
            if (KickStarter.settingsManager.playerSwitching == PlayerSwitching.Allow)
            {
                PlayerPrefab newPlayerPrefab = KickStarter.settingsManager.GetPlayerPrefab(playerID);

                if (newPlayerPrefab != null)
                {
                    if (KickStarter.player != null && KickStarter.player.ID == playerID)
                    {
                        Log("Cannot switch player - already controlling the desired prefab.");
                        return(0f);
                    }

                    if (newPlayerPrefab.playerOb != null)
                    {
                        KickStarter.saveSystem.SaveCurrentPlayerData();

                        Vector3    oldPlayerPosition = Vector3.zero;
                        Quaternion oldPlayerRotation = new Quaternion();
                        Vector3    oldPlayerScale    = Vector3.one;

                        PlayerData oldPlayerData         = new PlayerData();
                        NPCData    oldNPCData            = new NPCData();
                        bool       recordedOldPlayerData = false;
                        bool       recordedOldNPCData    = false;

                        if (KickStarter.player != null)
                        {
                            oldPlayerPosition = KickStarter.player.transform.position;
                            oldPlayerRotation = KickStarter.player.TransformRotation;
                            oldPlayerScale    = KickStarter.player.transform.localScale;

                            oldPlayerData         = KickStarter.player.SavePlayerData(oldPlayerData);
                            recordedOldPlayerData = true;
                        }

                        if (newPlayerPosition != NewPlayerPosition.ReplaceCurrentPlayer)
                        {
                            if (oldPlayer == OldPlayer.ReplaceWithAssociatedNPC &&
                                (runtimeOldPlayerNPC == null || !runtimeOldPlayerNPC.gameObject.activeInHierarchy) &&
                                KickStarter.player.associatedNPCPrefab != null)
                            {
                                GameObject newObject = (GameObject)Instantiate(KickStarter.player.associatedNPCPrefab.gameObject);
                                newObject.name      = KickStarter.player.associatedNPCPrefab.gameObject.name;
                                runtimeOldPlayerNPC = newObject.GetComponent <NPC>();
                            }

                            if ((oldPlayer == OldPlayer.ReplaceWithNPC || oldPlayer == OldPlayer.ReplaceWithAssociatedNPC) &&
                                runtimeOldPlayerNPC != null && runtimeOldPlayerNPC.gameObject.activeInHierarchy)
                            {
                                runtimeOldPlayerNPC.Teleport(oldPlayerPosition);
                                runtimeOldPlayerNPC.TransformRotation    = oldPlayerRotation;
                                runtimeOldPlayerNPC.transform.localScale = oldPlayerScale;

                                if (recordedOldPlayerData)
                                {
                                    ApplyRenderData(runtimeOldPlayerNPC, oldPlayerData);
                                }

                                // Force the rotation / sprite child to update
                                runtimeOldPlayerNPC._Update();
                            }
                        }

                        if (runtimeNewPlayerNPC == null || newPlayerPosition == NewPlayerPosition.ReplaceAssociatedNPC)
                        {
                            // Try to find from associated NPC prefab

                            if (newPlayerPrefab.playerOb.associatedNPCPrefab != null)
                            {
                                ConstantID prefabID = newPlayerPrefab.playerOb.associatedNPCPrefab.GetComponent <ConstantID>();
                                if (prefabID != null && prefabID.constantID != 0)
                                {
                                    newPlayerNPC_ID     = prefabID.constantID;
                                    runtimeNewPlayerNPC = AssignFile <NPC> (prefabID.constantID, null);
                                }
                            }
                        }

                        Quaternion newRotation = Quaternion.identity;
                        if (newPlayerPosition == NewPlayerPosition.ReplaceCurrentPlayer)
                        {
                            newRotation = oldPlayerRotation;
                        }
                        else if (newPlayerPosition == NewPlayerPosition.ReplaceNPC && runtimeNewPlayerNPC != null)
                        {
                            newRotation = runtimeNewPlayerNPC.TransformRotation;
                        }
                        else if (newPlayerPosition == NewPlayerPosition.AppearAtMarker && runtimeNewPlayerMarker != null)
                        {
                            newRotation = runtimeNewPlayerMarker.transform.rotation;
                        }

                        if (runtimeNewPlayerNPC != null)
                        {
                            oldNPCData = runtimeNewPlayerNPC.SaveData(oldNPCData);
                        }

                        bool replacesOldPlayer = newPlayerPosition == NewPlayerPosition.ReplaceCurrentPlayer &&
                                                 (!restorePreviousData || !KickStarter.saveSystem.DoesPlayerDataExist(playerID, true));
                        KickStarter.ResetPlayer(newPlayerPrefab.playerOb, playerID, true, newRotation, keepInventory, false, replacesOldPlayer, alwaysSnapCamera);
                        Player newPlayer = KickStarter.player;
                        PlayerMenus.ResetInventoryBoxes();

                        if (replacesOldPlayer && recordedOldPlayerData)
                        {
                            ApplyRenderData(newPlayer, oldPlayerData);
                        }

                        if (restorePreviousData && KickStarter.saveSystem.DoesPlayerDataExist(playerID, true))
                        {
                            int    sceneToLoad     = KickStarter.saveSystem.GetPlayerScene(playerID);
                            string sceneToLoadName = KickStarter.saveSystem.GetPlayerSceneName(playerID);

                            if (sceneToLoad >= 0 && sceneToLoad != UnityVersionHandler.GetCurrentSceneNumber())
                            {
                                KickStarter.saveSystem.loadingGame = LoadingGame.JustSwitchingPlayer;
                                KickStarter.sceneChanger.ChangeScene(new SceneInfo(string.Empty, sceneToLoad), true, false);
                            }
                            else if (!string.IsNullOrEmpty(sceneToLoadName) && sceneToLoadName != UnityVersionHandler.GetCurrentSceneName())
                            {
                                KickStarter.saveSystem.loadingGame = LoadingGame.JustSwitchingPlayer;
                                KickStarter.sceneChanger.ChangeScene(new SceneInfo(sceneToLoadName, -1), true, false);
                            }
                            else
                            {
                                // Same scene
                                if (runtimeNewPlayerNPC != null)
                                {
                                    newPlayer.RepositionToTransform(runtimeNewPlayerNPC.transform);
                                    runtimeNewPlayerNPC.HideFromView(newPlayer);
                                }
                            }
                        }
                        else
                        {
                            // No data to restore

                            if (newPlayerPosition == NewPlayerPosition.ReplaceCurrentPlayer)
                            {
                                newPlayer.Teleport(oldPlayerPosition);
                                newPlayer.SetRotation(oldPlayerRotation);
                                newPlayer.transform.localScale = oldPlayerScale;
                            }
                            else if (newPlayerPosition == NewPlayerPosition.ReplaceNPC || newPlayerPosition == NewPlayerPosition.ReplaceAssociatedNPC)
                            {
                                if (runtimeNewPlayerNPC != null)
                                {
                                    newPlayer.RepositionToTransform(runtimeNewPlayerNPC.transform);
                                    runtimeNewPlayerNPC.HideFromView(newPlayer);

                                    if (recordedOldNPCData)
                                    {
                                        ApplyRenderData(newPlayer, oldNPCData);
                                    }
                                }
                            }
                            else if (newPlayerPosition == NewPlayerPosition.AppearAtMarker)
                            {
                                if (runtimeNewPlayerMarker)
                                {
                                    newPlayer.RepositionToTransform(runtimeNewPlayerMarker.transform);
                                }
                            }
                            else if (newPlayerPosition == NewPlayerPosition.AppearInOtherScene)
                            {
                                if (chooseNewSceneBy == ChooseSceneBy.Name && newPlayerSceneName == UnityVersionHandler.GetCurrentSceneName() ||
                                    (chooseNewSceneBy == ChooseSceneBy.Number && newPlayerScene == UnityVersionHandler.GetCurrentSceneNumber()))
                                {
                                    // Already in correct scene
                                    if (runtimeNewPlayerNPC && runtimeNewPlayerNPC.gameObject.activeInHierarchy)
                                    {
                                        newPlayer.RepositionToTransform(runtimeNewPlayerNPC.transform);
                                        runtimeNewPlayerNPC.HideFromView(newPlayer);
                                    }
                                }
                                else
                                {
                                    KickStarter.sceneChanger.ChangeScene(new SceneInfo(chooseNewSceneBy, newPlayerSceneName, newPlayerScene), true, false, true);
                                }
                            }
                        }

                        if (KickStarter.mainCamera.attachedCamera && alwaysSnapCamera)
                        {
                            KickStarter.mainCamera.attachedCamera.MoveCameraInstant();
                        }

                        AssetLoader.UnloadAssets();
                    }
                    else
                    {
                        LogWarning("Cannot switch player - no player prefabs is defined.");
                    }
                }
            }

            return(0f);
        }
 public override void LoadNPCData(NPCData npcData, NPC npc)
 {
     npc.moveSpeedParameter = npcData.walkAnim;
     npc.talkParameter      = npcData.talkAnim;
     npc.turnParameter      = npcData.runAnim;;
 }
Exemple #6
0
        /**
         * <summary>Updates its own variables from a NPCData class.</summary>
         * <param name = "data">The NPCData class to load from</param>
         */
        public void LoadData(NPCData data)
        {
            charState = (data.inCustomCharState) ? CharState.Custom : CharState.Idle;

            EndPath();

            if (animationEngine == AnimationEngine.Sprites2DToolkit || animationEngine == AnimationEngine.SpritesUnity)
            {
                idleAnimSprite = data.idleAnim;
                walkAnimSprite = data.walkAnim;
                talkAnimSprite = data.talkAnim;
                runAnimSprite  = data.runAnim;
            }
            else if (animationEngine == AnimationEngine.Legacy)
            {
                idleAnim = AssetLoader.RetrieveAsset(idleAnim, data.idleAnim);
                walkAnim = AssetLoader.RetrieveAsset(walkAnim, data.walkAnim);
                runAnim  = AssetLoader.RetrieveAsset(runAnim, data.talkAnim);
                talkAnim = AssetLoader.RetrieveAsset(talkAnim, data.runAnim);
            }
            else if (animationEngine == AnimationEngine.Mecanim)
            {
                moveSpeedParameter = data.walkAnim;
                talkParameter      = data.talkAnim;
                turnParameter      = data.runAnim;;
            }

            walkSound = AssetLoader.RetrieveAsset(walkSound, data.walkSound);
            runSound  = AssetLoader.RetrieveAsset(runSound, data.runSound);

            if (data.speechLabel != "")
            {
                SetName(data.speechLabel, data.displayLineID);
            }

            portraitIcon.texture = AssetLoader.RetrieveAsset(portraitIcon.texture, data.portraitGraphic);
            portraitIcon.ClearSprites();
            portraitIcon.ClearCache();

            walkSpeedScale = data.walkSpeed;
            runSpeedScale  = data.runSpeed;

            // Rendering
            lockDirection = data.lockDirection;
            lockScale     = data.lockScale;
            if (spriteChild && spriteChild.GetComponent <FollowSortingMap>())
            {
                spriteChild.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else if (GetComponent <FollowSortingMap>())
            {
                GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else
            {
                ReleaseSorting();
            }

            if (data.lockDirection)
            {
                spriteDirection = data.spriteDirection;
            }
            if (data.lockScale)
            {
                spriteScale = data.spriteScale;
            }
            if (data.lockSorting)
            {
                if (spriteChild && spriteChild.GetComponent <Renderer>())
                {
                    spriteChild.GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                    spriteChild.GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
                else if (GetComponent <Renderer>())
                {
                    GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                    GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
            }

            AC.Char charToFollow = null;
            if (data.followTargetID != 0)
            {
                RememberNPC followNPC = Serializer.returnComponent <RememberNPC> (data.followTargetID);
                if (followNPC.GetComponent <AC.Char>())
                {
                    charToFollow = followNPC.GetComponent <AC.Char>();
                }
            }

            if (charToFollow != null || (data.followTargetIsPlayer && KickStarter.player != null))
            {
                FollowAssign(charToFollow, data.followTargetIsPlayer, data.followFrequency, data.followDistance, data.followDistanceMax, data.followFaceWhenIdle, data.followRandomDirection);
            }
            else
            {
                StopFollowing();
            }
            Halt();

            if (data.pathData != null && data.pathData != "" && GetComponent <Paths>())
            {
                Paths savedPath = GetComponent <Paths>();
                savedPath = Serializer.RestorePathData(savedPath, data.pathData);
                SetPath(savedPath, data.targetNode, data.prevNode, data.pathAffectY);
                isRunning = data.isRunning;
            }
            else if (data.pathID != 0)
            {
                Paths pathObject = Serializer.returnComponent <Paths> (data.pathID);

                if (pathObject != null)
                {
                    SetPath(pathObject, data.targetNode, data.prevNode);
                }
                else
                {
                    ACDebug.LogWarning("Trying to assign a path for NPC " + this.name + ", but the path was not found - was it deleted?", gameObject);
                }
            }

            if (data.lastPathID != 0)
            {
                Paths pathObject = Serializer.returnComponent <Paths> (data.lastPathID);

                if (pathObject != null)
                {
                    SetLastPath(pathObject, data.lastTargetNode, data.lastPrevNode);
                }
                else
                {
                    ACDebug.LogWarning("Trying to assign the previous path for NPC " + this.name + ", but the path was not found - was it deleted?", gameObject);
                }
            }

            // Head target
            if (data.isHeadTurning)
            {
                ConstantID _headTargetID = Serializer.returnComponent <ConstantID> (data.headTargetID);
                if (_headTargetID != null)
                {
                    SetHeadTurnTarget(_headTargetID.transform, new Vector3(data.headTargetX, data.headTargetY, data.headTargetZ), true);
                }
                else
                {
                    ClearHeadTurnTarget(true);
                }
            }
            else
            {
                ClearHeadTurnTarget(true);
            }

            if (GetComponentsInChildren <FollowSortingMap>() != null)
            {
                FollowSortingMap[] followSortingMaps = GetComponentsInChildren <FollowSortingMap>();
                SortingMap         customSortingMap  = Serializer.returnComponent <SortingMap> (data.customSortingMapID);

                foreach (FollowSortingMap followSortingMap in followSortingMaps)
                {
                    followSortingMap.followSortingMap = data.followSortingMap;
                    if (!data.followSortingMap && customSortingMap != null)
                    {
                        followSortingMap.SetSortingMap(customSortingMap);
                    }
                    else
                    {
                        followSortingMap.SetSortingMap(KickStarter.sceneSettings.sortingMap);
                    }
                }
            }
        }
Exemple #7
0
        /**
         * <summary>Updates a NPCData class with its own variables that need saving.</summary>
         * <param name = "npcData">The original NPCData class</param>
         * <returns>The updated NPCData class</returns>
         */
        public NPCData SaveData(NPCData npcData)
        {
            npcData.RotX = TransformRotation.eulerAngles.x;
            npcData.RotY = TransformRotation.eulerAngles.y;
            npcData.RotZ = TransformRotation.eulerAngles.z;

            npcData.inCustomCharState = (charState == CharState.Custom && GetAnimator() != null && GetAnimator().GetComponent <RememberAnimator>());

            if (animationEngine == AnimationEngine.Sprites2DToolkit || animationEngine == AnimationEngine.SpritesUnity)
            {
                npcData.idleAnim = idleAnimSprite;
                npcData.walkAnim = walkAnimSprite;
                npcData.talkAnim = talkAnimSprite;
                npcData.runAnim  = runAnimSprite;
            }
            else if (animationEngine == AnimationEngine.Legacy)
            {
                npcData.idleAnim = AssetLoader.GetAssetInstanceID(idleAnim);
                npcData.walkAnim = AssetLoader.GetAssetInstanceID(walkAnim);
                npcData.runAnim  = AssetLoader.GetAssetInstanceID(runAnim);
                npcData.talkAnim = AssetLoader.GetAssetInstanceID(talkAnim);
            }
            else if (animationEngine == AnimationEngine.Mecanim)
            {
                npcData.walkAnim = moveSpeedParameter;
                npcData.talkAnim = talkParameter;
                npcData.runAnim  = turnParameter;
            }

            npcData.walkSound = AssetLoader.GetAssetInstanceID(walkSound);
            npcData.runSound  = AssetLoader.GetAssetInstanceID(runSound);

            npcData.speechLabel     = GetName();
            npcData.displayLineID   = displayLineID;
            npcData.portraitGraphic = AssetLoader.GetAssetInstanceID(portraitIcon.texture);

            npcData.walkSpeed = walkSpeedScale;
            npcData.runSpeed  = runSpeedScale;

            // Rendering
            npcData.lockDirection = lockDirection;
            npcData.lockScale     = lockScale;
            if (spriteChild && spriteChild.GetComponent <FollowSortingMap>())
            {
                npcData.lockSorting = spriteChild.GetComponent <FollowSortingMap>().lockSorting;
            }
            else if (GetComponent <FollowSortingMap>())
            {
                npcData.lockSorting = GetComponent <FollowSortingMap>().lockSorting;
            }
            else
            {
                npcData.lockSorting = false;
            }
            npcData.spriteDirection = spriteDirection;
            npcData.spriteScale     = spriteScale;
            if (spriteChild && spriteChild.GetComponent <Renderer>())
            {
                npcData.sortingOrder = spriteChild.GetComponent <Renderer>().sortingOrder;
                npcData.sortingLayer = spriteChild.GetComponent <Renderer>().sortingLayerName;
            }
            else if (GetComponent <Renderer>())
            {
                npcData.sortingOrder = GetComponent <Renderer>().sortingOrder;
                npcData.sortingLayer = GetComponent <Renderer>().sortingLayerName;
            }

            npcData.pathID     = 0;
            npcData.lastPathID = 0;
            if (GetPath())
            {
                npcData.targetNode  = GetTargetNode();
                npcData.prevNode    = GetPreviousNode();
                npcData.isRunning   = isRunning;
                npcData.pathAffectY = GetPath().affectY;

                if (GetPath() == GetComponent <Paths>())
                {
                    npcData.pathData = Serializer.CreatePathData(GetComponent <Paths>());
                }
                else
                {
                    if (GetPath().GetComponent <ConstantID>())
                    {
                        npcData.pathID = GetPath().GetComponent <ConstantID>().constantID;
                    }
                    else
                    {
                        ACDebug.LogWarning("Want to save path data for " + name + " but path has no ID!", gameObject);
                    }
                }
            }

            if (GetLastPath())
            {
                npcData.lastTargetNode = GetLastTargetNode();
                npcData.lastPrevNode   = GetLastPrevNode();

                if (GetLastPath().GetComponent <ConstantID>())
                {
                    npcData.lastPathID = GetLastPath().GetComponent <ConstantID>().constantID;
                }
                else
                {
                    ACDebug.LogWarning("Want to save previous path data for " + name + " but path has no ID!", gameObject);
                }
            }

            if (followTarget)
            {
                if (!followTargetIsPlayer)
                {
                    if (followTarget.GetComponent <ConstantID>())
                    {
                        npcData.followTargetID        = followTarget.GetComponent <ConstantID>().constantID;
                        npcData.followTargetIsPlayer  = followTargetIsPlayer;
                        npcData.followFrequency       = followFrequency;
                        npcData.followDistance        = followDistance;
                        npcData.followDistanceMax     = followDistanceMax;
                        npcData.followFaceWhenIdle    = followFaceWhenIdle;
                        npcData.followRandomDirection = followRandomDirection;
                    }
                    else
                    {
                        ACDebug.LogWarning("Want to save follow data for " + name + " but " + followTarget.name + " has no ID!", gameObject);
                    }
                }
                else
                {
                    npcData.followTargetID       = 0;
                    npcData.followTargetIsPlayer = followTargetIsPlayer;
                    npcData.followFrequency      = followFrequency;
                    npcData.followDistance       = followDistance;
                    npcData.followDistanceMax    = followDistanceMax;
                    //followFaceWhenIdle = false;
                    npcData.followFaceWhenIdle    = followFaceWhenIdle;
                    npcData.followRandomDirection = followRandomDirection;
                }
            }
            else
            {
                npcData.followTargetID        = 0;
                npcData.followTargetIsPlayer  = false;
                npcData.followFrequency       = 0f;
                npcData.followDistance        = 0f;
                npcData.followDistanceMax     = 0f;
                npcData.followFaceWhenIdle    = false;
                npcData.followRandomDirection = false;
            }

            if (headFacing == HeadFacing.Manual && headTurnTarget != null)
            {
                npcData.isHeadTurning = true;
                npcData.headTargetID  = Serializer.GetConstantID(headTurnTarget);
                if (npcData.headTargetID == 0)
                {
                    ACDebug.LogWarning("The NPC " + gameObject.name + "'s head-turning target Transform, " + headTurnTarget + ", was not saved because it has no Constant ID", gameObject);
                }
                npcData.headTargetX = headTurnTargetOffset.x;
                npcData.headTargetY = headTurnTargetOffset.y;
                npcData.headTargetZ = headTurnTargetOffset.z;
            }
            else
            {
                npcData.isHeadTurning = false;
                npcData.headTargetID  = 0;
                npcData.headTargetX   = 0f;
                npcData.headTargetY   = 0f;
                npcData.headTargetZ   = 0f;
            }

            if (GetComponentInChildren <FollowSortingMap>() != null)
            {
                FollowSortingMap followSortingMap = GetComponentInChildren <FollowSortingMap>();
                npcData.followSortingMap = followSortingMap.followSortingMap;
                if (!npcData.followSortingMap && followSortingMap.GetSortingMap() != null)
                {
                    if (followSortingMap.GetSortingMap().GetComponent <ConstantID>() != null)
                    {
                        npcData.customSortingMapID = followSortingMap.GetSortingMap().GetComponent <ConstantID>().constantID;
                    }
                    else
                    {
                        ACDebug.LogWarning("The NPC " + gameObject.name + "'s SortingMap, " + followSortingMap.GetSortingMap().name + ", was not saved because it has no Constant ID");
                        npcData.customSortingMapID = 0;
                    }
                }
                else
                {
                    npcData.customSortingMapID = 0;
                }
            }
            else
            {
                npcData.followSortingMap   = false;
                npcData.customSortingMapID = 0;
            }

            return(npcData);
        }
        /**
         * <summary>Serialises appropriate GameObject values into a string.</summary>
         * <returns>The data, serialised as a string</returns>
         */
        public override string SaveData()
        {
            NPCData npcData = new NPCData();

            npcData.objectID = constantID;

            if (gameObject.layer == LayerMask.NameToLayer (KickStarter.settingsManager.hotspotLayer))
            {
                npcData.isOn = true;
            }
            else
            {
                npcData.isOn = false;
            }

            npcData.LocX = transform.position.x;
            npcData.LocY = transform.position.y;
            npcData.LocZ = transform.position.z;

            npcData.RotX = transform.eulerAngles.x;
            npcData.RotY = transform.eulerAngles.y;
            npcData.RotZ = transform.eulerAngles.z;

            npcData.ScaleX = transform.localScale.x;
            npcData.ScaleY = transform.localScale.y;
            npcData.ScaleZ = transform.localScale.z;

            if (GetComponent <NPC>())
            {
                NPC npc = GetComponent <NPC>();
                npcData = npc.SaveData (npcData);
            }

            return Serializer.SaveScriptData <NPCData> (npcData);
        }
Exemple #9
0
 public virtual NPCData SaveNPCData(NPCData npcData, NPC npc)
 {
     return(npcData);
 }
Exemple #10
0
 public virtual void LoadNPCData(NPCData npcData, NPC npc)
 {
 }
Exemple #11
0
        /**
         * <summary>Updates it's own variables from a NPCData class.</summary>
         * <param name = "data">The NPCData class to load from</param>
         */
        public void LoadData(NPCData data)
        {
            EndPath ();

            if (animationEngine == AnimationEngine.Sprites2DToolkit || animationEngine == AnimationEngine.SpritesUnity)
            {
                idleAnimSprite = data.idleAnim;
                walkAnimSprite = data.walkAnim;
                talkAnimSprite = data.talkAnim;
                runAnimSprite = data.runAnim;
            }
            else if (animationEngine == AnimationEngine.Legacy)
            {
                idleAnim = AssetLoader.RetrieveAsset (idleAnim, data.idleAnim);
                walkAnim = AssetLoader.RetrieveAsset (walkAnim, data.walkAnim);
                runAnim = AssetLoader.RetrieveAsset (runAnim, data.talkAnim);
                talkAnim = AssetLoader.RetrieveAsset (talkAnim, data.runAnim);
            }
            else if (animationEngine == AnimationEngine.Mecanim)
            {
                moveSpeedParameter = data.walkAnim;
                talkParameter = data.talkAnim;
                turnParameter = data.runAnim;;
            }

            walkSound = AssetLoader.RetrieveAsset (walkSound, data.walkSound);
            runSound = AssetLoader.RetrieveAsset (runSound, data.runSound);
            speechLabel = data.speechLabel;
            portraitIcon.texture = AssetLoader.RetrieveAsset (portraitIcon.texture, data.portraitGraphic);

            walkSpeedScale = data.walkSpeed;
            runSpeedScale = data.runSpeed;

            // Rendering
            lockDirection = data.lockDirection;
            lockScale = data.lockScale;
            if (spriteChild && spriteChild.GetComponent <FollowSortingMap>())
            {
                spriteChild.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else if (GetComponent <FollowSortingMap>())
            {
                GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else
            {
                ReleaseSorting ();
            }

            if (data.lockDirection)
            {
                spriteDirection = data.spriteDirection;
            }
            if (data.lockScale)
            {
                spriteScale = data.spriteScale;
            }
            if (data.lockSorting)
            {
                if (spriteChild && spriteChild.GetComponent <Renderer>())
                {
                    spriteChild.GetComponent <Renderer>().sortingOrder = data.sortingOrder;
                    spriteChild.GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
                else if (GetComponent <Renderer>())
                {
                    GetComponent <Renderer>().sortingOrder = data.sortingOrder;
                    GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
            }

            AC.Char charToFollow = null;
            if (data.followTargetID != 0)
            {
                RememberNPC followNPC = Serializer.returnComponent <RememberNPC> (data.followTargetID);
                if (followNPC.GetComponent <AC.Char>())
                {
                    charToFollow = followNPC.GetComponent <AC.Char>();
                }
            }

            FollowAssign (charToFollow, data.followTargetIsPlayer, data.followFrequency, data.followDistance, data.followDistanceMax);
            Halt ();

            if (data.pathData != null && data.pathData != "" && GetComponent <Paths>())
            {
                Paths savedPath = GetComponent <Paths>();
                savedPath = Serializer.RestorePathData (savedPath, data.pathData);
                SetPath (savedPath, data.targetNode, data.prevNode, data.pathAffectY);
                isRunning = data.isRunning;
            }
            else if (data.pathID != 0)
            {
                Paths pathObject = Serializer.returnComponent <Paths> (data.pathID);

                if (pathObject != null)
                {
                    SetPath (pathObject, data.targetNode, data.prevNode);
                }
                else
                {
                    Debug.LogWarning ("Trying to assign a path for NPC " + this.name + ", but the path was not found - was it deleted?");
                }
            }

            if (data.lastPathID != 0)
            {
                Paths pathObject = Serializer.returnComponent <Paths> (data.lastPathID);

                if (pathObject != null)
                {
                    SetLastPath (pathObject, data.lastTargetNode, data.lastPrevNode);
                }
                else
                {
                    Debug.LogWarning ("Trying to assign the previous path for NPC " + this.name + ", but the path was not found - was it deleted?");
                }
            }

            // Head target
            if (data.isHeadTurning)
            {
                SetHeadTurnTarget (new Vector3 (data.headTargetX, data.headTargetY, data.headTargetZ), true);
            }
            else
            {
                ClearHeadTurnTarget (true);
            }
        }
Exemple #12
0
        /**
         * <summary>Updates a NPCData class with it's own variables that need saving.</summary>
         * <param name = "npcData">The original NPCData class</param>
         * <returns>The updated NPCData class</returns>
         */
        public NPCData SaveData(NPCData npcData)
        {
            if (animationEngine == AnimationEngine.Sprites2DToolkit || animationEngine == AnimationEngine.SpritesUnity)
            {
                npcData.idleAnim = idleAnimSprite;
                npcData.walkAnim = walkAnimSprite;
                npcData.talkAnim = talkAnimSprite;
                npcData.runAnim = runAnimSprite;
            }
            else if (animationEngine == AnimationEngine.Legacy)
            {
                npcData.idleAnim = AssetLoader.GetAssetInstanceID (idleAnim);
                npcData.walkAnim = AssetLoader.GetAssetInstanceID (walkAnim);
                npcData.runAnim = AssetLoader.GetAssetInstanceID (runAnim);
                npcData.talkAnim = AssetLoader.GetAssetInstanceID (talkAnim);
            }
            else if (animationEngine == AnimationEngine.Mecanim)
            {
                npcData.walkAnim = moveSpeedParameter;
                npcData.talkAnim = talkParameter;
                npcData.runAnim = turnParameter;
            }

            npcData.walkSound = AssetLoader.GetAssetInstanceID (walkSound);
            npcData.runSound = AssetLoader.GetAssetInstanceID (runSound);

            npcData.speechLabel = speechLabel;
            npcData.portraitGraphic = AssetLoader.GetAssetInstanceID (portraitIcon.texture);

            npcData.walkSpeed = walkSpeedScale;
            npcData.runSpeed = runSpeedScale;

            // Rendering
            npcData.lockDirection = lockDirection;
            npcData.lockScale = lockScale;
            if (spriteChild && spriteChild.GetComponent <FollowSortingMap>())
            {
                npcData.lockSorting = spriteChild.GetComponent <FollowSortingMap>().lockSorting;
            }
            else if (GetComponent <FollowSortingMap>())
            {
                npcData.lockSorting = GetComponent <FollowSortingMap>().lockSorting;
            }
            else
            {
                npcData.lockSorting = false;
            }
            npcData.spriteDirection = spriteDirection;
            npcData.spriteScale = spriteScale;
            if (spriteChild && spriteChild.GetComponent <Renderer>())
            {
                npcData.sortingOrder = spriteChild.GetComponent <Renderer>().sortingOrder;
                npcData.sortingLayer = spriteChild.GetComponent <Renderer>().sortingLayerName;
            }
            else if (GetComponent <Renderer>())
            {
                npcData.sortingOrder = GetComponent <Renderer>().sortingOrder;
                npcData.sortingLayer = GetComponent <Renderer>().sortingLayerName;
            }

            npcData.pathID = 0;
            npcData.lastPathID = 0;
            if (GetPath ())
            {
                npcData.targetNode = GetTargetNode ();
                npcData.prevNode = GetPrevNode ();
                npcData.isRunning = isRunning;
                npcData.pathAffectY = GetPath ().affectY;

                if (GetPath () == GetComponent <Paths>())
                {
                    npcData.pathData = Serializer.CreatePathData (GetComponent <Paths>());
                }
                else
                {
                    if (GetPath ().GetComponent <ConstantID>())
                    {
                        npcData.pathID = GetPath ().GetComponent <ConstantID>().constantID;
                    }
                    else
                    {
                        Debug.LogWarning ("Want to save path data for " + name + " but path has no ID!");
                    }
                }
            }

            if (GetLastPath ())
            {
                npcData.lastTargetNode = GetLastTargetNode ();
                npcData.lastPrevNode = GetLastPrevNode ();

                if (GetLastPath ().GetComponent <ConstantID>())
                {
                    npcData.lastPathID = GetLastPath ().GetComponent <ConstantID>().constantID;
                }
                else
                {
                    Debug.LogWarning ("Want to save previous path data for " + name + " but path has no ID!");
                }
            }

            if (followTarget)
            {
                if (!followTargetIsPlayer)
                {
                    if (followTarget.GetComponent <ConstantID>())
                    {
                        npcData.followTargetID = followTarget.GetComponent <ConstantID>().constantID;
                        npcData.followTargetIsPlayer = followTargetIsPlayer;
                        npcData.followFrequency = followFrequency;
                        npcData.followDistance = followDistance;
                        npcData.followDistanceMax= followDistanceMax;
                    }
                    else
                    {
                        Debug.LogWarning ("Want to save follow data for " + name + " but " + followTarget.name + " has no ID!");
                    }
                }
                else
                {
                    npcData.followTargetID = 0;
                    npcData.followTargetIsPlayer = followTargetIsPlayer;
                    npcData.followFrequency = followFrequency;
                    npcData.followDistance = followDistance;
                    npcData.followDistanceMax = followDistanceMax;
                }
            }
            else
            {
                npcData.followTargetID = 0;
                npcData.followTargetIsPlayer = false;
                npcData.followFrequency = 0f;
                npcData.followDistance = 0f;
                npcData.followDistanceMax = 0f;
            }

            if (headFacing == HeadFacing.Manual)
            {
                npcData.isHeadTurning = true;
                npcData.headTargetX = headTurnTarget.x;
                npcData.headTargetY = headTurnTarget.y;
                npcData.headTargetZ = headTurnTarget.z;
            }
            else
            {
                npcData.isHeadTurning = false;
                npcData.headTargetX = 0f;
                npcData.headTargetY = 0f;
                npcData.headTargetZ = 0f;
            }

            return npcData;
        }
Exemple #13
0
        public override string SaveData()
        {
            NPCData npcData = new NPCData();

            npcData.objectID = constantID;

            if (gameObject.layer == LayerMask.NameToLayer(KickStarter.settingsManager.hotspotLayer))
            {
                npcData.isOn = true;
            }
            else
            {
                npcData.isOn = false;
            }

            npcData.LocX = transform.position.x;
            npcData.LocY = transform.position.y;
            npcData.LocZ = transform.position.z;

            npcData.RotX = transform.eulerAngles.x;
            npcData.RotY = transform.eulerAngles.y;
            npcData.RotZ = transform.eulerAngles.z;

            npcData.ScaleX = transform.localScale.x;
            npcData.ScaleY = transform.localScale.y;
            npcData.ScaleZ = transform.localScale.z;

            if (GetComponent <NPC>())
            {
                NPC npc = GetComponent <NPC>();

                if (npc.animationEngine == AnimationEngine.Sprites2DToolkit || npc.animationEngine == AnimationEngine.SpritesUnity)
                {
                    npcData.idleAnim = npc.idleAnimSprite;
                    npcData.walkAnim = npc.walkAnimSprite;
                    npcData.talkAnim = npc.talkAnimSprite;
                    npcData.runAnim  = npc.runAnimSprite;
                }
                else if (npc.animationEngine == AnimationEngine.Legacy)
                {
                    npcData.idleAnim = AssetLoader.GetAssetInstanceID(npc.idleAnim);
                    npcData.walkAnim = AssetLoader.GetAssetInstanceID(npc.walkAnim);
                    npcData.runAnim  = AssetLoader.GetAssetInstanceID(npc.runAnim);
                    npcData.talkAnim = AssetLoader.GetAssetInstanceID(npc.talkAnim);
                }
                else if (npc.animationEngine == AnimationEngine.Mecanim)
                {
                    npcData.walkAnim = npc.moveSpeedParameter;
                    npcData.talkAnim = npc.talkParameter;
                    npcData.runAnim  = npc.turnParameter;
                }

                npcData.walkSound = AssetLoader.GetAssetInstanceID(npc.walkSound);
                npcData.runSound  = AssetLoader.GetAssetInstanceID(npc.runSound);

                npcData.speechLabel     = npc.speechLabel;
                npcData.portraitGraphic = AssetLoader.GetAssetInstanceID(npc.portraitIcon.texture);

                npcData.walkSpeed = npc.walkSpeedScale;
                npcData.runSpeed  = npc.runSpeedScale;

                // Rendering
                npcData.lockDirection = npc.lockDirection;
                npcData.lockScale     = npc.lockScale;
                if (npc.spriteChild && npc.spriteChild.GetComponent <FollowSortingMap>())
                {
                    npcData.lockSorting = npc.spriteChild.GetComponent <FollowSortingMap>().lockSorting;
                }
                else if (npc.GetComponent <FollowSortingMap>())
                {
                    npcData.lockSorting = npc.GetComponent <FollowSortingMap>().lockSorting;
                }
                else
                {
                    npcData.lockSorting = false;
                }
                npcData.spriteDirection = npc.spriteDirection;
                npcData.spriteScale     = npc.spriteScale;
                if (npc.spriteChild && npc.spriteChild.GetComponent <Renderer>())
                {
                    npcData.sortingOrder = npc.spriteChild.GetComponent <Renderer>().sortingOrder;
                    npcData.sortingLayer = npc.spriteChild.GetComponent <Renderer>().sortingLayerName;
                }
                else if (npc.GetComponent <Renderer>())
                {
                    npcData.sortingOrder = npc.GetComponent <Renderer>().sortingOrder;
                    npcData.sortingLayer = npc.GetComponent <Renderer>().sortingLayerName;
                }

                npcData.pathID     = 0;
                npcData.lastPathID = 0;
                if (npc.GetPath(true))
                {
                    npcData.targetNode  = npc.GetTargetNode(true);
                    npcData.prevNode    = npc.GetPrevNode(true);
                    npcData.isRunning   = npc.isRunning;
                    npcData.pathAffectY = npc.activePath.affectY;

                    if (npc.GetPath(true) == GetComponent <Paths>())
                    {
                        npcData.pathData = Serializer.CreatePathData(GetComponent <Paths>());
                    }
                    else
                    {
                        if (npc.GetPath(true).GetComponent <ConstantID>())
                        {
                            npcData.pathID = npc.GetPath(true).GetComponent <ConstantID>().constantID;
                        }
                        else
                        {
                            Debug.LogWarning("Want to save path data for " + name + " but path has no ID!");
                        }
                    }
                }

                if (npc.GetPath(false))
                {
                    npcData.lastTargetNode = npc.GetTargetNode(false);
                    npcData.lastPrevNode   = npc.GetPrevNode(false);

                    if (npc.GetPath(false).GetComponent <ConstantID>())
                    {
                        npcData.lastPathID = npc.GetPath(false).GetComponent <ConstantID>().constantID;
                    }
                    else
                    {
                        Debug.LogWarning("Want to save previous path data for " + name + " but path has no ID!");
                    }
                }

                if (npc.followTarget)
                {
                    if (!npc.followTargetIsPlayer)
                    {
                        if (npc.followTarget.GetComponent <ConstantID>())
                        {
                            npcData.followTargetID       = npc.followTarget.GetComponent <ConstantID>().constantID;
                            npcData.followTargetIsPlayer = npc.followTargetIsPlayer;
                            npcData.followFrequency      = npc.followFrequency;
                            npcData.followDistance       = npc.followDistance;
                            npcData.followDistanceMax    = npc.followDistanceMax;
                        }
                        else
                        {
                            Debug.LogWarning("Want to save follow data for " + name + " but " + npc.followTarget.name + " has no ID!");
                        }
                    }
                    else
                    {
                        npcData.followTargetID       = 0;
                        npcData.followTargetIsPlayer = npc.followTargetIsPlayer;
                        npcData.followFrequency      = npc.followFrequency;
                        npcData.followDistance       = npc.followDistance;
                        npcData.followDistanceMax    = npc.followDistanceMax;
                    }
                }
                else
                {
                    npcData.followTargetID       = 0;
                    npcData.followTargetIsPlayer = false;
                    npcData.followFrequency      = 0f;
                    npcData.followDistance       = 0f;
                    npcData.followDistanceMax    = 0f;
                }

                if (npc.headFacing == HeadFacing.Manual)
                {
                    npcData.isHeadTurning = true;
                    npcData.headTargetX   = npc.headTurnTarget.x;
                    npcData.headTargetY   = npc.headTurnTarget.y;
                    npcData.headTargetZ   = npc.headTurnTarget.z;
                }
                else
                {
                    npcData.isHeadTurning = false;
                    npcData.headTargetX   = 0f;
                    npcData.headTargetY   = 0f;
                    npcData.headTargetZ   = 0f;
                }
            }

            return(Serializer.SaveScriptData <NPCData> (npcData));
        }
Exemple #14
0
        public override void LoadData(string stringData)
        {
            NPCData data = Serializer.LoadScriptData <NPCData> (stringData);

            if (data == null)
            {
                return;
            }

            if (data.isOn)
            {
                gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.hotspotLayer);
            }
            else
            {
                gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.deactivatedLayer);
            }

            transform.position    = new Vector3(data.LocX, data.LocY, data.LocZ);
            transform.eulerAngles = new Vector3(data.RotX, data.RotY, data.RotZ);
            transform.localScale  = new Vector3(data.ScaleX, data.ScaleY, data.ScaleZ);

            if (GetComponent <NPC>())
            {
                NPC npc = GetComponent <NPC>();

                npc.EndPath();

                if (npc.animationEngine == AnimationEngine.Sprites2DToolkit || npc.animationEngine == AnimationEngine.SpritesUnity)
                {
                    npc.idleAnimSprite = data.idleAnim;
                    npc.walkAnimSprite = data.walkAnim;
                    npc.talkAnimSprite = data.talkAnim;
                    npc.runAnimSprite  = data.runAnim;
                }
                else if (npc.animationEngine == AnimationEngine.Legacy)
                {
                    npc.idleAnim = AssetLoader.RetrieveAsset(npc.idleAnim, data.idleAnim);
                    npc.walkAnim = AssetLoader.RetrieveAsset(npc.walkAnim, data.walkAnim);
                    npc.runAnim  = AssetLoader.RetrieveAsset(npc.runAnim, data.talkAnim);
                    npc.talkAnim = AssetLoader.RetrieveAsset(npc.talkAnim, data.runAnim);
                }
                else if (npc.animationEngine == AnimationEngine.Mecanim)
                {
                    npc.moveSpeedParameter = data.walkAnim;
                    npc.talkParameter      = data.talkAnim;
                    npc.turnParameter      = data.runAnim;;
                }

                npc.walkSound            = AssetLoader.RetrieveAsset(npc.walkSound, data.walkSound);
                npc.runSound             = AssetLoader.RetrieveAsset(npc.runSound, data.runSound);
                npc.speechLabel          = data.speechLabel;
                npc.portraitIcon.texture = AssetLoader.RetrieveAsset(npc.portraitIcon.texture, data.portraitGraphic);

                npc.walkSpeedScale = data.walkSpeed;
                npc.runSpeedScale  = data.runSpeed;

                // Rendering
                npc.lockDirection = data.lockDirection;
                npc.lockScale     = data.lockScale;
                if (npc.spriteChild && npc.spriteChild.GetComponent <FollowSortingMap>())
                {
                    npc.spriteChild.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
                }
                else if (npc.GetComponent <FollowSortingMap>())
                {
                    npc.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
                }
                else
                {
                    npc.ReleaseSorting();
                }

                if (data.lockDirection)
                {
                    npc.spriteDirection = data.spriteDirection;
                }
                if (data.lockScale)
                {
                    npc.spriteScale = data.spriteScale;
                }
                if (data.lockSorting)
                {
                    if (npc.spriteChild && npc.spriteChild.GetComponent <Renderer>())
                    {
                        npc.spriteChild.GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                        npc.spriteChild.GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                    }
                    else if (npc.GetComponent <Renderer>())
                    {
                        npc.GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                        npc.GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                    }
                }

                AC.Char charToFollow = null;
                if (data.followTargetID != 0)
                {
                    RememberNPC followNPC = Serializer.returnComponent <RememberNPC> (data.followTargetID);
                    if (followNPC.GetComponent <AC.Char>())
                    {
                        charToFollow = followNPC.GetComponent <AC.Char>();
                    }
                }

                npc.FollowAssign(charToFollow, data.followTargetIsPlayer, data.followFrequency, data.followDistance, data.followDistanceMax);
                npc.Halt();

                if (data.pathData != null && data.pathData != "" && GetComponent <Paths>())
                {
                    Paths savedPath = GetComponent <Paths>();
                    savedPath = Serializer.RestorePathData(savedPath, data.pathData);
                    npc.SetPath(savedPath, data.targetNode, data.prevNode, data.pathAffectY);
                    npc.isRunning = data.isRunning;
                }
                else if (data.pathID != 0)
                {
                    Paths pathObject = Serializer.returnComponent <Paths> (data.pathID);

                    if (pathObject != null)
                    {
                        npc.SetPath(pathObject, data.targetNode, data.prevNode);
                    }
                    else
                    {
                        Debug.LogWarning("Trying to assign a path for NPC " + this.name + ", but the path was not found - was it deleted?");
                    }
                }

                if (data.lastPathID != 0)
                {
                    Paths pathObject = Serializer.returnComponent <Paths> (data.lastPathID);

                    if (pathObject != null)
                    {
                        npc.SetLastPath(pathObject, data.lastTargetNode, data.lastPrevNode);
                    }
                    else
                    {
                        Debug.LogWarning("Trying to assign the previous path for NPC " + this.name + ", but the path was not found - was it deleted?");
                    }
                }

                // Head target
                if (data.isHeadTurning)
                {
                    npc.SetHeadTurnTarget(new Vector3(data.headTargetX, data.headTargetY, data.headTargetZ), true);
                }
                else
                {
                    npc.ClearHeadTurnTarget(true);
                }
            }
        }
Exemple #15
0
        /**
         * <summary>Updates its own variables from a NPCData class.</summary>
         * <param name = "data">The NPCData class to load from</param>
         */
        public void LoadData(NPCData data)
        {
            charState = (data.inCustomCharState) ? CharState.Custom : CharState.Idle;

            EndPath();

            GetAnimEngine().LoadNPCData(data, this);

            walkSound = AssetLoader.RetrieveAsset(walkSound, data.walkSound);
            runSound  = AssetLoader.RetrieveAsset(runSound, data.runSound);

            if (!string.IsNullOrEmpty(data.speechLabel))
            {
                SetName(data.speechLabel, data.displayLineID);
            }

            portraitIcon.ReplaceTexture(AssetLoader.RetrieveAsset(portraitIcon.texture, data.portraitGraphic));

            walkSpeedScale = data.walkSpeed;
            runSpeedScale  = data.runSpeed;

            // Rendering
            lockDirection = data.lockDirection;
            lockScale     = data.lockScale;
            if (spriteChild && spriteChild.GetComponent <FollowSortingMap>())
            {
                spriteChild.GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else if (GetComponent <FollowSortingMap>())
            {
                GetComponent <FollowSortingMap>().lockSorting = data.lockSorting;
            }
            else
            {
                ReleaseSorting();
            }

            if (data.lockDirection)
            {
                spriteDirection = data.spriteDirection;
                UpdateFrameFlipping(true);
            }
            if (data.lockScale)
            {
                spriteScale = data.spriteScale;
            }
            if (data.lockSorting)
            {
                if (spriteChild && spriteChild.GetComponent <Renderer>())
                {
                    spriteChild.GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                    spriteChild.GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
                else if (GetComponent <Renderer>())
                {
                    GetComponent <Renderer>().sortingOrder     = data.sortingOrder;
                    GetComponent <Renderer>().sortingLayerName = data.sortingLayer;
                }
            }

            AC.Char charToFollow = null;
            if (data.followTargetID != 0)
            {
                RememberNPC followNPC = ConstantID.GetComponent <RememberNPC> (data.followTargetID);
                if (followNPC.GetComponent <AC.Char>())
                {
                    charToFollow = followNPC.GetComponent <AC.Char>();
                }
            }

            if (charToFollow != null || (data.followTargetIsPlayer && KickStarter.player))
            {
                FollowAssign(charToFollow, data.followTargetIsPlayer, data.followFrequency, data.followDistance, data.followDistanceMax, data.followFaceWhenIdle, data.followRandomDirection, false);
            }
            else
            {
                StopFollowing();
            }
            Halt();

            if (!string.IsNullOrEmpty(data.pathData) && GetComponent <Paths>())
            {
                Paths savedPath = GetComponent <Paths>();
                savedPath = Serializer.RestorePathData(savedPath, data.pathData);
                SetPath(savedPath, data.targetNode, data.prevNode, data.pathAffectY);
                isRunning = data.isRunning;
            }
            else if (data.pathID != 0)
            {
                Paths pathObject = ConstantID.GetComponent <Paths> (data.pathID);

                if (pathObject)
                {
                    SetPath(pathObject, data.targetNode, data.prevNode);
                }
                else
                {
                    ACDebug.LogWarning("Trying to assign a path for NPC " + this.name + ", but the path was not found - was it deleted?", gameObject);
                }
            }

            if (data.lastPathID != 0)
            {
                Paths pathObject = ConstantID.GetComponent <Paths> (data.lastPathID);

                if (pathObject)
                {
                    SetLastPath(pathObject, data.lastTargetNode, data.lastPrevNode);
                }
                else
                {
                    ACDebug.LogWarning("Trying to assign the previous path for NPC " + this.name + ", but the path was not found - was it deleted?", gameObject);
                }
            }

            // Head target
            if (data.isHeadTurning)
            {
                ConstantID _headTargetID = ConstantID.GetComponent <ConstantID> (data.headTargetID);
                if (_headTargetID)
                {
                    SetHeadTurnTarget(_headTargetID.transform, new Vector3(data.headTargetX, data.headTargetY, data.headTargetZ), true);
                }
                else
                {
                    ClearHeadTurnTarget(true);
                }
            }
            else
            {
                ClearHeadTurnTarget(true);
            }

            if (GetComponentsInChildren <FollowSortingMap>() != null)
            {
                FollowSortingMap[] followSortingMaps = GetComponentsInChildren <FollowSortingMap>();
                SortingMap         customSortingMap  = ConstantID.GetComponent <SortingMap> (data.customSortingMapID);

                foreach (FollowSortingMap followSortingMap in followSortingMaps)
                {
                    followSortingMap.followSortingMap = data.followSortingMap;
                    if (!data.followSortingMap && customSortingMap)
                    {
                        followSortingMap.SetSortingMap(customSortingMap);
                    }
                    else
                    {
                        followSortingMap.SetSortingMap(KickStarter.sceneSettings.sortingMap);
                    }
                }
            }

            if (GetAnimEngine() != null && GetAnimEngine().IKEnabled)
            {
                LeftHandIKController.LoadData(data.leftHandIKState);
                RightHandIKController.LoadData(data.rightHandIKState);
            }

            _spriteDirectionData.LoadData(data.spriteDirectionData);
        }