Created
January 30, 2017 15:17
-
-
Save tak-km/7d24fd5f3e62073585a7311d6050dd2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class OccupyController2 : MonoBehaviour { | |
[SerializeField] | |
List<GameObject> soldierList1 = new List<GameObject>(); | |
[SerializeField] | |
List<GameObject> soldierList2 = new List<GameObject>(); | |
[SerializeField] | |
float occupyGauge = 0; | |
[SerializeField] | |
int occupyState = 0; | |
[SerializeField] | |
ParticleSystem occupyParticle; | |
[SerializeField] | |
Slider occupySlider; | |
[SerializeField] | |
Image occupyFill; | |
[SerializeField] | |
bool stopOccupy = false; | |
[SerializeField] | |
int OccupySpeed =30; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
void OnTriggerEnter(Collider other){ | |
if (other.gameObject.CompareTag ("team1")) { | |
soldierList1.Add (other.gameObject); | |
} | |
if (other.gameObject.CompareTag ("team2")) { | |
soldierList2.Add (other.gameObject); | |
} | |
AreaOccupy (); | |
} | |
void OnTriggerExit(Collider other) | |
{ | |
if (other.gameObject.CompareTag ("team1") && soldierList1.Contains (other.gameObject)) { | |
stopOccupy = false; | |
soldierList1.Remove(other.gameObject); | |
} | |
if (other.gameObject.CompareTag ("team2") && soldierList2.Contains (other.gameObject)) { | |
stopOccupy = false; | |
soldierList2.Remove(other.gameObject); | |
} | |
AreaOccupy (); | |
} | |
void AreaOccupy(){ | |
if (soldierList1.Count > soldierList2.Count) { | |
if (occupyState == 2) { | |
stopOccupy = false; | |
StartCoroutine ("DeOccupy", 2); | |
} | |
if (occupyState == 0) { | |
stopOccupy = false; | |
StartCoroutine ("Occupy", 1); | |
} | |
if (occupyState == 1) { | |
stopOccupy = false; | |
//StopCoroutine("DeOccupy"); | |
StartCoroutine ("Occupy", 1); | |
} | |
} | |
if (soldierList2.Count > soldierList1.Count) { | |
if (occupyState == 1) { | |
stopOccupy = false; | |
StartCoroutine ("DeOccupy", 1); | |
} | |
if (occupyState == 0) { | |
stopOccupy = false; | |
StartCoroutine ("Occupy", 2); | |
} | |
if (occupyState == 2) { | |
stopOccupy = false; | |
//StopCoroutine("DeOccupy"); | |
StartCoroutine ("Occupy", 2); | |
} | |
} | |
if (soldierList1.Count == soldierList2.Count) { | |
if (soldierList1.Count == 0 && soldierList2.Count == 0 ) { | |
if (occupyState == 0) { | |
Debug.Log ("reset"); | |
stopOccupy = true; | |
StartCoroutine ("DeOccupy", 0); | |
} | |
if(occupyState == 1 || occupyState ==2) { | |
StopCoroutine ("DeOccupy"); | |
StartCoroutine ("Occupy", 0); | |
} | |
} else { | |
stopOccupy = true; | |
StopCoroutine ("DeOccupy"); | |
} | |
} | |
} | |
IEnumerator Occupy(int state) | |
{ | |
occupyGauge = occupySlider.value; | |
float enemygauge = 0; | |
switch (state) | |
{ | |
case 1: | |
occupyFill.color = Color.cyan; | |
break; | |
case 2: | |
occupyFill.color = Color.magenta; | |
break; | |
default: | |
break; | |
} | |
float time = 0; | |
while ( occupyGauge<100 ) { | |
if (stopOccupy != true) { | |
yield return new WaitForEndOfFrame (); | |
occupyGauge += OccupySpeed * Time.deltaTime; | |
time += Time.deltaTime; | |
occupySlider.value = occupyGauge; | |
} else { | |
stopOccupy = false; | |
AreaOccupy (); | |
break; | |
} | |
} | |
if(occupyGauge>100 ){ | |
switch (state) | |
{ | |
case 1: | |
occupyParticle.startColor = Color.cyan; | |
occupyState = state; | |
break; | |
case 2: | |
occupyParticle.startColor = Color.magenta; | |
occupyState = state; | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
IEnumerator DeOccupy(int state) | |
{ | |
occupyGauge = occupySlider.value; | |
switch (state) | |
{ | |
case 1: | |
occupyFill.color = Color.cyan; | |
break; | |
case 2: | |
occupyFill.color = Color.magenta; | |
break; | |
default: | |
break; | |
} | |
float time = 0; | |
while (occupyGauge>0 ) { | |
yield return new WaitForEndOfFrame (); | |
occupyGauge -= OccupySpeed * Time.deltaTime; | |
time += Time.deltaTime; | |
occupySlider.value = occupyGauge; | |
} | |
if(occupyGauge<0 ){ | |
occupyParticle.startColor = Color.gray; | |
occupyState = 0; | |
AreaOccupy (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment