Beispiel #1
0
    void Awake()
    {
        //if (photonView.IsMine)  //Uncomment for Photon
        //{
        RPGCharacterController.LocalPlayerInstance = this.gameObject;

        GameObject TPObj = GameObject.Find("TerrainPager");

        if (TPObj != null)
        {
            TerrainPager theTP = TPObj.GetComponent <TerrainPager>();
            if (theTP != null)
            {
                theTP.PlayerObject = this.gameObject;
            }
        }
        //}
        //else  //well, this plan didn't work...
        //{ //If not our instance, then turn off the camera, so we don't follow the wrong player.
        //if (CameraObject != null)
        //    CameraObject.SetActive(false);
        //}
        // #Critical
        // we flag as don't destroy on load so that instance survives level synchronization, thus giving a seamless experience when levels load.
        DontDestroyOnLoad(this.gameObject);
    }
Beispiel #2
0
    private static void vEarthDeleteRoads()
    {
        TerrainPager theTP = GameObject.Find("TerrainPager").GetComponent <TerrainPager>();

        if (theTP == null)
        {
            return;
        }

        //theTP.DeleteRoads();
    }
Beispiel #3
0
    private static void vEarthSaveRoads()
    {
        TerrainPager theTP = GameObject.Find("TerrainPager").GetComponent <TerrainPager>();

        if (theTP == null)
        {
            return;
        }

        //theTP.SaveRoads();//TEMP, ROADS
    }
Beispiel #4
0
    private static void vEarthStopTerrainPager()
    {
        TerrainPager theTP = GameObject.Find("TerrainPager").GetComponent <TerrainPager>();

        if (theTP == null)
        {
            return;
        }

        theTP.StopTerrainPager();
    }
Beispiel #5
0
    private static void vEarthImportOpenStreetMap()
    {
        TerrainPager theTP = GameObject.Find("TerrainPager").GetComponent <TerrainPager>();

        if (theTP == null)
        {
            return;
        }

        //FIX: put up a browser window to find a file.
        theTP.ImportOpenStreetMap("Assets/OSM/creswell_greater.osm");
    }
Beispiel #6
0
 // Update is called once per frame
 public void Update()
 {
     if (mTP == null)
     {
         mTP = GameObject.FindWithTag("TerrainPager").GetComponent <TerrainPager>();
         return;
     }
     else if (mTerrainObject == null)
     {
         mTerrainObject = mTP.GetTerrainBlock(this.transform.position);
         if (mTerrainObject != null)
         {
             mTerrain = mTerrainObject.GetComponent <Terrain>();
         }
     }
     if (mTerrain != null)
     {
         Vector3 newPos = transform.position;
         newPos.y = mTerrain.SampleHeight(newPos);
         this.transform.position = newPos;
     }
 }
