<repo_name>rhiannanberry/Seven-Dancing-Ghost-Simulators<file_sep>/Assets/Scripts/PlayerRaycast.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerRaycast : MonoBehaviour {
    [SerializeField]
    private Camera cam;
    [SerializeField]
    private float scale = 2f;
    public bool lockCursor = false;
    public DDRCube ddrCube;
    // Use this for initialization	
	// Update is called once per frame
    void Start ()
    {

    }

	void FixedUpdate () {
		if (Input.GetMouseButtonDown(0))
        {
            lockCursor = DetectHit();
        }
	}

    private bool DetectHit()
    {
        RaycastHit hit;
        Ray ray = new Ray(cam.transform.position, cam.transform.forward);

        if (Physics.Raycast(ray, out hit))
        {
            DDRCube dCopy = Instantiate<DDRCube>(ddrCube);
            dCopy.transform.rotation = transform.rotation;
            dCopy.setDestination(hit.point);
            return true;
        }
        return false;
    }
}
<file_sep>/Assets/Scripts/Arrow.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour {
    private Vector3 destination;
    private bool destHit = false;
    private Vector3 direction;
    private Vector3 startPos;
    private KeyCode arrowDir;
    private bool destArr = false;
    [SerializeField]
    private float time = 2f; //this should be TIME it takes to get to destination so all arrows match on all screen ratios
    [SerializeField]
    private float speed;
    private Material alt;
	// Use this for initialization
	void Start () {
        startPos = transform.position;
	}
	
    public void setDestination(Vector3 dest)
    {
        destination = dest;
        direction = destination - transform.position;
        speed = direction.magnitude / time;
        direction = direction.normalized;

    }

    public void setRotation(Quaternion adj)
    {
        transform.rotation = Camera.main.transform.rotation * adj;
    }

    public void setDestAndDir(bool val, KeyCode dir)
    {
        arrowDir = dir;
        destArr = val;
        if (val)
        {
            GetComponent<Renderer>().material = alt;
        }
    }
    public void resetPosition()
    {
        transform.position = startPos;
        destHit = false;
    }

	// Update is called once per frame
	void FixedUpdate () {
		if (!destArr)
        {
            if (transform.position == destination || destHit)
            {
                destHit = true;
                transform.Translate(direction * Time.deltaTime * speed, Space.World);
                //transform.position = startPos;
            } else
            {
                transform.position = Vector3.MoveTowards(transform.position, destination, Time.deltaTime * speed);
            }
        }
    }

    void OnTriggerStay(Collider collider)
    {
        if (destArr && collider.tag == "Arrow")
        {
            if (Input.GetKeyDown(arrowDir))
            {
                Debug.Log(arrowDir);

                (collider.GetComponentInParent<Arrow>()).resetPosition();
            }
        }
    }
}
<file_sep>/Assets/Scripts/Line.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Line : MonoBehaviour {
	public LineRenderer lr;
	List<Vector3> points;
	public void UpdateLine(Vector3 mousePos) {
		if (points == null) {
			points = new List<Vector3>();
			SetPoint(mousePos);
			return;
		}
		if (Vector2.Distance(points[points.Count-1], mousePos) > .03f) {
			SetPoint(mousePos);
		}
	}

	void SetPoint (Vector3 mousePos) {
		points.Add(mousePos);
		lr.positionCount = points.Count;
		lr.SetPosition(points.Count - 1, mousePos);
	}
}
<file_sep>/Assets/Scripts/Snowflake.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Snowflake : MonoBehaviour {

	Camera sourceCam;

	// Use this for initialization
	void Start () {
		sourceCam = transform.parent.GetComponent<Camera>();
		sourceCam.aspect = 1;
		float sideLenPixels = sourceCam.scaledPixelWidth;
		Vector3 start = sourceCam.ScreenToWorldPoint(new Vector2(0,0));
		Vector3 end = sourceCam.ScreenToWorldPoint(new Vector2(sideLenPixels, 0));
		float worldSize = Vector3.Distance(start, end)/4;
		//transform.SetParent(null);
		transform.localScale = Vector3.one * worldSize;
		//transform.SetParent(sourceCam.transform);
		Vector3 pos = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, Camera.main.nearClipPlane+1.5f));
		transform.position = pos;//new Vector3(transform.position.x, transform.position.y, pos.z);
		transform.localPosition = new Vector3(-1, 1, transform.localPosition.z);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
