示例#1
0
    void Update()
    {
        Ray        ray = new Ray(m_camera.transform.position, m_camera.transform.forward);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, m_maxDistance, 1 << 8)) // raycast forward from camera (only objects with "Visible" layer set)
        {
            m_objectToHit = hit.collider.gameObject.GetComponent <HitableObject>();
            if (m_currentObject = hit.collider.gameObject.GetComponent <InteractableObject>())
            {
                //print interaction key and hint to interact
                m_interactionHintText.text = m_interactionKey.Key.ToString() + " to " + (m_currentObject.m_itemDescription.m_isLiftable ? "pick up" : "interact with") + " a " + m_currentObject.m_itemDescription.m_name;
            }
            else
            {
                m_interactionHintText.text = "";
            }
        }
        else
        {
            m_interactionHintText.text = "";
            m_currentObject            = null;
            m_objectToHit = null;
        }
    }
示例#2
0
 void Start()
 {
     m_currentObject            = null;
     m_objectToHit              = null;
     m_interactionKey           = m_inputManager.GetInputUnit(InputManager.InteractGroup, InputManager.Interaction);
     m_interactionHintText.text = "";
     m_inventory = GetComponent <Inventory>();
 }
示例#3
0
        public virtual void RemoveContainedObject(HitableObject containedObject)
        {
            if (ContainedObjects != null)
            {
                ContainedObjects.Remove(containedObject);

                if (ContainedObjects.Count == 0)
                {
                    ContainedObjects = null;
                }
            }
        }
示例#4
0
        public virtual void AddContainedObject(HitableObject containedObject)
        {
            if (ContainedObjects == null)
            {
                ContainedObjects = new List <HitableObject>();
            }

            if (ContainedObjects.Contains(containedObject) == false)
            {
                ContainedObjects.Add(containedObject);
            }
        }
