Exemple #1
0
    //gets all VISIBILE SimObjs contained in a receptacle
    //this does not TAKE any of these items
    public static SimObj[] GetVisibleItemsFromReceptacle(Receptacle r, Camera agentCamera, Vector3 agentCameraPos, float maxDistance)
    {
        List <SimObj> items = new List <SimObj>();
        RaycastHit    hit;

        foreach (Transform t in r.Pivots)
        {
            if (t.childCount > 0)
            {
                SimObj item = t.GetChild(0).GetComponent <SimObj> ();
                //check whether the item is visible (center point only)
                //since it's inside a receptacle, it will be on the invisible layer
                if (CheckPointVisibility(item.CenterPoint, agentCamera))
                {
                    item.VisibleNow = true;
                    items.Add(item);
                }
                else
                {
                    item.VisibleNow = false;
                }
            }
        }
        return(items.ToArray());
    }
Exemple #2
0
        public override MetadataWrapper generateMetadataWrapper()
        {
            MetadataWrapper metaMessage = base.generateMetadataWrapper();

            metaMessage.lastAction        = lastAction;
            metaMessage.lastActionSuccess = lastActionSuccess;
            metaMessage.errorMessage      = errorMessage;

            if (errorCode != ServerActionErrorCode.Undefined)
            {
                metaMessage.errorCode = Enum.GetName(typeof(ServerActionErrorCode), errorCode);
            }

            List <InventoryObject> ios = new List <InventoryObject>();

            foreach (string objectId in inventory.Keys)
            {
                SimObj          so = inventory [objectId];
                InventoryObject io = new InventoryObject();
                io.objectId   = so.UniqueID;
                io.objectType = Enum.GetName(typeof(SimObjType), so.Type);
                ios.Add(io);
            }

            metaMessage.inventoryObjects = ios.ToArray();

            return(metaMessage);
        }
        public void PickupHandObject(ServerAction action)
        {
            GameObject hand    = getHand();
            bool       success = false;

            foreach (SimObj so in VisibleSimObjs(action))
            {
                // XXX CHECK IF OPEN
                if (!so.IsReceptacle && !IsOpenable(so))
                {
                    if (inventory.Count == 0)
                    {
                        addObjectInventory(so);
                        currentHandSimObj = so;

                        Rigidbody rb = so.GetComponentInChildren(typeof(Rigidbody)) as Rigidbody;
                        rb.freezeRotation     = true;
                        rb.constraints        = RigidbodyConstraints.FreezeAll;
                        rb.useGravity         = false;
                        so.transform.position = hand.transform.position;
//						so.transform.parent = this.transform;
//						so.transform.parent = m_CharacterController.transform;
                        so.transform.parent = hand.transform;
                        so.ResetScale();
                        so.transform.localPosition = new Vector3();
                        so.hasCollision            = false;
                        success = true;
                    }
                    break;
                }
            }
            actionFinished(success);
        }
        public void PutObject(ServerAction response)
        {
            bool success = false;

            if (inventory.ContainsKey(response.objectId))
            {
                foreach (SimObj rso in VisibleSimObjs(response.forceVisible))
                {
                    if (rso.IsReceptacle && (rso.UniqueID == response.receptacleObjectId || rso.Type == response.ReceptableSimObjType()))
                    {
                        SimObj so = removeObjectInventory(response.objectId);

                        if ((!IsOpenable(rso) || IsOpen(rso)) &&
                            ((response.forceVisible && SimUtil.AddItemToReceptacle(so, rso.Receptacle)) ||
                             SimUtil.AddItemToVisibleReceptacle(so, rso.Receptacle, m_Camera)))
                        {
                            success = true;
                        }
                        else
                        {
                            addObjectInventory(so);
                        }


                        break;
                    }
                }
            }
            actionFinished(success);
        }
        public SimObj removeObjectInventory(string objectId)
        {
            SimObj so = inventory [objectId];

            inventory.Remove(objectId);
            return(so);
        }
 void Awake()
 {
     ParentObj             = gameObject.GetComponent <SimObj> ();
     matArrayOn            = BurnerRenderer.sharedMaterials;
     matArrayOff           = BurnerRenderer.sharedMaterials;
     matArrayOn [MatIndex] = BurnerOnMat;
 }
