Ejemplo n.º 1
0
    public void GenerateAO(Vector3 pos, string name)
    {
        if (!aoCache.ContainsKey(name))
        {
            return;
        }

        ArtObject ao = aoCache[name];

        ArtObjectInstance newImport = new ArtObjectInstance();

        newImport.ArtObjectName = name;
        newImport.Position      = pos;
        newImport.Scale         = Vector3.one;

        GameObject newTrile;

        if (aoObjectCache.Count > 0)
        {
            newTrile = aoObjectCache[0];
            aoObjectCache.RemoveAt(0);
            newTrile.SetActive(true);
        }
        else
        {
            newTrile = Instantiate(aoPrefab);
        }

        MeshFilter        mf  = newTrile.GetComponent <MeshFilter>();
        MeshRenderer      mr  = newTrile.GetComponent <MeshRenderer>();
        MeshCollider      mc  = newTrile.GetComponent <MeshCollider>();
        ArtObjectImported aoI = newTrile.GetComponent <ArtObjectImported>();

        aoI.myInstance = newImport;

        mr.material   = FezToUnity.GeometryToMaterial(aoCache[name].Cubemap);
        mf.mesh       = aoMeshCache[aoCache[name]];
        mc.sharedMesh = aoMeshCache[aoCache[name]];

        newTrile.transform.position = pos;
        newTrile.transform.rotation = newImport.Rotation;

        newTrile.name             = name;
        newTrile.transform.parent = transform.FindChild("ArtObjects");
    }
Ejemplo n.º 2
0
    public void RemoveAO(ArtObjectImported aoI)
    {
        if (!loaded.ArtObjects.ContainsValue(aoI.myInstance))
        {
            return;
        }

        int removeIndex = 0;

        foreach (KeyValuePair <int, ArtObjectInstance> kvp in loaded.ArtObjects)
        {
            if (kvp.Value == aoI.myInstance)
            {
                removeIndex = kvp.Key;
                break;
            }
        }

        loaded.ArtObjects.Remove(removeIndex);

        CacheAO(aoI.gameObject);
    }
