Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"RememberNPC.cs"
 * 
 *	This script is attached to NPCs in the scene
 *	with path and transform data we wish to save.
 * 
 */

using UnityEngine;
using System.Collections;
using AC;

public class RememberNPC : ConstantID
{

	public AC_OnOff startState = AC_OnOff.On;

	
	public void Awake ()
	{
		SettingsManager settingsManager = AdvGame.GetReferences ().settingsManager;
		
		if (settingsManager && GetComponent <RememberHotspot>() == null && GameIsPlaying ())
		{
			if (startState == AC_OnOff.On)
			{
				this.gameObject.layer = LayerMask.NameToLayer (settingsManager.hotspotLayer);
			}
			else
			{
				this.gameObject.layer = LayerMask.NameToLayer (settingsManager.deactivatedLayer);
			}
		}
	}


	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 = npc.GetStandardAnimClipName (AnimStandard.Idle);
				npcData.walkAnim = npc.GetStandardAnimClipName (AnimStandard.Walk);
				npcData.runAnim = npc.GetStandardAnimClipName (AnimStandard.Run);
				npcData.talkAnim = npc.GetStandardAnimClipName (AnimStandard.Talk);
			}

			npcData.walkSound = npc.GetStandardSoundName (AnimStandard.Walk);
			npcData.runSound = npc.GetStandardSoundName (AnimStandard.Run);
			
			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.renderer)
			{
				npcData.sortingOrder = npc.spriteChild.renderer.sortingOrder;
				npcData.sortingLayer = npc.spriteChild.renderer.sortingLayerName;
			}
			else if (npc.renderer)
			{
				npcData.sortingOrder = npc.renderer.sortingOrder;
				npcData.sortingLayer = npc.renderer.sortingLayerName;
			}
			
			if (npc.GetPath ())
			{
				npcData.targetNode = npc.GetTargetNode ();
				npcData.prevNode = npc.GetPrevNode ();
				npcData.isRunning = npc.isRunning;
				
				if (npc.GetPath () == GetComponent <Paths>())
				{
					npcData.pathData = Serializer.CreatePathData (GetComponent <Paths>());
					npcData.pathID = 0;
				}
				else
				{
					if (npc.GetPath ().GetComponent <ConstantID>())
					{
						npcData.pathID = npc.GetPath ().GetComponent <ConstantID>().constantID;
					}
					else
					{
						Debug.LogWarning ("Want to save 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;
					}
					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;
				}
			}
			else
			{
				npcData.followTargetID = 0;
				npcData.followTargetIsPlayer = false;
				npcData.followFrequency = 0f;
				npcData.followDistance = 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)
			{
				npc.AssignStandardAnimClipFromResource (AnimStandard.Idle, data.idleAnim);
				npc.AssignStandardAnimClipFromResource (AnimStandard.Walk, data.walkAnim);
				npc.AssignStandardAnimClipFromResource (AnimStandard.Talk, data.talkAnim);
				npc.AssignStandardAnimClipFromResource (AnimStandard.Run, data.runAnim);
			}

			npc.AssignStandardSoundFromResource (AnimStandard.Walk, data.walkSound);
			npc.AssignStandardSoundFromResource (AnimStandard.Run, data.runSound);
			
			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.renderer)
				{
					npc.spriteChild.renderer.sortingOrder = data.sortingOrder;
					npc.spriteChild.renderer.sortingLayerName = data.sortingLayer;
				}
				else if (npc.renderer)
				{
					npc.renderer.sortingOrder = data.sortingOrder;
					npc.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);
			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?");
				}
			}
		}
	}

}


[System.Serializable]
public class NPCData
{
	public int objectID;
	
	public bool isOn;
	
	public float LocX;
	public float LocY;
	public float LocZ;
	
	public float RotX;
	public float RotY;
	public float RotZ;
	
	public float ScaleX;
	public float ScaleY;
	public float ScaleZ;
	
	public string idleAnim;
	public string walkAnim;
	public string talkAnim;
	public string runAnim;

	public string walkSound;
	public string runSound;

	public float walkSpeed;
	public float runSpeed;

	public bool lockDirection;
	public string spriteDirection;
	public bool lockScale;
	public float spriteScale;
	public bool lockSorting;
	public int sortingOrder;
	public string sortingLayer;
	
	public int pathID;
	public int targetNode;
	public int prevNode;
	public string pathData;
	public bool isRunning;
	
	public int followTargetID = 0;
	public bool followTargetIsPlayer = false;
	public float followFrequency = 0f;
	public float followDistance = 0f;
	
	public NPCData () { }
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Save system/RememberNPC.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000