Exemple #7
0
    //searches for a SimObj item under a receptacle by ID
    //this does not TAKE the item, it just searches for it
    public static bool FindItemFromReceptacleByType(SimObjType itemType, Receptacle r, out SimObj item)
    {
        item = null;
        //make sure we're not doing something insane
        if (r == null)
        {
            Debug.LogError("Receptacle was null, not searching for item");
            return(false);
        }
        if (itemType == SimObjType.Undefined)
        {
            Debug.LogError("Can't search for type UNDEFINED, not searching for item");
            return(false);
        }
        SimObj checkItem = null;

        for (int i = 0; i < r.Pivots.Length; i++)
        {
            if (r.Pivots [i].childCount > 0)
            {
                checkItem = r.Pivots [i].GetChild(0).GetComponent <SimObj> ();
                if (checkItem != null && checkItem.Type == itemType)
                {
                    //if the item under the pivot is a SimObj of the right type
                    //we've found what we're after
                    item = checkItem;
                    return(true);
                }
            }
        }
        //couldn't find it!
        return(false);
    }
Exemple #8
0
    //adds the item to a receptacle
    //enabled the object, parents it under an empty pivot, then makes it invisible to raycasts
    //returns false if there are no available pivots in the receptacle
    public static bool AddItemToReceptacle(SimObj item, Receptacle r)
    {
        //make sure we're not doing something insane
        if (item == null)
        {
            Debug.LogError("Can't add null item to receptacle");
            return(false);
        }
        if (r == null)
        {
            Debug.LogError("Receptacle was null, not adding item");
            return(false);
        }
        if (item.gameObject == r.gameObject)
        {
            Debug.LogError("Receptacle and item were same object, can't add item to itself");
            return(false);
        }
        //make sure there's room in the recepticle
        Transform emptyPivot = null;

        if (!GetFirstEmptyReceptaclePivot(r, out emptyPivot))
        {
            //Debug.Log ("Receptacle is full");
            return(false);
        }
        return(AddItemToReceptaclePivot(item, emptyPivot));
    }
Exemple #9
0
    //Puts an item back where you originally found it
    //returns true if successful, false if unsuccessful
    //if successful, object is placed into the world and activated
    //this only works on objects of SimObjManipulation type 'inventory'
    //if an object was spawned in a Receptacle, this function will also return false
    public static bool PutItemBackInStartupPosition(SimObj item)
    {
        if (item == null)
        {
            Debug.LogError("Item is null, not putting item back");
            return(false);
        }

        bool result = false;

        switch (item.Manipulation)
        {
        case SimObjManipType.Inventory:
            if (item.StartupTransform != null)
            {
                item.transform.position   = item.StartupTransform.position;
                item.transform.rotation   = item.StartupTransform.rotation;
                item.transform.localScale = item.StartupTransform.localScale;
                item.gameObject.SetActive(true);
                result = true;
            }
            else
            {
                Debug.LogWarning("Item had no startup transform. This probably means it was spawned in a receptacle.");
            }
            break;

        default:
            break;
        }
        return(result);
    }
Exemple #10
0
    //adds the item to a receptacle
    //enabled the object, parents it under an empty pivot, then makes it invisible to raycasts
    //returns false if there are no available pivots in the receptacle
    public static bool AddItemToReceptaclePivot(SimObj item, Transform pivot)
    {
        if (item == null)
        {
            Debug.LogError("SimObj item was null in AddItemToReceptaclePivot, not adding");
            return(false);
        }

        if (pivot == null)
        {
            Debug.LogError("Pivot was null when attempting to add item " + item.name + " to Receptacle pivot, not adding");
            return(false);
        }

        //if there's room, parent it under the empty pivot
        item.transform.parent        = pivot;
        item.transform.localPosition = Vector3.zero;
        item.transform.localRotation = Quaternion.identity;
        //don't scale the item

        //make sure the item is active (in case it's been in inventory)
        item.gameObject.SetActive(true);
        item.RecalculatePoints();

        //disable the item's colliders (since we're no longer raycasting it directly)
        item.VisibleToRaycasts = false;

        //we're done!
        return(true);
    }
