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.

127 lines
3.1 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;
float _stunTime = 0f;
public enum AttackType
{
FIREBALL,
CONE
}
[SerializeField] AttackType _attackType = AttackType.FIREBALL;
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("ontriggerenter");
_inCloud = true;
if (collision.CompareTag("Fireball"))
{
Fireball fireball = collision.GetComponent<Fireball>();
if (fireball.AttackType == AttackType.CONE)
{
_stunTime = .6666f;
}
else
{
_stunTime = .3333f;
}
_animator.Play("fall");
}
}
private void OnTriggerExit2D(Collider2D collision)
{
_inCloud = false;
}
void Start()
{
_rigidbody = this.GetComponent<Rigidbody2D>();
_animator = this.GetComponent<Animator>();
}
private void Update()
{
if (Input.GetKeyDown(FlapKey) && _stunTime <0f)
{
Flap();
Shoot();
}
}
void Flap()
{
_flapTime = FlapLength;
_animator.Play("fly");
}
void Shoot()
{
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.GetComponent<Fireball>().AttackType = AttackType.CONE;
fireball.transform.parent = this.transform;
}
}
void FixedUpdate()
{
if (_flapTime > 0f && _stunTime < 0f)
{
_rigidbody.AddForce(new Vector2(0f, FlapForce));
}
if (_stunTime > 0f)
{
_rigidbody.AddForce(new Vector2(0f, FlapForce * -.5f));
}
_flapTime -= Time.deltaTime;
_stunTime -= Time.deltaTime;
float horizontalSpeed = .8f;
float verticalSpeed = 0f;
if (_inCloud)
{
horizontalSpeed = -2f;
if (_rigidbody.velocity.y > 0)
verticalSpeed = -2f;
else if (_rigidbody.velocity.y < 0)
verticalSpeed = 2f;
}
if (_flapTime > 0f)
{
horizontalSpeed = -1.5f;
}
if (_stunTime > 0f)
{
horizontalSpeed = -2f;
}
transform.position = new Vector3(transform.position.x + horizontalSpeed, transform.position.y+ verticalSpeed, transform.position.z);
}
}