/// <summary> /// 获取几何对象的坐标点信息 /// </summary> /// <param name="feature"></param> /// <param name="type"></param> /// <returns></returns> internal static dynamic GetCoordinates(this IFeature feature, GeoJSONType type) { dynamic coords = null; switch (type) { case GeoJSONType.Point: Coord1 coord1 = new Coord1(); coords = coord1.GetCoords(feature); break; case GeoJSONType.MultiPoint: case GeoJSONType.LineString: Coord2 coord2 = new Coord2(); coords = coord2.GetCoords(feature); break; case GeoJSONType.MultiLineString: Coord3 coord31 = new Coord3(); coords = coord31.GetCoords(feature); break; case GeoJSONType.Polygon: Coord3 coord32 = new Coord3(); coords = coord32.GetClosedCoords(feature); break; default: return(null); } return(coords); }
/// <summary> /// 放置建筑 /// </summary> /// <param name="buildingId"></param> /// <param name="rotation">向某方向旋转多次,大于0为逆时针,小于0为顺时针</param> /// <param name="center">建筑的中心点</param> public bool PutBuilding(int buildingId, int rotation, Coord2 center) { if (buildingId == 0) { return(false); } var buildingInfo = _BuildingInfos[buildingId]; if (!GlobalResource.Ins.CostCheck(buildingInfo.Cost)) { return(false); } var offsetPos = buildingInfo.GetRotatedCenterPos(rotation); var rot = Quaternion.AngleAxis(-90 * rotation, Vector3.up); GameObjectPool.GetPrefab(buildingInfo.PrefabPath) .GetInstantiate() .GetComponent <BaseBuilding>() .Install(GetPos(center) + offsetPos, rot); foreach (var coord in buildingInfo.GetRotatedCoords(rotation)) { var newCoord = coord + center; var cell = new Cell() { BuildingId = buildingId }; SetCell(newCoord, cell); } GlobalResource.Ins.Cost(buildingInfo.Cost); return(true); }
public static Direction MakeDirection(Coord2 co1, Coord2 co2) { var co = co2 - co1; Direction lr = Direction.NONE; if (co.x < 0) { lr = Direction.LEFT; } else if (co.x > 0) { lr = Direction.RIGHT; } Direction tb = Direction.NONE; if (co.y < 0) { tb = Direction.BOTTOM; } else if (co.y > 0) { tb = Direction.TOP; } return((Direction)((byte)lr | (byte)tb)); }
private DropMode GetDropMode(Coord2 co, int turn) { if(!IsInside(co)) { return DropMode.STUCK; } var cell = GetCell(co); if(cell == null || !cell.CanDrop) { return DropMode.STUCK; } // 说明已经处理掉落过了,等待下一次处理 if(cell.drop == turn) { return DropMode.WAIT; } if(cell.IsEmpty) { if(cell.HasFlag(CellFlag.Hopeless)) { return DropMode.STUCK; } else { return DropMode.WAIT; } } return DropMode.DROP; }
//public CornerDir startCorner, endCorner; public LineSpan(int startX, int startY, int endX, int endY /*, CornerDir startCorner, CornerDir endCorner*/) { start = new Coord2(startX, startY); end = new Coord2(endX, endY); // this.startCorner = startCorner; // this.endCorner = endCorner; }
public ElementType GetElement(Coord2 co) { string[] elements = GetLayer(ElementLayer.OBJECT); int idx = co.y * width + co.x; ElementType element = Util.ParseEnum <ElementType>(elements[idx]); return(element); }
public bool Equals(Coord2 obj) { if (obj == null) { return(false); } return((this.x == obj.x) && (this.y == obj.y)); }
private void LoadGraphics() { var bottomleft = new PolarGraphicCoordinate(Coord1.Target, Width, Coord1.PerpendicularNegative().AngleDegree); var topleft = new PolarGraphicCoordinate(Coord1.Target, Width, Coord1.PerpendicularPositive().AngleDegree); var topright = new PolarGraphicCoordinate(Coord2.Origin, Width, Coord2.PerpendicularPositive().AngleDegree); var bottomright = new PolarGraphicCoordinate(Coord2.Origin, Width, Coord2.PerpendicularNegative().AngleDegree); _square = new Square(bottomleft.Target, topleft.Target, topright.Target, bottomright.Target, Color); }
public ElementType Drop(Coord2 co) { int idx = 0; _points.TryGetValue(co, out idx); return(_creators[idx].Create()); //var value = UnityEngine.Random.Range(0, 5); //return ElementType.NORMAL_A + value; }
public void AddDrop(int turn, bool isGen, Coord2 from, Coord2 to) { var info = new Move(); info.turn = turn; info.isGen = isGen; info.from = from; info.to = to; _drops.Add(info); }
private void Awake() { Ins = this; _Map = new Cell[MapSize.x, MapSize.y]; _MapCenter = MapSize / 2; if (_BuildingInfos == null) { LoadBuildingInfos(); } PutBaseBuildings(); }
public override bool Equals(object obj) { Coord2 co = (Coord2)obj; if (co.x == this.x && co.y == this.y) { return(true); } return(false); }
// 计算传送门 public Coord2 GetTop(Coord2 co) { Coord2 result; if (this.mOut.Count > 0 && this.mOut.TryGetValue(co, out result)) { return(result); } return(co + Direction.TOP); }
private void AddQuadUV(int ind, int rot) { Coord2 a = Coord2.TileCorners[(-rot + 4) % 4], b = Coord2.TileCorners[(-rot + 5) % 4], c = Coord2.TileCorners[(-rot + 6) % 4], d = Coord2.TileCorners[(-rot + 7) % 4]; uvs.Add(new Vector3(a.x, a.y, ind)); uvs.Add(new Vector3(b.x, b.y, ind)); uvs.Add(new Vector3(c.x, c.y, ind)); uvs.Add(new Vector3(d.x, d.y, ind)); }
// 交换 public bool TrySwap(Coord2 co1, Coord2 co2) { var c1 = GetCell(co1); var c2 = GetCell(co2); if(!c1.CanSwap || !c2.CanDrop) { return false; } Matcher.Match(mCells, new Cell[] { c1, c2 }); // process return true; }
public static void LoadChunk(string worldSave, Coord2 pos, Chunk chunk) { string saveFile = FolderName(worldSave) + "chunks/" + FileName(pos); // using(var stream = new LZ4Stream(File.Open(saveFile, FileMode.Open), LZ4StreamMode.Decompress)) // using(var reader = new BinaryReader(stream)) { // chunk.Deserialize(reader); // } using (var stream = File.Open(saveFile, FileMode.Open)) using (var reader = new BinaryReader(stream)) { chunk.Deserialize(reader); } }
/// Gets a chunk from the pool and initializes it public Chunk CreateChunk(Coord2 coord) { var chunk = GetChunk(coord); if (chunk != null) { return(chunk); } chunk = pool.GetObject(); chunk.Setup(coord); chunks.Add(coord, chunk); return(chunk); }
public static void SaveChunk(string worldSave, Coord2 pos, Chunk chunk) { string saveFile = FolderName(worldSave) + "chunks/" + FileName(pos); // using(var stream = new LZ4Stream(File.Open(saveFile, FileMode.Create, FileAccess.Write), LZ4StreamMode.Compress)) // using(var writer = new BinaryWriter(stream)) { // chunk.Serialize(writer); // } using (var stream = File.Open(saveFile, FileMode.Create, FileAccess.Write)) using (var writer = new BinaryWriter(stream)) { chunk.Serialize(writer); } }
public override bool Equals(object obj) { if (obj == null) { return(false); } Coord2 asCoord2 = obj as Coord2; if ((object)asCoord2 == null) { return(false); } return((this.x == asCoord2.x) && (this.y == asCoord2.y)); }
/// Setup this chunk pulled from the pool public void Setup(Coord2 position) { this.position = position; IsBuilt = false; IsGenerated = false; IsRendered = false; IsDirty = false; transform.parent = world.transform; transform.localPosition = (Coord3)position * ChunkSection.Size; name = "Chunk " + position; for (int i = 0; i < chunks.Length; i++) { var pos = new Coord3(position.x, i, position.y); chunks[i].Setup(pos); } }
private void SortCoords(float gridSize) { if (Coords == null || Coords.Length == 0) { return; } var min = new Coord2(int.MaxValue, int.MaxValue); var max = new Coord2(int.MinValue, int.MinValue); foreach (var coord2 in Coords) { if (coord2.x < min.x) { min.x = coord2.x; } if (coord2.y < min.y) { min.y = coord2.y; } if (coord2.x > max.x) { max.x = coord2.x; } if (coord2.y > max.y) { max.y = coord2.y; } } var center = (min + max) / 2; var offset = (min.ToVector2() + max.ToVector2()) * gridSize / 2; CenterPosOffset = (offset - center.ToVector2() * gridSize).PutDown(); for (int i = 0; i < Coords.Length; i++) { Coords[i] -= center; } }
public void GenerateShape(float divDuration, float divSize, DateTime zeroTime, Transform parent, Material baseMat) { DateTime now = DateTime.Now; List <Vector3> verts = new List <Vector3>(); List <int> tris = new List <int>(); pointList = new List <Coord2>(); if (shape) { Destroy(shape.gameObject); } if (guideSpan != SpanAlignment.None) { labels.DestroyAll(); } GameObject shapeObject = new GameObject(name); shape = shapeObject.transform; shapeObject.transform.parent = parent; // Debug.Log( "Generated color for " + name + ": " + color ); LineRenderer edgePrefabToUse = instance.edgePrefab; if (guideSpan == SpanAlignment.Months) { edgePrefabToUse = instance.monthEdgePrefab; } if (guideSpan == SpanAlignment.Years) { edgePrefabToUse = instance.yearEdgePrefab; } if (guideSpan == SpanAlignment.Decades) { edgePrefabToUse = instance.decadeEdgePrefab; } if (guideSpan != SpanAlignment.None) { blocks.Clear(); } switch (guideSpan) { case SpanAlignment.Months: { DateTime currStartDate = instance.mapStartTime; while (currStartDate <= now) { DateTime endDate = currStartDate.AddMonths(1).AddDays(1 - currStartDate.Day); if (endDate > now) { endDate = now; } blocks.Add(new TimeBlock(currStartDate, endDate, currStartDate.Month.ToString())); if (endDate == now) { break; } currStartDate = endDate; } } break; case SpanAlignment.Years: { DateTime currStartDate = calendarAligned ? instance.mapStartTime : instance.lifeStartTime; int age = 0; while (currStartDate <= now) { DateTime endDate = currStartDate.AddYears(1); if (calendarAligned) { endDate = endDate.AddDays(1 - endDate.DayOfYear); } if (endDate > now) { endDate = now; } blocks.Add(new TimeBlock(currStartDate, endDate, (calendarAligned ? currStartDate.Year : age).ToString())); if (endDate == now) { break; } currStartDate = endDate; age++; } } break; case SpanAlignment.Decades: { DateTime currStartDate = calendarAligned ? instance.mapStartTime : instance.lifeStartTime; int age = 0; while (currStartDate <= now) { DateTime endDate = currStartDate.AddYears(10); if (calendarAligned) { endDate = new DateTime(endDate.Year - endDate.Year % 10, 1, 1); } if (endDate > now) { endDate = now; } blocks.Add(new TimeBlock(currStartDate, endDate, (calendarAligned ? currStartDate.Year - currStartDate.Year % 10 : age) + "s")); if (endDate == now) { break; } currStartDate = endDate; age += 10; } } break; } foreach (var block in blocks) { int startDiv = (int)(block.startTime.Subtract(zeroTime).TotalDays / divDuration); int endDiv = (int)(block.endTime.Subtract(zeroTime).TotalDays / divDuration); bool[,] blockPoints = new bool[size, size]; bool[,] vertLines = new bool[size + 1, size + 1]; bool[,] horizLines = new bool[size + 1, size + 1]; List <Coord2> blockPointList = new List <Coord2>(); HashSet <Coord2> coveredPoints = new HashSet <Coord2>(); Dictionary <Coord2, int> rectSizes = new Dictionary <Coord2, int>(); /* * bool debug = Random.value < 0.1f; * /*/ bool debug = false; //*/ // Debug.Log( "Drawing shape for " + name + "; start: " + block.startTime + "; startDiv: " + startDiv // + "; end: " + block.endTime + "; endDiv: " + endDiv ); for (int i = startDiv; i < endDiv; i++) { int[] originCoords = HilbertCurve.IntToHilbert(i, 2); points [originCoords[0], originCoords[1]] = true; blockPoints[originCoords[0], originCoords[1]] = true; blockPointList.Add(new Coord2(originCoords[0], originCoords[1])); pointList.Add(new Coord2(originCoords[0], originCoords[1])); } blockPointList = blockPointList.OrderByDescending(p => Enumerable.Range(0, 8) .Select(n => (int)Mathf.Pow(2, n)) .Where(n => p.x % n == 0 && p.y % n == 0) .Max()).ToList(); if (debug) { Debug.Log("points in block: " + Utils.PrintVals(blockPointList.ToArray(), false, true)); } foreach (var point in blockPointList) { if (coveredPoints.Contains(point)) { continue; } //coveredPoints.Add( point ); int rectSize = 1; Coord2Range range = new Coord2Range(point); for (rectSize = 1; rectSize < 256; rectSize *= 2) { if (point.x % rectSize != 0 || point.y % rectSize != 0) { break; } if (point.x + rectSize > size || point.y + rectSize > size) { break; } var newRange = new Coord2Range(point, point + Coord2.one * (rectSize - 1)); if (newRange.Any(p => !blockPoints[p.x, p.y] || coveredPoints.Contains(p))) { break; } range = newRange; if (debug) { Debug.Log("rect size: " + rectSize + "; range: " + range + "; covered by range: " + Utils.PrintVals(range.ToArray(), false, true)); } } rectSize /= 2; foreach (var covered in range) { coveredPoints.Add(covered); } if (debug) { Debug.Log("covered points is now: " + Utils.PrintVals(coveredPoints.ToArray(), false, true)); } rectSizes[point] = rectSize; } if (debug) { Debug.Log("Rect sizes for block in " + name + ":\n" + string.Join("\n", rectSizes.Select(r => r.Key + ": " + r.Value).ToArray())); } foreach (var rect in rectSizes) { int vertInd = verts.Count; Vector3 origin = new Vector3(rect.Key.x, instance.elementMeshY, rect.Key.y) * divSize; verts.Add(origin); verts.Add(origin + Vector3.forward * divSize * rect.Value); verts.Add(origin + Vector3.right * divSize * rect.Value); verts.Add(origin + new Vector3(divSize, 0.0f, divSize) * rect.Value); tris.Add(vertInd); tris.Add(vertInd + 1); tris.Add(vertInd + 3); tris.Add(vertInd); tris.Add(vertInd + 3); tris.Add(vertInd + 2); } // add corners CornerDir[,] corners = new CornerDir[size + 1, size + 1]; // add individual edge segments for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { if (!blockPoints[x, y]) { continue; } if (((x == 0 || !blockPoints[x - 1, y]) && (y == 0 || !blockPoints[x, y - 1])) || ((x > 0 && blockPoints[x - 1, y]) && (y > 0 && blockPoints[x, y - 1]))) { corners[x, y] = CornerDir.LowerLeft; } if (((x == 0 || !blockPoints[x - 1, y]) && (y == size - 1 || !blockPoints[x, y + 1])) || ((x > 0 && blockPoints[x - 1, y]) && (y < size - 1 && blockPoints[x, y + 1]))) { corners[x, y + 1] = CornerDir.UpperLeft; } if (((x == size - 1 || !blockPoints[x + 1, y]) && (y == size - 1 || !blockPoints[x, y + 1])) || ((x < size - 1 && blockPoints[x + 1, y]) && (y < size - 1 && blockPoints[x, y + 1]))) { corners[x + 1, y + 1] = CornerDir.UpperRight; } if (((x == size - 1 || !blockPoints[x + 1, y]) && (y == 0 || !blockPoints[x, y - 1])) || ((x < size - 1 && blockPoints[x + 1, y]) && (y > 0 && blockPoints[x, y - 1]))) { corners[x + 1, y] = CornerDir.LowerRight; } if (x == 0 || !blockPoints[x - 1, y]) { vertLines[x, y] = true; } if (x == size - 1 || !blockPoints[x + 1, y]) { vertLines[x + 1, y] = true; } if (y == 0 || !blockPoints[x, y - 1]) { horizLines[x, y] = true; } if (y == size - 1 || !blockPoints[x, y + 1]) { horizLines[x, y + 1] = true; } } } // combine edge segments into lines List <LineSpan> spans = new List <LineSpan>(); // vertical lines first for (int x = 0; x <= size; x++) { for (int y = 0; y <= size; y++) { if (!vertLines[x, y]) { continue; } int startY = y; while (vertLines[x, y]) { y++; } spans.Add(new LineSpan(x, startY, x, y)); } } for (int y = 0; y <= size; y++) { for (int x = 0; x <= size; x++) { if (!horizLines[x, y]) { continue; } int startX = x; while (horizLines[x, y]) { x++; } spans.Add(new LineSpan(startX, y, x, y)); } } //Debug.Log( name + " block has " + spans.Count + " line spans" ); if (spans.Count == 0) { continue; } List <Coord2> lineCoords = new List <Coord2>(); LineSpan currSpan = spans.Random(); spans.Remove(currSpan); LineSpan nextSpan = null; Coord2 currEnd = currSpan.end; lineCoords.Add(currSpan.start); lineCoords.Add(currSpan.end); // Debug.Log( "currSpan is " + currEnd + "; all other spans:\n" // + Utils.PrintVals( spans.ToArray(), false, true ) ); // foreach (var span in spans) { // Debug.Log( "Match " + span + ": " + (span.start == currEnd || span.end == currEnd) ); // } while ((nextSpan = spans.FirstOrDefault(s => s.start == currEnd || s.end == currEnd)) != null) { currEnd = nextSpan.start == currEnd ? nextSpan.end : nextSpan.start; spans.Remove(nextSpan); currSpan = nextSpan; lineCoords.Add(currEnd); } LineRenderer lineRenderer = Instantiate(edgePrefabToUse, shapeObject.transform); lineRenderer.positionCount = lineCoords.Count; float yPosition = guideSpan == SpanAlignment.None ? instance.elementEdgeY : instance.guideEdgeY; lineRenderer.widthMultiplier *= divSize * 0.333f; float cornerShift = lineRenderer.widthMultiplier * 0.5f; // Vector3[] shunkLineVerts = instance.ShrinkPath( lineVerts.Select( c => ((Vector3) (Vector2) c) * divSize ).ToArray(), // lineRenderer.widthMultiplier * 0.5f ); Vector3[] lineVerts = lineCoords.Select(coord => { var cornerDir = corners[coord.x, coord.y]; Vector3 vert = new Vector3(coord.x * divSize, yPosition, coord.y * divSize); if (guideSpan != SpanAlignment.None) { return(vert); } if (cornerDir == CornerDir.UpperLeft || cornerDir == CornerDir.LowerLeft) { vert.x += cornerShift; } else { vert.x -= cornerShift; } if (cornerDir == CornerDir.UpperLeft || cornerDir == CornerDir.UpperRight) { vert.z -= cornerShift; } else { vert.z += cornerShift; } return(vert); }).ToArray(); lineRenderer.SetPositions(lineVerts); if (guideSpan == SpanAlignment.None) { Color edgeColor = color.ShiftLuma(0.1f); edgeColor.a = 1.0f; lineRenderer.startColor = lineRenderer.endColor = edgeColor; } // Debug.Log( "Generated edge color for " + name + ": " + edgeColor ); if (guideSpan != SpanAlignment.None) { Vector3 position = blockPointList.Select(v => new Vector3(v.x, 0.0f, v.y)).Average() * divSize + Vector3.up * instance.guideLabelY; if (!block.label) { block.label = Instantiate(labelPrefabToUse, labelParent.transform as RectTransform); //label.transform.position = position; block.label.localPosition = Utils.WorldToCanvasSpace(position, labelParent.transform as RectTransform, block.label); Text labelText = block.label.GetComponentInChildren <Text>(); labelText.text = block.labelString; labels.Add(block.label); } block.label.localPosition = Utils.WorldToCanvasSpace(position, labelParent.transform as RectTransform, block.label.transform as RectTransform); } } if (guideSpan == SpanAlignment.None) { Mesh mesh = new Mesh(); mesh.vertices = verts.ToArray(); mesh.triangles = tris.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); MeshFilter meshFilter = shapeObject.AddComponent <MeshFilter>(); meshFilter.sharedMesh = mesh; Material mat = new Material(baseMat); mat.color = color * 0.6f; // Debug.Log( "Applying mesh material color for " + name + ": " + color ); MeshRenderer meshRenderer = shapeObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = mat; Vector3 position = pointList.Select(v => new Vector3(v.x, 0.0f, v.y)).Average() * divSize + Vector3.up * instance.elementLabelY; //Debug.Log( name + " average point position: " + position ); //label.transform.position = position; label.localPosition = Utils.WorldToCanvasSpace(position, labelParent.transform as RectTransform, label.transform as RectTransform); } }
static Coord2() { Coord2.ZERO = new Coord2(0, 0, 0f, 0f); }
public bool IsInside(Coord2 co) { return Util.IsInside(Width, Height, co.x, co.y); }
public Cell GetCell(Coord2 co) { return mCells[co.x, co.y]; }
private void SetCell(Coord2 coord, Cell cell) { coord += _MapCenter; _Map[coord.x, coord.y] = cell; }
private Cell GetCell(Coord2 coord) { coord += _MapCenter; return(_Map[coord.x, coord.y]); }
public Vector3 GetPos(Coord2 coord) { return(new Vector3(coord.x * GridSize + Center.x, 0, coord.y * GridSize + Center.z)); }
// 直线掉落 private void DropLine(PriorityQueue<Cell> emptyCells, Dictionary<uint, DropInfo> dropMap, int turn, Cell cell, Coord2 top) { DropMode mode; var curCell = cell; while (true) { var topCell = GetCell(top); DropOne(dropMap, turn, false, topCell, curCell); curCell = topCell; // 下一次处理 top = mPortol.GetTop(curCell.coord); mode = GetDropMode(top, turn); if (mode != DropMode.DROP) { // gen?? emptyCells.Push(curCell); break; } } }
public bool CanDropElement(Coord2 co) { return(_points.ContainsKey(co)); }