Exemple #11
0
    void OnEnable()
    {
        equatorColor = RenderSettings.ambientEquatorColor;
        groundColor  = RenderSettings.ambientGroundColor;
        skyColor     = RenderSettings.ambientSkyColor;

        ParentObj = gameObject.GetComponent <SimObj>();
        if (ParentObj == null)
        {
            ParentObj = gameObject.AddComponent <SimObj>();
        }

        if (!Application.isPlaying)
        {
            Animator a = ParentObj.gameObject.GetComponent <Animator>();
            if (a == null)
            {
                a = ParentObj.gameObject.AddComponent <Animator>();
                a.runtimeAnimatorController = Resources.Load("ToggleableAnimController") as RuntimeAnimatorController;
            }
        }
        else
        {
            if (OnByDefault)
            {
                ParentObj.Animator.SetBool("AnimState1", true);
            }
        }
    }
Exemple #12
0
    void OnEnable()
    {
        EditorOpen = OpenByDefault;
        ParentObj  = gameObject.GetComponent <SimObj>();
        if (ParentObj == null)
        {
            ParentObj = gameObject.AddComponent <SimObj>();
        }
        ParentObj.Type = SimObjType.Blinds;

        if (!Application.isPlaying)
        {
            Animator a = ParentObj.gameObject.GetComponent <Animator>();
            if (a == null)
            {
                a = ParentObj.gameObject.AddComponent <Animator>();
                a.runtimeAnimatorController = Resources.Load("ToggleableAnimController") as RuntimeAnimatorController;
            }
        }
        else
        {
            if (OpenByDefault)
            {
                ParentObj.Animator.SetBool("AnimState1", true);
            }
        }
    }
Exemple #13
0
    //searches for a SimObj item under a receptacle by ID
    //this does not TAKE the item, it just searches for it
    public static bool FindItemFromReceptacleByID(string itemID, Receptacle r, out SimObj item)
    {
        item = null;
        //make sure we're not doing something insane
        if (r == null)
        {
            Debug.LogError("Receptacle was null, not searching for item");
            return(false);
        }

        if (!IsUniqueIDValid(itemID))
        {
            Debug.LogError("itemID " + itemID.ToString() + " is NOT valid, not searching for item");
            return(false);
        }
        SimObj checkItem = null;

        for (int i = 0; i < r.Pivots.Length; i++)
        {
            if (r.Pivots [i].childCount > 0)
            {
                checkItem = r.Pivots [i].GetChild(0).GetComponent <SimObj> ();
                if (checkItem != null && checkItem.UniqueID == itemID)
                {
                    //if the item under the pivot is a SimObj with the right id
                    //we've found what we're after
                    item = checkItem;
                    return(true);
                }
            }
        }
        //couldn't find it!
        return(false);
    }
Exemple #14
0
        protected bool closeSimObj(SimObj so)
        {
            bool inrange = false;

            //check if the object we are trying to open is in visible range
            foreach (SimObj o in currentVisibleObjects)
            {
                //check if the ID of the object we are looking at is in array of visible objects
                if (so.UniqueID == o.UniqueID)
                {
                    inrange = true;
                }
            }

            bool res = false;

            if (inrange)
            {
                if (OPEN_CLOSE_STATES.ContainsKey(so.Type))
                {
                    res = updateAnimState(so.Animator, OPEN_CLOSE_STATES[so.Type]["close"]);
                }
                else if (so.IsAnimated)
                {
                    res = updateAnimState(so.Animator, false);
                }
            }

            if (!inrange)
            {
                Debug.Log("Target out of range!");
            }

            return(res);
        }
