public void AddPath(Beast.BeastLink link, GameObject startSprite, GameObject continueSprite, GameObject endSprite, GameObject onlySprite = null) { if (onlySprite == null) { onlySprite = startSprite; } this.SetHasSprites(true); this.link = link; Vector3 currentPosition = this.transform.position; Vector3 preScale = this.spriteParent.transform.localScale; this.spriteParent.transform.localScale = new Vector3(1, 1, 1); for (int i = 0; i < link.directions.Length; i++) { GameObject sprite = onlySprite; if (link.directions.Length != 1) { if (i == 0) { sprite = startSprite; } else if (i == link.directions.Length - 1) { sprite = endSprite; } else { sprite = continueSprite; } } Vector3 rotation = new Vector3(); Vector3 position = new Vector3(); string direction = link.directions[i]; switch (direction) { case "left": rotation = new Vector3(0, 0, 180); position = currentPosition + new Vector3(-0.5f, 0, 0); this.AddLink(sprite, rotation, position); currentPosition += new Vector3(-1, 0, 0); break; case "right": rotation = new Vector3(0, 0, 0); position = currentPosition + new Vector3(0.5f, 0, 0); this.AddLink(sprite, rotation, position); currentPosition += new Vector3(1, 0, 0); break; case "up": rotation = new Vector3(0, 0, 90); position = currentPosition + new Vector3(0, 0.5f, 0); this.AddLink(sprite, rotation, position); currentPosition += new Vector3(0, 1, 0); break; case "down": rotation = new Vector3(0, 0, 270); position = currentPosition + new Vector3(0, -0.5f, 0); this.AddLink(sprite, rotation, position); currentPosition += new Vector3(0, -1, 0); break; } } this.spriteParent.transform.localScale = preScale; }
public void Render(Beast center, bool doAnimations = false) { Beast current = center; List<string> directions = new List<string>(); Vector3 target = this.transform.position; int dy = (int)(target.y); int dx = (int)(target.x); int x_sign = dx > 0 ? 1 : -1; int y_sign = dy > 0 ? 1 : -1; float v = ((float)dy * y_sign) / (dx * x_sign); float f = 0; string lastDirection = null; for (int x = 0; x < dx * x_sign; x++) { f += v; while (f >= 1) { lastDirection = y_sign == 1 ? "up" : "down"; directions.Add(lastDirection); current = y_sign == 1 ? current.up : current.down; f -= 1; } current = x_sign == 1 ? current.right : current.left; lastDirection = x_sign == 1 ? "right" : "left"; directions.Add(lastDirection); } if (dx == 0) { for (int y = 0; y < dy * y_sign; y++) { current = y_sign == 1 ? current.up : current.down; lastDirection = y_sign == 1 ? "up" : "down"; directions.Add(lastDirection); } } bool beastChanged = this.beast != current; bool innerChanged = this.innerBeast != current.inner; this.innerBeast = current.inner; this.beast = current; if (innerChanged && !beastChanged) { if (doAnimations) { StartCoroutine(this.DoInnerAnimation(directions.Count, current.inner)); } else { this.SetInnerBeastSprites(current.inner); } } if (beastChanged) { if (doAnimations) { StartCoroutine(this.DoAnimation(directions.Count, current, lastDirection)); } else { this.oldSprite.sprite = current.sprite; this.oldSprite.color = current.color; this.newSprite.sprite = current.sprite; this.newSprite.color = current.color; this.SetInnerBeastSprites(current.inner); } this.link = new Beast.BeastLink(center, directions.ToArray()); this.UpdateLeyLines(); } }