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.

392 lines
10 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragonMovement : MonoBehaviour
{
const bool ROTATE_WITH_FLAP = false;
const bool LEAN_WITH_SHOOT = true;
[SerializeField] KeyCode FlapKey;
Rigidbody2D _rigidbody;
float _flapTime = 0f;
[SerializeField] float FlapForce = 300f;
[SerializeField] float FlapLength = .25f;
2 years ago
Animator _animator;
2 years ago
bool _inCloud;
2 years ago
[SerializeField] GameObject _fireballPrefab;
[SerializeField] GameObject _fireballSpawnPoint;
[SerializeField] GameObject _coneSpawnPoint;
2 years ago
[SerializeField] GameObject _flash;
[SerializeField] string _dragonName;
[SerializeField] Color _color;
2 years ago
[SerializeField] List<SpriteRenderer> _ammoGui = new List<SpriteRenderer>();
2 years ago
[SerializeField] List<SpriteRenderer> _ammoGui2 = new List<SpriteRenderer>();
[SerializeField] GameObject _ammoNormal;
[SerializeField] GameObject _ammoGlide;
const float DIVE_TIME = 0.4f;
float _buttonHoldTime = 0f;
2 years ago
ArrayList _ghosts = new ArrayList();
float _stunTime = 0f;
2 years ago
float _debugCurSpeed;
2 years ago
bool _victory = false;
2 years ago
int _ammo = 0;
float _speedBoostTime = 0f;
2 years ago
float _timeToUpdateGhosts = 0f;
bool _isInDive = false;
2 years ago
public enum AttackType
{
FIREBALL,
CONE
}
[SerializeField] AttackType _attackType = AttackType.FIREBALL;
2 years ago
const string CONE_NAME = "cone";
2 years ago
public int Ammo { get => _ammo;
set {
_ammo = value;
for (int i = 0; i < _ammoGui.Count; ++i)
{
if (i < _ammo)
{
_ammoGui[i].enabled = true;
2 years ago
_ammoGui2[i].enabled = true;
2 years ago
}
else
{
_ammoGui[i].enabled = false;
2 years ago
_ammoGui2[i].enabled = false;
2 years ago
}
}
}
}
2 years ago
private void OnTriggerEnter2D(Collider2D collision)
{
2 years ago
/*
GameObject cone = transform.Find(CONE_NAME)?.gameObject;
if (cone)
{
GameObject.Destroy(cone);
}
*/
2 years ago
CheckCloudCollision(collision);
2 years ago
if (collision.CompareTag("Bird"))
{
Bird bird = collision.GetComponent<Bird>();
bird.Eat();
2 years ago
Ammo = 5;
//_speedBoostTime = 1f;
2 years ago
_attackType = bird.AttackType;
2 years ago
if (_attackType == AttackType.FIREBALL)
ColorizeAmmo(Color.red);
else
ColorizeAmmo(Color.green);
2 years ago
}
if (collision.CompareTag("FinishLine"))
{
Game game = FindObjectOfType<Game>();
game.Victory(_dragonName, _color);
}
2 years ago
if (collision.CompareTag("Lightning"))
{
Stun(.4f);
2 years ago
}
if (collision.CompareTag("Fireball"))
{
Fireball fireball = collision.GetComponent<Fireball>();
if (fireball != null && fireball.transform.parent != null && fireball.transform.parent.gameObject == this.gameObject)
return;
if (fireball.AttackType == AttackType.CONE)
{
2 years ago
Stun(.4f);
}
else
{
Stun(.6f);
}
}
if (collision.CompareTag("Pterodactyl"))
{
Stun(.8f);
}
2 years ago
}
2 years ago
void ColorizeAmmo(Color color)
{
for (int i = 0; i < _ammoGui.Count; ++i)
{
_ammoGui[i].color = color;
_ammoGui2[i].color = color;
}
}
2 years ago
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)
{
_isInDive = false;
2 years ago
_stunTime = length;
_animator.Play("fall");
2 years ago
ShowNormalAmmo();
2 years ago
}
2 years ago
private void OnTriggerExit2D(Collider2D collision)
{
_inCloud = false;
}
void Start()
{
2 years ago
GameObject ghostContainer = GameObject.Find("Ghosts");
for (int i = 0; i < 5; ++i)
{
GameObject ghost = transform.Find("Ghost" + (i + 1).ToString()).gameObject;
ghost.transform.parent = ghostContainer.transform;
_ghosts.Add(ghost);
}
2 years ago
Ammo = 0;
_rigidbody = this.GetComponent<Rigidbody2D>();
2 years ago
_animator = this.GetComponent<Animator>();
2 years ago
//Ammo = 5;
//_attackType = AttackType.CONE;
}
private void Update()
{
2 years ago
_speedBoostTime -= Time.deltaTime;
2 years ago
_timeToUpdateGhosts -= Time.deltaTime;
if (_timeToUpdateGhosts < 0)
{
UpdateGhosts();
}
2 years ago
if (Input.GetKeyDown(FlapKey) && _stunTime <0f)
{
2 years ago
Flap();
Shoot();
}
if (_victory)
{
float scale = 8f * Time.deltaTime;
transform.localScale = transform.localScale + new Vector3(scale, scale, 0f);
}
}
2 years ago
void Flap()
{
_flapTime = FlapLength;
_animator.Play("fly");
transform.eulerAngles = new Vector3(-45f, transform.eulerAngles.y, transform.eulerAngles.z);
2 years ago
ShowNormalAmmo();
}
void ShowNormalAmmo()
{
_ammoNormal.SetActive(true);
_ammoGlide.SetActive(false);
2 years ago
}
void NormalFlight()
{
_animator.Play("glide");
ShowNormalAmmo();
}
2 years ago
void Shoot()
{
2 years ago
if (Ammo <= 0)
return;
2 years ago
_speedBoostTime = .2f;
2 years ago
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;
Rigidbody2D rigidBody = fireball.AddComponent<Rigidbody2D>();
rigidBody.isKinematic = true;
}
2 years ago
}
void FixedUpdate()
{
if (_flapTime > 0f && _stunTime < 0f)
{
_rigidbody.AddForce(new Vector2(0f, FlapForce));
}
if (_stunTime > 0f)
{
2 years ago
_rigidbody.AddForce(new Vector2(FlapForce * -.05f, FlapForce * -.05f));
transform.Rotate(Vector3.forward, Time.deltaTime * 1000f);
if (_flapTime < 0f)
{
_flapTime = 0f;
}
}
else if (_flapTime > 0 && ROTATE_WITH_FLAP)
{
transform.eulerAngles = new Vector3(0f, 0f, 30f);
}
else if (_speedBoostTime > 0 && LEAN_WITH_SHOOT)
{
transform.eulerAngles = new Vector3(0f, 0f, -15f);
}
2 years ago
else
{
transform.rotation = Quaternion.identity;
}
_flapTime -= Time.deltaTime;
_stunTime -= Time.deltaTime;
if (Input.GetKey(FlapKey))
{
_buttonHoldTime += Time.deltaTime;
}
else
{
_buttonHoldTime = 0f;
}
2 years ago
const float SPEED = .4f;
2 years ago
float horizontalSpeed = SPEED;
2 years ago
float verticalSpeed = 0f;
if (_buttonHoldTime >= DIVE_TIME)
{
if (_isInDive == false)
{
_isInDive = true;
_ammoNormal.SetActive(false);
_ammoGlide.SetActive(true);
_animator.Play("speedup");
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, 0f);
_rigidbody.AddForce(new Vector2(FlapForce * .1f, FlapForce * -5.5f));
}
horizontalSpeed += (_buttonHoldTime) * 3f;
}
2 years ago
else
{
if (_isInDive)
{
NormalFlight();
_isInDive = false;
}
2 years ago
}
2 years ago
if (_stunTime > 0f)
{
2 years ago
horizontalSpeed = -5f;
2 years ago
}
2 years ago
const float MAX_SPEED = SPEED * 2.2f;
if (horizontalSpeed > MAX_SPEED)
{
horizontalSpeed = MAX_SPEED;
}
2 years ago
if (_speedBoostTime > 0f)
{
horizontalSpeed *= 2.3333f *3f;
2 years ago
}
2 years ago
_debugCurSpeed = horizontalSpeed;
2 years ago
float x = transform.position.x + horizontalSpeed;
x = Mathf.Max(x, 32);
transform.position = new Vector3(x, transform.position.y+ verticalSpeed, transform.position.z);
2 years ago
}
2 years ago
public void Flash()
{
_flash.SetActive(true);
Invoke("Unflash", .2f);
}
public void Unflash()
{
_flash.SetActive(false);
}
2 years ago
void UpdateGhosts()
{
if (_isInDive == false)
2 years ago
{
foreach (GameObject ghost in _ghosts)
{
ghost.transform.position = new Vector3(-10000, -10000, -10000);
}
}
else
{
for (int i = _ghosts.Count - 1; i >= 0; --i)
{
GameObject thisGhost = (GameObject)_ghosts[i];
GameObject prevGhost;
if (i == 0)
{
prevGhost = this.gameObject;
}
else
prevGhost = (GameObject)_ghosts[i - 1];
thisGhost.GetComponent<SpriteRenderer>().sprite = prevGhost.GetComponent<SpriteRenderer>().sprite;
//thisGhost.scale = prevGhost.scale;
//make the horizontal offset increase with the amount of time in the glide, capping out at -12, and starting out at -5
thisGhost.transform.position = new Vector3(prevGhost.transform.position.x - 12f, prevGhost.transform.position.y, prevGhost.transform.position.z + .1f);
//if (i == 0 && _hidden)
//{
//thisGhost.transform.position = new Vector3(-10000, -10000, 0.1f);
//}
}
}
_timeToUpdateGhosts = .025f;
}
}