Momo-Space-Diner-Code-Repo / InstantiateObject.cs
InstantiateObject.cs
Raw
using ML.SDK;
using UnityEngine;
using System.Collections.Generic;

public class InstantiateObject : MonoBehaviour
{
    // The axis of rotation (X or Y)
    public enum RotationAxis { X_Axis, Y_Axis }
    public RotationAxis rotationAxis = RotationAxis.Y_Axis;

    // Rotation angles for open and closed positions
    private Quaternion closedRotation;
    private Quaternion openRotation;
    public float rotationSpeed = 2f;

    // Control door state
    private bool isOpening = false;
    private bool isClosing = false;

    // Flag to track door state
    private bool isDoorOpen = false;
    public MLClickable clickableComponent;
    public GameObject FoodObject;
    public Transform spawnlocation;
    const string EVENT_ID = "OnObjectInstantiate";
    public int Count = 0;
    const string EVENT_ID_CHANGEOBJNAME = "ChangeOBJName";
    private GameObject newChicken;
    public MLSyncVarAttribute MaxCount;
    public int MaximumCount = 10;

    // List to keep track of spawned objects
    public List<GameObject> SpawnedObjects = new List<GameObject>();

    public void OnPlayerClickFood(MLPlayer player)
    {
        Debug.Log("Local Button Pressed");
        this.InvokeNetwork(EVENT_ID, EventTarget.All, null, true);
    }

    public void OnNetworkSpawnObject(object[] args)
    {

        if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
        {
            return;
        }

        if (Count < MaximumCount)
        {
            SpawnObject();
        }
    }

    public void OnNetworkChangeOBJname(object[] args)
    {
        if (this == null || gameObject == null || gameObject.name == null || args[0] == null)
        {
            return;
        }

        Debug.Log("Name change called from master client");
        gameObject.name = (string)args[0];
        Debug.Log("New name : " + gameObject.name);
    }

    EventToken InstantiateToken;
    EventToken ChangeToken;

    void Start()
    {
        clickableComponent.OnPlayerClick.AddListener(OnPlayerClickFood);
        InstantiateToken = this.AddEventHandler(EVENT_ID, OnNetworkSpawnObject);
        ChangeToken = this.AddEventHandler(EVENT_ID_CHANGEOBJNAME, OnNetworkChangeOBJname);
    }

    void Update()
    {
        // Update logic if needed
    }

    // Add this method to handle object removal
    public void HandleObjectDestroyed(GameObject destroyedObject)
    {

        if (this == null || gameObject == null || gameObject.name == null )
        {
            return;
        }

        SpawnedObjects.Remove(destroyedObject);
        Count -= 1;
    }

    // Modify SpawnObject() to set the reference to the parent script
    public void SpawnObject()
    {
        Count += 1;
        if (MassiveLoopClient.IsMasterClient)
        {
            newChicken = Object.Instantiate(FoodObject, spawnlocation.position, Quaternion.identity);
            newChicken.name += Count;
            this.InvokeNetwork(EVENT_ID_CHANGEOBJNAME, EventTarget.All, null, newChicken.name);

            // Add the spawned object to the list
            SpawnedObjects.Add(newChicken);

            // Attach the TrackedObject script and set the parent script reference
            TrackedObject trackedObj = (TrackedObject)newChicken.AddComponent(typeof(TrackedObject));
            trackedObj.parentScript = this;
        }
    }

    // Component for tracking object destruction
    public class TrackedObject : MonoBehaviour
    {
        public InstantiateObject parentScript;

        private void OnDestroy()
        {
            if (this == null || gameObject == null || gameObject.name == null)
            {
                return;
            }

            if (parentScript != null)
            {
                parentScript.HandleObjectDestroyed(this.gameObject);
            }
        }
    }
}