<file_sep>/Assets/Scripts/SongController.cs
﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class SongController : MonoBehaviour {
    public struct Metadata {
        public bool valid;

        public string title;
        public string subtitle;
        public string artist;

        public string musicPath; //gonna use resources load for this probably like Resources.Load("music/0000")
        //likely will be necessary to import the succes and failure sounds as well

        public float offset;

        public float bpm;

        public NoteData noteData;
    }

    public struct NoteData {
        public List<List<NoteData>> bars;
    }

    public struct Notes {
        public bool left;
        public bool right;
        public bool up;
        public bool down;
    }

	// Use this for initialization
	void Start () {
		
	}

    void setSong(string songName) {
        //TODO: introduce error handling for invalid sm

        bool inNotes = false; //are we parsing info metadata or notes

        Metadata songData = new Metadata();
        songData.valid = true;
        songData.musicPath = songName; //does this need extension or?

        string[] fileData = File.ReadAllLines("SMFiles/" + songName + ".sm");
        foreach (string line in fileData) {
            if (line.StartsWith("//")) {
                continue;
            } else if (!inNotes && line.StartsWith("#")) {
                string key = line.Substring(0, line.IndexOf(":")).Trim('#').Trim(':');
                switch (key.ToUpper()) {
                    case "TITLE":
                        songData.title = line.Substring(line.IndexOf(':')).Trim(':').Trim(';');
                        break;
                    case "SUBTITLE":
                        songData.subtitle = line.Substring(line.IndexOf(':')).Trim(':').Trim(';');
                        break;
                    case "OFFSET":
                        if (!float.TryParse(line.Substring(line.IndexOf(':')).Trim(':').Trim(';'), out songData.offset)) {
                            //Error Parsing
                            songData.offset = 0.0f;
                        }
                        break;
                    case "DISPLAYBPM":
                        if (!float.TryParse(line.Substring(line.IndexOf(':')).Trim(':').Trim(';'), out songData.bpm) || songData.bpm == 0) {
                            //Error Parsing - BPM not valid
                            songData.valid = false;
                            songData.bpm = 0.0f;
                        }
                        break;
                    case "NOTES":
                        inNotes = true;
                        break;
                    default:
                        break;
                }
            }
            if (inNotes)
            {

            }
        }
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
<file_sep>/Assets/Room Building/Baseboard.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Baseboard : MonoBehaviour {
	Wall wall;
	MeshFilter meshFilter;
	Mesh prefabMesh;
	Mesh mesh;


	public void UpdateBaseboard() {
		/*
		 *
		 * Get relevant values and create necessary arrays
		 *
		 */

		wall = GetComponentInParent<Wall>();
		prefabMesh = wall.baseboard.GetComponent<MeshFilter>().sharedMesh;
		meshFilter = GetComponent<MeshFilter>();
		mesh = meshFilter.sharedMesh;

		int prefVertexCount = prefabMesh.vertexCount;
		Vector3[] prefVertices = prefabMesh.vertices;	//sillohuette of the baseboard

		int meshVertexCount = prefVertexCount*2;
		Vector3[] meshVertices = new Vector3[meshVertexCount];
		Vector2[] meshUVs = new Vector2[meshVertexCount];

		int endCapTriCount = prefVertexCount-2;
		int tubeTriCount = 2*(prefVertexCount-1);
		int triCount = 2*endCapTriCount + tubeTriCount;
		int triVertexCount = triCount*3;
		int[] meshTriangles = new int[triVertexCount];

		int additionalLoopCount = 1 + wall.doors.Length * 2;


		if (mesh == null) {
			mesh = new Mesh();
			meshFilter.mesh = mesh;
		} 

		/*
		 * Update vertex array and UV array
		 */

		
		//translation matrix just moves indiv. vertices like transform.Translate() does
		Matrix4x4 transMatrix = Matrix4x4.Translate(Vector3.right*wall.width);
		for (int i = 0; i < prefVertexCount; i++) {
			Vector3 translation = transMatrix.MultiplyPoint3x4(prefVertices[i]);
			meshVertices[i] = prefVertices[i];
			meshVertices[i+prefVertexCount] = translation;

			meshUVs[i] = prefVertices[i];
			meshUVs[i+prefVertexCount] = translation;
		}

		/*
		 * Update triangle vertex array
		 */
		int currentTriVertex = 0;

		//left end cap
		for (int j = 0; j < prefVertexCount - 2; j++) {
			meshTriangles[currentTriVertex++] = j+1;
			meshTriangles[currentTriVertex++] = 0;
			meshTriangles[currentTriVertex++] = j+2;
		}

		//tube
		for (int k = 0; k < prefVertexCount-1; k++) {
			//back triangle
			meshTriangles[currentTriVertex++] = k + prefVertexCount;
			meshTriangles[currentTriVertex++] = k;
			meshTriangles[currentTriVertex++] = k + 1;

			//front triangle
			meshTriangles[currentTriVertex++] = k + prefVertexCount;
			meshTriangles[currentTriVertex++] = k + 1;
			meshTriangles[currentTriVertex++] = k + prefVertexCount + 1;
		}

		//right end cap
		for (int l = prefVertexCount; l < meshVertexCount-2; l++) {
			meshTriangles[currentTriVertex++] = 0;
			meshTriangles[currentTriVertex++] = l+1;
			meshTriangles[currentTriVertex++] = l+2;
		}

		/*
		 * Set all new mesh values and calculate normals
		 */

		mesh.vertices = meshVertices;
		mesh.triangles = meshTriangles;
		mesh.uv = meshUVs;
		mesh.RecalculateNormals();
		Debugging();
	}

	private void GenerateVerticesAndUV() {
		Matrix4x4[] transMatrices = new Matrix4x4[additionalLoopCount];
		int loopCnt = 0;
		for (int m = 0; m < wall.doors.Length; m++) {
			transMatrices[loopCnt++] = Matrix4x4.Translate(Vector3.right*wall.doors[m].transform.localPosition.x);
			transMatrices[loopCnt++] = Matrix4x4.Translate(Vector3.right*(wall.doors[m].transform.localPosition.x + wall.doors[m].width));
		}
		transMatrices[loopCnt++] = Matrix4x4.Translate(Vector3.right*(wall.width));

		for (int i = 0; i < prefVertexCount; i++) {
			meshVertices[i] = prefVertices[i];
			meshUVs[i] = prefVertices[i];

			int transCnt = 0;
			foreach (Matrix4x4 matrix in transMatrices) {
				transCnt += prefVertexCount;
				Vector3 translation = matrix.MultiplyPoint3x4(prefVertices[i]);
				meshVertices[i + transCnt] = translation;
				meshUVs[i+ transCnt] = translation;
			}	
		}

	}

	private void Debugging() {
		Debug.Log("DOOR BOTTOM LEFT: " + wall.doors[0].transform.localPosition.x);
		Debug.Log("DOOR BOTTOM RIGHT: " + wall.doors[0].transform.localPosition.x + wall.doors[0].width);

		Debug.DrawLine(Vector3.down, Vector3.right*(wall.doors[0].transform.localPosition.x + wall.doors[0].width)*wall.transform.localScale.x, Color.red, 0);
	}
	
}
<file_sep>/Assets/Scripts/PlayerController.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private float speed = 5f;
    private float lookSensitivity = 3f;

    private PlayerDriver driver;
    private PlayerRaycast pRaycast;
    // Use this for initialization
    void Start()
    {
        driver = GetComponent<PlayerDriver>();
        pRaycast = GetComponent<PlayerRaycast>();
    }

    // Update is called once per frame
    void Update()
    {
        if (pRaycast.lockCursor)
        {
            if(Cursor.lockState != CursorLockMode.None)
            {
                Cursor.lockState = CursorLockMode.None;
            }
            driver.Move(Vector3.zero);
            driver.Rotate(Vector3.zero);
            driver.RotateCamera(0f);
            return;
        }
        float xMove = Input.GetAxis("Horizontal");
        float zMove = Input.GetAxis("Vertical");

        Vector3 horiMove = transform.right * xMove;
        Vector3 vertMove = transform.forward * zMove;

        Vector3 fVelocity = (horiMove + vertMove) * speed;
        driver.Move(fVelocity);

        //spins the actual character
        float yRot = Input.GetAxisRaw("Mouse X");
        Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSensitivity;
        driver.Rotate(rotation);

        //just moves the camera up or down
        float xRot = Input.GetAxisRaw("Mouse Y");
        float camRotationX = xRot * lookSensitivity;
        driver.RotateCamera(camRotationX);
    }
}
<file_sep>/Assets/Scripts/DDRCube.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DDRCube : MonoBehaviour {
    private Camera cam;
    private Vector3 startUp, startLeft, startDown, startRight;
    private Quaternion rotUp, rotDown, rotLeft, rotRight;
    public Arrow arrow;
    public Vector3 destination;
    // Use this for initialization
    void Start () {
        cam = Camera.main;
        rotUp = Quaternion.AngleAxis(180f, Vector3.forward);
        rotDown = Quaternion.AngleAxis(0f, Vector3.forward);
        rotLeft = Quaternion.AngleAxis(-90f, Vector3.forward);
        rotRight = Quaternion.AngleAxis(90f, Vector3.forward);
        startLeft = cam.ViewportToWorldPoint(new Vector3(0, 0.5f, 0.5f));
        startRight = cam.ViewportToWorldPoint(new Vector3(1, 0.5f, 0.5f));
        startDown = cam.ViewportToWorldPoint(new Vector3(0.5f, 0, 0.5f));
        startUp = cam.ViewportToWorldPoint(new Vector3(0.5f, 1, 0.5f));

        Arrow fRight = Instantiate<Arrow>(arrow);
        Arrow fLeft = Instantiate<Arrow>(arrow);
        Arrow fUp = Instantiate<Arrow>(arrow);
        Arrow fDown = Instantiate<Arrow>(arrow);
        fRight.setDestAndDir(true, KeyCode.D);
        fLeft.setDestAndDir(true, KeyCode.A);
        fUp.setDestAndDir(true, KeyCode.W);
        fDown.setDestAndDir(true, KeyCode.S);
        fRight.transform.position = destination;
        fLeft.transform.position = destination;
        fUp.transform.position = destination;
        fDown.transform.position = destination;
        fRight.setRotation(rotRight);
        fLeft.setRotation(rotLeft);
        fUp.setRotation(rotUp);
        fDown.setRotation(rotDown);

        //cube1.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);

        Arrow arrowRight = Instantiate<Arrow>(arrow);

        arrowRight.transform.position = startRight;
        arrowRight.setRotation(rotRight);

        Arrow arrowDown = Instantiate<Arrow>(arrow);
        arrowDown.transform.position = startDown;
        arrowDown.setRotation(rotDown);

        Arrow arrowLeft = Instantiate<Arrow>(arrow);
        arrowLeft.transform.position = startLeft;
        arrowLeft.setRotation(rotLeft);

        Arrow arrowUp = Instantiate<Arrow>(arrow);
        arrowUp.transform.position = startUp;
        arrowUp.setRotation(rotUp);

        //Arrow arrowDown = Instantiate<Arrow>(arrow);
        //arrowDown.transform.position = startDown;
        arrowRight.setDestination(destination);
        arrowLeft.setDestination(destination);
        arrowDown.setDestination(destination);
        arrowUp.setDestination(destination);
    }
	
    public void setDestination(Vector3 dest)
    {
        destination = dest;
    }


	// Update is called once per frame
	void Update () {//when evalcuating distance just use sqrmag to save performance
    }
}
<file_sep>/Assets/Scripts/LineCreator.cs
﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineCreator : MonoBehaviour {
	public GameObject linePrefab;
	public RenderTexture rt;
	public Texture2D snowflake;
	Line activeLine;
	Vector2 bottomLeft, topRight;
	bool updateTexture = false;	
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0)) {
			GameObject lineGO = Instantiate(linePrefab);
			lineGO.transform.SetParent(this.transform);
			lineGO.transform.position = Camera.main.transform.position;
			lineGO.transform.rotation = Camera.main.transform.rotation;
			activeLine = lineGO.GetComponent<Line>();
			bottomLeft = topRight = Input.mousePosition;
		}

		if (Input.GetMouseButtonUp(0)) {
			updateTexture = true;
			
		}

		if (activeLine != null) {
			bottomLeft.x = Mathf.Min(bottomLeft.x, Input.mousePosition.x);
			bottomLeft.y = Mathf.Min(bottomLeft.y, Input.mousePosition.y);
			topRight.x = Mathf.Max(topRight.x, Input.mousePosition.x);
			topRight.y = Mathf.Max(topRight.y, Input.mousePosition.y);
			Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane+1.49f));
			activeLine.UpdateLine(mousePos);
		}
	}

	private void OnPostRender() {
		if (updateTexture) {
			snowflake.ReadPixels(new Rect(0,0, rt.width, rt.height),0,0,false);
			snowflake.Apply();
			snowflake.FloodFillArea(0,0, Color.red);
			snowflake.Apply();
			activeLine.gameObject.layer += 1; //moved to drawn layer so it's hidden from main camera
			activeLine = null;
			Debug.Log("BottomLeft Screenspace: " + bottomLeft + ", TopRight: " + topRight);
			updateTexture = false;
		}
	}
}
