Skip to content

Instantly share code, notes, and snippets.

@tak-km
Last active January 30, 2017 13:17
Show Gist options
  • Save tak-km/55be867ab60c8339c52a95d3ec63bb4e to your computer and use it in GitHub Desktop.
Save tak-km/55be867ab60c8339c52a95d3ec63bb4e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OccupyController : MonoBehaviour {
[SerializeField]
List<GameObject> soldierList1 = new List<GameObject>();
[SerializeField]
List<GameObject> soldierList2 = new List<GameObject>();
[SerializeField]
float occpupyGauge = 0;
[SerializeField]
float occpupyGauge2 = 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;
if (occupySlider.value > 0 && occpupyGauge2 > 0) {
StartCoroutine ("DeOccupy", 2);
} else {
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;
if (occupySlider.value > 0 && occpupyGauge > 0) {
StartCoroutine ("DeOccupy", 1);
} else {
StartCoroutine ("Occupy", 2);
}
}
if (occupyState == 2) {
stopOccupy = false;
//StopCoroutine("DeOccupy");
StartCoroutine ("Occupy", 2);
}
}
if (soldierList1.Count == soldierList2.Count) {
stopOccupy = true;
StopCoroutine("DeOccupy");
}
}
IEnumerator Occupy(int state)
{
float gauge = 0;
float enemygauge = 0;
switch (state)
{
case 1:
occupyFill.color = Color.cyan;
gauge = occpupyGauge;
occpupyGauge2 = 0;
break;
case 2:
occupyFill.color = Color.magenta;
gauge = occpupyGauge2;
occpupyGauge = 0;
break;
default:
break;
}
float time = 0;
while ( gauge<100 ) {
if (stopOccupy != true) {
yield return new WaitForEndOfFrame ();
gauge += OccupySpeed * Time.deltaTime;
time += Time.deltaTime;
occupySlider.value = gauge;
switch (state)
{
case 1:
occpupyGauge = gauge;
break;
case 2:
occpupyGauge2 = gauge;
break;
default:
break;
}
} else {
stopOccupy = false;
AreaOccupy ();
break;
}
}
if(gauge>100 ){
occupyState = state;
switch (occupyState)
{
case 1:
occupyParticle.startColor = Color.cyan;
break;
case 2:
occupyParticle.startColor = Color.magenta;
break;
default:
occupyParticle.startColor = Color.white;
break;
}
}
}
IEnumerator DeOccupy(int state)
{
Debug.Log ("deoccupy");
bool exception = false;
float gauge = 0;
switch (state)
{
case 1:
occupyFill.color = Color.cyan;
gauge = occpupyGauge;
break;
case 2:
occupyFill.color = Color.magenta;
gauge = occpupyGauge2;
break;
default:
break;
}
float time = 0;
while ( gauge>0 ) {
yield return new WaitForEndOfFrame ();
gauge -= OccupySpeed * Time.deltaTime;
//Debug.Log (gauge);
time += Time.deltaTime;
occupySlider.value = gauge;
switch (state) {
case 1:
occpupyGauge = gauge;
break;
case 2:
occpupyGauge2 = gauge;
break;
default:
break;
}
}
if(gauge<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