Skip to content

Commit

Permalink
Started working on save/loader
Browse files Browse the repository at this point in the history
  • Loading branch information
jedjoud10 committed Nov 19, 2019
1 parent 5c2b78e commit 44ab307
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
15 changes: 0 additions & 15 deletions Assets/Play Mode/Interpreter/SaverLoader.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Assets/Player/Building Mechanic/Scripts/BuildingScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class BuildingScript : MonoBehaviour
private AnchorScript newanchor;//The new anchor to add the new block to
private LayerMask mask;//Mask used to disable anchors in collision
private GameObject PreviewPiece;//The gameobject of the spawned preview piece
private int pieceNumber;//Piece number to help saving and loading
// Start is called before the first frame update
void Start()
{
Expand Down Expand Up @@ -84,10 +85,12 @@ public void AddBuildingPiece(AnchorScript anchor)//Add piece to the anchor, then
{
GameObject newpiece = Instantiate(SelectedBuildingPiecePrefab, anchor.transform.position, anchor.transform.rotation);//Spawn the piece
FixedJoint newjoint = newpiece.AddComponent<FixedJoint>();//Adds the joint to not let it move
newpiece.name = SelectedBuildingPiecePrefab.name + "-" + pieceNumber;
newjoint.connectedBody = anchor.parent;//Connect the new piece to the anchor's parent piece
anchor.gameObject.SetActive(false);//Disable the anchor sine we place a piece at it's place
UpdateAnchors(null);
newpiece.transform.parent = MainPlayerGameobject.transform;
pieceNumber++;
}
}
}
55 changes: 55 additions & 0 deletions Assets/Player/Building Mechanic/Scripts/SaverLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class SaverLoader
{
public void Save(string fileName, Rigidbody[] pieces) //Save pieces
{
Piece[] newPieces = new Piece[pieces.Length];
string finalText;
for (int i = 0; i < pieces.Length; i++)
{
newPieces[i].pieceName = pieces[i].name;//Set name of piece
if (pieces[i].GetComponent<FixedJoint>() != null)//Only for pieces who are not start piece
{
newPieces[i].parentPiece = pieces[i].GetComponent<FixedJoint>().connectedBody;//Set parent piece
}
if (pieces[i].name == "StructuralPiece")
{
newPieces[i].pieceType = PieceType.StructuralPiece;
}
if (pieces[i].name == "ControlUnit")
{
newPieces[i].pieceType = PieceType.ControlUnit;
}
newPieces[i].position = pieces[i].transform.position;//Set position
}
string path = Application.dataPath + "/SavedMachines/" + fileName;
if (!Directory.Exists(Application.dataPath + "/SavedMachines"))//Dirrectory handling
{
Directory.CreateDirectory(Application.dataPath + "/SavedMachines");
}
foreach (var newpiece in newPieces)
{
//finalText.Insert();
}
//File.WriteAllText();
}
public void Load(string fileName) //Load pieces
{

}
private struct Piece
{
public string pieceName;
public Rigidbody parentPiece;
public PieceType pieceType;
public Vector3 position;
}
private enum PieceType
{
StructuralPiece, ControlUnit
}
}

0 comments on commit 44ab307

Please sign in to comment.