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.

76 lines
2.1 KiB

2 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cloud : MonoBehaviour
{
2 years ago
[SerializeField] GameObject _lightning;
[SerializeField] List<Sprite> _sprites;
2 years ago
float _timeToHideLightning = 0f;
float _lightningTimeout = 0f;
float _timeToRandomStrike;
const bool DO_RANDOM_STRIKES = true;
2 years ago
// Start is called before the first frame update
void Start()
{
ReInit();
2 years ago
HideLightning();
SetTimeForNextRandomStrike();
2 years ago
}
// 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);
2 years ago
_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;
2 years ago
}
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);
}
2 years ago
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)];
2 years ago
_lightning.transform.position = new Vector3(x, _lightning.transform.position.y, _lightning.transform.position.z);
_timeToHideLightning = .2f;
_lightningTimeout = 2.0f;
}
2 years ago
}