コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        //If the 'collect collectible' is pressed (don't know what that button is yet)
        if (Input.GetKeyDown(KeyCode.E))
        {
            //Detect collectibles in a circle. Returns the first one it finds (we assume you can only pick up one collectible at a time)
            Collider2D hit = Physics2D.OverlapCircle(gameObject.transform.position, 1, LayerMask.GetMask("Interactable"));
            //If a collectible has been found
            if (hit)
            {
                //Check if thing collided with has a CollectibleInfo script to take info from
                if (hit.transform.GetComponent <CollectibleInfo>())
                {
                    //Get the CollectibleInfo script
                    CollectibleInfo CI = hit.transform.GetComponent <CollectibleInfo>();
                    //Get the information out of the script and pass it through the collect function
                    collectibleSystemManagerScript.Collect(CI.text, CI.image, CI.description);
                    //Remove the collectible object (now collected)
                    Destroy(hit.transform.gameObject);
                }
            }

            //DEBUG: Simulate the player collecting a collectible
            //collectibleSystemManagerScript.Collect("Curk!", new Texture2D(0, 0), "test");
        }
    }
コード例 #2
0
    /// <summary>
    /// Returns a state of the collectible's on/off property
    /// </summary>
    /// <returns></returns>
    public ObjectState GetState()
    {
        CollectibleInfo infoState = new CollectibleInfo();

        infoState.active         = active;
        infoState.collectibleNum = gameWonScript.collectibleNum;
        return(infoState);
    }
コード例 #3
0
    /// <summary>
    /// Adjusts the collectible's state (on/off) based on the specified state
    /// </summary>
    /// <param name="objectState">State of the collectibles</param>
    public void SetState(ObjectState objectState)
    {
        CollectibleInfo infoState = (CollectibleInfo)objectState;

        active = infoState.active;
        gameObject.SetActive(active);
        gameWonScript.collectibleNum = infoState.collectibleNum;
    }
コード例 #4
0
        public static List <CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            BoundingSphereD sphere   = new BoundingSphereD(fromPosition, radius);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                CollectibleInfo info  = new CollectibleInfo();
                MyCubeBlock     block = null;
                MyCubeGrid      grid  = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId     = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                    {
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    }
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    m_retvalCollectibleInfos.Add(info);
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId       = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId     = floatingObj.EntityId;
                        info.DefinitionId = defId;
                        info.Amount       = floatingObj.Item.Amount;
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return(m_retvalCollectibleInfos);
        }
コード例 #5
0
        public static List<CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            BoundingSphereD sphere = new BoundingSphereD(fromPosition, radius);
            var entities = MyEntities.GetEntitiesInSphere(ref sphere);
            foreach (var entity in entities)
            {
                CollectibleInfo info = new CollectibleInfo();
                MyCubeBlock block = null;
                MyCubeGrid grid = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    m_retvalCollectibleInfos.Add(info);
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId = floatingObj.EntityId;
                        info.DefinitionId = defId;
                        info.Amount = floatingObj.Item.Amount;
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return m_retvalCollectibleInfos;
        }
コード例 #6
0
        public static List<CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius, bool doRaycast = false)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            List<MyPhysics.HitInfo> hits = new List<MyPhysics.HitInfo>();

            BoundingSphereD sphere = new BoundingSphereD(fromPosition, radius);
            var entities = MyEntities.GetEntitiesInSphere(ref sphere);
            foreach (var entity in entities)
            {
                bool addCollectibleInfo = false;

                CollectibleInfo info = new CollectibleInfo();
                MyCubeBlock block = null;
                MyCubeGrid grid = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    addCollectibleInfo = true;
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId = floatingObj.EntityId;
                        info.DefinitionId = defId;
                        info.Amount = floatingObj.Item.Amount;
                        addCollectibleInfo = true;
                    }
                }

                if (addCollectibleInfo)
                {
                    bool hitSomething = false;
                    MyPhysics.CastRay(fromPosition, entity.WorldMatrix.Translation, hits, MyPhysics.CollisionLayers.DefaultCollisionLayer);
                    foreach (var hit in hits)
                    {
                        var hitEntity = hit.HkHitInfo.GetHitEntity();
                        if (hitEntity == entity) continue;
                        if (hitEntity is MyCharacter) continue;
                        if (hitEntity is MyFracturedPiece) continue;
                        if (hitEntity is MyFloatingObject) continue;
                        MyCubeBlock dummy = null;
                        if (TryGetAsComponent(hitEntity as MyEntity, out dummy) != null) continue;
                        hitSomething = true;
                        break;
                    }

                    if (!hitSomething)
                    {
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return m_retvalCollectibleInfos;
        }
コード例 #7
0
        public static List <CollectibleInfo> FindCollectiblesInRadius(Vector3D fromPosition, double radius, bool doRaycast = false)
        {
            Debug.Assert(m_retvalCollectibleInfos.Count == 0, "The result of the last call of FindComponentsInRadius was not cleared!");

            List <MyPhysics.HitInfo> hits = new List <MyPhysics.HitInfo>();

            BoundingSphereD sphere   = new BoundingSphereD(fromPosition, radius);
            var             entities = MyEntities.GetEntitiesInSphere(ref sphere);

            foreach (var entity in entities)
            {
                bool addCollectibleInfo = false;

                CollectibleInfo info  = new CollectibleInfo();
                MyCubeBlock     block = null;
                MyCubeGrid      grid  = TryGetAsComponent(entity, out block);
                if (grid != null)
                {
                    info.EntityId     = grid.EntityId;
                    info.DefinitionId = GetComponentId(block.SlimBlock);
                    if (block.BlockDefinition.Components != null)
                    {
                        info.Amount = block.BlockDefinition.Components[0].Count;
                    }
                    else
                    {
                        Debug.Assert(false, "Block definition does not have any components!");
                        info.Amount = 0;
                    }
                    addCollectibleInfo = true;
                }
                else if (entity is MyFloatingObject)
                {
                    var floatingObj = entity as MyFloatingObject;
                    var defId       = floatingObj.Item.Content.GetObjectId();
                    if (MyDefinitionManager.Static.GetPhysicalItemDefinition(defId).Public)
                    {
                        info.EntityId      = floatingObj.EntityId;
                        info.DefinitionId  = defId;
                        info.Amount        = floatingObj.Item.Amount;
                        addCollectibleInfo = true;
                    }
                }

                if (addCollectibleInfo)
                {
                    bool hitSomething = false;
                    MyPhysics.CastRay(fromPosition, entity.WorldMatrix.Translation, hits, MyPhysics.CollisionLayers.DefaultCollisionLayer);
                    foreach (var hit in hits)
                    {
                        var hitEntity = hit.HkHitInfo.GetHitEntity();
                        if (hitEntity == entity)
                        {
                            continue;
                        }
                        if (hitEntity is MyCharacter)
                        {
                            continue;
                        }
                        if (hitEntity is MyFracturedPiece)
                        {
                            continue;
                        }
                        if (hitEntity is MyFloatingObject)
                        {
                            continue;
                        }
                        MyCubeBlock dummy = null;
                        if (TryGetAsComponent(hitEntity as MyEntity, out dummy) != null)
                        {
                            continue;
                        }
                        hitSomething = true;
                        break;
                    }

                    if (!hitSomething)
                    {
                        m_retvalCollectibleInfos.Add(info);
                    }
                }
            }
            entities.Clear();

            return(m_retvalCollectibleInfos);
        }