예제 #1
0
    static bool CanMoveTo(Vector2Int prev, Vector2Int cur, State goer, bool lastCell)
    {
        Region reg     = MapMetrics.GetRegion(cur),
               prevreg = MapMetrics.GetRegion(prev);

        if (reg.iswater != prevreg.iswater)
        {
            return(false);
        }
        State s = reg.ocptby == null ? reg.owner : reg.ocptby;

        if (goer == s)
        {
            return(true);
        }
        if (s == null)
        {
            return(true);
        }
        if (!lastCell && reg.Capital == cur)
        {
            return(false);
        }
        Army army = Army.ArmyInPoint(cur);

        if (army != null && !goer.diplomacy.haveWar(army?.owner.diplomacy))
        {
            return(false);
        }

        return(goer.diplomacy.canMove(s.diplomacy) || prevreg.owner == s);
    }
예제 #2
0
 void FindLand(Vector2Int pos)
 {
     foreach (var d in MapMetrics.OctoDelta)
     {
         Vector2Int  p = pos + d;
         TerrainType land;
         if (MapMetrics.InsideMap(p.y, p.x) && (land = Main.GetTerrainType(p)) != TerrainType.Water)
         {
             if (NearestLand.HasValue)
             {
                 var a = NearestLand.Value - pos;
                 if (Mathf.Abs(a.x) + Mathf.Abs(a.y) > Mathf.Abs(d.x) + Mathf.Abs(d.y))
                 {
                     NearestLand = p;
                 }
             }
             else
             {
                 NearestLand = p;
             }
         }
     }
     if (NearestLand.HasValue && (NearestLand.Value - pos).sqrMagnitude > 2)
     {
         NearestLand = null;
     }
 }
예제 #3
0
    static Vector3 Position(float t)
    {
        t = 0.5f + t * t * 10f;
        float h = MapMetrics.Height(target.x, target.z, true);

        return(new Vector3(0, t * t + h, -t));
    }
예제 #4
0
    public void SetMapMode(MapMode mode)
    {
        if (mode == mapMode)
        {
            return;
        }
        if (mode == MapMode.Terrain)
        {
            terrainMat.SetFloat("_TerrainMode", 1);
            if (!CameraController.showstate)
            {
                Trees.gameObject.SetActive(true);
                foreach (Transform tree in Trees)
                {
                    MapMetrics.UpdateTree(tree.gameObject);
                }
            }
        }

        if (mode == MapMode.Politic)
        {
            terrainMat.SetTexture("_MainTex", regionMap);
            terrainMat.SetTexture("_OccupeMap", occupationMap);
            terrainMat.SetFloat("_TerrainMode", 0);
            Trees.gameObject.SetActive(false);
        }

        mapMode = mode;
    }
예제 #5
0
 public static void ChangeShowState()
 {
     Main.instance.terrainMat.SetFloat("_BorderMod", showstate?1:0);
     for (int i = 0; i < state.Count; i++)
     {
         state[i].SetNameStatus(!showstate);
         foreach (Army army in state[i].army)
         {
             if (!army.Fogged)
             {
                 army.Active = !showstate;
             }
         }
     }
     for (int i = 0; i < regions.Count; i++)
     {
         regions[i].StateBorder = showstate;
     }
     if (Main.mapMode == MapMode.Terrain)
     {
         if (showstate)
         {
             Main.instance.Trees.gameObject.SetActive(false);
         }
         else
         {
             Main.instance.Trees.gameObject.SetActive(true);
             foreach (Transform tree in Main.instance.Trees)
             {
                 MapMetrics.UpdateTree(tree.gameObject);
             }
         }
     }
 }
예제 #6
0
 public void SetOnArmy(Army army)
 {
     Army        = army;
     army.Active = false;
     MapMetrics.UpdateAgentVision(army.curPosition, army.curPosition, army.VisionRadius, -1);
     army.gameObject.SetActive(false);
 }
