private void SpreadVein(Vein vein, Vector2Int position, ushort chunkSize) { ActiveVein activeVein = new ActiveVein(vein); Vector2Int lastRandomDirection = Vector2Int.Zero; while ((int)UnityEngine.Random.Range(0, 101) <= activeVein.CurrentExpansionRate) { Vector2Int randomDirection = RandomDirection(); while (randomDirection.Equals(lastRandomDirection)) { randomDirection = RandomDirection(); } Vector2Int nextPosition = position + randomDirection; if (nextPosition.x >= 0 && nextPosition.x < chunkSize && nextPosition.y >= 0 && nextPosition.y < chunkSize) { if (tiles[nextPosition.x, nextPosition.y] == TileId.Dirt || tiles[nextPosition.x, nextPosition.y] == TileId.Empty) { tiles[nextPosition.x, nextPosition.y] = vein.Tile.TileId; position = nextPosition; } } activeVein.DropCurrentExpansionRate(); } }
private TileId StartVein(Vector2Int tileChunkPos, Vector2Int chunkPos, List <ChunkVein> veins, ushort chunkSize, out bool startVein) { Vein vein = PickVein(veins); if (vein != null) { startVein = true; SpreadVein(vein, tileChunkPos, chunkSize); return(vein.Tile.TileId); } else { startVein = false; return(TileId.Empty); } }
private void DropItem() { for (int i = 0; i < 3; i++) { string Name = this.name.Substring(0, this.name.Length - 4); float placeX = Random.Range(-0.5f, 0.5f); float placeY = Random.Range(-0.5f, 0.5f); if (!this.name.Contains("Stone")) { // Erzadern GameObject Spawn = Instantiate(Prefabliste.Instance().GetGameObject(Name + "ore"), new Vector3(0, 0, 0), Quaternion.identity); Spawn.transform.position = this.transform.position + new Vector3(placeX, placeY, 0); Spawn.name = Name + "ore"; var spritetake = Spawn.GetComponent <SpriteRenderer>(); spritetake.sortingOrder = 1; } else { // Stein GameObject Spawn = Instantiate(Prefabliste.Instance().GetGameObject(Name), new Vector3(0, 0, 0), Quaternion.identity); Spawn.transform.position = this.transform.position + new Vector3(placeX, placeY, 0); Spawn.name = Name; var spritetake = Spawn.GetComponent <SpriteRenderer>(); spritetake.sortingOrder = 1; } } float placeVX = Random.Range(-1f, 1f); float placeVY = Random.Range(-1f, 1f); // Vein GameObject Vein = Instantiate(Prefabliste.Instance().GetGameObject("Vein"), new Vector3(0, 0, 0), Quaternion.identity); Vein.transform.position = this.transform.position + new Vector3(placeVX, placeVY, 0); Vein.name = "Vein"; Vein myVein = Vein.GetComponent <Vein>(); myVein.vein = this.name; var spritetakeV = Vein.GetComponent <SpriteRenderer>(); spritetakeV.sortingOrder = 1; SoundManager.SendMessage("PlaySound", "stonebreak"); Destroy(this.gameObject); Charakter.SendMessage("EXP", 10); }
public ChunkVein(Vein vein, int Ypos) { Vein = vein; float minDepth = Mathf.Abs(vein.MinDepth); float maxDepth = Mathf.Abs(vein.MaxDepth); float depth = Mathf.Abs(Ypos); float graphPoint = (depth - minDepth) / (maxDepth - minDepth); Chance = vein.Frequency.Evaluate(graphPoint); #if debugLogVeinMath Debug.Log("Depth = " + depth + " Min depth = " + minDepth + " Max depth= " + maxDepth); Debug.Log("Depth sits at " + graphPoint + " between min and max"); Debug.Log("Chance at that pos = " + Chance); #endif }
/// <summary> /// Returns whether or not this vein and the given vein cut the target in two /// </summary> /// <param name="other"></param> /// <returns></returns> public bool formsSlice(Vein other, int stencilCount) { int start = interdataStart.stencilLineSegmentID; for (int i = start + 1; i <= start + stencilCount; i++) { if (i % stencilCount == other.interdataStart.stencilLineSegmentID % stencilCount) { Debug.Log("formsSLice: is slice"); return(true); } if (i % stencilCount == other.interdataEnd.stencilLineSegmentID % stencilCount) { Debug.Log("formsSLice: is bumps"); return(false); } } Debug.Log("formsSLice: default"); return(false); }
public void slice(Vein other) { this.interdataEnd = other.interdataEnd; }
public static void Solve() { _maxX = 2000; string inputPath = FileUtils.GetProjectFilePath("Days/Day17/ProblemA/input.txt"); String[] lines = File.ReadAllLines(inputPath); List <Vein> veins = new List <Vein>(); Int32 deepestY = Int32.MinValue; _smallestYPoint = Int32.MaxValue; foreach (var line in lines) { var l = line.Trim(); if (!string.IsNullOrEmpty(l) && !l.StartsWith('/')) { Vein v = new Vein(l); if (v.MaxY > deepestY) { deepestY = v.MaxY; } if (v.MinY < _smallestYPoint) { _smallestYPoint = v.MinY; } veins.Add(v); } } Log.WriteLine("Parsed " + veins.Count + " lowest vein was " + deepestY); _maxY = deepestY + 1; _largestYPoint = deepestY; _tiles = new Tile[_maxX * _maxY]; for (int i = 0; i < _maxX; i++) { for (int j = 0; j < _maxY; j++) { Tile t = new Tile(); t.TileIndex = GetTileIndex(i, j); t.X = i; t.Y = j; t.TileType = TileType.Sand; _tiles[t.TileIndex] = t; } } for (int i = 0; i < _tiles.Length; i++) { var t = _tiles[i]; t.Left = GetTile(t.X - 1, t.Y); t.Bottom = GetTile(t.X, t.Y + 1); t.Right = GetTile(t.X + 1, t.Y); } foreach (var vein in veins) { for (int i = vein.MinX; i <= vein.MaxX; i++) { for (int j = vein.MinY; j <= vein.MaxY; j++) { GetTile(i, j).TileType = TileType.Clay; } } } var fountain = GetTile(500, 0); fountain.TileType = TileType.Fountain; Flood(fountain, null); RenderGameBoard(); Log.WriteLine("Total tiles flooded: " + FloodCount); }
public ActiveVein(Vein vein) { Vein = vein; CurrentExpansionRate = vein.ExpansionRate; }
public void cutShape(Shape stencil, bool splitFurther = true) { if (stencil.Direction != this.Direction) { throw new UnityException("Trying to cut shape with stencil of different thread type! shape: " + Direction + ", stencil: " + stencil.Direction); } //Show paths (for debugging) //showPath(points); //showPath(stencilPoints); //Gather overlap info List <IntersectionData> intersectionData = new List <IntersectionData>(); for (int i = 0; i < GlobalPoints.Count; i++) { int i2 = (i + 1) % GlobalPoints.Count; //Line Checking LineSegment targetLine = new LineSegment(GlobalPoints, i); //Check to see if the bounds overlap if (stencil.bounds.Intersects(targetLine.Bounds)) { bool startInStencil = stencil.OverlapPoint(targetLine.startPos); bool endInStencil = stencil.OverlapPoint(targetLine.endPos); //Check which stencil edges intersect the line segment bool intersectsSegment = false; for (int j = 0; j < stencil.GlobalPoints.Count; j++) { LineSegment stencilLine = new LineSegment(stencil.GlobalPoints, j); Vector2 intersection = Vector2.zero; bool intersects = targetLine.Intersects(stencilLine, ref intersection); //If it intersects, if (intersects) { //Record a data point intersectsSegment = true; float distanceToPoint = (intersection - targetLine.startPos).magnitude; IntersectionData interdata = new IntersectionData(intersection, i, j, intersects, startInStencil, endInStencil, distanceToPoint); intersectionData.Add(interdata); } } //If no line segment intersections were found, if (!intersectsSegment) { //but one or more end points are in the stencil, if (startInStencil || endInStencil) { //Make an intersection data point anyway, with slightly different arguments IntersectionData interdata = new IntersectionData(Vector2.zero, i, -1, IntersectionData.IntersectionType.INSIDE); intersectionData.Add(interdata); } } //else, else { //do nothing because the bounds lied about the line segment and stencil colliding //don't worry, it's a known thing that can happen: //bounds checking is quick but liable to give false positives } } } // // Refine intersection data entries // //Sort the data entries intersectionData.Sort(new IntersectionData.IntersectionDataComparer()); //Set the intersection type of the data int side = 0;//0 =not set, 1 =inside, -1 =outside foreach (IntersectionData interdata in intersectionData) { if (side == 0) { side = (interdata.startsInStencil) ? 1 : -1; } if (interdata.segmentIntersection) { side *= -1; interdata.type = (side > 0) ? IntersectionData.IntersectionType.ENTER : IntersectionData.IntersectionType.EXIT; } else { interdata.type = (side > 0) ? IntersectionData.IntersectionType.INSIDE : IntersectionData.IntersectionType.OUTSIDE; } } IntersectionData.printDataList(intersectionData, GlobalPoints); // //Start cutting // //Replace line segments inside the stencil int dataCount = intersectionData.Count; //Search for start of vein of changes List <Vein> veins = new List <Vein>(); //only need to go through the loop once, //because the veins will find their own end points: //here we just need to find the start of each vein for (int iData = 0; iData < dataCount; iData++) { IntersectionData interdata = intersectionData[iData]; //if this segment enters the stencil at this data point, if (interdata.type == IntersectionData.IntersectionType.ENTER) { //then it's a vein start Vein vein = new Vein(iData, interdata, intersectionData); veins.Add(vein); } } //Process found veins if (veins.Count == 1) { Vector2[] newPath = veins[0].getStencilPath(stencil.GlobalPoints); //Replace vein with stencil path int removeCount = veins[0].getRemoveCount(GlobalPoints.Count); replacePoints(newPath, veins[0].VeinStart + 1, removeCount); } else { //Process all the veins IndexOffset.IndexOffsetContainer offsets = new IndexOffset.IndexOffsetContainer(GlobalPoints.Count); for (int i = 0; i < veins.Count; i++) { Vein vein = veins[i]; //Update vein with new offsets vein.updateIndexes(offsets); //Check next vein bool slices = false; if (i < veins.Count - 1) { Vein vein2 = veins[i + 1]; vein.updateIndexes(offsets); slices = vein.formsSlice(vein2, stencil.GlobalPoints.Count); Debug.Log("slices: " + slices); if (slices) { vein.slice(vein2); if (splitFurther) { if (this.pc2d) { //make a new collider to make the new piece PolygonCollider2D pc2dNew = GameObject.Instantiate(pc2d.gameObject) .GetComponent <PolygonCollider2D>(); Shape newShape = new Shape(pc2dNew); newShape.rotatePoints(vein2.VeinStart); newShape.finalize(); childrenShapes.Add(newShape); pc2dNew.transform.parent = pc2d.transform.parent; pc2dNew.transform.position = pc2d.transform.position; newShape.cutShape(stencil, false); } } //skip the next vein i++; } } if (true || !slices) { //Replace vein with stencil path Vector2[] newPath = vein.getStencilPath(stencil.GlobalPoints); int removeCount = vein.getRemoveCount(GlobalPoints.Count); replacePoints(newPath, vein.VeinStart + 1, removeCount); //Add offset to the collection IndexOffset offset = new IndexOffset(vein.VeinStart, newPath.Length - removeCount); offsets.Add(offset); } } } // // Finish up // finalize(); }