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.

283 lines
6.7 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonMovement : MonoBehaviour
{
[SerializeField] KeyCode FlapKey;
Rigidbody2D _rigidbody;
float _flapTime = 0f;
[SerializeField] float FlapForce = 300f;
[SerializeField] float FlapLength = .25f;
Animator _animator;
bool _inCloud;
[SerializeField] GameObject _fireballPrefab;
[SerializeField] GameObject _fireballSpawnPoint;
[SerializeField] GameObject _coneSpawnPoint;
[SerializeField] GameObject _flash;
[SerializeField] string _dragonName;
[SerializeField] Color _color;
[SerializeField] TrailRenderer _trail;
[SerializeField] List<SpriteRenderer> _ammoGui = new List<SpriteRenderer>();
float _stunTime = 0f;
float _debugCurSpeed;
bool _victory = false;
int _ammo = 0;
float _speedBoostTime = 0f;
public enum AttackType
{
FIREBALL,
CONE
}
[SerializeField] AttackType _attackType = AttackType.FIREBALL;
const string CONE_NAME = "cone";
public int Ammo { get => _ammo;
set {
_ammo = value;
for (int i = 0; i < _ammoGui.Count; ++i)
{
if (i < _ammo)
{
_ammoGui[i].enabled = true;
}
else
{
_ammoGui[i].enabled = false;
}
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
GameObject cone = transform.Find(CONE_NAME)?.gameObject;
if (cone)
{
GameObject.Destroy(cone);
}
CheckCloudCollision(collision);
if (collision.CompareTag("Bird"))
{
Bird bird = collision.GetComponent<Bird>();
bird.Eat();
Ammo = 3;
_speedBoostTime = 1f;
_attackType = bird.AttackType;
}
if (collision.CompareTag("FinishLine"))
{
Game game = FindObjectOfType<Game>();
game.Victory(_dragonName, _color);
}
if (collision.CompareTag("Lightning"))
{
Stun(.4f);
}
if (collision.CompareTag("Fireball"))
{
Fireball fireball = collision.GetComponent<Fireball>();
if (fireball.AttackType == AttackType.CONE)
{
Stun(.4f);
}
else
{
Stun(.6f);
}
}
}
private void OnTriggerStay2D(Collider2D collision)
{
CheckCloudCollision(collision);
}
private void CheckCloudCollision(Collider2D collision)
{
if (collision.CompareTag("Cloud"))
{
Cloud cloud = collision.GetComponent<Cloud>();
cloud.TriggerLighting(transform.position.x);
}
}
void Stun(float length)
{
_stunTime = length;
_animator.Play("fall");
}
private void OnTriggerExit2D(Collider2D collision)
{
_inCloud = false;
}
void Start()
{
Ammo = 0;
_rigidbody = this.GetComponent<Rigidbody2D>();
_animator = this.GetComponent<Animator>();
_trail.startColor = _color;
_trail.endColor = _color;
}
private void Update()
{
_speedBoostTime -= Time.deltaTime;
if (Input.GetKeyDown(FlapKey) && _stunTime <0f)
{
Flap();
Shoot();
}
if (_victory)
{
float scale = 8f * Time.deltaTime;
transform.localScale = transform.localScale + new Vector3(scale, scale, 0f);
}
}
void Flap()
{
_flapTime = FlapLength;
_animator.Play("fly");
}
void Shoot()
{
if (Ammo <= 0)
return;
Ammo--;
if (_attackType == AttackType.FIREBALL)
{
GameObject.Instantiate(_fireballPrefab, _fireballSpawnPoint.transform.position, Quaternion.identity);
}
else if (_attackType == AttackType.CONE)
{
GameObject fireball = GameObject.Instantiate(_fireballPrefab, _coneSpawnPoint.transform.position, Quaternion.identity);
fireball.name = CONE_NAME;
fireball.GetComponent<Fireball>().AttackType = AttackType.CONE;
fireball.transform.parent = this.transform;
}
}
public void ClearTrail()
{
_trail.Clear();
}
void FixedUpdate()
{
if (_flapTime > 0f && _stunTime < 0f)
{
_rigidbody.AddForce(new Vector2(0f, FlapForce));
}
if (_stunTime > 0f)
{
_rigidbody.AddForce(new Vector2(FlapForce * -.05f, FlapForce * -.05f));
transform.Rotate(Vector3.forward, Time.deltaTime * 1000f);
if (_flapTime < 0f)
{
_flapTime = 0f;
}
}
else
{
transform.rotation = Quaternion.identity;
}
_flapTime -= Time.deltaTime;
_stunTime -= Time.deltaTime;
const float SPEED = .4f;
float horizontalSpeed = SPEED;
float verticalSpeed = 0f;
const float GLIDE_TIME = 0.6f;
if (_flapTime < - GLIDE_TIME)
{
if (_trail.enabled == false)
{
_trail.Clear();
_trail.enabled = true;
}
_animator.Play("speedup");
horizontalSpeed += (_flapTime + GLIDE_TIME) * -2f;
}
else
{
_trail.enabled = false;
}
/* //old cloud behavior
if (_inCloud)
{
if (_flapTime < 0)
_flapTime = 0;
if (_animator.GetCurrentAnimatorStateInfo(0).IsName("speedup"))
{
_animator.Play("glide");
}
horizontalSpeed = -3f;
if (_rigidbody.velocity.y > 0)
verticalSpeed = -2f;
else if (_rigidbody.velocity.y < 0)
verticalSpeed = 2f;
}
*/
if (_stunTime > 0f)
{
horizontalSpeed = -5f;
}
const float MAX_SPEED = SPEED * 2.2f;
if (horizontalSpeed > MAX_SPEED)
{
horizontalSpeed = MAX_SPEED;
}
if (_speedBoostTime > 0f)
{
horizontalSpeed *= 2.3333f;
}
_debugCurSpeed = horizontalSpeed;
float x = transform.position.x + horizontalSpeed;
x = Mathf.Max(x, 32);
transform.position = new Vector3(x, transform.position.y+ verticalSpeed, transform.position.z);
}
public void Flash()
{
_flash.SetActive(true);
Invoke("Unflash", .2f);
}
public void Unflash()
{
_flash.SetActive(false);
}
}