Exemple #1
0
        /// <summary>
        /// If this config has the specified chunk type to process with
        /// </summary>
        /// <typeparam name="TChunk"></typeparam>
        /// <returns></returns>
        public bool HasChunk <TChunk>() where TChunk : Chunk, new()
        {
            var   nChunk = new TChunk();
            Chunk chunk;

            return(_chunks.TryGetValue(nChunk.InternalChunkID, out chunk));
        }
Exemple #2
0
        /// <summary>
        /// Register a chunk processor with this config
        /// </summary>
        /// <typeparam name="TChunk"></typeparam>
        /// <returns>the Chunk that will be doing the processing. Use this to change its behaviour if you have to</returns>
        public TChunk RegisterChunk <TChunk>() where TChunk : Chunk, new()
        {
            var nChunk = new TChunk();

            _chunks[nChunk.InternalChunkID] = nChunk;
            return(nChunk);
        }
Exemple #3
0
    public void destroySceneryObject(GameObject obj)
    {
        SceneryObject sc_obj = obj.GetComponent <SceneryObject> ();
        TChunk        chunk  = loaded_chunks.getElem(sc_obj.chunk_index_x, sc_obj.chunk_index_y);

        chunk.generator.destroySceneryObject(obj, ref Scenery_Obj_Pool);
    }
Exemple #4
0
    public void UpdateLoadedChunks()
    {
        TChunk        tchunk  = getChunkFromPosition(last_cam_position);
        List <TChunk> tchunks = getChunksAround(ref tchunk);

        updateLoadedSceneryChunks(ref tchunks);
    }
Exemple #5
0
 public bool elemIsValid(TChunk chunk)
 {
     if (chunk.index_x >= 0 && chunk.index_y >= 0)
     {
         return(true);
     }
     return(false);
 }
Exemple #6
0
    public TChunk getChunkTileFromPosition(Vector3 position, out int tile_index_x, out int tile_index_z)
    {
        TChunk chunk = getChunkFromPosition(position);

        chunk.generator.getTileIndexesFromPosition(new Vector3(position.x - chunk.generator.origin.x, 0, position.z - chunk.generator.origin.z), out tile_index_x, out tile_index_z);

        return(chunk);
    }
Exemple #7
0
    public void UpdateChunk(int chunkIndexX, int chunkIndexY)
    {
        TChunk elem = loaded_chunks.getElem(chunkIndexX, chunkIndexY);

        elem.generator.unloadScenery(ref Scenery_Obj_Pool);
        elem.generator.unloadPaths(ref Path_Obj_Pool);
        loaded_chunks.removeElem(ref elem);

        UpdateLoadedChunks();
    }
Exemple #8
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
        {
            // se cambia el personaje con Tab
            if (currentCharacter == builder)
            {
                changeCurrentCharacter(explorer);
            }
            else if (currentCharacter == scientist)
            {
                changeCurrentCharacter(builder);
            }
            else
            {
                changeCurrentCharacter(scientist);
            }
        }

        // handle left mouse button clicks
        if (Input.GetMouseButtonDown(0))
        {
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                // did not click on a UI element or scenery object

                if (gettingMousePositionOnWorld)                                        // CAMBIOS

                // TILE INDEXES
                {
                    Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hit;
                    if (Physics.Raycast(ray, out hit))
                    {
                        int    indx;
                        int    indz;
                        TChunk chunk = GameController.gameController.terrainGenerator.getChunkTileFromPosition(hit.point, out indx, out indz);
                        pointInWorld = hit.point;
                        chunkIndexX  = chunk.index_x;
                        chunkIndexY  = chunk.index_y;
                        tileIndexX   = indz;
                        tileIndexY   = indx;
                        //Debug.Log("Chunk indexes: ("+chunk.index_x+", "+chunk.index_y+"), "+"Tile indexes: ("+indx+", "+indz+")");
                    }
                    // END OF TILE INDEXES
                }
                else
                {
                    currentCharacter.GetComponent <PlayerMovement> ().checkNewDestination();
                    changeCharacterOnMouseDown();
                }
            }
        }
    }
Exemple #9
0
    public void removeElem(ref TChunk chunk)
    {
        int           elem_index = chunk.index_y * world_size_x + chunk.index_x;
        List <TChunk> entry      = hash_table [getEntryIndex(elem_index)];

        for (int i = 0; i < entry.Count; ++i)
        {
            if (entry[i].index_x == chunk.index_x && entry[i].index_y == chunk.index_y)
            {
                indexes_saved.Remove(new Vector2(chunk.index_x, chunk.index_y));
                entry.RemoveAt(i);
                return;
            }
        }
    }