예제 #7
0
    void BuildWorld()
    {
        CalculateContinentTerritory();
        int tile = MapMetrics.Tile;

        MapMesh.regionIndex = regionIndex;
        MapMesh.regions     = regions;
        for (int i = 0; i < h; i += tile)
        {
            for (int j = 0; j < w; j += tile)
            {
                MapMesh mapMesh = Instantiate(mapMeshPrefab);
                MapMeshes.Add(mapMesh);
                mapMesh.TriangulateMap(j, i);
                mapMesh.transform.localPosition = Vector3.zero;
            }
        }

        SetProvNames();
        SetTowns();
        foreach (var state in states)
        {
            state.Text       = Instantiate(Text);
            state.Text.layer = 8;
            state.SetName();
            state.SetNameStatus(true);
            Region cap = state.Capital;
            cap.Corona = Instantiate(CoronaPrefab, cap.Town.transform);
            cap.Corona.transform.localPosition = Vector3.up * 0.8f;
        }


        for (int i = 0; i < regions.Count; i++)
        {
            regions[i].UpdateBorder();
        }

        regionMap = MapMetrics.map = new Texture2D(w, h, TextureFormat.RGBA32, false);
        MapMetrics.provincemap = new Texture2D(w, h, TextureFormat.RGBA32, false, false);
        occupationMap          = MapMetrics.occupemap = new Texture2D(w, h, TextureFormat.RGBA32, false);

        regionMap.filterMode     = FilterMode.Point;
        occupationMap.filterMode = FilterMode.Point;
        MapMetrics.UpdateColorMap();
        MapMetrics.UpdateOccupedMap();
        MapMetrics.UpdateProvincesMap();
        System.IO.File.WriteAllBytes("DasIst.png", regionMap.EncodeToPNG());
        terrainMat.SetTexture("_OccupeMap", MapMetrics.occupemap);
        terrainMat.SetColor("_Select", Color.white);

        Shader.SetGlobalTexture("_StateColor", MapMetrics.map);
        Shader.SetGlobalTexture("_ProvincesColor", MapMetrics.provincemap);
        Shader.SetGlobalVector("_NoiseConst", new Vector4(MapMetrics.noiseScale, MapMetrics.cellPerturbStrength, 0, 0));
        Shader.SetGlobalTexture("_SplatMap", MapMetrics.GetSplatMap());
        Shader.SetGlobalVector("_Size", new Vector4(1f / w, 1f / h, w, h));
        Shader.SetGlobalTexture("_HeightMap", MapMetrics.GetHeightMap);
        Shader.SetGlobalTexture("_Noise", MapMetrics.noise);
        Shader.SetGlobalFloat("_MaxHeight", MapMetrics.MaxHeight);
        Shader.SetGlobalFloat("_SeaLevel", MapMetrics.SeaLevel);
    }
예제 #8
0
    void BuildPort(Region r)
    {
        Vector2Int loc = r.GetPortPosition();
        Vector2    rot = Vector2.zero;
        int        n   = 0;

        if (MapMetrics.InsideMap(loc.y, loc.x) && regions[regionIndex[loc.y, loc.x]].iswater)
        {
            n++; rot += new Vector2(1, 1);
        }
        if (MapMetrics.InsideMap(loc.y, loc.x - 1) && regions[regionIndex[loc.y, loc.x - 1]].iswater)
        {
            n++; rot += new Vector2(-1, 1);
        }
        if (MapMetrics.InsideMap(loc.y - 1, loc.x) && regions[regionIndex[loc.y - 1, loc.x]].iswater)
        {
            n++; rot += new Vector2(1, -1);
        }
        if (MapMetrics.InsideMap(loc.y - 1, loc.x - 1) && regions[regionIndex[loc.y - 1, loc.x - 1]].iswater)
        {
            n++; rot += new Vector2(-1, -1);
        }
        // rot /= n;
        Port port = Instantiate(PrefabHandler.GetPort(r.owner.fraction), Ports);

        port.transform.position = MapMetrics.PerturbedCorner(loc);
        port.transform.rotation = Quaternion.Euler(0, Vector2.SignedAngle(rot, new Vector2(0, -1)), 0);
        port.CornerPosition     = loc + rot.normalized * 0.5f;
        port.Region             = r;
        r.Port = port;
        port.gameObject.SetActive(false);
    }
예제 #9
0
 void SetProvNames()
 {
     foreach (var reg in regions)
     {
         reg.Text = Instantiate(Text, Names);
         Vector3 pos = MapMetrics.GetCellPosition(reg.Capital, true);
         reg.Text.transform.position = pos;
         reg.Text.layer = 8;
     }
 }
예제 #10
0
 public void CellChange(Vector2Int oldCell, Vector2Int newCell)
 {
     FindLand(newCell);
     if (curOwner == Player.curPlayer)
     {
         MapMetrics.UpdateAgentVision(oldCell, newCell, VisionRadius);
         MapMetrics.UpdateSplatMap();
         Army.FoggedArmy();
     }
 }
