Esempio n. 1
0
 public static void OffsetNodes(List <LogicHex> nodes, LogicHex offset)
 {
     for (int i = 0; i < nodes.Count; ++i)
     {
         nodes[i] = nodes[i].Add(offset);
     }
 }
Esempio n. 2
0
        public LogicPoint3 HexToLogicPos(LogicHex hex)
        {
            int tile   = map.Get(hex.q, hex.r);
            int height = LogicTile.GetAltitude(tile);

            ///int tile = LogicTile.SetTerrain(0, TerrainType.Grass);

            return(HexToLogicPos(hex, height));
        }
Esempio n. 3
0
        public static LogicPoint3 HexToLogicPos(LogicHex hex, int height)
        {
            LogicPoint3 p = new LogicPoint3();

            p.x = (hex.r * TILE_SIZE) + (hex.s * HALF_TILE_SIZE);
            p.y = height * ALTITUDE_SCALE;
            p.z = -hex.s * HEXAGON_OFFSET;

            return(p);
        }
Esempio n. 4
0
        public static void PlotNodes(LogicHexMap map, List <LogicHex> nodes, int radius, TerrainType terrain)
        {
            for (int i = 0; i < nodes.Count; ++i)
            {
                LogicHex node = nodes[i];

                int tile = map.Get(node);
                tile = LogicTile.SetTerrain(tile, terrain);

                map.Set(node, tile);
            }
        }
Esempio n. 5
0
        public static void RandomOffsetNodes(List <LogicHex> nodes, int maxDistance, int seed)
        {
            for (int i = 0; i < nodes.Count; ++i)
            {
                LogicHex node = nodes[i];

                int qNoise = LogicPerlinNoise.Noise((node.q << 12) + seed, (node.s << 15) + seed, seed * 7, maxDistance);
                int rNoise = LogicPerlinNoise.Noise((node.q << 14) + seed, (node.s << 13) + seed, seed * 10, maxDistance);

                nodes[i] = nodes[i].Add(new LogicHex(qNoise, rNoise));
            }
        }
Esempio n. 6
0
        public void Touch2Hex(LogicHex hex)
        {
            //tiles.Clear();

            int tile = LogicTile.SetTerrain(0, TerrainType.Rock);

            //LogicHex origo = new LogicHex(9, 9);
            //tiles.Set(origo, tile);
            touch2 = hex;

            UpdateMap();

            //map.Set(hex, tile);
        }
Esempio n. 7
0
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            seedStepVelocity -= 100;
            if (seedStepVelocity < -5000)
            {
                seedStepVelocity = -5000;
            }
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            seedStepVelocity += 100;
            if (seedStepVelocity > 5000)
            {
                seedStepVelocity = 5000;
            }
        }
        else
        {
            seedStepVelocity = seedStepVelocity / 2;
        }

        if (seedStepVelocity != 0)
        {
            updateWorld  = true;
            config.seed += seedStepVelocity;
        }

        if (updateWorld)
        {
            updateWorld = false;
            world.UpdateMap();
            Render();
        }

        if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (meshCollider != null)
            {
                RaycastHit hit;

                if (meshCollider.Raycast(ray, out hit, 1000f))
                {
                    if (cursor != null)
                    {
                        cursor.transform.position = hit.point;
                    }
                    LogicHex hex = WorldToHexPos(hit.point);

                    if (Input.GetMouseButton(0))
                    {
                        world.TouchHex(hex);
                    }
                    else
                    {
                        world.Touch2Hex(hex);
                    }

                    Render();
                }
            }
        }
    }
Esempio n. 8
0
    Vector3 HexToWorldPos(LogicHex hex)
    {
        LogicPoint3 p = world.HexToLogicPos(hex);

        return(new Vector3(p.x * tileScaleFactor, p.y * tileScaleFactor, p.z * tileScaleFactor));
    }
