diff options
Diffstat (limited to 'nGJ2019')
| -rw-r--r-- | nGJ2019/Assets/Scripts/DragonMovement.cs | 190 | ||||
| -rw-r--r-- | nGJ2019/Assets/Scripts/SwarmSystem.cs | 51 | 
2 files changed, 228 insertions, 13 deletions
| diff --git a/nGJ2019/Assets/Scripts/DragonMovement.cs b/nGJ2019/Assets/Scripts/DragonMovement.cs new file mode 100644 index 0000000..67f5cde --- /dev/null +++ b/nGJ2019/Assets/Scripts/DragonMovement.cs @@ -0,0 +1,190 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; + +public class DragonMovement : MonoBehaviour +{ +	public List<Renderer> solids; +	 +	public float horizontalSpeed = 0.1f;	 +	public float verticalSpeed = 0.2f;	 +	 +	public float horizontalBound = 6f;	 +	public float verticalBound = 4f; +	 +	private SwarmSystem swarm; +	 +	private enum State {normal, swirl, slim, spread}; +	private State state = State.normal; +		 +	private void moveUp() +	{ +		transform.Translate(Vector3.up * verticalSpeed); +		if(transform.position.y > verticalBound) +			transform.position = new Vector3(transform.position.x, verticalBound, transform.position.z); +	} +	 +	private void moveDown() +	{ +		transform.Translate(Vector3.down * verticalSpeed); +		if(transform.position.y < -verticalBound) +			transform.position = new Vector3(transform.position.x, -verticalBound, transform.position.z); +	} +	 +	private void moveLeft() +	{ +		transform.Translate(Vector3.left * horizontalSpeed); +		if(transform.position.x < -horizontalBound) +			transform.position = new Vector3(-horizontalBound, transform.position.y, transform.position.z); +	} +	 +	private void moveRight() +	{ +		transform.Translate(Vector3.right * horizontalSpeed); +		if(transform.position.x > horizontalBound) +			transform.position = new Vector3(horizontalBound, transform.position.y, transform.position.z); +	} +	 +	private void turnVisible(bool visible) +	{ +		foreach(Renderer solid in solids) +		{ +			solid.enabled = visible; +		} +	} +	 +	private IEnumerator transformSlim() +	{ +		while(state == State.slim && transform.localScale.y > 0.2f) +		{ +			transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y-0.03f, transform.localScale.z-0.03f); +			yield return new WaitForSeconds(0.01f); +		} +	} +	 +	private IEnumerator transformSpread() +	{ +		while(state == State.spread && swarm.Noise < 3) +		{ +			swarm.Noise += 0.05f; +			yield return new WaitForSeconds(0.01f); +		} +	} +	 +	private IEnumerator transformAntiSlim() +	{ +		while(state == State.normal && transform.localScale.y < 1) +		{ +			transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y+0.05f, transform.localScale.z+0.05f); +			yield return new WaitForSeconds(0.01f); +		} +		if(state == State.normal) +		{ +			turnVisible(true); +			swarm.activate(false); +		} +	} +	 +	private IEnumerator transformAntiSpread() +	{ +		while(state == State.normal && swarm.Noise > 0) +		{ +			swarm.Noise -= 0.08f; +			yield return new WaitForSeconds(0.01f); +		} +		if(state == State.normal) +		{ +			turnVisible(true); +			swarm.activate(false); +		} +	} +	 +	private void turnSwirl() +	{ +		resetTurn(); +		turnVisible(false); +		swarm.activate(true); +		state = State.swirl; +	} +	 +	private void turnSlim() +	{ +		resetTurn(); +		turnVisible(false); +		swarm.activate(true); +		state = State.slim; +		StartCoroutine("transformSlim"); +	} +	 +	private void turnSpread() +	{ +		resetTurn(); +		turnVisible(false); +		swarm.activate(true); +		state = State.spread; +		StartCoroutine("transformSpread"); +	} +	 +	private void turnNormal() +	{ +		State prev = state; +		state = State.normal; +		if(prev == State.swirl) +		{ +			turnVisible(true); +			swarm.activate(false); +		} +		 +		if(prev == State.slim) +			StartCoroutine("transformAntiSlim"); +		 +		if(prev == State.spread) +			StartCoroutine("transformAntiSpread"); +	} +	 +	private void resetTurn() +	{ +		transform.localScale = Vector3.one; +		swarm.Noise = 0; +	} +	 +	void Start() +	{ +		swarm = GetComponent<SwarmSystem>(); +		swarm.activate(true); +	} +	 +	void Update() +	{ +		if(Input.GetKey("w")) +			moveUp(); +		if(Input.GetKey("a")) +			moveLeft(); +		if(Input.GetKey("s")) +			moveDown(); +		if(Input.GetKey("d")) +			moveRight(); +		 +		if(Input.GetKeyDown("r")) +			turnSwirl(); +		if(Input.GetKeyUp("r")) +			turnNormal(); +		 +		if(Input.GetKeyDown("f")) +			turnSlim(); +		if(Input.GetKeyUp("f")) +			turnNormal(); +		 +		if(Input.GetKeyDown("e")) +			turnSpread(); +		if(Input.GetKeyUp("e")) +			turnNormal(); +	} +	 +	void OnDrawGizmosSelected() +	{ +		Gizmos.color = Color.cyan; +		Gizmos.DrawWireCube(Vector3.zero, new Vector3(2*horizontalBound, 2*verticalBound, 0)); +	} +	 +}
\ No newline at end of file diff --git a/nGJ2019/Assets/Scripts/SwarmSystem.cs b/nGJ2019/Assets/Scripts/SwarmSystem.cs index d33595a..9f19dd0 100644 --- a/nGJ2019/Assets/Scripts/SwarmSystem.cs +++ b/nGJ2019/Assets/Scripts/SwarmSystem.cs @@ -4,11 +4,15 @@ using System.Collections.Generic;  public class SwarmSystem : MonoBehaviour  {  	public GameObject swarmPrefab; +	public GameObject anchorPrefab;  	public int size = 1;  	public bool showGuides = false;  	private List<Transform> units = new List<Transform>(); +	private List<Transform> anchors = new List<Transform>(); +	private List<Vector3> noiseDirecs = new List<Vector3>(); +	  	private List<Vector3> boneStarts = new List<Vector3>();  	private List<Vector3> boneEnds = new List<Vector3>();  	private List<Transform> boneTransforms = new List<Transform>(); @@ -21,6 +25,8 @@ public class SwarmSystem : MonoBehaviour  	private List<int> unitTris = new List<int>();  	private List<List<int>> triGraph; +	public float Noise {get; set;} +	  	private Mesh getCurrentMesh()  	{  		Mesh mesh = new Mesh(); @@ -82,19 +88,17 @@ public class SwarmSystem : MonoBehaviour  	private bool triangleIndexIntersection(int idx, Vector3[] vertices, int[] triangles, Ray ray, out Vector3 intersect)  	{ -		return triangleIntersection(meshRender.transform.TransformPoint(vertices[triangles[idx*3]]),  -							 meshRender.transform.TransformPoint(vertices[triangles[idx*3+1]]),  -							 meshRender.transform.TransformPoint(vertices[triangles[idx*3+2]]),  -							 ray, out intersect); +		return triangleIntersection( meshRender.transform.TransformPoint(vertices[triangles[idx*3]]),  +									 meshRender.transform.TransformPoint(vertices[triangles[idx*3+1]]),  +									 meshRender.transform.TransformPoint(vertices[triangles[idx*3+2]]),  +									 ray, out intersect);  	}  	private Vector3 meshIntersection(int unitIdx, Vector3[] vertices, int[] triangles, Vector3 boneCenter)  	{ -		Transform unit = units[unitIdx]; -		  		Vector3 intersect; -		Ray ray = new Ray(boneCenter, unit.position-boneCenter); +		Ray ray = new Ray(boneCenter, anchors[unitIdx].position-boneCenter);  		if(unitTris[unitIdx] != -1)  		{ @@ -122,7 +126,7 @@ public class SwarmSystem : MonoBehaviour  			}  		} -		return unit.position; +		return anchors[unitIdx].position;  	}  	private void calculateTriGraph() @@ -203,8 +207,6 @@ public class SwarmSystem : MonoBehaviour  				boneTips.Add(b);  			}  		} -		 -		Debug.Log(boneTips.Count);  	}  	private void refreshMesh() @@ -298,16 +300,37 @@ public class SwarmSystem : MonoBehaviour  			Vector3 p = (1-r1)*mesh.vertices[mesh.triangles[k*3]] + (r1*(1-r2))*mesh.vertices[mesh.triangles[k*3+1]] + (r1*r2)*mesh.vertices[mesh.triangles[k*3+2]]; +			GameObject anchor = (GameObject)Instantiate(anchorPrefab, meshRender.transform.TransformPoint(p), Quaternion.identity); +			  			GameObject spawn = (GameObject)Instantiate(swarmPrefab, meshRender.transform.TransformPoint(p), Quaternion.identity);  			spawn.transform.parent = transform; +			anchor.transform.parent = transform;  			units.Add(spawn.transform); +			anchors.Add(anchor.transform); +			noiseDirecs.Add(Random.onUnitSphere);  			unitTris.Add(k);  		}  	} +	public void activate(bool turnOn) +	{ +		foreach(Transform u in units) +			u.gameObject.SetActive(turnOn); +	} +	 +	public Transform rootBone() +	{ +		return meshRender.bones[0]; +	} +	 +	public void setNoise(float noise) +	{ +		 +	} +	  	void Start()  	{  		meshRender = GetComponentInChildren<SkinnedMeshRenderer>(); @@ -329,6 +352,8 @@ public class SwarmSystem : MonoBehaviour  		Mesh mesh = getCurrentMesh(); +		Transform root = meshRender.bones[0].parent; +		  		Vector3[] vertices = mesh.vertices;  		int[] triangles = mesh.triangles; @@ -340,11 +365,11 @@ public class SwarmSystem : MonoBehaviour  			Vector3 boneCenter = findBoneCenter(u, out boneDirec); -			unit.RotateAround(boneCenter, boneDirec, 1f); +			anchors[u].position = boneCenter + (Quaternion.AngleAxis(1f, boneDirec) * (anchors[u].position - boneCenter)); -			//unit.position = 0.5f*(unit.position + boneCenter); +			anchors[u].position = meshIntersection(u, vertices, triangles, boneCenter); -			unit.position = meshIntersection(u, vertices, triangles, boneCenter); +			unit.position = anchors[u].position + Noise*noiseDirecs[u];  		}  	} |