73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using System.Collections.Generic;
|
|
using PhysicsTools;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class Scene3Events : MonoBehaviour
|
|
{
|
|
private Rope mainRope;
|
|
|
|
public GameObject prefab;
|
|
|
|
private List<GameObject> lstPrefab;
|
|
|
|
private List<int> freeSegments;
|
|
|
|
public Text txtHelp;
|
|
|
|
private void Start()
|
|
{
|
|
string text = "Camera Control\r\nDrag Mouse To Rotate\r\nArrow Keys and Page Up/Down to move\r\n\r\n'Space': Throw a ball in direction of camera\r\n'r': Reset the scene\r\n'i': Insert prefab randomly\r\n'd': Delete prefab randomly";
|
|
txtHelp.text = text;
|
|
lstPrefab = new List<GameObject>();
|
|
freeSegments = new List<int>();
|
|
}
|
|
|
|
private void attachNewPrefabToMainRope()
|
|
{
|
|
if (mainRope == null)
|
|
{
|
|
mainRope = GameObject.Find("MainRope").GetComponent<Rope>();
|
|
int count = mainRope.getSegments().Count;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
freeSegments.Add(i);
|
|
}
|
|
}
|
|
int num = (int)Mathf.Ceil(Random.value * (float)freeSegments.Count);
|
|
if (num >= 0 && num < freeSegments.Count)
|
|
{
|
|
int index = freeSegments[num];
|
|
freeSegments.RemoveAt(num);
|
|
GameObject seg = mainRope.getSegments()[index].seg;
|
|
Vector3 position = seg.transform.position;
|
|
GameObject gameObject = Object.Instantiate(prefab, position, Quaternion.identity);
|
|
GameObject gameObject2 = gameObject.transform.GetChild(0).gameObject;
|
|
Rope component = gameObject2.GetComponent<Rope>();
|
|
GameObject seg2 = component.getSegments()[0].seg;
|
|
UnityEngine.Joint[] components = seg2.GetComponents<UnityEngine.Joint>();
|
|
FixedJoint fixedJoint = seg2.AddComponent<FixedJoint>();
|
|
fixedJoint.connectedBody = seg.GetComponent<Rigidbody>();
|
|
lstPrefab.Add(gameObject);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.I))
|
|
{
|
|
attachNewPrefabToMainRope();
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.D))
|
|
{
|
|
int num = (int)(Mathf.Ceil(Random.value * (float)lstPrefab.Count) - 1f);
|
|
if (num >= 0 && num < lstPrefab.Count)
|
|
{
|
|
GameObject obj = lstPrefab[num];
|
|
lstPrefab.RemoveAt(num);
|
|
Object.Destroy(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|