forked from cgvr/DeltaVR
92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
|
|
[System.Serializable]
|
|
public class BoolRow
|
|
{
|
|
public bool[] cells;
|
|
}
|
|
|
|
|
|
public class ShapeScanner : MonoBehaviour
|
|
{
|
|
public List<BoolRow> configuration;
|
|
public ShapeScannerRay rayPrefab;
|
|
public Transform raySpawnCorner1;
|
|
public Transform raySpawnCorner2;
|
|
public Transform rayParent;
|
|
|
|
public Material requiredAndActive;
|
|
public Material requiredAndPassive;
|
|
public Material notRequiredAndActive;
|
|
public Material notRequiredAndPassive;
|
|
|
|
public TextMeshProUGUI displayText;
|
|
private int rayCount;
|
|
private int correctRayStates;
|
|
|
|
private void Awake()
|
|
{
|
|
correctRayStates = 0;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
float raySpawnDistanceX = raySpawnCorner2.localPosition.x - raySpawnCorner1.localPosition.x;
|
|
float raySpawnDistanceZ = raySpawnCorner2.localPosition.z - raySpawnCorner1.localPosition.z;
|
|
|
|
int rayRowCount = configuration.Count;
|
|
for (int i = 0; i < rayRowCount; i++)
|
|
{
|
|
float rayPosX = raySpawnCorner1.localPosition.x + i * raySpawnDistanceX / (rayRowCount - 1);
|
|
for (int j = 0; j < rayRowCount; j++)
|
|
{
|
|
rayCount++;
|
|
|
|
// Local position
|
|
float rayPosZ = raySpawnCorner1.localPosition.z + j * raySpawnDistanceZ / (rayRowCount - 1);
|
|
Vector3 rayPos = new Vector3(rayPosX, 0, rayPosZ);
|
|
ShapeScannerRay ray = Instantiate(rayPrefab, rayParent);
|
|
ray.transform.localPosition = rayPos;
|
|
|
|
bool rayCollisionRequired = configuration[i].cells[j];
|
|
if (rayCollisionRequired)
|
|
{
|
|
ray.Initialize(this, rayCollisionRequired, requiredAndActive, requiredAndPassive);
|
|
} else
|
|
{
|
|
ray.Initialize(this, rayCollisionRequired, notRequiredAndActive, notRequiredAndPassive);
|
|
IncrementCorrectRayCount();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void IncrementCorrectRayCount()
|
|
{
|
|
correctRayStates++;
|
|
UpdateDisplay();
|
|
}
|
|
|
|
public void DecrementCorrectRayCount()
|
|
{
|
|
correctRayStates--;
|
|
UpdateDisplay();
|
|
}
|
|
|
|
private void UpdateDisplay()
|
|
{
|
|
int percentage = Mathf.RoundToInt((float) correctRayStates / rayCount * 100);
|
|
displayText.text = percentage.ToString() + " %";
|
|
}
|
|
}
|