Exemple #10
0
    public TChunk getChunkFromPosition(Vector3 position)
    {
        float chunk_size = tile_size * side_tile_count;

        int index_x = (int)(position.x / chunk_size);
        int index_y = (int)Mathf.Abs(position.z / chunk_size);

        if (index_x >= 0 && index_x < terrain_chunks_x && index_y < terrain_chunks_y)
        {
            return(TerrainChunks[index_y, index_x]);
        }

        TChunk invalid = new TChunk();

        return(invalid);
    }
Exemple #11
0
    public void addElem(ref TChunk chunk)
    {
        int           elem_index = chunk.index_y * world_size_x + chunk.index_x;
        List <TChunk> entry      = hash_table [getEntryIndex(elem_index)];

        for (int i = 0; i < entry.Count; ++i)
        {
            if (entry[i].index_x == chunk.index_x && entry[i].index_y == chunk.index_y)
            {
                // already exists
                return;
            }
        }

        indexes_saved.Add(new Vector2(chunk.index_x, chunk.index_y));
        entry.Add(chunk);
    }
Exemple #12
0
    // load scenery and path objects in visible_chunks. This is done every frame.
    private void updateLoadedSceneryChunks(ref List <TChunk> visible_chunks)
    {
        List <Vector2> saved_indexes = loaded_chunks.getSavedIndexes();

        for (int i = 0; i < saved_indexes.Count; ++i)
        {
            bool isVisible = false;
            for (int j = 0; j < visible_chunks.Count; ++j)
            {
                if (visible_chunks[j].index_x == saved_indexes[i].x && visible_chunks[j].index_y == saved_indexes[i].y)
                {
                    isVisible = true;
                    break;
                }
            }

            if (!isVisible)
            {
                // remove chunk from table
                TChunk elem = loaded_chunks.getElem((int)saved_indexes[i].x, (int)saved_indexes[i].y);
                elem.generator.unloadScenery(ref Scenery_Obj_Pool);
                elem.generator.unloadPaths(ref Path_Obj_Pool);
                loaded_chunks.removeElem(ref elem);
            }
        }

        for (int i = 0; i < visible_chunks.Count; ++i)
        {
            if (!loaded_chunks.elemIsValid(loaded_chunks.getElem(visible_chunks[i].index_x, visible_chunks[i].index_y)))
            {
                // add chunk to table
                TChunk elem = visible_chunks[i];
                elem.generator.loadScenery(ref Scenery_Obj_Pool);
                elem.generator.loadPaths(ref Path_Obj_Pool);
                loaded_chunks.addElem(ref elem);
            }
        }
    }