Esempio n. 9
0
    void Render()
    {
        LogicArray2 <int> tiles = world.map;

        LogicArray2 <RenderTile> renderTiles = new LogicArray2 <RenderTile>(tiles.width, tiles.height, null);

        { //Vertex creation
            int index = 0;
            for (int y = 0; y < tiles.height; ++y)
            {
                for (int x = 0; x < tiles.width; ++x)
                {
                    int     tile = tiles.Get(x, y);
                    Vector3 pos  = HexToWorldPos(new LogicHex(x, y));

                    Color32 color = TerrainTypeToColor(LogicTile.GetTerrain(tile));
                    renderTiles.Set(x, y, new RenderTile(tile, pos, Vector3.zero, color));

                    ++index;
                }
            }
        }

        {                                                    //Smooth normal calculations
            int index = 0;
            for (int y = 1; y < renderTiles.height - 1; ++y) //Skip edge tiles
            {
                for (int x = 1; x < renderTiles.width - 1; ++x)
                {
                    RenderTile tile = renderTiles.Get(x, y);

                    Vector3 p0 = tile.position;
                    for (int h = 0; h < 6; ++h)
                    {
                        LogicHex dir1 = LogicHex.Direction(h);
                        LogicHex dir2 = LogicHex.Direction(h + 1);

                        Vector3 p1 = renderTiles.Get(x + dir1.q, y + dir1.r).position;
                        Vector3 p2 = renderTiles.Get(x + dir2.q, y + dir2.r).position;

                        Vector3 side1 = p1 - p0;
                        Vector3 side2 = p2 - p0;
                        Vector3 perp  = Vector3.Cross(side1, side2);

                        //float perpLength = perp.magnitude;
                        //perp /= perpLength;

                        tile.normal += perp;
                    }

                    float normLength = tile.normal.magnitude;
                    tile.normal /= normLength;

                    ++index;
                }
            }
        }

        { //Mesh creation (splitting in chunks, triangulation etc.)
            int widthInChunks  = renderTiles.width / CHUNK_SIZE;
            int heightInChunks = renderTiles.height / CHUNK_SIZE;

            if (chunks == null)
            {
                chunks = new LogicArray2 <Chunk>(widthInChunks, heightInChunks, null);
            }

            for (int cy = 0; cy < heightInChunks; ++cy)
            {
                for (int cx = 0; cx < widthInChunks; ++cx)
                {
                    DynamicMesh dynamicMesh = new DynamicMesh();

                    //Chunk bounds
                    int startY = cy * CHUNK_SIZE;
                    int endY   = startY + CHUNK_SIZE;
                    int startX = cx * CHUNK_SIZE;
                    int endX   = startX + CHUNK_SIZE;

                    if (cx == widthInChunks - 1)
                    {
                        endX--;
                    }

                    if (cy == widthInChunks - 1)
                    {
                        endY--;
                    }


                    for (int y = startY; y < endY; ++y)
                    {
                        for (int x = startX; x < endX; ++x)
                        {
                            RenderTile tile0 = renderTiles.Get(x, y);
                            RenderTile tile1 = renderTiles.Get(x + 1, y);
                            RenderTile tile2 = renderTiles.Get(x, y + 1);
                            RenderTile tile3 = renderTiles.Get(x + 1, y + 1);

                            RenderTriangle(dynamicMesh, tile0, tile1, tile2);
                            RenderTriangle(dynamicMesh, tile2, tile1, tile3);
                        }
                    }

                    Mesh  mesh  = dynamicMesh.Create();
                    Chunk chunk = GetChunk(cy, cx);
                    chunk.SetMesh(mesh);
                }
            }
        }

        //Debug.Log("Triangles:" + dynamicMesh.triangles.Count + "Vertices:" + dynamicMesh.vertices.Count);

        //Mesh mesh = dynamicMesh.Create();

        //meshFilter.mesh = mesh;

        //meshCollider.sharedMesh = mesh;
        //Debug.Log("NavigationBlob::MeshUpdate duration=" + (Time.realtimeSinceStartup - timer));
    }