예제 #11
0
    public void WhitePeace(State annexator, State target)
    {
        foreach (var reg in annexator.regions)
        {
            if (reg.ocptby == target)
            {
                reg.ocptby = null;
            }
        }
        foreach (var army in annexator.army)
        {
            if (army.navAgent.target?.curOwner == target)
            {
                army.navAgent.target = null;
            }
        }
        foreach (var reg in target.regions)
        {
            if (reg.ocptby == annexator)
            {
                reg.ocptby = null;
            }
        }

        foreach (var army in target.army)
        {
            if (army.navAgent.target?.curOwner == annexator)
            {
                army.navAgent.target = null;
            }
        }

        foreach (var reg in regions)
        {
            reg.UpdateSplateState(curPlayer);
        }
        foreach (var agent in curPlayer.army)
        {
            MapMetrics.UpdateAgentVision(agent.curPosition, agent.curPosition, agent.VisionRadius, 1);
        }
        foreach (var agent in curPlayer.ships)
        {
            MapMetrics.UpdateAgentVision(agent.curPosition, agent.curPosition, agent.VisionRadius, 1);
        }
        if (target.stateAI.autoArmyCommander.IsOn)
        {
            target.stateAI.autoArmyCommander.RecalculateRegions();
        }
        if (annexator.stateAI.autoArmyCommander.IsOn)
        {
            annexator.stateAI.autoArmyCommander.RecalculateRegions();
        }
        MapMetrics.UpdateOccupedMap();
        MapMetrics.UpdateSplatMap();
    }
        /// <summary>
        /// Constructor of the ShapIndex class.  It reads
        /// the .shp & .shx files and sets up the feature data structures. It 
        /// parses the header of the .shx file, reads the entire .shx file and 
        /// then reads the .shp file.
        /// </summary>
        /// @param filename The filename the shapefile we want to load.
        /// @param metrics The MapMetrics object for this Map.
        public ShapeIndex( string filename, MapMetrics metrics )
        {
            _Filename		= filename;
            _mapMetrics		= metrics;
            _extents		= new RectangleD();

            // 1.. 2.. 3.. 4..  8^)
            ParseHeader( filename );// .shx file
            ReadIndex( filename ); // .shx file
            LoadFile(); // .shp file
            SetupLabels();
        }
예제 #13
0
    public static void CalculateTownCollide(NavAgent agent)
    {
        Vector2 p = agent.pos;

        Vector2Int cell   = NavAgent.ToInt(p);
        Region     region = agent.lastCollidedTown;

        bool Collide()
        {
            if ((region.pos - p).sqrMagnitude < townRadiusSqr)
            {
                if (region.curOwner == agent.owner)
                {
                    agent.InTown = true;
                    return(true);
                }
                Vector2 n = (p - region.pos);
                agent.CollideForce += n.normalized * (1f - n.sqrMagnitude / townRadiusSqr);

                return(true);
            }
            return(false);
        }

        if (region != null)
        {
            if (Collide())
            {
                return;
            }
        }
        region = MapMetrics.GetRegion(cell);
        if (region.Capital == cell)
        {
            if (Collide())
            {
                return;
            }
        }
        foreach (var d in MapMetrics.OctoDelta)
        {
            if ((region = MapMetrics.GetRegion(cell + d))?.Capital == cell + d)
            {
                if (Collide())
                {
                    return;
                }
            }
        }
        agent.InTown           = false;
        agent.lastCollidedTown = null;
    }
예제 #14
0
    public static void SetState(State state)
    {
        if (state != null)
        {
            MenuManager.SetState(state);
        }

        curPlayer = state;
        foreach (var r in curPlayer.regions)
        {
            if (r.data.portLevel != 0)
            {
                Ship.CreateShip(r);
            }
        }
        foreach (Region r in regions)
        {
            r.UpdateSplateState(curPlayer);
            r.UpdateBorder();
        }
        for (int i = 0; i < states.Count; i++)
        {
            states[i].SetNameStatus(!CameraController.showstate);
            states[i].stateAI.autoBuilder.IsOn         = (state != states[i]);
            states[i].stateAI.autoReasercher.IsOn      = (state != states[i]);
            states[i].stateAI.autoTrader.IsOn          = (state != states[i]);
            states[i].stateAI.autoRegimentBuilder.IsOn = (state != states[i]);
            states[i].stateAI.autoArmyCommander.IsOn   = (state != states[i]);
            states[i].stateAI.autoPersonControl.IsOn   = (state != states[i]);
            states[i].stateAI.autoDiplomacy.IsOn       = (state != states[i]);
        }
        foreach (var army in Army.AllArmy)
        {
            army.GetComponent <ArmyAI>().enabled = army.owner != state;
        }
        foreach (var army in state.army)
        {
            MapMetrics.UpdateAgentVision(army.curPosition, army.curPosition, army.VisionRadius, 1);
        }
        foreach (var ship in state.ships)
        {
            MapMetrics.UpdateAgentVision(ship.curPosition, ship.curPosition, ship.VisionRadius, 1);
        }
        CameraController.SetTarget(curPlayer.Capital.pos);
        StateStatistic.ShowStates();
        Army.FoggedArmy();
        MapMetrics.UpdateSplatMap();
        MenuManager.HiddenProvinceMenu();
        GameTimer.UpdateListenerQueue();
    }
