Placido_GDD

MVC CubeData

Feb 3rd, 2022 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class CubeData : MonoBehaviour
  7. {
  8.     private SpriteRenderer spriteRenderer;
  9.     public int spriteID;
  10.     private int spriteRoll;
  11.     public int spawnerID;
  12.     public Sprite[] BoxSprites;
  13.     public string[] BoxTags;
  14.     public GameObject[] BoxList;
  15.  
  16.     public int BoxID;
  17.     Ray2D Ray_2DLeft;
  18.     Ray2D Ray_2DRight;
  19.     Ray2D Ray_2DUp;
  20.     Ray2D Ray_2DDown;
  21.     public Vector2[] CubeRays;
  22.     public Vector2[] RayOffset;
  23.     public float rayDistance;
  24.     public bool isActive;
  25.     public Transform ObjTransform;
  26.     public bool isMoving;
  27.     RaycastHit2D Left_2DRay;
  28.     RaycastHit2D Right_2DRay;
  29.     RaycastHit2D Up_2DRay;
  30.     RaycastHit2D Down_2DRay;
  31.     void Start()
  32.     {
  33.         isMoving = true;
  34.         ObjTransform = this.gameObject.transform;
  35.         BoxList = new GameObject[4];
  36.         spriteRenderer = GetComponent<SpriteRenderer>();
  37.         SetSprite();
  38.         InvokeRepeating("RandomBox", 10.0f, 10.0f);
  39.     }
  40.  
  41.     private void Update()
  42.     {
  43.         Draw_2DRays();
  44.         C_2DRaycasts();
  45.        
  46.     }
  47.  
  48.     private void FixedUpdate()
  49.     {
  50.         CheckBottom();
  51.     }
  52.     void CheckBottom()
  53.     {
  54.         RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up,rayDistance);
  55.         if (hit.collider != null)
  56.         {
  57.             Debug.Log(hit.collider);
  58.             isMoving = false;
  59.         }
  60.     }
  61.     public void RandomBox()
  62.     {
  63.         if (this.gameObject.tag == "Grey")
  64.         {
  65.             GreyBox();
  66.         }
  67.     }
  68.     public void GreyBox()
  69.     {
  70.         //spriteID = Random.Range(0, BoxSprites.Length - 2);
  71.         spriteRoll = Random.Range(0, 100);
  72.         if (spriteRoll > 0 && spriteRoll <= 20) //20% chance
  73.         {
  74.             spriteID = 3;
  75.         }
  76.         else if(spriteRoll > 20 && spriteRoll <= 50)//40% chance
  77.         {
  78.             spriteID = 1;
  79.         }
  80.         else if (spriteRoll > 50 && spriteRoll <= 100)//40% chance
  81.         {
  82.             spriteID = 0;
  83.         }
  84.         spriteRenderer = GetComponent<SpriteRenderer>();
  85.         spriteRenderer.sprite = BoxSprites[spriteID];
  86.         this.gameObject.tag = BoxTags[spriteID];
  87.     }
  88.     public void SetSprite()
  89.     {
  90.         spriteRoll = Random.Range(0, 100);
  91.         if (spriteRoll > 0 && spriteRoll <= 10) //10% chance
  92.         {
  93.             spriteID = 3;
  94.         }
  95.         else if (spriteRoll > 10 && spriteRoll <= 40)//30% chance
  96.         {
  97.             spriteID = 2;
  98.         }
  99.         else if (spriteRoll > 40 && spriteRoll <= 70)//30% chance
  100.         {
  101.             spriteID = 1;
  102.         }
  103.         else if (spriteRoll > 70 && spriteRoll <= 100)//30% chance
  104.         {
  105.             spriteID = 0;
  106.         }
  107.  
  108.         spriteRenderer = GetComponent<SpriteRenderer>();
  109.         spriteRenderer.sprite = BoxSprites[spriteID];
  110.         this.gameObject.tag = BoxTags[spriteID];
  111.     }
  112.  
  113.     public void CleanBoxList()
  114.     {
  115.         System.Array.Clear(BoxList, 0, BoxList.Length);
  116.     }
  117.     void Draw_2DRays()
  118.     {
  119.         Ray_2DLeft = new Ray2D(ObjTransform.position - (Vector3)RayOffset[0], (CubeRays[0]));
  120.         Debug.DrawRay(ObjTransform.position - (Vector3)RayOffset[0], CubeRays[0], Color.green);
  121.  
  122.         Ray_2DRight = new Ray2D(ObjTransform.position + (Vector3)RayOffset[1], (CubeRays[1]));
  123.         Debug.DrawRay(ObjTransform.position + (Vector3)RayOffset[1], (CubeRays[1]), Color.green);
  124.  
  125.         Ray_2DUp = new Ray2D(ObjTransform.position + (Vector3)RayOffset[2], (CubeRays[2]));
  126.         Debug.DrawRay(ObjTransform.position + (Vector3)RayOffset[2], (CubeRays[2]), Color.green);
  127.  
  128.         Ray_2DDown = new Ray2D(ObjTransform.position - (Vector3)RayOffset[3], (CubeRays[3]));
  129.         Debug.DrawRay(ObjTransform.position - (Vector3)RayOffset[3], (CubeRays[3]), Color.green);
  130.  
  131.         Left_2DRay = Physics2D.Raycast(ObjTransform.position - (Vector3)RayOffset[0], CubeRays[0],rayDistance);
  132.         Right_2DRay = Physics2D.Raycast(ObjTransform.position + (Vector3)RayOffset[1], CubeRays[1],rayDistance);
  133.         Up_2DRay = Physics2D.Raycast(ObjTransform.position + (Vector3)RayOffset[2], CubeRays[2],rayDistance);
  134.         Down_2DRay = Physics2D.Raycast(ObjTransform.position - (Vector3)RayOffset[3], (CubeRays[3]),rayDistance);
  135.  
  136.     }
  137.  
  138.     void C_2DRaycasts()
  139.     {
  140.         if (this.gameObject.tag == "Red" && isMoving == false)
  141.         {
  142.             if (Left_2DRay.collider != null && Left_2DRay.collider.gameObject.tag == "Red")
  143.             {
  144.                 BoxList[0] = Left_2DRay.rigidbody.gameObject;
  145.             }
  146.             if (Right_2DRay.collider != null && Right_2DRay.collider.gameObject.tag == "Red")
  147.             {
  148.                 BoxList[1] = Right_2DRay.rigidbody.gameObject;
  149.             }
  150.         }
  151.  
  152.         else if (this.gameObject.tag == "Blue" && isMoving == false)
  153.         {
  154.             if (Up_2DRay.collider != null && Up_2DRay.collider.gameObject.tag == "Blue")
  155.             {
  156.                 BoxList[2] = Up_2DRay.rigidbody.gameObject;
  157.             }
  158.             if (Down_2DRay.collider != null && Down_2DRay.collider.gameObject.tag == "Blue")
  159.             {
  160.                 BoxList[3] = Down_2DRay.rigidbody.gameObject;
  161.             }
  162.         }
  163.  
  164.     }
  165.  
  166.  
  167. }
Add Comment
Please, Sign In to add comment