[Code] using UnityEngine; using UnityEngine.Networking; using System.Collections; using System.Collections.Generic; using UnityEngine.UI;
public class NinjaStealth : NetworkBehaviour { [SyncVar(hook = "SyncVisibility")] private bool isVisible = true; [SyncVar(hook = "RestealthMethod")] private bool canRestealth = true; private bool canMove = true; private bool canUnstealth; private float restealthCountdown = 15; private float invisibleTime; private float alpha = 1.0f; private float fadeSpeed = 1f; private float duration = 1.0F; new public Text guiText; private Color colorStart = Color.white; private Color colorEnd = Color.clear; private Color m_Color; private Material m_Material; private Light directionalLight; private Transform ninja; public Transform smoke; public ParticleSystem smokebomb; public SpriteRenderer spriteRend; public NinjaController ninjController;
void Start()
{
directionalLight = GameObject.FindGameObjectWithTag("Light").GetComponent<Light>();
ninja = GameObject.FindGameObjectWithTag("Ninja").transform;
m_Material = GetComponentInChildren<SpriteRenderer>().material;
smokebomb = gameObject.GetComponent<ParticleSystem>();
spriteRend = GetComponentInChildren<SpriteRenderer>();
ninjController = GameObject.FindGameObjectWithTag("Ninja").GetComponent<NinjaController>();
m_Color = m_Material.color;
}
void Update()
{
if (!isVisible && isServer)
{
invisibleTime -= Time.deltaTime;
if (invisibleTime <= 0.0f)
{
isVisible = true;
}
}
if (!canRestealth && isServer)
{
restealthCountdown -= Time.deltaTime;
if (restealthCountdown <= 0.0f)
{
canRestealth = true;
}
}
if (!isLocalPlayer)
return;
if (Input.GetKeyDown(KeyCode.E) && (canRestealth))
{
canRestealth = false;
canUnstealth = true;
CmdGoInvisible();
}
if (Input.GetKeyDown(KeyCode.R) && (canUnstealth) && (!isVisible))
{
CmdGoVisible();
}
}
[Command]
private void CmdGoVisible()
{
isVisible = true;
restealthCountdown = 15f;
canRestealth = false;
}
[Command]
private void CmdGoInvisible()
{
isVisible = false;
invisibleTime = 20.0f;
}
[Command]
private void CmdRestealthing()
{
canRestealth = true;
}
private void RestealthMethod(bool canRestealth)
{
this.canRestealth = canRestealth;
if(isLocalPlayer)
{
canRestealth = true;
}
}
private void SyncVisibility(bool isVisible)
{
this.isVisible = isVisible;
if (!isVisible)
{
ColorChange();
smoke.transform.parent = gameObject.transform;
smoke.position = gameObject.transform.position;
smoke.GetComponentInChildren<ParticleSystem>().Play();
smoke.parent = null;
}
else
{
GetComponentInChildren<Renderer>().material.color = Color.white;
}
if (isLocalPlayer)
{
if (!isVisible)
{
smoke.GetComponentInChildren<ParticleSystem>().Play();
smoke.parent = null;
StartCoroutine(AlphaFade());
}
else
{
StopCoroutine("AlphaFade");
StopCoroutine("FadeIn");
StartCoroutine(FadeOut());
StartCoroutine(Unstealth());
}
}
}
private void ColorChange()
{
GetComponentInChildren<Renderer>().material.color = Color.clear;
}
IEnumerator AlphaFade()
{
{
for (float f = 1f; f >= 0; f -= 0.1f)
{
m_Color.a = f;
gameObject.GetComponentInChildren<Renderer>().material.color = m_Color;
yield return new WaitForSeconds(.001f);
if (f < .1f)
{
directionalLight.intensity = 0;
Material newMat = Resources.Load("Smoke", typeof(Material)) as Material;
gameObject.GetComponentInChildren<Renderer>().material = newMat;
alpha = 1;
StartCoroutine(FadeIn());
yield return null;
}
}
}
}
IEnumerator FadeIn()
{
float ratio = 0;
while (ratio < 1)
{
ratio += Time.deltaTime * .5f;
directionalLight.intensity = .5f;
directionalLight.intensity = Mathf.Lerp(.5f, 0f, ratio);
yield return null;
}
}
IEnumerator FadeOut()
{
float ratio = 0;
while (ratio < 1)
{
ratio += Time.deltaTime * fadeSpeed;
directionalLight.intensity = 0f;
directionalLight.intensity = Mathf.Lerp(0, .5f, ratio);
yield return null;
}
}
IEnumerator Unstealth()
{
yield return new WaitForSeconds(.1f);
Material plainMat = Resources.Load("Default", typeof(Material)) as Material;
gameObject.GetComponentInChildren<Renderer>().material = plainMat;
smoke.GetComponentInChildren<ParticleSystem>().Stop();
}
} [/Code]