Beispiel #7
0
    private static void vEarthLoadShapes()
    {
        TerrainPager theTP = GameObject.Find("TerrainPager").GetComponent <TerrainPager>();

        if (theTP == null)
        {
            return;
        }


        //Have to load up values in terrainPager, it does this at runtime. Better way??
        theTP.SquareSize         = 10.0f;
        theTP.HeightmapRes       = 257;        //TEMP, 257, FiX FIX FIX, have to get back into FG to fix this though.
        theTP.TileWidth          = (float)(theTP.HeightmapRes - 1) * theTP.SquareSize;
        theTP.MapCenterLatitude  = 44.0f;      // 21.936f;//22.0f;////HERE: this is the geographic center of the whole map.
        theTP.MapCenterLongitude = -123.0047f; // 123.0047f;//  -159.380f;//-159.5f;//GET THIS FROM THE GUI! //.005?? Something is broken, by just five thousandths. ??

        float rLat = theTP.MapCenterLatitude * Mathf.Deg2Rad;

        theTP.MetersPerDegreeLatitude  = 111132.92f - 559.82f * Mathf.Cos(2 * rLat) + 1.175f * Mathf.Cos(4 * rLat);
        theTP.MetersPerDegreeLongitude = 111412.84f * Mathf.Cos(rLat) - 93.5f * Mathf.Cos(3 * rLat);
        theTP.DegreesPerMeterLongitude = 1.0f / theTP.MetersPerDegreeLongitude;
        theTP.DegreesPerMeterLatitude  = 1.0f / theTP.MetersPerDegreeLatitude;
        theTP.TileWidthLongitude       = theTP.DegreesPerMeterLongitude * theTP.TileWidth;
        theTP.TileWidthLatitude        = theTP.DegreesPerMeterLatitude * theTP.TileWidth;


        string mDbName = "w130n40.db";
        string conn = "URI=file:" + Application.dataPath + "/" + mDbName;//Will this break on build as well? Move to Resources?
        string selectQuery, upsertQuery;

        IDbConnection mDbConn;
        IDbCommand    mDbCmd;
        IDataReader   reader;

        mDbConn = (IDbConnection) new SqliteConnection(conn);
        mDbConn.Open(); //Open connection to the database.
        mDbCmd = mDbConn.CreateCommand();

        Coordinates terrCoord = new Coordinates();
        Coordinates nodeCoord = new Coordinates();
        Coordinates objCoord  = new Coordinates();

        if (theTP.MapNodePrefab == null)
        {
            Debug.Log("Map Node Prefab not found, exiting vEarthLoadShapes.");
            return;
        }


        GameObject[] objs         = Selection.gameObjects;
        GameObject[] allObjs      = Object.FindObjectsOfType <GameObject>();
        int          terrainCount = 0;

        foreach (GameObject terrObj in objs)
        {
            Terrain terr = terrObj.GetComponent <Terrain>();
            if (terr != null)
            {
                TerrainData terrData = terr.terrainData;
                if (terrData.name.IndexOf('.') <= 0) // if we are not on a proper tile that came from a terrainData asset.
                {
                    continue;
                }

                Vector3 terrPos = terr.transform.position;
                terrainCount++;

                string tileName = terrData.name.Substring(terrData.name.IndexOf('.') + 1);
                string objName  = "";
                int    nodeId   = 0;
                //Debug.Log("Terrain selected! tile name " + tileName + "   all objects: " + allObjs.Length +
                //    " mapNodes " + mapNodeObjs.Length + " players " + playerObjs.Length + " peasants " + peasantObjs.Length);
                selectQuery        = "SELECT latitude,longitude FROM mapNode WHERE name='" + tileName + "' AND type='Terrain';";//type='Terrain' is probably overkill, but why not.
                mDbCmd.CommandText = selectQuery;
                reader             = mDbCmd.ExecuteReader();
                //Debug.Log(selectQuery);
                while (reader.Read())
                {
                    //Playing fast and loose here but I KNOW I gave everybody a latitude and longitude...
                    terrCoord.latitude  = reader.GetDouble(reader.GetOrdinal("latitude"));
                    terrCoord.longitude = reader.GetDouble(reader.GetOrdinal("longitude"));
                }
                reader.Close();


                selectQuery = "SELECT id,latitude,longitude,name FROM mapNode WHERE type='MapNode' AND latitude >= " + terrCoord.latitude +
                              " AND latitude < " + (terrCoord.latitude + theTP.TileWidthLatitude) + " AND longitude >= " +
                              terrCoord.longitude + " AND longitude < " + (terrCoord.longitude + theTP.TileWidthLongitude) + ";";
                mDbCmd.CommandText = selectQuery;
                reader             = mDbCmd.ExecuteReader();
                while (reader.Read())
                {
                    nodeId              = reader.GetInt32(reader.GetOrdinal("id"));
                    nodeCoord.latitude  = reader.GetDouble(reader.GetOrdinal("latitude"));
                    nodeCoord.longitude = reader.GetDouble(reader.GetOrdinal("longitude"));
                    try { objName = reader.GetString(reader.GetOrdinal("name")); }
                    catch { }

                    if (nodeId <= 0)
                    {
                        continue;
                    }

                    Debug.Log("Loading map node: " + nodeCoord.longitude + "   " + nodeCoord.latitude);

                    GameObject myNode = GameObject.Instantiate(theTP.MapNodePrefab);
                    Vector3    myPos  = new Vector3();
                    myPos   = theTP.ConvertLatLongToXYZ(nodeCoord.longitude, 0, nodeCoord.latitude);
                    myPos.y = terr.SampleHeight(myPos);
                    myNode.transform.position          = myPos;
                    myNode.GetComponent <SimBase>().id = nodeId;
                    //.y = terrData.GetHeight();
                    //Debug.Log("!!!!!!!!!!!!!!! " + tileName + " INSTANTIATED a mapnode!!!!! " + objName + " pos " + myNode.transform.position + "!!!!!!!!!!!!!");
                    myNode.name = objName;

                    //Debug.Log(tileName + " found a mapnode! " + objName + "  " + objCoord.longitude + " " + objCoord.latitude);
                    //Next, select all mapShapes associated with this node...
                    //And then, for each shape, find the appropriate file.


                    string shapeQuery = "SELECT * FROM mapShape WHERE node_id=" + nodeId + ";";
                    //Debug.Log(shapeQuery);
                    IDbCommand mDbCmd2 = mDbConn.CreateCommand();
                    mDbCmd2.CommandText = shapeQuery;
                    IDataReader subReader = mDbCmd2.ExecuteReader();
                    while (subReader.Read())
                    {
                        Vector3    pos, scale;
                        Quaternion q;
                        int        id;
                        int        file_id;

                        try
                        {
                            float x, y, z, rx, ry, rz, rw, sx, sy, sz;
                            id      = subReader.GetInt32(subReader.GetOrdinal("id"));
                            file_id = subReader.GetInt32(subReader.GetOrdinal("file_id"));
                            x       = subReader.GetFloat(subReader.GetOrdinal("x"));
                            y       = subReader.GetFloat(subReader.GetOrdinal("y"));
                            z       = subReader.GetFloat(subReader.GetOrdinal("z"));
                            rx      = subReader.GetFloat(subReader.GetOrdinal("rx"));
                            ry      = subReader.GetFloat(subReader.GetOrdinal("ry"));
                            rz      = subReader.GetFloat(subReader.GetOrdinal("rz"));
                            rw      = subReader.GetFloat(subReader.GetOrdinal("rw"));
                            sx      = subReader.GetFloat(subReader.GetOrdinal("sx"));
                            sy      = subReader.GetFloat(subReader.GetOrdinal("sy"));
                            sz      = subReader.GetFloat(subReader.GetOrdinal("sz"));
                            pos     = new Vector3(x, y, z);
                            q       = new Quaternion(rx, ry, rz, rw);
                            scale   = new Vector3(sx, sy, sz);
                        }
                        catch
                        {
                            Debug.Log("There was a problem loading shapes for mapNode " + nodeId);
                            continue;
                        }

                        //NOW, we have pos, rot and scale, and file id. I could have done this in the one query above but not sure how to mix joins with select *
                        string pathQuery = "SELECT path FROM mapShapeFile WHERE id=" + file_id + ";";
                        //Debug.Log(pathQuery);
                        IDbCommand mDbCmd3 = mDbConn.CreateCommand();
                        mDbCmd3.CommandText = pathQuery;
                        IDataReader subSubReader = mDbCmd3.ExecuteReader();
                        subSubReader.Read();
                        string path = subSubReader.GetString(subSubReader.GetOrdinal("path"));
                        subSubReader.Close();
                        subSubReader.Dispose();
                        mDbCmd3.Dispose();

                        if (path.Length == 0)
                        {
                            continue;
                        }

                        //Debug.Log("Should be creating prefab: " + path);
                        //And now we have the path, since I did *not* do a join in the original query for lazy and stupid reasons. (FIX FIX FIX)
                        UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath(path + ".prefab", typeof(GameObject));
                        GameObject         myObj  = GameObject.Instantiate(prefab) as GameObject;
                        Vector3            objPos = myNode.transform.position + pos;
                        objPos.y = terr.SampleHeight(objPos);
                        myObj.transform.position               = objPos;
                        myObj.transform.rotation               = q;     // Ah, hmm, as currently recorded, map nodes don't have any rotation. This might suck.
                        myObj.transform.localScale             = scale; //Ditto with scale.
                        myObj.GetComponent <SimBase>().id      = id;
                        myObj.GetComponent <SimBase>().file_id = file_id;
                        myObj.GetComponent <SimBase>().mapNode = myNode;
                    }
                    subReader.Close();
                    subReader.Dispose();
                    mDbCmd2.Dispose();
                }
                reader.Close();
            }
        }
    }