예제 #15
0
    public void Annexation(State annexator, State target, List <Region> land)
    {
        foreach (var war in target.diplomacy.war)
        {
            foreach (var reg in war.Enemy(target).regions)
            {
                if (reg.ocptby == target)
                {
                    reg.ocptby = null;
                }
            }
        }
        foreach (var reg in land)
        {
            reg.owner.regions.Remove(reg);
            reg.owner  = annexator;
            reg.ocptby = null;
            annexator.regions.Add(reg);
        }
        foreach (var reg in regions)
        {
            reg.UpdateSplateState(curPlayer);
        }

        /*foreach (var agent in curPlayer.army)
         *  MapMetrics.UpdateAgentVision(agent.curPosition, agent.curPosition, agent.VisionRadius, 1);
         * foreach (var agent in curPlayer.ships)
         *  MapMetrics.UpdateAgentVision(agent.curPosition, agent.curPosition, agent.VisionRadius, 1);*/
        annexator.SetName();
        if (target.regions.Count == 0)
        {
            target.DestroyState();
        }
        else
        {
            target.SetName();
        }
        if (target.stateAI.autoArmyCommander.IsOn)
        {
            target.stateAI.autoArmyCommander.RecalculateRegions();
        }
        if (annexator.stateAI.autoArmyCommander.IsOn)
        {
            annexator.stateAI.autoArmyCommander.RecalculateRegions();
        }
        MapMetrics.UpdateColorMap();
        MapMetrics.UpdateOccupedMap();
        MapMetrics.UpdateSplatMap();
    }
예제 #16
0
    public void TriangulateMap(int x0, int y0)
    {
        List <int>     triangles = new List <int>();
        List <Vector2> uvs = new List <Vector2>(), uvs1 = new List <Vector2>();
        List <Vector3> vertices = new List <Vector3>();

        for (int i = y0; i < y0 + tile + 1; i++)
        {
            for (int j = x0; j < x0 + tile + 1; j++)
            {
                uvs.Add(new Vector2(1f * j / MapMetrics.SizeM, 1f * i / MapMetrics.SizeN));
                Vector3 v = MapMetrics.PerturbedCorner(new Vector2Int(j, i));
                Navigation.field[i + y0, j + x0] = new Vector2(v.x, v.z);
                vertices.Add(new Vector3(v.x, v.y, v.z));
                uvs1.Add(new Vector2(v.x, v.z));
            }
        }
        for (int i = 0; i < tile; i++)
        {
            for (int j = 0; j < tile; j++)
            {
                int a = i * (tile + 1) + j;
                int b = a + tile + 1;
                triangles.Add(a);
                triangles.Add(b);
                triangles.Add(b + 1);

                triangles.Add(b + 1);
                triangles.Add(a + 1);
                triangles.Add(a);
            }
        }
        mapMesh.SetVertices(vertices);
        mapMesh.SetUVs(0, uvs);
        mapMesh.SetUVs(1, uvs1);
        mapMesh.SetTriangles(triangles, 0);
        ListPool <Vector3> .Add(vertices);

        ListPool <int> .Add(triangles);

        ListPool <Vector2> .Add(uvs);

        ListPool <Vector2> .Add(uvs1);

        mapMesh.RecalculateNormals();
        mapMesh.RecalculateBounds();
        GetComponent <MeshCollider>().sharedMesh = mapMesh;
    }
        /// <summary>
        /// The constructor for the Shapefile class initializes the base class
        /// ShapeIndex and sets up the protected members of the class needed
        /// for implementing the IMapLayer interface.
        /// </summary>
        /// @param filename The filename the shapefile we want to load.
        /// @param metrics The MapMetrics object for this Map.
        public Shapefile( string filename, MapMetrics metrics )
            : base(filename, metrics)
        {
            _db					= new Dbase( filename );
            _pen				= new Pen( Color.Black  );
            _brush				= new SolidBrush( Color.Tan );
            _labelSourceField	= 0;
            _labelForeColor		= Color.Black;
            _labelFillColor		= Color.White;
            _labelFont			= new Font( "Tahoma", 8 );
            _labelBackground	= true;
            _highlightedFeature	= -1;
            _layerName			= _Filename;

            drawThreads			= new RenderThread[ NUM_THREADS ];
            threads				= new Thread[ NUM_THREADS ];
            threadBoundary		= _features.Length / NUM_THREADS; // integer division, I know.
        }
예제 #18
0
 public bool CanDesant(Vector3 point)
 {
     if (!NearestLand.HasValue || Army == null)
     {
         return(false);
     }
     Army.navAgent.curCell = NearestLand.Value;
     Army.navAgent.pos     = NearestLand.Value;
     if (Army.TryMoveTo(point))
     {
         Army.Active = true;
         Army.gameObject.SetActive(true);
         MapMetrics.UpdateAgentVision(Army.curPosition, Army.curPosition, Army.VisionRadius, 1);
         Army = null;
         return(true);
     }
     return(false);
 }
예제 #19
0
    private void ShipInitialization(Region region)
    {
        Vector2 v = region.Port.CornerPosition;

        SetPosition(v);
        curOwner = region.owner;

        navAgent         = gameObject.AddComponent <NavAgent>();
        navAgent.pos     = v;
        navAgent.curCell = new Vector2Int((int)v.x, (int)v.y);
        FindLand(navAgent.curCell);
        navAgent.SetToMovable(this);
        Select(false);
        if (Player.curPlayer == curOwner)
        {
            MapMetrics.UpdateAgentVision(navAgent.curCell, navAgent.curCell, VisionRadius, 1);
        }
    }