示例#5
0
        public static bool IsSightBlocked(HitableObject origin, HitableObject target, int lineWidth)
        {
            if (origin.InteriorID != target.InteriorID)
            {
                return(true);
            }

            Point originPoint = origin.Position.ToPoint();
            Point targetPoint = target.Position.ToPoint();

            Rect unionRect = ShapeCollision.ConvertLineToRect(originPoint, targetPoint);
            List <HitableObject> hittedObjects = GetHittedObjects(unionRect, origin.InteriorID, origin);

            Vector2[] polyRect = GeometryUtils.GetRectangleFromLine(originPoint, targetPoint, lineWidth);

            foreach (var hitableObject in hittedObjects)
            {
                if (hitableObject != target && ShapeCollision.RectIntersectsPoly(hitableObject.HitBoxBounds, polyRect))
                {
                    return(true);
                }
            }

            // Check if blocks are of type hitable && if they intersect with the polyRect
            Point topLeft     = GeometryUtils.GetChunkPosition(unionRect.Left, unionRect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
            Point bottomRight = GeometryUtils.GetChunkPosition(unionRect.Right, unionRect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

            for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
            {
                for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                {
                    if (IsBlockHitable(blockX, blockY, origin.InteriorID) && ShapeCollision.RectIntersectsPoly(new Rect(blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y), polyRect))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#6
0
        public static bool IsSightBlocked(HitableObject origin, HitableObject target)
        {
            if (origin.InteriorID != target.InteriorID)
            {
                return(true);
            }

            Vector2 originVector = origin.Position.ToVector();
            Vector2 targetVector = target.Position.ToVector();

            Rect unionRect = ShapeCollision.ConvertLineToRect(originVector, targetVector);
            List <HitableObject> hittedObjects = GetHittedObjects(unionRect, origin.InteriorID, origin);

            foreach (var hitableObject in hittedObjects)
            {
                if (hitableObject != target && ShapeCollision.LineIntersectsRectangle(originVector, targetVector, hitableObject.HitBoxBounds))
                {
                    return(true);
                }
            }

            // Check if blocks are of type hitable
            Point topLeft     = GeometryUtils.GetChunkPosition(unionRect.Left, unionRect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
            Point bottomRight = GeometryUtils.GetChunkPosition(unionRect.Right, unionRect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

            for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
            {
                for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                {
                    if (IsBlockHitable(blockX, blockY, origin.InteriorID) && ShapeCollision.LineIntersectsRectangle(originVector, targetVector, new Rect(blockX * WorldGrid.BlockSize.X, blockY * WorldGrid.BlockSize.Y, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y)))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#7
0
        public static LivingEntity GetClosestLivingTarget(Circle hitboxBounds, string interiorId, HitableObject origin, int maxAggro)
        {
            Rect         rectangleHitboxBounds = new Rect(hitboxBounds.CenterX - hitboxBounds.Radius, hitboxBounds.CenterY - hitboxBounds.Radius, 2 * hitboxBounds.Radius, 2 * hitboxBounds.Radius);
            LivingEntity livingTarget          = GetClosestLivingTarget(rectangleHitboxBounds, interiorId, origin, maxAggro);

            return(hitboxBounds.Intersects(livingTarget.HitBoxBounds) ? livingTarget : null);
        }
示例#8
0
        public static bool IsRectBlockedAccurate(HitableObject origin, Rect rect)
        {
            ThreadingUtils.assertMainThread();

            if (origin.InteriorID == Interior.Outside)
            {
                // Check if blocks are of type blocking
                Point topLeft     = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
                Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

                for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
                {
                    for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                    {
                        Point          chunkPos       = GeometryUtils.GetChunkPosition(blockX, blockY, WorldGrid.WorldChunkBlockSize.X, WorldGrid.WorldChunkBlockSize.Y);
                        WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkPos.X, chunkPos.Y);

                        int blockType = worldGridChunk.GetBlockType(blockX, blockY);

                        if (IsBlockBlocking(blockType))
                        {
                            return(true);
                        }
                    }
                }

                // Check collision with interactive && contained objects
                Point chunkTopLeft     = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);
                Point chunkBottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);

                for (int chunkX = chunkTopLeft.X; chunkX <= chunkBottomRight.X; chunkX++)
                {
                    for (int chunkY = chunkTopLeft.Y; chunkY <= chunkBottomRight.Y; chunkY++)
                    {
                        WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY);

                        if (worldGridChunk.OverlappingObjects != null)
                        {
                            foreach (HitableObject hitableObject in worldGridChunk.OverlappingObjects)
                            {
                                if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect))
                                {
                                    return(true);
                                }
                            }
                        }

                        if (worldGridChunk.ContainedObjects != null)
                        {
                            foreach (var hitableObject in worldGridChunk.ContainedObjects)
                            {
                                if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect))
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }

                return(false);
            }
            else
            {
                Interior interior = SimulationGame.World.InteriorManager.Get(origin.InteriorID);

                // Check if blocks are of type blocking
                Point topLeft     = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
                Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

                for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
                {
                    for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                    {
                        if (blockX < 0 || blockX >= interior.Dimensions.X)
                        {
                            return(true);
                        }
                        if (blockY < 0 || blockY >= interior.Dimensions.Y)
                        {
                            return(true);
                        }

                        int blockType = interior.GetBlockType(blockX, blockY);

                        if (IsBlockBlocking(blockType))
                        {
                            return(true);
                        }
                    }
                }

                foreach (var hitableObject in interior.ContainedObjects)
                {
                    if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect))
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
示例#9
0
        public static bool IsRectBlockedFast(HitableObject origin, Rect rect)
        {
            ThreadingUtils.assertMainThread();

            if (origin.InteriorID == Interior.Outside)
            {
                // Check if blocks are of type blocking
                Point topLeft     = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
                Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

                for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
                {
                    for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                    {
                        if (!SimulationGame.World.WalkableGrid.IsBlockWalkable(blockX, blockY))
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            else
            {
                Interior interior = SimulationGame.World.InteriorManager.Get(origin.InteriorID);

                // Check if blocks are of type blocking
                Point topLeft     = GeometryUtils.GetChunkPosition(rect.Left, rect.Top, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);
                Point bottomRight = GeometryUtils.GetChunkPosition(rect.Right, rect.Bottom, WorldGrid.BlockSize.X, WorldGrid.BlockSize.Y);

                for (int blockX = topLeft.X; blockX <= bottomRight.X; blockX++)
                {
                    for (int blockY = topLeft.Y; blockY <= bottomRight.Y; blockY++)
                    {
                        if (blockX < 0 || blockX >= interior.Dimensions.X)
                        {
                            return(true);
                        }
                        if (blockY < 0 || blockY >= interior.Dimensions.Y)
                        {
                            return(true);
                        }

                        int blockType = interior.GetBlockType(blockX, blockY);

                        if (IsBlockBlocking(blockType))
                        {
                            return(true);
                        }
                    }
                }

                foreach (var hitableObject in interior.ContainedObjects)
                {
                    if (hitableObject.IsBlocking() && hitableObject != origin && hitableObject.BlockingBounds.Intersects(rect))
                    {
                        return(true);
                    }
                }

                return(false);
            }
        }
示例#10
0
        public static List <HitableObject> GetHittedObjects(Rect hitboxBounds, string interiorId, HitableObject origin)
        {
            ThreadingUtils.assertMainThread();
            List <HitableObject> hittedObjecs = new List <HitableObject>();

            if (interiorId == Interior.Outside)
            {
                // Check collision with interactive && contained objects
                Point chunkTopLeft     = GeometryUtils.GetChunkPosition(hitboxBounds.Left, hitboxBounds.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);
                Point chunkBottomRight = GeometryUtils.GetChunkPosition(hitboxBounds.Right, hitboxBounds.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);

                for (int chunkX = chunkTopLeft.X; chunkX <= chunkBottomRight.X; chunkX++)
                {
                    for (int chunkY = chunkTopLeft.Y; chunkY <= chunkBottomRight.Y; chunkY++)
                    {
                        WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY);

                        if (worldGridChunk.OverlappingObjects != null)
                        {
                            foreach (HitableObject hitableObject in worldGridChunk.OverlappingObjects)
                            {
                                if (hitableObject != origin && hitableObject.IsHitable() && hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                                {
                                    if (hittedObjecs.Contains(hitableObject) == false && (hitableObject is LivingEntity == false || ((LivingEntity)hitableObject).IsDead() == false))
                                    {
                                        hittedObjecs.Add(hitableObject);
                                    }
                                }
                            }
                        }

                        if (worldGridChunk.ContainedObjects != null)
                        {
                            foreach (var hitableObject in worldGridChunk.ContainedObjects)
                            {
                                if (hitableObject != origin && hitableObject.IsHitable() && hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                                {
                                    if (hittedObjecs.Contains(hitableObject) == false && (hitableObject is LivingEntity == false || ((LivingEntity)hitableObject).IsDead() == false))
                                    {
                                        hittedObjecs.Add(hitableObject);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Interior interior = SimulationGame.World.InteriorManager.Get(interiorId);

                foreach (var hitableObject in interior.ContainedObjects)
                {
                    if (hitableObject != origin && hitableObject.IsHitable() && hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                    {
                        if (hittedObjecs.Contains(hitableObject) == false && (hitableObject is LivingEntity == false || ((LivingEntity)hitableObject).IsDead() == false))
                        {
                            hittedObjecs.Add(hitableObject);
                        }
                    }
                }
            }

            return(hittedObjecs);
        }
示例#11
0
        public static List <HitableObject> GetHittedObjects(Circle hitboxBounds, string interiorId, HitableObject origin)
        {
            Rect rectangleHitboxBounds         = new Rect(hitboxBounds.CenterX - hitboxBounds.Radius, hitboxBounds.CenterY - hitboxBounds.Radius, 2 * hitboxBounds.Radius, 2 * hitboxBounds.Radius);
            List <HitableObject> hittedObjects = GetHittedObjects(rectangleHitboxBounds, interiorId, origin);

            for (int i = 0; i < hittedObjects.Count; i++)
            {
                if (hitboxBounds.Intersects(hittedObjects[i].HitBoxBounds) == false)
                {
                    hittedObjects.RemoveAt(i);
                    i--;
                }
            }

            return(hittedObjects);
        }
示例#12
0
        public static List <LivingEntity> GetLivingHittedObjects(Rect hitboxBounds, string interiorId, HitableObject origin, int maxAggro)
        {
            List <LivingEntity> hittedObjects = GetLivingHittedObjects(hitboxBounds, interiorId, origin);

            for (int i = 0; i < hittedObjects.Count; i++)
            {
                if (((LivingEntity)origin).GetAggroTowardsEntity(hittedObjects[i]) > maxAggro)
                {
                    hittedObjects.RemoveAt(i);
                    i--;
                }
            }

            return(hittedObjects);
        }
示例#13
0
        public static List <LivingEntity> GetLivingHittedObjects(Circle hitboxBounds, string interiorId, HitableObject origin, int maxAggro)
        {
            Rect rectangleHitboxBounds        = new Rect(hitboxBounds.CenterX - hitboxBounds.Radius, hitboxBounds.CenterY - hitboxBounds.Radius, 2 * hitboxBounds.Radius, 2 * hitboxBounds.Radius);
            List <LivingEntity> hittedObjects = GetLivingHittedObjects(rectangleHitboxBounds, interiorId, origin);

            for (int i = 0; i < hittedObjects.Count; i++)
            {
                if (hitboxBounds.Intersects(hittedObjects[i].HitBoxBounds) == false || ((LivingEntity)origin).GetAggroTowardsEntity(hittedObjects[i]) > maxAggro)
                {
                    hittedObjects.RemoveAt(i);
                    i--;
                }
            }

            return(hittedObjects);
        }
示例#14
0
        public static LivingEntity GetClosestLivingTarget(Rect hitboxBounds, string interiorId, HitableObject origin, int maxAggro)
        {
            ThreadingUtils.assertMainThread();

            HitableObject closestTarget   = null;
            float         closestDistance = float.PositiveInfinity;

            if (interiorId == Interior.Outside)
            {
                // Check collision with interactive && contained objects
                Point chunkTopLeft     = GeometryUtils.GetChunkPosition(hitboxBounds.Left, hitboxBounds.Top, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);
                Point chunkBottomRight = GeometryUtils.GetChunkPosition(hitboxBounds.Right, hitboxBounds.Bottom, WorldGrid.WorldChunkPixelSize.X, WorldGrid.WorldChunkPixelSize.Y);

                for (int chunkX = chunkTopLeft.X; chunkX <= chunkBottomRight.X; chunkX++)
                {
                    for (int chunkY = chunkTopLeft.Y; chunkY <= chunkBottomRight.Y; chunkY++)
                    {
                        WorldGridChunk worldGridChunk = SimulationGame.World.GetFromChunkPoint(chunkX, chunkY);

                        if (worldGridChunk.OverlappingObjects != null)
                        {
                            foreach (HitableObject hitableObject in worldGridChunk.OverlappingObjects)
                            {
                                if (hitableObject is LivingEntity &&
                                    hitableObject != origin &&
                                    hitableObject.IsHitable() &&
                                    ((LivingEntity)origin).GetAggroTowardsEntity((LivingEntity)hitableObject) <= maxAggro &&
                                    hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                                {
                                    var distance = GeometryUtils.GetDiagonalDistance(hitableObject.Position.X, hitableObject.Position.Y, origin.Position.X, origin.Position.Y);

                                    if (distance < closestDistance)
                                    {
                                        closestDistance = distance;
                                        closestTarget   = hitableObject;
                                    }
                                }
                            }
                        }


                        if (worldGridChunk.ContainedObjects != null)
                        {
                            foreach (var hitableObject in worldGridChunk.ContainedObjects)
                            {
                                if (hitableObject is LivingEntity &&
                                    hitableObject != origin &&
                                    hitableObject.IsHitable() &&
                                    ((LivingEntity)origin).GetAggroTowardsEntity((LivingEntity)hitableObject) <= maxAggro &&
                                    hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                                {
                                    var distance = GeometryUtils.GetDiagonalDistance(hitableObject.Position.X, hitableObject.Position.Y, origin.Position.X, origin.Position.Y);

                                    if (distance < closestDistance)
                                    {
                                        closestDistance = distance;
                                        closestTarget   = hitableObject;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Interior interior = SimulationGame.World.InteriorManager.Get(interiorId);

                foreach (var hitableObject in interior.ContainedObjects)
                {
                    if (hitableObject is LivingEntity &&
                        hitableObject != origin &&
                        hitableObject.IsHitable() &&
                        ((LivingEntity)origin).GetAggroTowardsEntity((LivingEntity)hitableObject) <= maxAggro &&
                        hitableObject.HitBoxBounds.Intersects(hitboxBounds))
                    {
                        var distance = GeometryUtils.GetDiagonalDistance(hitableObject.Position.X, hitableObject.Position.Y, origin.Position.X, origin.Position.Y);

                        if (distance < closestDistance)
                        {
                            closestDistance = distance;
                            closestTarget   = hitableObject;
                        }
                    }
                }
            }

            return((LivingEntity)closestTarget);
        }
        protected static void Serialize(HitableObject hitableObject, ref JObject jObject)
        {
            GameObjectSerializer.Serialize(hitableObject, ref jObject);

            SerializationUtils.AddToObject(jObject, hitableObject, hitableObjectType, serializeableProperties);
        }
        protected static void Deserialize(ref JObject jObject, HitableObject hitableObject)
        {
            GameObjectSerializer.Deserialize(ref jObject, hitableObject);

            SerializationUtils.SetFromObject(jObject, hitableObject, hitableObjectType, serializeableProperties);
        }