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; } }
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); }
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); } }
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); }