예제 #20
0
    public static bool isPossibleMove(Vector2Int from, Vector2Int to, State goer)
    {
        Region startRegion = MapMetrics.GetRegion(from);
        Region endRegion   = MapMetrics.GetRegion(to);

        if (startRegion == endRegion)
        {
            return(true);
        }
        else if (startRegion.Continent != endRegion.Continent)
        {
            return(false);
        }

        attended.Clear();
        visitedRegions.Clear();

        attended.Enqueue(startRegion);
        visitedRegions.Add(startRegion);

        while (attended.Count != 0)
        {
            Region invest = attended.Dequeue();

            foreach (var neib in invest.neib)
            {
                if (neib.iswater)
                {
                    continue;
                }
                if (neib == endRegion)
                {
                    return(goer == neib.curOwner || goer.diplomacy.canMove(neib.curOwner.diplomacy));
                }
                if (!visitedRegions.Contains(neib) && (goer == neib.curOwner || goer.diplomacy.canMove(neib.curOwner.diplomacy)))
                {
                    attended.Enqueue(neib);
                    visitedRegions.Add(neib);
                }
            }
        }

        return(false);
    }
예제 #21
0
    public void StartGame(byte SeaLevel, Texture2D[] landTex, Texture2D[] landnorm, byte[] terrain, byte[] HeightArr, int[,] regIndex, List <Region> region, List <State> st, List <List <Vector2Int> > river)
    {
        MapMetrics.SizeN  = h = regIndex.GetLength(0);
        MapMetrics.SizeM  = w = regIndex.GetLength(1);
        seaLevel          = SeaLevel;
        TerrainMask       = landTex;
        TerrainNormalMask = landnorm;
        terrainIndexes    = terrain;
        River             = river;
        MapMetrics.noise  = noiseSorse;
        MapMetrics.SetSplatMap(new Texture2D(w, h, TextureFormat.RGBA32, false));
        MapMetrics.SetHeights(HeightArr, h, w);
        Navigation.Init(h, w);
        regionIndex = Player.regionIndex = MapMetrics.cellStateIndex = regIndex;

        regions = CameraController.regions = Player.regions =
            MapMetrics.regions = region;
        states = CameraController.state = CodePanel.state = Player.states = st;
        MapMetrics.SeaLevel = seaLevelf = MapMetrics.MaxHeight * SeaLevel / 255f;
        distantion          = new float[h, w];
        parent = new Vector2Int[h, w];
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                distantion[i, j] = -1;//not used
            }
        }
        mapMode = MapMode.None;
        MenuManager.SetMiniMap();

        transform.GetChild(0).transform.position = new Vector3(w / 2, seaLevelf - 0.01f, h / 2);

        CreateWorldBorder();

        BuildWorld();
        CreateTerrain();
        CreateArmy();
        SetMapMode(MapMode.Politic);
        CameraController.SetTarget(st[0].Capital.position);
        MenuManager.StartTimer();
        Player.SetState(st[0]);
    }
예제 #22
0
    void CreateTerrain()
    {
        TerrainMaskArray       = new Texture2DArray(w, h, 4, TerrainMask[0].format, TerrainMask[0].mipmapCount > 0);
        TerrainNormalMaskArray = new Texture2DArray(w, h, 4, TerrainNormalMask[0].format, TerrainNormalMask[0].mipmapCount > 0);

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < TerrainMask[i].mipmapCount; j++)
            {
                Graphics.CopyTexture(TerrainMask[i], 0, j, TerrainMaskArray, i, j);
                Graphics.CopyTexture(TerrainNormalMask[i], 0, j, TerrainNormalMaskArray, i, j);
            }
        }
        TerrainMaskArray.Apply();
        TerrainNormalMaskArray.Apply();
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                int t = terrainIndexes[i * w + j] & 31;
                if (t == (int)TerrainType.ForestLeaf || t == (int)TerrainType.ForestSpire)
                {
                    GameObject tree = Instantiate(terrainItem[t - (int)TerrainType.ForestLeaf], Trees);
                    tree.transform.position = MapMetrics.GetCellPosition(i, j);
                }
            }
        }
        foreach (var list in River)
        {
            GameObject r = MapMesh.AddRiver(list);
            r.transform.SetParent(WorldBorders.transform);
            r.GetComponent <MeshRenderer>().sharedMaterial = riverMat;
        }



        terrainMat.SetTexture("_TerrainNormTex", TerrainNormalMaskArray);
        terrainMat.SetTexture("_TerrainTex", TerrainMaskArray);
    }
예제 #23
0
    public void Occupated(Region r, State occupant)
    {
        r.Destoyed = false;
        State realOwner = r.owner;
        State curOwner  = r.curOwner;

        if (occupant != r.owner)
        {
            r.ocptby = occupant;

            Debug.Log(string.Format("Страна {0} захватила {1}", occupant.name, r.name));
        }
        else
        {
            r.ocptby = null;

            Debug.Log(string.Format("Страна {0} вернула {1}", occupant.name, r.name));
        }

        if (realOwner.stateAI.autoArmyCommander.IsOn)
        {
            realOwner.stateAI.autoArmyCommander.RecalculateRegions();
        }

        AutoArmyCommander autoArmy = realOwner.stateAI.autoArmyCommander;

        if (autoArmy.IsOn)
        {
            autoArmy.RecalculateRegions();
        }


        if (!r.InFogFrom(curPlayer))
        {
            MapMetrics.SetRegionSplatState(r.territory, LandShowMode.Visible);
        }
        MapMetrics.UpdateOccupedMap();
        MapMetrics.UpdateSplatMap();
    }
