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
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"RememberTransform.cs"
 * 
 *	This script is attached to gameObjects in the scene
 *	with transform data we wish to save.
 * 
 */

using UnityEngine;
using System.Collections;
using AC;

public class RememberTransform : ConstantID
{

	public bool saveParent;


	public TransformData SaveData ()
	{
		TransformData transformData = new TransformData();
		
		transformData.objectID = constantID;
		
		transformData.LocX = transform.position.x;
		transformData.LocY = transform.position.y;
		transformData.LocZ = transform.position.z;
		
		transformData.RotX = transform.eulerAngles.x;
		transformData.RotY = transform.eulerAngles.y;
		transformData.RotZ = transform.eulerAngles.z;
		
		transformData.ScaleX = transform.localScale.x;
		transformData.ScaleY = transform.localScale.y;
		transformData.ScaleZ = transform.localScale.z;

		if (saveParent)
		{
			// Attempt to find the "hand" bone of a character
			Transform t = transform.parent;
			while (t.parent != null)
			{
				t = t.parent;

				if (t.GetComponent <AC.Char>())
				{
					AC.Char parentCharacter = t.GetComponent <AC.Char>();
					
					if (parentCharacter is Player || (parentCharacter.GetComponent <ConstantID>() && parentCharacter.GetComponent <ConstantID>().constantID != 0))
					{
						if (transform.parent == parentCharacter.leftHandBone || transform.parent == parentCharacter.rightHandBone)
						{
							if (parentCharacter is Player)
							{
								transformData.parentIsPlayer = true;
								transformData.parentIsNPC = false;
								transformData.parentID = 0;
							}
							else
							{
								transformData.parentIsPlayer = false;
								transformData.parentIsNPC = true;
								transformData.parentID = parentCharacter.GetComponent <ConstantID>().constantID;
							}
							
							if (transform.parent == parentCharacter.leftHandBone)
							{
								transformData.heldHand = ActionCharHold.Hand.Left;
							}
							else
							{
								transformData.heldHand = ActionCharHold.Hand.Right;
							}
							
							return transformData;
						}
					}
					
					break;
				}
			}

			if (transform.parent.GetComponent <ConstantID>() && transform.parent.GetComponent <ConstantID>().constantID != 0)
			{
				transformData.parentID = transform.parent.GetComponent <ConstantID>().constantID;
			}
			else
			{
				transformData.parentID = 0;
				Debug.LogWarning ("Could not save " + this.name + "'s parent since it has no Constant ID");
			}
		}

		return transformData;
	}


	public void LoadData (TransformData data)
	{
		if (data.parentIsPlayer)
		{
			Player player = GameObject.FindWithTag (Tags.player).GetComponent <Player>();
			if (player != null)
			{
				if (data.heldHand == ActionCharHold.Hand.Left)
				{
					transform.parent = player.leftHandBone;
				}
				else
				{
					transform.parent = player.rightHandBone;
				}
			}
		}
		else if (data.parentID != 0)
		{
			ConstantID parentObject = Serializer.returnComponent <ConstantID> (data.parentID);

			if (parentObject != null)
			{
				if (data.parentIsNPC && parentObject.GetComponent <NPC>())
				{
					if (data.heldHand == ActionCharHold.Hand.Left)
					{
						transform.parent = parentObject.GetComponent <NPC>().leftHandBone;
					}
					else
					{
						transform.parent = parentObject.GetComponent <NPC>().rightHandBone;
					}
				}
				else
				{
					transform.parent = parentObject.gameObject.transform;
				}
			}
		}
		
		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);
	}

}


[System.Serializable]
public class TransformData
{
	
	public int objectID;
	
	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 int parentID;
	public bool parentIsNPC = false;
	public bool parentIsPlayer = false;
	public ActionCharHold.Hand heldHand;

	public TransformData () { }
	
}

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

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