// Start is called before the first frame update void Start() { state = LightningState.inactive; Camera camera = Camera.main; float halfHeight = camera.orthographicSize; extendScreenHeight = halfHeight * 2 * 1.3f; endPoint.position = new Vector3(endPoint.position.x, startPoint.position.y - extendScreenHeight, endPoint.position.z); midPosition = (startPoint.position + endPoint.position) / 2; light.SetPosition(0, startPoint.position); lightFullColor = light.startColor; lightFadeoutColor = new Color(light.startColor.r, light.startColor.g, light.startColor.b, 0); screenLightFullColor = screenLight.GetComponent <SpriteRenderer>().color; screenLightFadeOutColor = new Color(screenLightFullColor.r, screenLightFullColor.g, screenLightFullColor.b, 0); seedCollider = GameObject.Find("seed").GetComponent <Collider2D>(); foreach (LightningBoltScript bolt in lightningBolt) { bolt.StartPosition = startPoint.position; bolt.EndPosition = startPoint.position; } UpdateEndPosition(); //StartCoroutine(StartActivity()); }
void grow() { float growth = growthSpeed * Time.deltaTime; updateSize(growth); if (myCollider.size.x >= maxLength) { state = LightningState.Stable; StartCoroutine(startShrinking()); } }
void AnimateLightning() { for (int i = 0; i < iterationPerFrame; i++) { if (state == LightningState.goingUp) { branches[currentIndex].gameObject.SetActive(true); currentIndex++; } else if (state == LightningState.goingDown) { branches[currentIndex].gameObject.SetActive(false); currentIndex--; } if (currentIndex < 0) { currentTimer = 0; currentIndex = 0; timeBetween.Randomise(); OnNewLightning.Invoke(); state = LightningState.waitingBetween; return; } if (currentIndex >= branches.Count) { currentTimer = 0; timeAtApex.Randomise(); currentIndex = branches.Count - 1; state = LightningState.waitingApex; return; } } if (state == LightningState.waitingApex) { currentTimer += Time.deltaTime; if (currentTimer > timeAtApex) { state = LightningState.goingDown; } } else if (state == LightningState.waitingBetween) { currentTimer += Time.deltaTime; if (currentTimer > timeBetween) { UpdateStorm(); state = LightningState.goingUp; } } }
IEnumerator startShrinking() { yield return(new WaitForSeconds(timeBeforeShrink)); state = LightningState.Shrink; if (direction == 1f) { initialPosition = new Vector2(initialPosition.x + myCollider.size.x, initialPosition.y); } else { initialPosition = transform.position; } direction *= -1; }
void OnSerializeNetworkView(BitStream stream,NetworkMessageInfo info) { Vector3 pos; Vector3 targetPos; if (stream.isWriting) { pos = transform.position; targetPos = target.transform.position; stream.Serialize(ref pos); stream.Serialize(ref targetPos); } else { pos = Vector3.zero; targetPos = Vector3.zero; stream.Serialize(ref pos); stream.Serialize(ref targetPos); for (var i = m_BufferedState.Length - 1; i >= 1; i --) { m_BufferedState[i] = m_BufferedState[i-1]; } LightningState state = new LightningState(); state.timeStamp = (float)info.timestamp; state.pos = pos; state.targetPos = targetPos; m_BufferedState[0] = state; m_TimestampCount = Mathf.Min(m_TimestampCount + 1, m_BufferedState.Length); for (int i = 0; i < m_TimestampCount-1; i++) { if (m_BufferedState[i].timeStamp < m_BufferedState[i+1].timeStamp) { Debug.Log("State inconsistent"); } } } }
// Update is called once per frame void Update() { //if (Input.GetMouseButtonDown(0)) //{ // StartCoroutine(StartActivity()); //} //Debug.Log(state); countTime += Time.deltaTime; switch (state) { case LightningState.inactive: light.startColor = lightFadeoutColor; light.endColor = lightFadeoutColor; foreach (LightningBoltScript bolt in lightningBolt) { bolt.EndPosition = startPoint.position; bolt.gameObject.SetActive(false); } screenLight.SetActive(false); if (Camera.main.transform.position.y < midPosition.y) { if (CanGenerateLight()) { state = LightningState.showLight; countTime = 0; } else { state = LightningState.end; } //Debug.Log("pass middle" + midPosition.y + " " + Camera.main.transform.position.y); } break; case LightningState.showLight: if (countTime < lightFadeInTime) { light.startColor = Color.Lerp(lightFadeoutColor, lightFullColor, countTime / lightFadeInTime); light.endColor = light.startColor; } if (countTime > lightFadeInTime + lightStayTime) { countTime = 0; state = LightningState.fadeoutLight; } break; case LightningState.fadeoutLight: if (countTime < lightFadeOutTime) { light.startColor = Color.Lerp(lightFullColor, lightFadeoutColor, countTime / lightFadeOutTime); light.endColor = light.startColor; } else { countTime = 0; state = LightningState.showLightning; } break; case LightningState.showLightning: if (countTime < lightningBoltMoveTime) { Debug.Log("show light " + endPoint.position); foreach (LightningBoltScript bolt in lightningBolt) { bolt.EndPosition = Vector3.Lerp(startPoint.position, endPoint.position, countTime / lightningBoltMoveTime); bolt.gameObject.SetActive(true); } } else { countTime = 0; state = LightningState.screenLight; screenLight.SetActive(true); } break; case LightningState.screenLight: CheckCollide(); if (countTime < screenLightFadeOutTime + screenLightStayTime && countTime >= screenLightStayTime) { screenLight.GetComponent <SpriteRenderer>().color = Color.Lerp(screenLightFullColor, screenLightFadeOutColor, countTime / screenLightFadeOutTime); } else if (countTime > screenLightFadeOutTime + screenLightStayTime) { foreach (LightningBoltScript bolt in lightningBolt) { bolt.gameObject.SetActive(false); //bolt.EndPosition = Vector3.Lerp(startPoint.position, endPoint.position, countTime / lightningBoltMoveTime); } state = LightningState.end; } break; case LightningState.end: break; } }