Example #1
0
    void Start()
    {
        if (BUILDINGROOF_LAYER_MASK == -1)
        {
            BUILDINGROOF_LAYER_MASK = LayerMask.GetMask(new string[] { "BuildingRoof" });
        }

        RaycastHit hit;
        bool       buildingWasHit = Physics.Raycast(transform.position, Vector3.forward, out hit, 40f, BUILDINGROOF_LAYER_MASK);

        // Subscribe to know who are going here
        PubSub.subscribe("TargetPOI(" + node.Id + "):Add", this);
        PubSub.subscribe("TargetPOI(" + node.Id + "):Remove", this);
        PubSub.subscribe("gameIsReady", this);

        // Add InformationPOI, since we now have inited this icon
        this.gameObject.AddComponent <InformationPOI>();

        // Get small and big icon gameobjects
        bigIcon   = Misc.FindDeepChild(transform, "icon-big").gameObject;
        smallIcon = Misc.FindDeepChild(transform, "icon-small").gameObject;

        // Set big icon material
        MeshRenderer bigIconMeshRenderer = bigIcon.GetComponent <MeshRenderer> ();

        bigIconMeshRenderer.material = groupMaterials[group];

        isFalling = true;
        Game.instance.addInitAnimationRequest();

        if (buildingWasHit)
        {
            BuildingRoof buildingRoof = hit.transform.gameObject.GetComponent <BuildingRoof> ();
            // Land on the building
            targetZ = -buildingRoof.getTargetHeight() - DISTANCE_FROM_ROOFTOPS;
        }
        else
        {
            // Land on the ground
            targetZ = -0.1f - DISTANCE_FROM_ROOFTOPS;
        }

        // Set random velocity
        fallSpeed = Misc.randomPlusMinus((transform.position.z - targetZ) / 3f, 1f);
    }
Example #2
0
    public bool createSplitMeshes(List <Vector3> outer, List <Vector3> inner)
    {
        Rect rectOfOuter = Misc.GetRectOfVectorList(outer);
        // Vector3 centerOuter = Misc.GetCenterOfVectorList(outer);
        Vector3 centerInner = Misc.GetCenterOfVectorList(inner);

        if (Misc.IsPointInsideRect(centerInner, rectOfOuter))
        {
            // Now try to split this with a line going from this point straight left and right (x axis)
            Vector3 intersectionCheckPoint = centerInner - new Vector3(rectOfOuter.width, 0f, 0f);
            Vector3 intersectionCheckLine  = new Vector3(rectOfOuter.width * 2, 0f, 0f);

            // Loop through outers in pairs and keep them in separate lists
            List <Vector3> beforeSplitOuter        = new List <Vector3>();
            List <Vector3> afterSplitOuter         = new List <Vector3>();
            List <Vector3> outerIntersectionPoints = new List <Vector3>();
            bool           beforeSplit             = true;
            for (int i = 0; i < outer.Count - 1; i++)
            {
                Vector3 vec1 = outer[i];
                Vector3 vec2 = outer[i + 1];
                // Add current vector
                if (beforeSplit)
                {
                    beforeSplitOuter.Add(vec1);
                }
                else
                {
                    afterSplitOuter.Add(vec1);
                }

                Vector3 intersectionPoint;
                bool    intersected = Math3d.LineLineIntersection(out intersectionPoint, vec1, vec2 - vec1, intersectionCheckPoint, intersectionCheckLine);
                if (intersected)
                {
                    // We have crossed the intersection point, add this to both and shift the boolean value, to push coming values to the other list
                    beforeSplitOuter.Add(intersectionPoint);
                    afterSplitOuter.Add(intersectionPoint);
                    // Also keep track of the actual intersection points
                    outerIntersectionPoints.Add(intersectionPoint);
                    beforeSplit = !beforeSplit;
                }
            }

            // Loop through inners in pairs and keep them in separate lists
            List <Vector3> beforeSplitInner        = new List <Vector3>();
            List <Vector3> afterSplitInner         = new List <Vector3>();
            List <Vector3> innerIntersectionPoints = new List <Vector3>();
            beforeSplit = true;
            for (int i = 0; i < inner.Count - 1; i++)
            {
                Vector3 vec1 = inner[i];
                Vector3 vec2 = inner[i + 1];
                // Add current vector
                if (beforeSplit)
                {
                    beforeSplitInner.Add(vec1);
                }
                else
                {
                    afterSplitInner.Add(vec1);
                }

                Vector3 intersectionPoint;
                bool    intersected = Math3d.LineLineIntersection(out intersectionPoint, vec1, vec2 - vec1, intersectionCheckPoint, intersectionCheckLine);
                if (intersected)
                {
                    // We have crossed the intersection point, add this to both and shift the boolean value, to push coming values to the other list
                    beforeSplitInner.Add(intersectionPoint);
                    afterSplitInner.Add(intersectionPoint);
                    // Also keep track of the actual intersection points
                    innerIntersectionPoints.Add(intersectionPoint);
                    beforeSplit = !beforeSplit;
                }
            }

            // TODO - If we have more than two intersection points for either inner or outer, we should try and rotate the intersectionCheckLine and redo above
            // - If eg. outer would happen to be irregular, a third intersection could occur, probably failing everything

            // We now have two lists for outer and two lists for inner, and one list for each to know where the intersections are at
            List <Vector3> topMostOuters    = Misc.GetTopMost(beforeSplitOuter, afterSplitOuter);
            List <Vector3> topMostInners    = Misc.GetTopMost(beforeSplitInner, afterSplitInner);
            List <Vector3> bottomMostOuters = topMostOuters == beforeSplitOuter ? afterSplitOuter : beforeSplitOuter;
            List <Vector3> bottomMostInners = topMostInners == beforeSplitInner ? afterSplitInner : beforeSplitInner;

            // Tie together topmost outer and inner, so they make one solid block
            List <Vector3> topPart = Misc.TieTogetherOuterAndInner(topMostOuters, topMostInners, outerIntersectionPoints, innerIntersectionPoints);
            // Tie together bottommost outer and inner, so they make one solid block
            List <Vector3> bottomPart = Misc.TieTogetherOuterAndInner(bottomMostOuters, bottomMostInners, outerIntersectionPoints, innerIntersectionPoints);

            if (topPart != null && bottomPart != null)
            {
                // Create the mesh
                // DebugFn.print(intersectionCheckPoint);
                // DebugFn.print(intersectionCheckLine);
                // Debug.Log(outerIntersectionPoints.Count);
                // Debug.Log(innerIntersectionPoints.Count);
                // DebugFn.square(topPart[0]);
                // DebugFn.square(bottomPart[0]);
                // DebugFn.print(topPart);
                // DebugFn.print(bottomPart);
                // DebugFn.DebugPath(topPart);
                // DebugFn.DebugPath(bottomPart);

                GameObject   top   = createMesh(topPart, "top", this.transform);
                BuildingRoof topBR = top.AddComponent <BuildingRoof>();
                topBR.createMeshCollider(false);
                topBR.slave  = true;
                topBR.parent = this;
                slaves.Add(topBR);

                GameObject   bottom   = createMesh(bottomPart, "bottom", this.transform);
                BuildingRoof bottomBR = bottom.AddComponent <BuildingRoof>();
                bottomBR.createMeshCollider(false);
                bottomBR.slave  = true;
                bottomBR.parent = this;
                slaves.Add(bottomBR);

                return(true);
            }
        }

        return(false);
    }