Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. /// <summary>
  6. /// Agent behaviour class
  7. /// Author: Thomas Elliot Jones
  8. /// </summary>
  9.  
  10. public class FoodBehaviour : MonoBehaviour {
  11.  
  12.     public AgentBehaviour agent;
  13.  
  14.     public bool foodLeft;
  15.  
  16.     private float amount;
  17.     private float decrement;
  18.     private int rand;
  19.  
  20.     private Vector3 randScale;
  21.  
  22.     /*
  23.      * If scale is 1, y = 0.1
  24.      * If scale is 2, y = 0.5
  25.      * If scale is 3, y = 1
  26.      * If scale is 4, y = 1.5
  27.      * If scale is 5, y = 2.
  28.      */
  29.  
  30.     // Use this for initialization
  31.     void Start () {
  32.         foodLeft = true;
  33.         decrement = 0.25f;
  34.  
  35.         //rand = Random.Range(1, 5);
  36.         //Randomly assign a scale to the food blocks.
  37.         //randScale = new Vector3(rand, rand, rand);
  38.         //this.transform.localScale = randScale;
  39.  
  40.         // Set the amount to match its scale.
  41.         amount = this.transform.localScale.x * this.transform.localScale.y * this.transform.localScale.z;
  42.     }
  43.    
  44.     // Update is called once per frame
  45.     void Update () {
  46.         if (amount <= 0f)
  47.         {
  48.             foodLeft = false;
  49.             this.gameObject.GetComponent<MeshRenderer>().enabled = false;
  50.             this.transform.localScale = new Vector3(1f, 1f, 1f);
  51.             //this.transform.GetComponent<SphereCollider>().radius = 3;
  52.         }
  53.     }
  54.  
  55.     void OnTriggerEnter(Collider other)
  56.     {
  57.         // Check if the object is an ant.
  58.         if (other.gameObject.tag == "Agent")
  59.         {
  60.             if (this.foodLeft && (!other.gameObject.GetComponent<AgentBehaviour>().transportingFood))
  61.             {
  62.                 amount = amount - decrement;
  63.                 // If the agent is not carrying food and has food left, decrement the amount
  64.                 // and its size accordingly.
  65.                 Vector3 newSize = new Vector3(Mathf.Pow(amount, 1f / 1.5f), Mathf.Pow(amount, 1f / 1.5f), Mathf.Pow(amount, 1f / 1.5f));
  66.                 this.transform.localScale = newSize;
  67.                 this.transform.position = new Vector3(this.transform.position.x, this.transform.localScale.y / 4.5f, this.transform.position.z);
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement