/// <summary> /// Displays a block with the blocks type at position x,y. If the block is already displayed, the previous displayer will be destroyed /// </summary> public void DisplayObject(int x, int y) { if (visualiser == null) { SpriteRenderer renderer = MonoBehaviour.Instantiate(visualiserPrefab) as SpriteRenderer; renderer.transform.position = new Vector3((float)x, (float)y, 0); if (parent == null) { parent = new GameObject().transform; parent.gameObject.name = "Blocks"; } if (type == BlockType.player) { renderer.name = "PLAYER"; } renderer.transform.SetParent(parent); BlockVisualManager manager = renderer.GetComponent <BlockVisualManager>(); manager.block = this; manager.posx = x; manager.posy = y; visualiser = renderer; Sprite sprite = GetSprite(type); visualiser.sprite = sprite; visualiser.gameObject.GetComponent <RandomlyTexturedObjectController>().Initialize(); visualiser.gameObject.GetComponent <RandomlyTexturedObjectController> ().SetColor(GenerateTexture.GetPallete(type)); } }
/// <summary> /// Gets the sprite used for the given type /// </summary> public static Sprite GetSprite(BlockType type) { Texture2D texture = GenerateTexture.CreateRandomTexture(GenerateTexture.GetAvailableColors(GenerateTexture.GetPallete(type)), 10, 10); return(Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), Mathf.Max(new int[2] { texture.height, texture.width }))); }
/// <summary> /// Sets the type of the block, and updates it's display (if any) to reflect the new type /// </summary> public void SetType(BlockType newType) { if (newType != type) { if (isAlsoGoal && newType == BlockType.background) { type = BlockType.goal; if (visualiser != null) { visualiser.sprite = GetSprite(BlockType.goal); } return; } type = newType; if (visualiser != null) { visualiser.sprite = GetSprite(type); visualiser.gameObject.GetComponent <RandomlyTexturedObjectController> ().SetColor(GenerateTexture.GetPallete(type)); } } }