Ejemplo n.º 3
0
    IEnumerator LoadLevelCoroutine()
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        yield return(new WaitForEndOfFrame());

        //Calculate level visibility
        foreach (KeyValuePair <TrileEmplacement, TrileInstance> kvp in loaded.Triles)
        {
            TrileEmplacement currP = kvp.Key;

            if (kvp.Value.TrileId < 0)
            {
                visibility.Add(currP, false);
                continue;
            }

            if (kvp.Value.ForceSeeThrough || s.Triles[kvp.Value.TrileId].SeeThrough)
            {
                visibility.Add(currP, true);
                continue;
            }

            TrileEmplacement[] checkPos = new TrileEmplacement[6];

            checkPos[0] = new TrileEmplacement(currP.X, currP.Y, currP.Z + 1);
            checkPos[1] = new TrileEmplacement(currP.X, currP.Y, currP.Z - 1);

            checkPos[2] = new TrileEmplacement(currP.X, currP.Y + 1, currP.Z);
            checkPos[3] = new TrileEmplacement(currP.X, currP.Y - 1, currP.Z);

            checkPos[4] = new TrileEmplacement(currP.X + 1, currP.Y, currP.Z);
            checkPos[5] = new TrileEmplacement(currP.X - 1, currP.Y, currP.Z);

            bool isVisible = false;

            foreach (TrileEmplacement pos in checkPos)
            {
                if (!loaded.Triles.ContainsKey(pos))
                {
                    visibility.Add(currP, true);
                    isVisible = true;
                    break;
                }
                else if (loaded.Triles[pos].TrileId < 0)
                {
                    visibility.Add(currP, true);
                    isVisible = true;
                    break;
                }
                else if (loaded.Triles[pos].ForceSeeThrough || s.Triles[loaded.Triles[pos].TrileId].SeeThrough)
                {
                    visibility.Add(currP, true);
                    isVisible = true;
                    break;
                }
            }
            if (!isVisible)
            {
                visibility.Add(currP, false);
            }
        }

        int index = 0;

        //Generate triles
        {
            foreach (KeyValuePair <TrileEmplacement, TrileInstance> kvp in loaded.Triles)
            {
                if (!visibility[kvp.Key])
                {
                    continue;
                }

                GameObject g = NewTrileObject(kvp.Key);

                index++;

                if (index > 50)
                {
                    index = 0;
                    if (loadOverFrames)
                    {
                        yield return(new WaitForEndOfFrame());
                    }
                }
            }

            //Generate Planes
            {
                foreach (KeyValuePair <int, BackgroundPlane> kvp in loaded.BackgroundPlanes)
                {
                    BackgroundPlane b = kvp.Value;

                    if (b.Hidden || b.Animated || !b.Visible)
                    {
                        continue;
                    }

                    GameObject newPlane;

                    if (planeObjectCache.Count > 0)
                    {
                        newPlane = planeObjectCache[0];
                        planeObjectCache.RemoveAt(0);
                        newPlane.gameObject.SetActive(true);
                    }
                    else
                    {
                        newPlane = Instantiate(planePrefab);
                    }

                    newPlane.transform.rotation   = b.Rotation;
                    newPlane.transform.position   = b.Position - (Vector3.one / 2);
                    newPlane.transform.localScale = new Vector3(-b.Size.x, -b.Size.y, b.Size.z);

                    MeshRenderer mr = newPlane.GetComponent <MeshRenderer>();

                    try {
                        Texture2D tex = FmbUtil.ReadObject <Texture2D>(OutputPath.OutputPathDir + "background planes/" + b.TextureName.ToLower() + ".xnb");

                        if (tex != null)
                        {
                            //tex.alphaIsTransparency=true;
                            mr.material.mainTexture            = tex;
                            mr.material.mainTexture.filterMode = FilterMode.Point;
                        }
                        else
                        {
                            Debug.Log("Tex Null!");
                        }
                    } catch (System.Exception e) {
                        Debug.Log(e);
                        Debug.Log(OutputPath.OutputPathDir + "background planes/" + b.TextureName.ToLower() + ".xnb");
                        Destroy(newPlane);
                        continue;
                    }

                    newPlane.name = b.TextureName;

                    index++;

                    if (index > 5)
                    {
                        index = 0;
                        if (loadOverFrames)
                        {
                            yield return(new WaitForEndOfFrame());
                        }
                    }

                    newPlane.transform.rotation = Quaternion.Euler(newPlane.transform.eulerAngles.x, newPlane.transform.eulerAngles.y - 180, newPlane.transform.eulerAngles.z);
                    newPlane.transform.parent   = transform.FindChild("Planes");
                }
            }

            //Generate Art Objects
            {
                foreach (KeyValuePair <int, ArtObjectInstance> kvp in loaded.ArtObjects)
                {
                    GameObject newTrile;

                    if (aoObjectCache.Count > 0)
                    {
                        newTrile = aoObjectCache[0];
                        aoObjectCache.RemoveAt(0);
                        newTrile.SetActive(true);
                    }
                    else
                    {
                        newTrile = Instantiate(aoPrefab);
                    }

                    MeshFilter        mf  = newTrile.GetComponent <MeshFilter>();
                    MeshRenderer      mr  = newTrile.GetComponent <MeshRenderer>();
                    MeshCollider      mc  = newTrile.GetComponent <MeshCollider>();
                    ArtObjectImported aoI = newTrile.GetComponent <ArtObjectImported>();

                    aoI.myInstance = kvp.Value;

                    mr.material   = FezToUnity.GeometryToMaterial(aoCache[kvp.Value.ArtObjectName].Cubemap);
                    mf.mesh       = aoMeshCache[aoCache[kvp.Value.ArtObjectName]];
                    mc.sharedMesh = aoMeshCache[aoCache[kvp.Value.ArtObjectName]];

                    newTrile.transform.position = kvp.Value.Position - (Vector3.one / 2);
                    newTrile.transform.rotation = kvp.Value.Rotation;

                    index++;

                    if (index > 5)
                    {
                        index = 0;
                        if (loadOverFrames)
                        {
                            yield return(new WaitForEndOfFrame());
                        }
                    }

                    newTrile.name             = kvp.Value.ArtObjectName;
                    newTrile.transform.parent = transform.FindChild("ArtObjects");
                }
            }
        }

        sw.Stop();
        Debug.Log("Objects created in " + sw.ElapsedMilliseconds + " ms");
    }
