Skip to content
Snippets Groups Projects
Commit 77800ef9 authored by Bocska Karina's avatar Bocska Karina
Browse files

basic save load setup done

parent 41419a41
No related branches found
No related tags found
1 merge request!1Feature save load
......@@ -4495,6 +4495,7 @@ MonoBehaviour:
spawnPrefab: {fileID: 8126816444565180231, guid: f24fe5caf4a57c148bd86d63b19a1248, type: 3}
inputTransform: {fileID: 1044421913}
triggerEventAction: {fileID: 4987378303488695193, guid: c348712bda248c246b8c49b3db54643f, type: 3}
programController: {fileID: 426651922}
--- !u!4 &426651920
Transform:
m_ObjectHideFlags: 0
......@@ -4535,6 +4536,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 3972891316024844e9f4db4ba6131e3b, type: 3}
m_Name:
m_EditorClassIdentifier:
spawnBlockEvent: {fileID: 426651919}
UserTransform: {fileID: 1013153632}
saveEventAction: {fileID: -1827717145013635445, guid: c348712bda248c246b8c49b3db54643f, type: 3}
loadEventAction: {fileID: 1743316162407726226, guid: c348712bda248c246b8c49b3db54643f, type: 3}
--- !u!1001 &427008753
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -11543,6 +11548,11 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1009026816}
m_CullTransparentMesh: 1
--- !u!4 &1013153632 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 7179462289238023466, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3}
m_PrefabInstance: {fileID: 1825262782}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1014711966
GameObject:
m_ObjectHideFlags: 0
......
......@@ -11,6 +11,8 @@ public class BlockDeform : MonoBehaviour
private readonly DeformAnchor[] _deformAnchors = new DeformAnchor[6];
private readonly GameObject[] _spawnedAnchors = new GameObject[6];
private bool loadedType = false;
private bool IsPointOnPlane(Vector3 point, Vector3 normal, Vector3 pointOnPlane, double epsilon)
{
double d = Vector3.Dot(normal, pointOnPlane);
......@@ -100,11 +102,20 @@ public class BlockDeform : MonoBehaviour
}
}
private void Awake()
{
_mesh = this.GetComponent<MeshFilter>().mesh;
}
// Start is called before the first frame update
void Start()
{
_mesh = this.GetComponent<MeshFilter>().mesh;
if (!this.loadedType)
InitAnchors();
}
private void InitAnchors()
{
Collider blockCollider = this.GetComponent<Collider>();
for (int i = 0; i < 6; i++)
......@@ -119,7 +130,6 @@ public class BlockDeform : MonoBehaviour
_deformAnchors[0].SetupAnchor(this, this.transform.forward, blockCollider.bounds.extents, wrapDistance);
_deformAnchors[1].SetupAnchor(this, -this.transform.forward, blockCollider.bounds.extents, wrapDistance);
// y axis achor setup
_deformAnchors[2].SetupAnchor(this, this.transform.up, blockCollider.bounds.extents, wrapDistance);
_deformAnchors[3].SetupAnchor(this, -this.transform.up, blockCollider.bounds.extents, wrapDistance);
......@@ -127,13 +137,37 @@ public class BlockDeform : MonoBehaviour
// x axis anchor setup
_deformAnchors[4].SetupAnchor(this, this.transform.right, blockCollider.bounds.extents, wrapDistance);
_deformAnchors[5].SetupAnchor(this, -this.transform.right, blockCollider.bounds.extents, wrapDistance);
}
// Update is called once per frame
void Update()
public void AdjustVertices(Vector3[] vertices, Vector3[] anchorPosition)
{
this.loadedType = true;
InitAnchors();
_mesh = this.GetComponent<MeshFilter>().mesh;
if (_mesh.vertices.Length != vertices.Length)
{
Debug.LogError("cannot adjust vertices: different sized vertex arrays");
return;
}
Vector3[] adjustedVertices = _mesh.vertices;
for (int i = 0; i < adjustedVertices.Length; i++)
{
adjustedVertices[i] = vertices[i];
}
_mesh.vertices = adjustedVertices;
_mesh.RecalculateBounds();
MeshCollider collider = GetComponent<MeshCollider>();
collider.sharedMesh = _mesh;
for (int i = 0; i < _spawnedAnchors.Length; i++)
{
_spawnedAnchors[i].transform.localPosition = anchorPosition[i];
}
UpdateDeformAnchors();
}
private void FixedUpdate()
......@@ -145,6 +179,11 @@ public class BlockDeform : MonoBehaviour
}
}
public GameObject[] GetAnchors()
{
return _spawnedAnchors;
}
public Vector3 GetPosition()
{
return transform.position;
......
......@@ -36,6 +36,7 @@ public class DeformAnchor : MonoBehaviour
private void Awake()
{
mesh = this.GetComponent<MeshFilter>().mesh;
grabInteractable = GetComponent<XRGrabInteractable>();
grabInteractable.selectEntered.AddListener(HandleSelectEnter);
grabInteractable.selectExited.AddListener(HandleSelectExit);
......@@ -63,18 +64,6 @@ public class DeformAnchor : MonoBehaviour
_previousPosition = _position.transform.position;
}
// Start is called before the first frame update
void Start()
{
mesh = this.GetComponent<MeshFilter>().mesh;
}
// Update is called once per frame
void Update()
{
}
public void SetMeshScale(List<Vector3> vertices)
{
......
......@@ -78,11 +78,30 @@ public class ProgramController : MonoBehaviour
private Save CreateSave()
{
Save save = new();
save.UserTransform = this.UserTransform.position;
save.UserTransform = new Vector3[] { this.UserTransform.position, this.UserTransform.eulerAngles };
foreach (GameObject block in _spawnedBlocks)
{
save.Blocks.Add(block.transform.position);
Vector3[] blockVertices = block.GetComponent<MeshFilter>().mesh.vertices;
Vector3[] blockData = new Vector3[2 + blockVertices.Length];
blockData[0] = block.transform.position;
blockData[1] = block.transform.eulerAngles;
for (int i = 0; i < blockVertices.Length; i++)
{
blockData[i + 2] = blockVertices[i];
}
save.Blocks.Add(blockData);
GameObject[] anchors = block.GetComponent<BlockDeform>().GetAnchors();
Vector3[] anchorsData = new Vector3[anchors.Length];
for (int i = 0; i < anchors.Length; i++)
{
anchorsData[i] = anchors[i].transform.localPosition;
}
save.BlockAnchors.Add(anchorsData);
}
return save;
......@@ -90,7 +109,6 @@ public class ProgramController : MonoBehaviour
public void SaveProgram(InputAction.CallbackContext context)
{
Debug.Log("saving");
Save save = CreateSave();
SurrogateSelector surrogateSelector = new();
surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate());
......@@ -101,15 +119,20 @@ public class ProgramController : MonoBehaviour
FileStream fs = File.Create(this._savePath);
binaryFormatter.Serialize(fs, save);
fs.Close();
Debug.Log("finished saving");
}
public void LoadProgram(InputAction.CallbackContext context)
{
Debug.Log("loading");
if (File.Exists(this._savePath))
{
if (_spawnedBlocks.Count > 0) _spawnedBlocks.Clear();
if (_spawnedBlocks.Count > 0)
{
foreach (GameObject block in _spawnedBlocks)
{
Destroy(block);
}
_spawnedBlocks.Clear();
}
SurrogateSelector surrogateSelector = new();
surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), new Vector3SerializationSurrogate());
......@@ -121,7 +144,9 @@ public class ProgramController : MonoBehaviour
Save save = (Save)binaryFormatter.Deserialize(fs);
fs.Close();
spawnBlockEvent.LoadBlocks(save.Blocks);
UserTransform.position = save.UserTransform[0];
UserTransform.eulerAngles = save.UserTransform[1];
spawnBlockEvent.LoadBlocks(save.Blocks, save.BlockAnchors);
}
}
......
......@@ -48,15 +48,23 @@ public class SpawnBlockEvent : MonoBehaviour
}
}
public void LoadBlocks(List<Vector3> blocks)
public void LoadBlocks(List<Vector3[]> blocks, List<Vector3[]> anchors)
{
foreach (Vector3 blockPosition in blocks)
for (int i = 0; i < blocks.Count; i++)
{
GameObject spawnedObject = Instantiate(spawnPrefab, blockPosition, Quaternion.identity);
Vector3[] blockData = blocks[i];
Vector3[] anchorData = anchors[i];
GameObject spawnedObject = Instantiate(spawnPrefab, blockData[0], Quaternion.identity);
spawnedObject.transform.parent = this.transform;
programController.RegisterBlock(spawnedObject);
Vector3[] vertices = new Vector3[blockData.Length - 2];
Array.Copy(blockData, 2, vertices, 0, blockData.Length - 2);
spawnedObject.GetComponent<BlockDeform>().AdjustVertices(vertices, anchorData);
spawnedObject.transform.eulerAngles = blockData[1];
programController.RegisterBlock(spawnedObject);
}
}
......@@ -78,13 +86,13 @@ public class SpawnBlockEvent : MonoBehaviour
// Start is called before the first frame update
void Start()
{
GameObject spawnedObject = Instantiate(spawnPrefab, this.transform.position, Quaternion.identity);
Collider objectCollider = spawnedObject.GetComponent<Collider>();
Vector3 adjustment = Vector3.Scale(this.transform.up, objectCollider.bounds.size / 2);
spawnedObject.transform.position += adjustment;
spawnedObject.transform.parent = this.transform;
// GameObject spawnedObject = Instantiate(spawnPrefab, this.transform.position, Quaternion.identity);
// Collider objectCollider = spawnedObject.GetComponent<Collider>();
// Vector3 adjustment = Vector3.Scale(this.transform.up, objectCollider.bounds.size / 2);
// spawnedObject.transform.position += adjustment;
// spawnedObject.transform.parent = this.transform;
programController.RegisterBlock(spawnedObject);
// programController.RegisterBlock(spawnedObject);
}
// Update is called once per frame
......
......@@ -5,6 +5,7 @@ using UnityEngine;
[System.Serializable]
public class Save
{
public Vector3 UserTransform;
public List<Vector3> Blocks = new();
public Vector3[] UserTransform = new Vector3[2];
public List<Vector3[]> Blocks = new();
public List<Vector3[]> BlockAnchors = new();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment