public void SetNodeObject(RectObject obj) { Vector2Int pos = obj.GetCurrentPos(); for (int x = 0; x < obj.Width; x++) { for (int y = 0; y < obj.Height; y++) { int nx = pos.x + x; int ny = pos.y + y; if (nx < 0 || nx >= Width || ny < 0 || ny >= Height) { continue; } MapGrid[pos.x + x, pos.y + y].Object = obj; } } }
public void MergeDebris(RectObject debris) { Color col = debris.GetComponent <SpriteRenderer>().sprite.texture.GetPixel(0, 0); Vector2Int pos = debris.GetCurrentPos(); Vector2Int bot = pos; bot.y -= 1; if (IsInBounds(bot) && IsInBounds(pos)) { Node b = MapGrid[bot.x, bot.y]; Node n = MapGrid[pos.x, pos.y]; if (b.Solid && !b.Object && !n.Object) //Checking if bottom node (the node debris is on) is solid but NOT an object. Also checking current node if not occupied by any object. { Destroy(debris.gameObject); n.Solid = true; MapSprite.texture.SetPixel(pos.x, pos.y, col); mapChanged = true; } } }
public void FluidCheck(RectObject fluid) { float flowSpeed = 0.25f; if (fluid.Velocity.y < 0) // && Mathf.Abs(fluid.Velocity.x) > flowSpeed) { return; } Vector2Int pos = fluid.GetCurrentPos(); Vector2Int bot = pos; bot.y--; for (int i = 1; i < 4; i++) { Vector2Int left = bot; left.x -= i; Vector2Int righ = bot; righ.x += i; Node lNode = null; Node rNode = null; if (IsInBounds(left)) { lNode = MapGrid[left.x, left.y]; } if (IsInBounds(righ)) { rNode = MapGrid[righ.x, righ.y]; } Node flow = null; float dir = 0; if (lNode && rNode) { if (!lNode.Solid && !rNode.Solid) { if (GRandom.GetBool()) { flow = lNode; dir = -flowSpeed; } else { flow = rNode; dir = flowSpeed; } } else if (!lNode.Solid) { flow = lNode; dir = -flowSpeed; } else if (!rNode.Solid) { flow = rNode; dir = flowSpeed; } } else if (lNode && !lNode.Solid) { flow = lNode; dir = -flowSpeed; } else if (rNode && !rNode.Solid) { flow = rNode; dir = flowSpeed; } if (flow && !flow.IsSolid(fluid)) { Node u = MapGrid[flow.Position.x, flow.Position.y + 1]; if (u.IsSolid(fluid)) { continue; } // fluid.AddForce(new Vector2(dir, 0)); ClearNodeObject(fluid.Position, 1, 1); fluid.LastPosition = fluid.Position; fluid.Position = new Vector2(fluid.Position.x + dir, fluid.Position.y); //fluid.Position.x += 0.5f; //fluid.Position.y += 0.5f; SetNodeObject(fluid); } } }