Ejemplo n.º 4
0
    void PropertiesEditorMode()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit rh;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh))
            {
                if (rh.transform.tag == "Trile")
                {
                    lastTrile   = rh.transform.GetComponent <TrileImported>();
                    trileBounds = LevelManager.Instance.getTrileBounds(lastTrile.myInstance.TrileId);
                    isTrile     = true;
                    ObjectProperties.Instance.SetToTrile(lastTrile.myInstance.TrileId);
                }
                else if (rh.transform.tag == "ArtObject")
                {
                    lastAO   = rh.transform.GetComponent <ArtObjectImported>();
                    aoBounds = LevelManager.Instance.getAOBounds(lastAO.myInstance.ArtObjectName);
                    isTrile  = false;
                    ObjectProperties.Instance.SetToAO(lastAO.myInstance.ArtObjectName);
                }
            }
        }
        else if (Input.GetMouseButtonDown(1))
        {
            //PropertiesEditor.Instance.SelectLevel();
        }

        if (Input.GetKeyDown(KeyCode.Backspace) || Input.GetKeyDown(KeyCode.Delete))
        {
            if (isTrile)
            {
                LevelManager.Instance.RemoveTrile(new TrileEmplacement((int)lastTrile.transform.position.x, (int)lastTrile.transform.position.y, (int)lastTrile.transform.position.z));
            }
            else
            {
                LevelManager.Instance.RemoveAO(lastAO);
            }

            return;
        }

        //If we right click

        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit rh;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh))
            {
                if (rh.transform.tag == "Trile")
                {
                    if (lastTrile == null)
                    {
                        isTrile     = true;
                        lastTrile   = rh.transform.GetComponent <TrileImported>();
                        trileBounds = LevelManager.Instance.getTrileBounds(lastTrile.myInstance.TrileId);
                    }
                    else if (rh.transform != lastTrile.transform)
                    {
                        isTrile     = true;
                        lastTrile   = rh.transform.GetComponent <TrileImported>();
                        trileBounds = LevelManager.Instance.getTrileBounds(lastTrile.myInstance.TrileId);
                    }
                }
                else if (rh.transform.tag == "ArtObject")
                {
                    if (lastAO == null)
                    {
                        isTrile  = false;
                        lastAO   = rh.transform.GetComponent <ArtObjectImported>();
                        aoBounds = LevelManager.Instance.getAOBounds(lastAO.myInstance.ArtObjectName);
                    }
                    else if (rh.transform != lastAO.transform)
                    {
                        isTrile  = false;
                        lastAO   = rh.transform.GetComponent <ArtObjectImported>();
                        aoBounds = LevelManager.Instance.getAOBounds(lastAO.myInstance.ArtObjectName);
                    }
                }
                objectDragging.SetNormalAndPosition(rh.normal, rh.point);
            }

            if (isTrile)
            {
                if (lastTrile == null)
                {
                    return;
                }

                preTrileMove = new TrileEmplacement((int)lastTrile.transform.position.x, (int)lastTrile.transform.position.y, (int)lastTrile.transform.position.z);
                lastTrile.GetComponent <BoxCollider>().enabled = false;
            }
            else
            {
                if (lastAO == null)
                {
                    return;
                }

                lastAO.GetComponent <MeshCollider>().enabled = false;
            }
        }

        if (Input.GetMouseButton(1))
        {
            distOffset -= Input.mouseScrollDelta.y / 13;

            distOffset = Mathf.Clamp(distOffset, -1, 1);

            RaycastHit rh;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rh) && !Input.GetKey(KeyCode.Space))
            {
                objectDragging.SetNormalAndPosition(rh.normal, rh.point);
            }

            if (isTrile)
            {
                if (lastTrile == null)
                {
                    return;
                }

                float pos;

                Ray cam = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (objectDragging.Raycast(cam, out pos))
                {
                    lastTrile.transform.position  = cam.GetPoint(pos) + (objectDragging.normal / 2);
                    lastTrile.transform.position += trileBounds.center;
                    lastTrile.transform.position += objectDragging.normal * distOffset;
                }
            }
            else
            {
                if (lastAO == null)
                {
                    return;
                }

                float pos;

                Ray cam = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (objectDragging.Raycast(cam, out pos))
                {
                    lastAO.transform.position  = cam.GetPoint(pos) - (objectDragging.normal / 2);
                    lastAO.transform.position += aoBounds.center;
                    lastAO.transform.position += objectDragging.normal * distOffset;
                }
            }
        }

        if (Input.GetMouseButtonUp(1))
        {
            if (isTrile)
            {
                if (lastTrile == null)
                {
                    return;
                }

                lastTrile.transform.position = roundToGrid(1, lastTrile.transform.position);
                TrileEmplacement newPlacment = new TrileEmplacement(Mathf.FloorToInt(lastTrile.transform.position.x), Mathf.RoundToInt(lastTrile.transform.position.y), Mathf.RoundToInt(lastTrile.transform.position.z));

                if (LevelManager.Instance.isOccupied(newPlacment))
                {
                    Debug.Log("Trile exists at " + new Vector3(newPlacment.X, newPlacment.Y, newPlacment.Z) + " id is " + LevelManager.Instance.loaded.Triles[newPlacment].TrileId);
                    lastTrile.transform.position = new Vector3(preTrileMove.X, preTrileMove.Y, preTrileMove.Z);
                    lastTrile.GetComponent <BoxCollider>().enabled = true;
                }
                else
                {
                    LevelManager.Instance.MoveTrile(preTrileMove, newPlacment, lastTrile.myInstance);
                    lastTrile.GetComponent <BoxCollider>().enabled = true;
                }
            }
            else
            {
                if (lastAO == null)
                {
                    return;
                }

                lastAO.transform.position = roundToGrid(16, lastAO.transform.position);
                LevelManager.Instance.MoveAO(lastAO.myInstance.Id, lastAO.transform.position);
                lastAO.GetComponent <MeshCollider>().enabled = true;
            }
        }

        //If we middle click
        if (Input.GetMouseButton(2))
        {
            if (isTrile)
            {
                if (lastTrile == null)
                {
                    return;
                }

                lastTrile.transform.rotation = Quaternion.Euler(0, lastTrile.transform.eulerAngles.y - Input.GetAxis("Mouse X") * 5, 0);
            }
            else
            {
                if (lastAO == null)
                {
                    return;
                }

                lastAO.transform.rotation = Quaternion.Euler(0, lastAO.transform.eulerAngles.y - Input.GetAxis("Mouse X") * 5, 0);
            }
        }

        //If we let go of middle click
        if (Input.GetMouseButtonUp(2))
        {
            if (isTrile)
            {
                if (lastTrile == null)
                {
                    return;
                }

                lastTrile.transform.rotation = Quaternion.Euler(0, Mathf.RoundToInt(lastTrile.transform.eulerAngles.y / 90) * 90, 0);
            }
            else
            {
                if (lastAO == null)
                {
                    return;
                }

                lastAO.transform.rotation = Quaternion.Euler(0, Mathf.RoundToInt(lastAO.transform.eulerAngles.y / 90) * 90, 0);
            }
        }
    }