예제 #24
0
    void SetTowns()
    {
        foreach (var reg in regions)
        {
            if (!reg.iswater)
            {
                GameObject go = new GameObject(reg.id.ToString());
                reg.Town = go;
                go.tag   = "Town";
                go.AddComponent <SphereCollider>().radius = 0.71f;
                go.transform.SetParent(Towns);
                go.layer = 9;
                GameObject town = new GameObject();
                town.transform.SetParent(go.transform);
                town.transform.localPosition = Vector3.zero;

                town = Instantiate(flagPrefab, go.transform);
                town.transform.localPosition = Vector3.zero;
                reg.flag = town.transform;
                reg.pos  = NavAgent.FromV3(go.transform.position = MapMetrics.RayCastedGround(reg.Capital + Vector2.one * 0.5f));
                go.transform.rotation = Quaternion.Euler(0, UnityEngine.Random.Range(-90, 90), 0);
                go.transform.GetChild(1).localRotation = Quaternion.Inverse(go.transform.rotation);
                if (reg.portIdto >= 0)
                {
                    BuildPort(reg);
                }
                else
                {
                    reg.data.buildings[(int)BuildingType.Port] = 0;
                }

                TownBar bar = Instantiate(PrefabHandler.GethpTownBarPrefab, instance.mainCanvas);
                reg.bar           = bar;
                bar.currentRegion = reg;
                reg.RebuildTown();
            }
        }
    }
예제 #25
0
    public void SetName()
    {
        Vector2Int        left, center, right;
        List <Vector2Int> ter = new List <Vector2Int>();

        for (int i = 0; i < regions.Count; i++)
        {
            ter.Add(regions[i].Capital);
        }
        MapMetrics.CalculLCR(ter, out left, out center, out right);

        Vector3 l = MapMetrics.GetCellPosition(left, true);
        Vector3 c = MapMetrics.GetCellPosition(center, true);
        Vector3 r = MapMetrics.GetCellPosition(right, true);

        l -= c; r -= c;
        l  = new Vector3(l.x, l.z, l.y);
        r  = new Vector3(r.x, r.z, r.y);


        left  -= center;
        right -= center;
        float maxd = left.magnitude + right.magnitude;
        float maxS = maxd * 2.4f / name.Length;
        int   S    = Mathf.RoundToInt(regions.Count * 0.6f);

        if (maxS < 1 || S < 1)
        {
            Text.SetActive(false);
            return;
        }
        int fontSize = (int)Mathf.Clamp((4 + Mathf.Clamp(S, 0, 10)), 3, maxS);

        Text.transform.position = c;
        Text.transform.SetParent(Main.instance.Names);

        Text.GetComponent <CurvedText>().SetProperies(name, l, r, fontSize);
    }
        /// <summary>
        /// The constructor for the RenderThread. Sets up the private members
        /// of the class, but DOES NOT start the rendering process.
        /// </summary>
        /// @param metrics The MapMetrics object for this Map
        /// @param features The VectorFeature array
        /// @param type The type of shapefile we're going to draw
        /// @param beginning The array index of the VectorFeature to begin drawing with
        /// @param ending The array index of the VectorFeature to end drawing with
        /// @param pen The pen to draw the features with
        /// @param brush The brush to fill the features with
        public RenderThread( MapMetrics metrics, 
							 VectorFeature[] features,
							 ShapeType type,
							 int beginning,
							 int ending,
							 Pen pen,
							 Brush brush )
        {
            _mapMetrics = metrics;
            _features = features;
            _shapeType = type;
            _beginningFeature = beginning;
            _endingFeature = ending;
            _pen = (Pen) pen.Clone();
            _brush = (Brush) brush.Clone();
            _gr = Graphics.FromImage( _mapMetrics.Canvas );
        }