Exemple #15
0
        //take a pickupable item, place in inventory if inventory is not full
        protected void TakeItem(SimObj item)
        {
            // print(item.Manipulation);

            //check so we only have one item in the inventory at a time
            if (inventory.Count == 0)
            {
                //make item visible to raycasts
                //unparent in case it's in a receptacle
                item.VisibleToRaycasts = true;
                item.transform.parent  = null;
                //disable the item entirely
                item.gameObject.SetActive(false);
                //set the position to 'up' so it's rotated correctly when it comes back
                item.transform.up = Vector3.up;
                //reset the scale (to prevent floating point weirdness)
                item.ResetScale();

                //now add to inventory
                addObjectInventory(item);
                Inventory_Text.GetComponent <Text>().text = "In Inventory: " + item.UniqueID + " | Press 'Space' to put in Receptacle";
            }

            else
            {
                print("inventory full!");
            }
        }
Exemple #16
0
 public IEnumerator EnableSimObjPhysics(SimObj simObj)
 {
     //always wait for 1 frame to allow sim object to wake itself up
     yield return null;
     //move the simObj to the object root to ensure it's not parented under another rigidbody
     simObj.transform.parent = SceneManager.Current.ObjectsParent;
     //make the object non-kinematic
     simObj.GetComponent <Rigidbody> ().isKinematic = false;
     //pray it doesn't explode
 }
Exemple #17
0
        //remove current object in inventory
        public SimObj removeObjectInventory(string objectId)
        {
            SimObj so = inventory[objectId];

            inventory.Remove(objectId);

            current_Object_In_Inventory = null;
            Inventory_Text.GetComponent <Text>().text = "In Inventory: Nothing!";
            return(so);
        }
Exemple #18
0
 //tries to get a SimObj from a collider, returns false if none found
 public static bool GetSimObjFromCollider(Collider c, out SimObj o)
 {
     o = null;
     if (c.attachedRigidbody == null)
     {
         //all sim objs must have a kinematic rigidbody
         return(false);
     }
     o = c.attachedRigidbody.GetComponent <SimObj> ();
     return(o != null);
 }
Exemple #19
0
    // generates a object ID for a sim object
    public void AssignObjectID(SimObj obj)
    {
        // object ID is a string consisting of:
        //[SimObjType]_[X0.00]:[Y0.00]:[Z0.00]
        Vector3 pos  = obj.transform.position;
        string  xPos = (pos.x >= 0 ? "+" : "") + pos.x.ToString("00.00");
        string  yPos = (pos.y >= 0 ? "+" : "") + pos.y.ToString("00.00");
        string  zPos = (pos.z >= 0 ? "+" : "") + pos.z.ToString("00.00");

        obj.ObjectID = obj.Type.ToString() + "|" + xPos + "|" + yPos + "|" + zPos;
    }
Exemple #20
0
 //TAKES an item
 //removes it from any receptacles, then disables it entirely
 //this works whether the item is standalone or in a receptacle
 public static void TakeItem(SimObj item)
 {
     //make item visible to raycasts
     //unparent in case it's in a receptacle
     item.VisibleToRaycasts = true;
     item.transform.parent  = null;
     //disable the item entirely
     item.gameObject.SetActive(false);
     //set the position to 'up' so it's rotated correctly when it comes back
     item.transform.up = Vector3.up;
     //reset the scale (to prevent floating point weirdness)
     item.ResetScale();
 }
        public bool IsOpen(SimObj simobj)
        {
            Animator anim = simobj.Animator;
            AnimatorControllerParameter param = anim.parameters [0];

            if (OPEN_CLOSE_STATES.ContainsKey(simobj.Type))
            {
                return(anim.GetInteger(param.name) == OPEN_CLOSE_STATES[simobj.Type]["open"]);
            }
            else
            {
                return(anim.GetBool(param.name));
            }
        }
        protected bool openSimObj(SimObj so)
        {
            bool res = false;

            if (OPEN_CLOSE_STATES.ContainsKey(so.Type))
            {
                res = updateAnimState(so.Animator, OPEN_CLOSE_STATES[so.Type]["open"]);
            }
            else if (so.IsAnimated)
            {
                res = updateAnimState(so.Animator, true);
            }
            return(res);
        }
