You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cloud : MonoBehaviour
{
[SerializeField] GameObject _lightning;
[SerializeField] List<Sprite> _sprites;
float _timeToHideLightning = 0f;
float _lightningTimeout = 0f;
float _timeToRandomStrike;
const bool DO_RANDOM_STRIKES = true;
// Start is called before the first frame update
void Start()
{
ReInit();
HideLightning();
SetTimeForNextRandomStrike();
}
// Update is called once per frame
void Update()
{
const float SPEED = 1f;
this.transform.position = new Vector3(transform.position.x - SPEED, transform.position.y, transform.position.z);
_lightningTimeout -= Time.deltaTime;
_timeToHideLightning -= Time.deltaTime;
_timeToRandomStrike -= Time.deltaTime;
if (_timeToHideLightning < 0f)
{
HideLightning();
}
if (_timeToRandomStrike < 0f && DO_RANDOM_STRIKES)
{
TriggerLighting(_lightning.transform.position.x);
SetTimeForNextRandomStrike();
}
}
void SetTimeForNextRandomStrike()
{
_timeToRandomStrike = Random.Range(4f, 10f);
}
private void HideLightning()
{
_lightning.SetActive(false);
_timeToHideLightning = float.MaxValue;
}
public void ReInit()
{
float scale = Random.Range(1.5f, 2.5f);
float x = Random.Range(-100f, 100f);
float y = Random.Range(95f, 975f);
transform.localScale = new Vector3(scale, scale, 2f);
transform.position = new Vector3(transform.position.x + x, y, transform.position.z);
}
public void TriggerLighting(float x)
{
if (_lightningTimeout > 0)
return;
_lightning.SetActive(true);
_lightning.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = _sprites[Random.Range(0, _sprites.Count)];
_lightning.transform.position = new Vector3(x, _lightning.transform.position.y, _lightning.transform.position.z);
_timeToHideLightning = .2f;
_lightningTimeout = 2.0f;
}
}