예제 #27
0
    public static GameObject AddRiver(List <Vector2Int> point)
    {
        List <int> triangles = ListPool <int> .Get();

        List <Vector3> vertices = ListPool <Vector3> .Get();

        List <Vector2> uvs = ListPool <Vector2> .Get();

        List <Vector3> bord = ListPool <Vector3> .Get();

        Vector2Int d1, d2;

        d1 = point[1] - point[0];

        bord.Add(MapMetrics.GetCellPosition(point[0]));
        bord.Add(0.5f * (bord[0] + MapMetrics.GetCellPosition(point[1])));
        for (int i = 1; i < point.Count - 1; i++)
        {
            d2 = point[i + 1] - point[i];
            bord.Add(MapMetrics.GetCellPosition(point[i]));
            if (d1.x * d2.x + d1.y * d2.y == 0)
            {
                d1 = d2 - d1;
                bord[bord.Count - 1] += 0.25f * new Vector3(d1.x, 0, d1.y);
            }
            bord.Add(0.5f * (MapMetrics.GetCellPosition(point[i]) + MapMetrics.GetCellPosition(point[i + 1])));
            d1 = d2;
        }
        for (int i = 0; i < bord.Count; i++)
        {
            bord[i] += Vector3.up * 0.1f;
        }
        Vector3 dir1, dir2, norm = Vector3.zero;
        float   width = 0.1f;

        vertices.Add(bord[0] + norm * width);
        vertices.Add(bord[0] - norm * width);
        uvs.Add(new Vector2(0, 0));
        uvs.Add(new Vector2(0, 1));
        for (int i = 1; i < bord.Count; i++)
        {
            if (i < bord.Count - 1)
            {
                dir1 = bord[i] - bord[i - 1];
                dir2 = bord[i + 1] - bord[i];
                norm = (Vector3.Cross(dir1, Vector3.up) + Vector3.Cross(dir2, Vector3.up)).normalized;
            }
            vertices.Add(bord[i] + norm * width);
            vertices.Add(bord[i] - norm * width);
            uvs.Add(new Vector2(i, 0));
            uvs.Add(new Vector2(i, 1));

            triangles.Add(i * 2);
            triangles.Add(i * 2 - 1);
            triangles.Add(i * 2 - 2);

            triangles.Add(i * 2 + 1);
            triangles.Add(i * 2 - 1);
            triangles.Add(i * 2);
        }

        Mesh       mesh;
        GameObject river = new GameObject();

        river.AddComponent <MeshRenderer>();
        river.AddComponent <MeshFilter>().mesh = mesh = new Mesh();

        mesh.SetVertices(vertices);
        mesh.SetUVs(0, uvs);

        mesh.SetTriangles(triangles, 0);
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        ListPool <Vector3> .Add(vertices);

        ListPool <int> .Add(triangles);

        ListPool <Vector2> .Add(uvs);

        ListPool <Vector2Int> .Add(point);

        ListPool <Vector3> .Add(bord);

        return(river);
    }
예제 #28
0
 public void SetPosition(Vector2 v)
 {
     transform.position = MapMetrics.GetPosition(v, true);
 }
예제 #29
0
    void Update()
    {
        CursorType cursor = CursorType.Default;

        if (!EventSystem.current.IsPointerOverGameObject())
        {
            if (CastingSkill)
            {
                cursor = CursorType.CastSkill;
            }
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                int x = (int)hit.point.x;
                int y = (int)hit.point.z;
                if (hit.transform.tag == "Map")
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        if (!MapMetrics.InsideMap(y, x))
                        {
                            if (curRegion != null)
                            {
                                curRegion.Selected = false;
                            }
                            curRegion = null;
                            if (army != null)
                            {
                                DeselectArmy();
                            }
                        }
                        else
                        {
                            RegionTap(Main.regions[regionIndex[y, x]]);
                        }
                    }

                    if (Input.GetMouseButtonDown(1))
                    {
                        army?.TryMoveTo(hit.point);
                        ship?.TryMoveTo(hit.point);
                        if (curPort)
                        {
                            curPort.ShipOut(hit.point);
                        }
                    }
                }
                State      other  = null;
                IFightable target = null;
                if (hit.transform.tag == "Unit")
                {
                    Army sel = hit.transform.GetComponent <Army>();
                    if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
                    {
                        ArmyTap(sel, Input.GetMouseButtonDown(0));
                    }
                    other  = sel.curOwner;
                    target = sel;
                    if (CastingSkill)
                    {
                        if (CastSkill is IArmyCastable cast)
                        {
                            if (cast.CanCastOnArmy(sel))
                            {
                                cursor = CursorType.CanCastSkill;
                            }
                        }
                    }
                    else
                    {
                        if (other == curPlayer && sel != army)
                        {
                            cursor = CursorType.Select;
                        }
                    }
                }
                if (hit.transform.tag == "Ship")
                {
                    Ship sel = hit.transform.GetComponent <Ship>();
                    if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
                    {
                        ShipTap(sel, Input.GetMouseButtonDown(0));
                    }
                    other = sel.curOwner;
                    if (CastingSkill)
                    {
                    }
                    else
                    {
                        if (other == curPlayer && sel != ship)
                        {
                            cursor = CursorType.Select;
                        }
                    }
                }
                if (hit.transform.tag == "Town")
                {
                    Region reg = MapMetrics.regions[int.Parse(hit.transform.name)];
                    highlightRegion = reg;
                    TownTap(reg, Input.GetMouseButtonDown(0), Input.GetMouseButtonDown(1));
                    reg.bar.Active = true;
                    other          = reg.curOwner;
                    target         = reg;
                    if (CastingSkill)
                    {
                        if (CastSkill is IRegionCastable cast)
                        {
                            if (cast.CanCastOnRegion(reg))
                            {
                                cursor = CursorType.CanCastSkill;
                            }
                        }
                    }
                    else
                    {
                        if (other == curPlayer && reg != curRegion)
                        {
                            cursor = CursorType.Select;
                        }
                    }
                }
                else
                {
                    if (highlightRegion != null)
                    {
                        highlightRegion.bar.Active = false;
                    }
                }
                if (hit.transform.GetComponent <Port>())
                {
                    Port port = hit.transform.GetComponent <Port>();
                    if (Input.GetMouseButtonDown(0) && port.curOwner == curPlayer && port.Ship)
                    {
                        curPort = port;
                        DeselectShip();
                    }
                    if (Input.GetMouseButtonDown(1) && ship && port.CanshipOn(ship))
                    {
                        ship.TryMoveTo(port);
                    }
                }
                if (!CastingSkill && target != null && army != null && army.CanAttack(target, TargetType))
                {
                    switch (TargetType)
                    {
                    case DamageType.Melee:
                    case DamageType.Charge: cursor = CursorType.MeleeAttack; break;

                    case DamageType.Range: cursor = CursorType.RangeAttack; break;

                    case DamageType.Siege: cursor = CursorType.SiegeAttack; break;
                    }
                }
            }
        }
        if (curRegion == null)
        {
            MenuManager.HiddenProvinceMenu();
        }
        if (cursor != cursorType)
        {
            cursorType = cursor;
            CursorHandler.SetCursor(cursorType);
        }
    }
