Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.44 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GhostScript : MonoBehaviour
  6. {
  7.     private Vector2 teleportPoint;
  8.     private float distanceBetweenPlayerAndTeleportPoint;
  9.     private bool hasBeenReset;
  10.     private bool isNotAttacking;
  11.     public float attackSpeed = 7f;
  12.     private Vector2 attackDirection;
  13.     public int timesTeleported;
  14.     public int timesReset;
  15.     private int teleportLimit;
  16.     private int resetLimit;
  17.     private SpriteRenderer spriteRend;
  18.     private float xPositionDifference;
  19.     private bool facingRight = true;
  20.     public float moveSpeed = 1f;
  21.     private Rigidbody2D rb;
  22.     private Rigidbody2D prb;
  23.     private GameObject player;
  24.     private GameObject ghost;
  25.     private GameObject fleeObject;
  26.     private Vector2 moveDirection;
  27.     private Vector2 fleeDirection;
  28.     public float distanceBetweenPlayerAndGhost;
  29.     private bool hasStartedTeleporting;
  30.     public enum SState
  31.     {
  32.         isChasingPlayer,
  33.         isTeleporting,
  34.         isAttacking,
  35.         isLeaving
  36.     }
  37.     public SState ghostState;
  38.  
  39.     void Start()
  40.     {
  41.         hasBeenReset = true;
  42.         isNotAttacking = true;
  43.         spriteRend = GetComponent<SpriteRenderer>();
  44.         timesTeleported = 0;
  45.         timesReset = 0;
  46.         hasStartedTeleporting = false;
  47.         ghost = GameObject.Find("Ghost");
  48.         player = GameObject.Find("player_sprite");
  49.         fleeObject = GameObject.Find("death_platform");
  50.         prb = player.GetComponent<Rigidbody2D>();
  51.         ghostState = SState.isChasingPlayer;
  52.         rb = GetComponent<Rigidbody2D>();
  53.     }
  54.  
  55.     void FixedUpdate()
  56.     {
  57.         xPositionDifference = prb.position.x - rb.position.x;
  58.  
  59.         if (facingRight == false && xPositionDifference > 0)
  60.         {
  61.             Flip();
  62.         }
  63.         else if (facingRight == true && xPositionDifference < 0)
  64.         {
  65.             Flip();
  66.         }
  67.     }
  68.  
  69.     void Update()
  70.     {
  71.         teleportPoint = new Vector2(Random.Range(-5.5f, 5.5f), Random.Range(-4.5f, 4.2f));
  72.         distanceBetweenPlayerAndTeleportPoint = Vector3.Distance(teleportPoint, player.transform.position);
  73.         resetLimit = (Random.Range(1, 1));
  74.         teleportLimit = (Random.Range(3, 10));
  75.         distanceBetweenPlayerAndGhost = Vector3.Distance(player.transform.position, ghost.transform.position);
  76.  
  77.         if (distanceBetweenPlayerAndTeleportPoint <= 5f)
  78.         {
  79.             teleportPoint = new Vector2(Random.Range(-5.5f, 5.5f), Random.Range(-4.5f, 4.2f));
  80.         }
  81.  
  82.         if (ghostState == SState.isChasingPlayer)
  83.         {
  84.             moveDirection = (player.transform.position - transform.position).normalized * moveSpeed;
  85.             moveDirection = moveDirection.normalized;
  86.             rb.velocity = new Vector2(moveDirection.x * moveSpeed / 5, moveDirection.y * moveSpeed / 5);
  87.         }
  88.  
  89.         if (ghostState == SState.isChasingPlayer && distanceBetweenPlayerAndGhost <= 2f && hasStartedTeleporting == false)
  90.         {
  91.             hasStartedTeleporting = true;
  92.             ghostState = SState.isTeleporting;
  93.             StartCoroutine(fadeOut());
  94.             StartCoroutine(teleportCoroutine());
  95.             StartCoroutine(colliderController());
  96.         }
  97.  
  98.         if (ghostState == SState.isTeleporting)
  99.         {
  100.             rb.velocity = new Vector2(0, 0);
  101.         }
  102.  
  103.         if (ghostState == SState.isAttacking && isNotAttacking == true)
  104.         {
  105.             isNotAttacking = false;
  106.             StartCoroutine(attackCoroutine());
  107.         }
  108.  
  109.         if (ghostState == SState.isAttacking && hasBeenReset == true)
  110.         {
  111.             if (ghost.transform.position.x <= -5.4f || ghost.transform.position.x >= 5.6f || ghost.transform.position.y <= -4.4f || ghost.transform.position.y >= 4.3f)
  112.             {
  113.                 hasBeenReset = false;
  114.                 StartCoroutine(resetCoroutine());
  115.             }
  116.         }
  117.  
  118.         if (ghostState == SState.isLeaving)
  119.         {
  120.             if (ghost.transform.position.x <= -5.4f || ghost.transform.position.x >= 5.6f || ghost.transform.position.y <= -4.4f || ghost.transform.position.y >= 4.3f)
  121.             {
  122.                 StartCoroutine(selfDestructCoroutine());
  123.             }
  124.             else
  125.             {
  126.  
  127.             }
  128.         }  
  129.     }
  130.  
  131.     void Flip()
  132.     {
  133.         facingRight = !facingRight;
  134.         Vector3 Scaler = transform.localScale;
  135.         Scaler.x *= -1;
  136.         transform.localScale = Scaler;
  137.     }
  138.  
  139.     IEnumerator fadeOut()
  140.     {
  141.         if (timesTeleported <= teleportLimit)
  142.         {
  143.             for (float f = 1f; f >= -0.10f; f -= 0.10f)
  144.             {
  145.                 Color c = spriteRend.material.color;
  146.                 c.a = f;
  147.                 spriteRend.material.color = c;
  148.                 yield return new WaitForSeconds(0.05f);
  149.                 StartCoroutine(fadeIn());
  150.             }
  151.         }
  152.         else
  153.         {
  154.  
  155.         }
  156.     }
  157.  
  158.     IEnumerator fadeIn()
  159.     {
  160.         for (float f = 0.10f; f <= 1; f += 0.10f)
  161.         {
  162.             Color c = spriteRend.material.color;
  163.             c.a = f;
  164.             spriteRend.material.color = c;
  165.             yield return new WaitForSeconds(0.05f);
  166.         }
  167.     }
  168.  
  169.     IEnumerator teleportCoroutine()
  170.     {
  171.         if (timesTeleported <= teleportLimit)
  172.         {
  173.             timesTeleported += 1;
  174.             yield return new WaitForSeconds(0.5f);
  175.             rb.position = teleportPoint;
  176.             yield return new WaitForSeconds(0.5f);
  177.             StartCoroutine(fadeOut());
  178.             StartCoroutine(teleportCoroutine());
  179.             StartCoroutine(colliderController());
  180.         }
  181.         else
  182.         {
  183.             ghostState = SState.isAttacking;
  184.         }
  185.     }
  186.  
  187.     IEnumerator colliderController()
  188.     {
  189.         GetComponent<PolygonCollider2D>().enabled = false;
  190.         yield return new WaitForSeconds(0.8f);
  191.         GetComponent<PolygonCollider2D>().enabled = true;
  192.         yield return new WaitForSeconds(0.1f);
  193.         GetComponent<PolygonCollider2D>().enabled = false;
  194.     }
  195.  
  196.     IEnumerator attackCoroutine()
  197.     {
  198.         GetComponent<PolygonCollider2D>().enabled = true;
  199.         yield return new WaitForSeconds(1.0f);
  200.         GetComponent<PolygonCollider2D>().enabled = true;
  201.         attackDirection = (player.transform.position - transform.position) * attackSpeed;
  202.         attackDirection = attackDirection.normalized;
  203.         rb.velocity = new Vector2(attackDirection.x * attackSpeed, attackDirection.y * attackSpeed);
  204.     }
  205.  
  206.     IEnumerator resetCoroutine()
  207.     {
  208.         if (timesReset <= resetLimit)
  209.         {
  210.             timesReset += 1;
  211.             timesTeleported = 0;
  212.             isNotAttacking = true;
  213.             ghostState = SState.isTeleporting;
  214.             StartCoroutine(fadeOut());
  215.             StartCoroutine(teleportCoroutine());
  216.             StartCoroutine(colliderController());
  217.             yield return new WaitForSeconds(0.05f);
  218.             hasBeenReset = true;
  219.         }
  220.         else
  221.         {
  222.             ghostState = SState.isLeaving;
  223.         }
  224.     }
  225.  
  226.     IEnumerator selfDestructCoroutine()
  227.     {
  228.         rb.velocity = new Vector2(0, 0);
  229.         yield return new WaitForSeconds(1.0f);
  230.         fleeDirection = (fleeObject.transform.position - transform.position).normalized * moveSpeed;
  231.         fleeDirection = fleeDirection.normalized;
  232.         rb.velocity = new Vector2(fleeDirection.x * moveSpeed, fleeDirection.y * moveSpeed);
  233.         Destroy(gameObject, 3f);
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement