📦 zeyap / RevolutionSolidGame

📄 PolygonControl.cs · 119 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PolygonControl : MonoBehaviour {

	delegate void ObjectBehaviour(int objectIndex);
	ObjectBehaviour objectBehaviour;

	public static List<Polygon> polygons;
	public static List<ActiveObject> activeObjects;
	private Vector3 midPos;
	public static int MaxPolygonNum=12;
	public static int MaxPanelNum=4;

	// Use this for initialization
	void Awake(){
		midPos = GameObject.Find ("middle").transform.position;
	}

	void Start () {

		InitPolygon ();
		ActivateObjects ();
		StartCoroutine("RecoverObjects");

		//polygonBehaviour += FadeInOrOut;
		objectBehaviour += Rotate;
		objectBehaviour += Move;

	}
	
	// Update is called once per frame
	void Update () {
		if (objectBehaviour != null) {
			for (int i = 0; i < activeObjects.Count; i++) {
				objectBehaviour (i);
				FadeInOrOut (i);
			}
		}
	}
		
	void InitPolygon(){
		polygons = new List<Polygon> ();//constructor
		for (int i = 0; i < MaxPolygonNum; i++) {
			if (i < MaxPanelNum) {
				polygons.Add (new Polygon (i));
			} else {
				polygons.Add (new Polygon (i));
			}
			//yield return new WaitForSeconds (2);
		}
	}

	void ActivateObjects(){
		activeObjects = new List<ActiveObject> ();//constructor
		for (int i = 0; i < MaxPanelNum; i++) {
			activeObjects.Add (new ActiveObject (i,i));
			//yield return new WaitForSeconds (2);
		}
	}

	IEnumerator RecoverObjects(){
		while (true) {
			//recover & shift sectionPanel-polygon correspondence
			for(int i=0;i<MaxPanelNum;i++){
				if (activeObjects [i].isKilled == true) {
					//replace with one of the polygons(that is currently not on screen
					int k;
					while(true){
						k = Mathf.FloorToInt (Random.value * MaxPolygonNum);
						int j;
						for (j = 0; j < MaxPanelNum; j++) {
							if (activeObjects [j].polygonIndex == k)
								break;//for
						}
						if (j == MaxPanelNum) {
							break;//while
						}
					}
					activeObjects [i].polygonIndex = k;
					activeObjects [i].Refresh ();
					/*
					Debug.Log (i);
					Debug.Log ("+1");*/
				}
				yield return new WaitForSeconds (2);
			}
			yield return new WaitForSeconds (2);

		}
	}
		

	void Rotate(int objIndex){
		if (activeObjects [objIndex].isKilled == false) {
			activeObjects [objIndex].gameObject.transform.Rotate (0.5f, 0.5f, 0.5f);
		}
	}

	void Move(int objIndex){
		if (activeObjects [objIndex].isKilled == false && activeObjects [objIndex].gameObject.transform.position != midPos) {
			activeObjects [objIndex].gameObject.transform.position=Vector3.MoveTowards(activeObjects [objIndex].gameObject.transform.position,midPos,Time.deltaTime*activeObjects[objIndex].speed);
		}
	}

	void FadeInOrOut(int objIndex){
		if (!activeObjects [objIndex].isKilled) {
			activeObjects [objIndex].gameObject.GetComponent<MeshRenderer> ().material.SetFloat ("_AlphaScale",Mathf.Clamp(activeObjects [objIndex].alphaScale+=0.2f,0.0f,1.0f));
		}

		if (activeObjects [objIndex].isKilled) {
			activeObjects [objIndex].gameObject.GetComponent<MeshRenderer> ().material.SetFloat ("_AlphaScale",Mathf.Clamp(activeObjects [objIndex].alphaScale-=0.2f,0.0f,1.0f));
			activeObjects [objIndex].gameObject.transform.position = new Vector3 (-100,-100,0);
		}
	}
		
}