예제 #30
0
 public static void Save(string path)
 {
     SaveLoad.Save(seaLevel, TerrainMask, TerrainNormalMask, terrainIndexes, MapMetrics.GetHeightsArray(), regionIndex, regions, states, path);
 }
예제 #31
0
    public static List <Vector2Int> FindPath(Vector2Int from, Vector2Int to, State goer)
    {
        if (MapMetrics.GetRegion(from).Continent != MapMetrics.GetRegion(to).Continent)
        {
            return(null);
        }
        Node prev = new Node(Heuristic(from, to), from), next;

        distantion[from.y, from.x] = 0;
        Vector2Int pos;

        pq.Enqueue(prev);
        float dnext = 1000, dprev = CellMoveCost(from), dist;
        bool  find = false;

        while (pq.Count != 0)
        {
            prev = pq.Dequeue();
            used.Enqueue(prev.pos);
            if (prev.pos == to)
            {
                find = true;
                break;
            }
            dprev = CellMoveCost(prev.pos);
            for (int i = 0; i < 8; i++)
            {
                pos  = new Vector2Int(dx[i], dy[i]) + prev.pos;
                next = new Node(Heuristic(prev.pos, pos), pos);

                if (MapMetrics.InsideMap(pos.y, pos.x) && (dnext = CellMoveCost(pos)) < 100 && CanMoveTo(prev.pos, pos, goer, pos == to))
                {
                    dist = distantion[prev.pos.y, prev.pos.x] + ((i & 1) == 0 ? 0.5f : 0.7f) * (dnext + dprev);
                    if (distantion[pos.y, pos.x] >= 0 && distantion[pos.y, pos.x] <= dist)
                    {
                        continue;
                    }
                    if (distantion[pos.y, pos.x] < 0 || distantion[pos.y, pos.x] > dist)
                    {
                        if (distantion[pos.y, pos.x] < 0)
                        {
                            pq.Enqueue(new Node(Heuristic(prev.pos, next.pos) + dist, next.pos));
                        }
                        distantion[pos.y, pos.x] = dist;
                        parent[pos.y, pos.x]     = prev.pos;
                    }
                }
            }
        }

        List <Vector2Int> path = null;

        if (find)
        {
            path = ListPool <Vector2Int> .Get();

            while (from != to)
            {
                path.Add(to);
                to = parent[to.y, to.x];
            }
            //path.Add(to);without fromPoint
            path.Reverse();
        }
        while (used.Count != 0)
        {
            pos = used.Dequeue();
            distantion[pos.y, pos.x] = -1;
        }
        while (pq.Count != 0)
        {
            pos = pq.Dequeue().pos;
            distantion[pos.y, pos.x] = -1;
        }
        return(path);
    }
예제 #32
0
 public static void SetTarget(Vector2 pos)
 {
     p0 = MapMetrics.GetPosition(pos);
     ChangeShowState();
 }
예제 #33
0
 /// <summary>
 /// 初始化地图碰撞体
 /// </summary>
 private void InitMapCollider(Vector2Int mapSize)
 {
     var(ColliderWidth, ColliderHeight) = MapMetrics.GetMapColliderSize(mapSize);
     this.MapCollider.size   = new Vector3(ColliderWidth, ColliderHeight, 0.1f);
     this.MapCollider.center = new Vector3(ColliderWidth * 0.5f - 0.5f, ColliderHeight * 0.5f - 0.5f);
 }