Exemple #23
0
        virtual protected IEnumerator checkOpenAction(SimObj so)
        {
            yield return(null);

            bool result = false;

            //Debug.Log ("checkOpenAction");
            for (int i = 0; i < actionDuration; i++)
            {
                //Debug.Log ("checkOpenAction action duration " + i);
                //Debug.Log ("Action duration " + i);
                Vector3 currentPosition = this.transform.position;
                //Debug.Log ("collided in open");

                currentPosition = this.transform.position;
                for (int j = 0; j < actionDuration; j++)
                {
                    yield return(null);
                }
                Vector3 snapDiff = currentPosition - this.transform.position;
                snapDiff.y = Mathf.Min(Math.Abs(snapDiff.y), 0.05f);
//				Debug.Log ("currentY " + currentPosition.y);
//				Debug.Log ("positionY " + this.transform.position.y);
//				Debug.Log ("snapDiff " + snapDiff.y);
                if (snapDiff.magnitude >= 0.005f)
                {
                    result = false;
                    break;
                }
                else
                {
                    result = true;
                }
            }


            if (!result)
            {
                Debug.Log("check open failed");
                closeSimObj(so);
                transform.position = lastPosition;
                for (int j = 0; j < actionDuration; j++)
                {
                    Debug.Log("open yield return null");
                    yield return(null);
                }
            }

            actionFinished(result);
        }
Exemple #24
0
    void OnSceneGUI() {
        SimObj simObj = (SimObj)target;

        if (SimUtil.ShowIDs) {
            Handles.color = Color.white;
            Handles.Label((simObj.transform.position + Vector3.up * 0.1f), simObj.Type.ToString() + " : " + simObj.ObjectID.ToString(), EditorStyles.miniButton);
        }
        if (SimUtil.ShowBasePivots) {
            Handles.color = Color.white;
            // this was originally Handles.CircleCap, which is obsolete, had to add the EventType.Repaint parameter in upgrading to new CircleHandleCap implementation
            // not sure if the event type should be EventType.Repaint or EventType.Layout, but if Repaint isn't working change it to Layout
            Handles.CircleHandleCap(0, simObj.transform.position, Quaternion.Euler(-90f, 0f, 0f), 1f, EventType.Repaint);
        }
    }
Exemple #25
0
    void Update()
    {
        if (parentObj == null)
        {
            parentObj = gameObject.GetComponent <SimObj> ();
        }

        //anim state is 1-4
        int animState = -1;

        if (Application.isPlaying)
        {
            animState = parentObj.Animator.GetInteger("AnimState1");
        }
                #if UNITY_EDITOR
        else
        {
            animState = EditorState + 1;
        }
                #endif
        if (animState > States.Length)
        {
            animState = -1;
        }

        //stateIndex is 0-3
        int stateIndex = animState - 1;
        if (currentState != stateIndex && stateIndex >= 0)
        {
            currentState = stateIndex;
            for (int i = 0; i < States.Length; i++)
            {
                if (i == currentState)
                {
                    parentObj.Type = States [i].Type;
                    if (States [i].Obj != null)
                    {
                        States [i].Obj.SetActive(true);
                    }
                }
                else
                {
                    if (States [i].Obj != null)
                    {
                        States [i].Obj.SetActive(false);
                    }
                }
            }
        }
    }
Exemple #26
0
    void OnSceneGUI()
    {
        SimObj simObj = (SimObj)target;

        if (SimUtil.ShowIDs)
        {
            Handles.color = Color.white;
            Handles.Label((simObj.transform.position + Vector3.up * 0.1f), simObj.Type.ToString() + " : " + simObj.UniqueID.ToString(), EditorStyles.miniButton);
        }
        if (SimUtil.ShowBasePivots)
        {
            Handles.color = Color.white;
            Handles.CircleCap(0, simObj.transform.position, Quaternion.Euler(-90f, 0f, 0f), 0.05f);
        }
    }