Exemple #13
0
    private List <TChunk> getChunksAround(ref TChunk chunk)
    {
        List <TChunk> chunks = new List <TChunk> ();

        addChunkToListIfExists(chunk.index_x - 3, chunk.index_y, ref chunks);
        addChunkToListIfExists(chunk.index_x - 2, chunk.index_y, ref chunks);
        addChunkToListIfExists(chunk.index_x - 1, chunk.index_y, ref chunks);
        addChunkToListIfExists(chunk.index_x, chunk.index_y, ref chunks);
        addChunkToListIfExists(chunk.index_x + 1, chunk.index_y, ref chunks);

        addChunkToListIfExists(chunk.index_x - 3, chunk.index_y - 1, ref chunks);
        addChunkToListIfExists(chunk.index_x - 2, chunk.index_y - 1, ref chunks);
        addChunkToListIfExists(chunk.index_x - 1, chunk.index_y - 1, ref chunks);
        addChunkToListIfExists(chunk.index_x, chunk.index_y - 1, ref chunks);
        addChunkToListIfExists(chunk.index_x + 1, chunk.index_y - 1, ref chunks);

        addChunkToListIfExists(chunk.index_x - 3, chunk.index_y + 1, ref chunks);
        addChunkToListIfExists(chunk.index_x - 2, chunk.index_y + 1, ref chunks);
        addChunkToListIfExists(chunk.index_x - 1, chunk.index_y + 1, ref chunks);
        addChunkToListIfExists(chunk.index_x, chunk.index_y + 1, ref chunks);
        addChunkToListIfExists(chunk.index_x + 1, chunk.index_y + 1, ref chunks);

        addChunkToListIfExists(chunk.index_x - 3, chunk.index_y + 2, ref chunks);
        addChunkToListIfExists(chunk.index_x - 2, chunk.index_y + 2, ref chunks);
        addChunkToListIfExists(chunk.index_x - 1, chunk.index_y + 2, ref chunks);
        addChunkToListIfExists(chunk.index_x, chunk.index_y + 2, ref chunks);
        addChunkToListIfExists(chunk.index_x + 1, chunk.index_y + 2, ref chunks);

        addChunkToListIfExists(chunk.index_x - 3, chunk.index_y + 3, ref chunks);
        addChunkToListIfExists(chunk.index_x - 2, chunk.index_y + 3, ref chunks);
        addChunkToListIfExists(chunk.index_x - 1, chunk.index_y + 3, ref chunks);
        addChunkToListIfExists(chunk.index_x, chunk.index_y + 3, ref chunks);
        addChunkToListIfExists(chunk.index_x + 1, chunk.index_y + 3, ref chunks);

        return(chunks);
    }
        //	return if changed
        static bool ParseCapture(Capture NameMatch, ref string Json, System.Func <string, string> ReplaceHeriachyDoubleArrayWithMemberName)
        {
            //	todo: have a full heirachy name, not just this element
            var HeirachyName = NameMatch.ToString().Trim(new char[] { '"' });

            var InjectMemberName = ReplaceHeriachyDoubleArrayWithMemberName.Invoke(HeirachyName);

            if (InjectMemberName == null)
            {
                return(false);
            }

            //	grab the whole double array string after semi colon
            int NameEnd    = NameMatch.Index + NameMatch.Length;
            int ArrayStart = Json.IndexOf('[', NameEnd);

            if (ArrayStart == -1)
            {
                throw new System.Exception("Can't find start of array");
            }

            int DoubleArrayStart = ArrayStart;
            int DoubleArrayEnd   = ArrayStart;
            var DoubleArray      = ExtractNextChunk('[', ']', Json, ref DoubleArrayStart, ref DoubleArrayEnd);

            //	get each array
            var ArrayElements = new List <TChunk>();
            int ChunkStart    = 1;

            while (true)
            {
                var NewChunk = new TChunk();
                NewChunk.Start = ChunkStart;
                NewChunk.End   = ChunkStart;
                NewChunk.Chunk = ExtractNextChunk('[', ']', DoubleArray, ref NewChunk.Start, ref NewChunk.End);
                if (NewChunk.Chunk == null)
                {
                    break;
                }

                ChunkStart = NewChunk.End;

                //	correct for final usage
                NewChunk.Start += ArrayStart;
                NewChunk.End   += ArrayStart;
                ArrayElements.Add(NewChunk);
            }

            //Debug.Log ("Found " + ArrayElements.Count + " elements;");
            //foreach (var Element in ArrayElements)
            //	Debug.Log (Element.Chunk);

            //	inject object & member name for every element
            //	do back to front so we don't need to correct indexes for elements
            ArrayElements.Reverse();
            //	{	"myvector":
            var InjectPrefix = " { \"" + InjectMemberName + "\" : ";
            var InjectSuffix = " } ";

            foreach (var Element in ArrayElements)
            {
                Json = Json.Insert(Element.End, InjectSuffix);
                Json = Json.Insert(Element.Start, InjectPrefix);
            }

            return(true);
        }
Exemple #15
0
        /// <summary>
        /// Remove the specified chunk type from the processors
        /// </summary>
        /// <typeparam name="TChunk"></typeparam>
        public void RemoveChunk <TChunk>() where TChunk : Chunk, new()
        {
            var nChunk = new TChunk();

            _chunks.Remove(nChunk.InternalChunkID);
        }
Exemple #16
0
    public void destroyPathObject(int chunk_index_x, int chunk_index_y, int tile_index_x, int tile_index_y)
    {
        TChunk chunk = loaded_chunks.getElem(chunk_index_x, chunk_index_y);

        chunk.generator.destroyPathObject(tile_index_x, tile_index_y);
    }
Exemple #17
0
    public bool tileContainsPath(int chunk_index_x, int chunk_index_y, int tile_index_x, int tile_index_y)
    {
        TChunk chunk = loaded_chunks.getElem(chunk_index_x, chunk_index_y);

        return(chunk.generator.tileContainsPath(tile_index_x, tile_index_y));
    }
