//Here will all the pixel coordinates be calculated and made the alpha of the pixel zero. public void SubstractSpritesAlpha(CircleCollider2D cc) { //declares the positions of the pixel from the worldposition where the cc collides. (if collides) V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); //the radius of the irlce is declared. int radius = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); int x, y, posX, negativePosX, posY, negativePosY, d; // in these two for loops every pixel coordinate is calculated and made transparent. for (x = 0; x <= radius; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(radius * radius - x * x)); for (y = 0; y <= d; y++) { posX = c.x + x; negativePosX = c.x - x; posY = c.y + y; negativePosY = c.y - y; spriteRenderer.sprite.texture.SetPixel(posX, posY, zeroAlphaColor); spriteRenderer.sprite.texture.SetPixel(negativePosX, posY, zeroAlphaColor); spriteRenderer.sprite.texture.SetPixel(posX, negativePosY, zeroAlphaColor); spriteRenderer.sprite.texture.SetPixel(negativePosX, negativePosY, zeroAlphaColor); } } //sprite renderer is applied. spriteRenderer.sprite.texture.Apply(); //updates the collider. UpdateSpriteCollider(); }
public void DestroyGround(CircleCollider2D cc) { Debug.Log("DestroyGround"); V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld / Mathf.RoundToInt(transform.localScale.x)); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D cc) { //Debug.Log ("Destroy"); V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); int r = Mathf.RoundToInt((cc.bounds.size.x * widthPixel / widthWorld) * 0.8f); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); }
public void DestroyGround(CircleCollider2D cc) { V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); // c => centro do circulo de destruiçao em pixels int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); // r => raio do circulo de destruiçao em int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D collider) { Debug.Log("DestroyGround"); V2int intPunkt = World2Pixel(collider.bounds.center.x, collider.bounds.center.y); int radius = Mathf.RoundToInt(collider.bounds.size.x * widthPixel / widthWorld); int x, y, px, nx, py, ny, d; for (x = 0; x <= radius; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(radius * radius - x * x)); for (y = 0; y <= d; y++) { px = intPunkt.x + x; nx = intPunkt.x - x; py = intPunkt.y + y; ny = intPunkt.y - y; sprite.sprite.texture.SetPixel(px, py, color); sprite.sprite.texture.SetPixel(nx, py, color); sprite.sprite.texture.SetPixel(px, ny, color); sprite.sprite.texture.SetPixel(nx, ny, color); } } sprite.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(JSONObject coll) { V2int c = World2Pixel(coll.GetField("centerX").f, coll.GetField("centerY").f); int r = Mathf.RoundToInt(coll.GetField("sizeX").f *widthPixel / widthWorld); Debug.Log(c); Debug.Log(r); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D cc) { Debug.Log("called DestroyGround"); V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); // c => center of the circle of destruction in pixels int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); // r => radius of the circle of destruction in Debug.Log(c.x + " " + c.y + " " + r); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt((r * r) - (x * x))); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D> ()); gameObject.AddComponent <PolygonCollider2D>(); }
public void MyDestroyGround(float posX, float posY) { /*Vector2 target = new Vector2 (posX, posY); * Vector3 pc = cam.WorldToScreenPoint (target); * Vector3 pixelCenter = new Vector3 (pc.x, pc.y, 0f);*/ V2int c = World2Pixel(posX, posY); int r = (int)(widthPixel / widthWorld); Debug.Log(c.x + " " + c.y + " " + r); //make alpha zero int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt((r * r) - (x * x))); for (y = 0; y <= 3 * d; y++) { px = (int)c.x + x; nx = (int)c.x - x; py = (int)c.y + y; ny = (int)c.y - y; //Debug.Log (px + " " + py + " " + nx + " " + ny); sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D> ()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D cc) { V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); int px, nx, py, ny; for (int i = 0; i <= r; i++) { int temp = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - i * i)); for (int j = 0; j <= temp; j++) { px = c.x + i; nx = c.x - i; py = c.y + j; ny = c.y - j; sr.sprite.texture.SetPixel(px, py, hole); sr.sprite.texture.SetPixel(nx, py, hole); sr.sprite.texture.SetPixel(px, ny, hole); sr.sprite.texture.SetPixel(nx, ny, hole); } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); createCollider.SetCollider(); }
void Start () { chairManager = GameObject.Find("ChairManager").GetComponent<ChairManager>(); selected = false; myRenderer = GetComponent<Renderer>(); // find out where I am in the grid and save my pos // this means chairs can be placed in the editor and they'll figure out their exact co-ordinates on their own float closestDistance = Mathf.Infinity; for (int z = 0; z < grid.totalHeight; z++) { for (int x = 0; x < grid.totalWidth; x++) { // only look at empty tiles if (grid.isValidChairPosition(new V2int(x, z)) ) { V2int searchPosition = new V2int(x,z); float currentDistance = (grid.coordForPosition(transform.position) - searchPosition).sqrMagnitude; if (currentDistance < closestDistance) { closestDistance = currentDistance; pos = new V2int(x,z); } } } } // tell the floor manager there's a chair here grid.addChair(pos, this); // snap the mesh to the correct location transform.position = grid.positionForCoord(pos) + grid.chairOffset; }
public bool Equals(V2int p) { if ((object)p == null) { return false; } return (_x == p.x) && (_y == p.y); }
///// <summary> ///// Update method. ///// </summary> //void Update() //{ //} /// <summary> /// Destroys part of the ground based on the CircleCollider2D dimension. /// </summary> /// <param name="destroyerCircleCollider">Collider that defines the area to destroy.</param> public void DestroyGround(CircleCollider2D destroyerCircleCollider) { // Center coordinates of the collider. V2int colliderCenter = World2Pixel(destroyerCircleCollider.bounds.center.x, destroyerCircleCollider.bounds.center.y); // Collider ray. int colliderRay = Mathf.RoundToInt(destroyerCircleCollider.bounds.size.x * groundImgPixelWidth / groundTotalWidth); int x, y, px, nx, py, ny, verticalCathetus; // Moving X from 0 to the collider ray length... for (x = 0; x <= colliderRay; x++) { // Every X defines a triangle. // --> 0 to X is the horizontal cathetus. // --> colliderRay is the hypotenuse. // We need to calculate the vertical cathetus. verticalCathetus = (int)Mathf.RoundToInt(Mathf.Sqrt(colliderRay * colliderRay - x * x)); // Moving Y from 0 to the vertical cathetus length... for (y = 0; y <= verticalCathetus; y++) { // I am considering only one quarter, but with that I'm going to work in all the four ones. // I need positive and negative coordinates of X and Y. px = colliderCenter.x + x; nx = colliderCenter.x - x; py = colliderCenter.y + y; ny = colliderCenter.y - y; // For every singlecoordinate found by combining positive and negative X's and Y's, // I am going to override the pixel positioned in that coordinate with // the transparent color. if ((px >= 0 && px <= groundImgPixelWidth) && (py >= 0 && py <= groundImgPixelHeight)) { spriteRenderer.sprite.texture.SetPixel(px, py, colorTransparent); } if ((nx >= 0 && nx <= groundImgPixelWidth) && (py >= 0 && py <= groundImgPixelHeight)) { spriteRenderer.sprite.texture.SetPixel(nx, py, colorTransparent); } if ((px >= 0 && px <= groundImgPixelWidth) && (ny >= 0 && ny <= groundImgPixelHeight)) { spriteRenderer.sprite.texture.SetPixel(px, ny, colorTransparent); } if ((nx >= 0 && nx <= groundImgPixelWidth) && (ny >= 0 && ny <= groundImgPixelHeight)) { spriteRenderer.sprite.texture.SetPixel(nx, ny, colorTransparent); } } } // Now I'll apply those updates on the texture. That's why I operate on a clone // and not on the original texture. spriteRenderer.sprite.texture.Apply(); // Now it's time to refresh the collider, that will ignore transparent pixels and fit // the new texture. ResetPolygonCollider2D(); }
private V2int World2Pixel(float x, float y) { V2int v = new V2int(); float dx = x-transform.position.x; v.x = Mathf.RoundToInt(0.5f*widthPixel+ dx*widthPixel/widthWorld); float dy = y - transform.position.y; v.y = Mathf.RoundToInt(0.5f * heightPixel + dy * heightPixel / heightWorld); return v; }
private V2int World2Pixel(float x, float y) { V2int v = new V2int(); float dx = x - transform.position.x; v.x = Mathf.RoundToInt(0.5f * widthPixel + dx * widthPixel / widthWorld); float dy = y - transform.position.y; v.y = Mathf.RoundToInt(0.5f * heightPixel + dy * heightPixel / heightWorld); return(v); }
/// <summary> /// Gets the pixel coordinates from the position in the plane. /// </summary> /// <param name="x">X coordinate.</param> /// <param name="y">Y coordinate.</param> /// <returns></returns> private V2int World2Pixel(float x, float y) { V2int v = new V2int(); float dx = x - transform.position.x; v.x = Mathf.RoundToInt(0.5f * groundImgPixelWidth + dx * groundImgPixelWidth / groundTotalWidth); float dy = y - transform.position.y; v.y = Mathf.RoundToInt(0.5f * groundImgPixelHeight + dy * groundImgPixelHeight / groundTotalHeight); return(v); }
public void DestroyGround(CircleCollider2D cc) { sr.sprite.texture.Apply(); V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); Texture2D tex = sr.sprite.texture; Texture2D tex_clone = (Texture2D)Instantiate(tex); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; tex_clone.SetPixel(px, py, transp); tex_clone.SetPixel(nx, py, transp); tex_clone.SetPixel(px, ny, transp); tex_clone.SetPixel(nx, ny, transp); } } tex_clone.Apply(); sr.sprite = Sprite.Create(tex_clone, new Rect(0f, 0f, tex_clone.width, tex_clone.height), new Vector2(0.5f, 0.5f), 100f); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D cc) { V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; if (px < widthPixel && px > 0 && py < heightPixel && py > 0) { sr.sprite.texture.SetPixel(px, py, transp); } if (nx < widthPixel && nx > 0 && py < heightPixel && py > 0) { sr.sprite.texture.SetPixel(nx, py, transp); } if (px < widthPixel && px > 0 && ny < heightPixel && ny > 0) { sr.sprite.texture.SetPixel(px, ny, transp); } if (nx < widthPixel && nx > 0 && ny < heightPixel && ny > 0) { sr.sprite.texture.SetPixel(nx, ny, transp); } } } sr.sprite.texture.Apply(); Destroy(GetComponent <PolygonCollider2D>()); gameObject.AddComponent <PolygonCollider2D>(); }
public void DestroyGround(CircleCollider2D cc) { // Debug.Log ("chamou DestroyGround"); V2int c = World2Pixel(cc.bounds.center.x, cc.bounds.center.y); // c => centro do circulo de destruiçao em pixels int r = Mathf.RoundToInt(cc.bounds.size.x * widthPixel / widthWorld); // r => raio do circulo de destruiçao em int x, y, px, nx, py, ny, d; for (x = 0; x <= r; x++) { d = (int)Mathf.RoundToInt(Mathf.Sqrt(r * r - x * x)); for (y = 0; y <= d; y++) { px = c.x + x; nx = c.x - x; py = c.y + y; ny = c.y - y; sr.sprite.texture.SetPixel(px, py, transp); sr.sprite.texture.SetPixel(nx, py, transp); sr.sprite.texture.SetPixel(px, ny, transp); sr.sprite.texture.SetPixel(nx, ny, transp); } } sr.sprite.texture.Apply(); sr.sprite = Sprite.Create(tex_cur, new Rect(0f, 0f, tex_cur.width, tex_cur.height), new Vector2(0.5f, 0.5f), 100f); //InitSprite(tex_cur); StartCoroutine(AddSpriteCollider()); }
public bool isAtLocation(V2int pos) { Vector3 delta = transform.position - grid.positionForCoord(pos); return (new Vector2(delta.x,delta.z)).sqrMagnitude < .001f; }
void Update () { Ray ray; RaycastHit hit; if (currentChair == null) { if (Input.GetMouseButtonDown(0) && chairsCanBeMoved) // player clicked - look for a chair { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000, chairLayerMask)) { currentChair = hit.collider.transform; pickedUpThisFrame = true; // prevent dropping the chair later this frame from the same mouse click // change material on the chair hit.collider.GetComponent<Renderer>().material = chairPicked; hit.collider.GetComponent<Chair>().selected = true; // prevents it changing its material on mouse-over //tell the floorManager that this space is now empty // floorManager.toggleChair(hit.collider.GetComponent<Chair>().posX, hit.collider.GetComponent<Chair>().posY, false); Chair current = grid.removeChair(hit.collider.GetComponent<Chair>().pos); currentChair = current.transform; } } } if (currentChair != null) // snap the chair to the grid { ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 1000, floorLayerMask)) { // find closest empty floorTile to the ray hit location float closestDistance = Mathf.Infinity; V2int closestPos = null; for (int z = 0; z < grid.totalHeight; z++) { for (int x = 0; x < grid.totalWidth; x++) { // only look at empty tiles V2int pos = new V2int(x,z); if ( grid.isValidChairPosition(pos) ) { V2int searchPosition = new V2int(x,z); float currentDistance = (grid.coordForPosition(hit.point) - searchPosition).sqrMagnitude; if (currentDistance < closestDistance) { closestDistance = currentDistance; closestPos = pos; } } } } // position the chair currentChair.position = grid.positionForCoord(closestPos) + grid.chairOffset; // update its internal x and y storage currentChair.GetComponent<Chair>().pos = closestPos; } if (Input.GetMouseButtonDown(0)) // player clicked - drop the chair { if (!pickedUpThisFrame) { // change material on the chair currentChair.GetComponent<Renderer>().material = chairNormal; currentChair.GetComponent<Chair>().selected = false; // allow it to change material again // tell the grid there's now a chair here grid.addChair(grid.coordForPosition(currentChair.position),currentChair.GetComponent<Chair>()); // drop it currentChair = null; } } } pickedUpThisFrame = false; // allows dropping the chair on next click }
private bool isValid (V2int point) { bool valid = point.x >= 0 && point.x < _grid.GetLength(0) && point.y >= 0 && point.y < _grid.GetLength(1) && _grid[point.x,point.y] && !_manager.PersonAtCoord().Keys.ToList().Contains(point); return valid; }
private List<V2int> untestedSurrounding(V2int p) { List<V2int> surrounding = new List<V2int> {p+V2int.t,p+V2int.r,p+V2int.b,p+V2int.l}; return surrounding.FindAll(s=>isValid(s) && !_tested.Contains(s)).ToList(); }
public PathFinder(Manager manager, bool[,] grid, V2int start, V2int target) { this._manager = manager; this._grid = grid; this._start = start; this._target = target; }
public GoalLocation(Person person, V2int pos) { this._pos = pos; init(person); }