Exemple #27
0
    //checks whether the item
    public static bool CheckItemBounds(SimObj item, Vector3 agentPosition)
    {
        //if the item doesn't use custom bounds this is an automatic pass
        if (!item.UseCustomBounds)
        {
            return(true);
        }

        //use the item's bounds transform as a bounding box
        //this is NOT axis-aligned so objects rotated strangely may return unexpected results
        //but it should work for 99% of cases
        Bounds itemBounds = new Bounds(item.BoundsTransform.position, item.BoundsTransform.lossyScale);

        return(itemBounds.Contains(agentPosition));
    }
Exemple #28
0
    //checks whether a point is in view of the camera
    //and whether it can be seen via raycast
    static bool CheckPointVisibility(SimObj item, Vector3 itemTargetPoint, Camera agentCamera, Vector3 agentCameraPos, int raycastLayerMask, bool checkTrigger, float maxDistance, out RaycastHit hit)
    {
        hit = new RaycastHit();
        Vector3 viewPoint = agentCamera.WorldToViewportPoint(itemTargetPoint);

        if (viewPoint.z > 0 &&     //in front of camera
            viewPoint.x < ViewPointRangeHigh && viewPoint.x > ViewPointRangeLow &&        //within x bounds
            viewPoint.y < ViewPointRangeHigh && viewPoint.y > ViewPointRangeLow)              //within y bounds
        {
            Vector3 itemDirection = Vector3.zero;
            //do a raycast in the direction of the item
            itemDirection = (itemTargetPoint - agentCameraPos).normalized;
            //extend the range depending on how much we're looking downward
            //base this on the angle of the RAYCAST direction not the camera direction
            Vector3 agentForward = agentCamera.transform.forward;
            agentForward.y = 0f;
            agentForward.Normalize();
            //clap the angle so we can't wrap around
            float maxDistanceLerp = 0f;
            float lookAngle       = Mathf.Clamp(Vector3.Angle(agentForward, itemDirection), 0f, MaxDownwardLookAngle) - MinDownwardLooKangle;
            maxDistanceLerp = lookAngle / MaxDownwardLookAngle;
            maxDistance     = Mathf.Lerp(maxDistance, maxDistance * DownwardRangeExtension, maxDistanceLerp);
            //try to raycast for the object
            if (Physics.Raycast(
                    agentCameraPos,
                    itemDirection,
                    out hit,
                    maxDistance,
                    raycastLayerMask,
                    (checkTrigger ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore)))
            {
                //check to see if we hit the item we're after
                if (hit.collider.attachedRigidbody != null && hit.collider.attachedRigidbody.gameObject == item.gameObject)
                {
                                        #if UNITY_EDITOR
                    Debug.DrawLine(agentCameraPos, hit.point, Color.Lerp(Color.green, Color.blue, maxDistanceLerp));
                                        #endif
                    return(true);
                }
                                #if UNITY_EDITOR
                //Debug.DrawRay (agentCameraPos, itemDirection, Color.Lerp (Color.red, Color.clear, 0.75f));
                                #endif
            }
        }
        return(false);
    }
Exemple #29
0
    void OnEnable()
    {
        ParentObj = gameObject.GetComponent <SimObj>();
        if (ParentObj == null)
        {
            ParentObj = gameObject.AddComponent <SimObj>();
        }

        if (!Application.isPlaying)
        {
            Animator a = ParentObj.gameObject.GetComponent <Animator>();
            if (a == null)
            {
                a = ParentObj.gameObject.AddComponent <Animator>();
                a.runtimeAnimatorController = Resources.Load("ToggleableAnimController") as RuntimeAnimatorController;
            }
        }
    }
        public void MaskObject(ServerAction action)
        {
            Unmask(action);
            currentMaskMaterials = new Dictionary <int, Material[]> ();
            foreach (SimObj so in VisibleSimObjs(action))
            {
                currentMaskObj = so;

                foreach (MeshRenderer r in so.gameObject.GetComponentsInChildren <MeshRenderer> () as MeshRenderer[])
                {
                    currentMaskMaterials [r.GetInstanceID()] = r.materials;
                    Material material = new Material(Shader.Find("Unlit/Color"));
                    material.color = Color.magenta;
                    Material[] newMaterials = new Material[] { material };
                    r.materials = newMaterials;
                }
                actionFinished(true);
            }
        }