Exemple #18
0
    // object initialization
    public void init()
    {
        TerrainGenerator  = gameObject.GetComponent <GenerateTerrain> ();
        SceneryObject_Ref = SceneryObject_Prefab;

        slope_height = tile_size * slope_factor;
        chunk_size_x = side_tile_count * tile_size;
        chunk_size_y = side_tile_count * tile_size;
        origin       = gameObject.transform.position;


        MapLoader mloader = new MapLoader();

        mloader.openFile(texture_size_x, texture_size_y, heightmap_path);
        List <int[, ]> lmaps = mloader.getLevelMapList(level_count, level_zero_grayscale, side_tile_count, out terrain_chunks_x, out terrain_chunks_y);

        mloader = new MapLoader();
        mloader.openFile(texture_size_x, texture_size_y, tileTypeMap_path);
        List <TileType[, ]> ttmaps = mloader.getTileTypeMapList(side_tile_count);

        WorldLevelMaps    = setWorldLevelMaps(ref lmaps, terrain_chunks_x, terrain_chunks_y);
        WorldTileTypeMaps = setWorldTileTypeMaps(ref ttmaps, terrain_chunks_x, terrain_chunks_y);

        initSceneryObjPool();
        initPathObjPool();
        loaded_chunks = new TC_HashTable(terrain_chunks_x * terrain_chunks_y, terrain_chunks_x, terrain_chunks_x, terrain_chunks_y);

        last_cam_position = Vector3.zero;


        TerrainChunks = new TChunk[terrain_chunks_y, terrain_chunks_x];

        for (int i = 0; i < terrain_chunks_y; ++i)
        {
            for (int j = 0; j < terrain_chunks_x; ++j)
            {
                int index_x = j;
                int index_y = i;

                List <int[]> neighbor_level_maps = getNeighborLevelMaps(index_x, index_y, ref WorldLevelMaps);

                Vector3 chunk_origin = origin;
                chunk_origin.x += chunk_size_x * index_x;
                chunk_origin.z -= chunk_size_y * index_y;
                Vector3 chunk_center = new Vector3(chunk_origin.x + chunk_size_x * 0.5f, 0, chunk_origin.z - chunk_size_y * 0.5f);

                GameObject new_terrain_chunk = (GameObject)Instantiate(TerrainChunk_Prefab);
                new_terrain_chunk.transform.parent = transform;
                GenerateTerrainChunk generator = new_terrain_chunk.GetComponent <GenerateTerrainChunk> ();
                generator.generate(index_x, index_y, tile_size, slope_height, side_tile_count, chunk_origin, chunk_center, ref WorldLevelMaps, WorldTileTypeMaps, terrain_chunks_x, terrain_chunks_y, textures_x, textures_y, ref neighbor_level_maps, new_terrain_chunk.GetComponent <MeshFilter> ());

                GameObject new_ws_chunk = (GameObject)Instantiate(WaterSurfaceChunk_Prefab);
                new_ws_chunk.transform.parent   = new_terrain_chunk.transform;
                new_ws_chunk.transform.position = new_terrain_chunk.transform.position;
                generator.generateWaterSurface(new_ws_chunk);


                TChunk terrain_chunk = new TChunk(index_x, index_y, chunk_origin, chunk_center, ref new_terrain_chunk, ref new_ws_chunk, ref generator);
                TerrainChunks[index_y, index_x] = terrain_chunk;
            }
        }
    }
Exemple #19
0
    public int[,] getChunkSceneryMap(int chunk_index_x, int chunk_index_y)
    {
        TChunk chunk = loaded_chunks.getElem(chunk_index_x, chunk_index_y);

        return(chunk.generator.getSceneryMap());
    }
Exemple #20
0
    public PathType[,] getChunkPathMap(int chunk_index_x, int chunk_index_y)
    {
        TChunk chunk = loaded_chunks.getElem(chunk_index_x, chunk_index_y);

        return(chunk.generator.getPathMap());
    }
Exemple #21
0
    public bool tileIsSuitableForBridge(int chunk_index_x, int chunk_index_y, int tile_index_x, int tile_index_y)
    {
        TChunk chunk = loaded_chunks.getElem(chunk_index_x, chunk_index_y);

        return(chunk.generator.tileIsSuitableForBridge(tile_index_x, tile_index_y));
    }