public void SetSprite(int id, AddableType type) { Util.Assert(type == AddableType.BOOSTER || type == AddableType.ENEMY); Sprite sprite = null; if (type == AddableType.BOOSTER) { sprite = SpriteList.GetBooster(id); } else // == Enemy { sprite = SpriteList.GetEnemy(id); } this.image.sprite = sprite; this.spriteID = id; }
private void Start() { if (this.TileType == TileType.BOOSTER) { this.image.sprite = SpriteList.GetBooster(spriteID); } else if (this.TileType == TileType.ENEMY) { this.image.sprite = SpriteList.GetEnemy(spriteID); } if (changeSprite != null) { changeSprite.onClick.AddListener(new UnityEngine.Events.UnityAction( () => { SelectThisTile(); if (!SpritePicker.Instance.IsOpen || currentPickingSprite != this) { currentPickingSprite = this; SpritePicker.Instance.Activate(this, this.TileType, pickable => { image.sprite = pickable.Sprite; spriteID = pickable.ID; foreach (Element e in AllAssociatedElementsInLevel) { e.Sprite = pickable.Sprite; } }); } else { SpritePicker.Instance.Deactivate(); currentPickingSprite = null; } } )); } }
// Loading into game public static void DeserializeDungeonToPlayable( Upload upload, string stage, int sceneOnVictory, int sceneOnExit, DungeonInfo infoTarget, GameObject floorsParent, GameObject floorPrefab, GameObject wallPrefab, GameObject upstairsPrefab, GameObject downstairsPrefab, GameObject goldKeyPrefab, GameObject blueKeyPrefab, GameObject redKeyPrefab, GameObject playerPrefab, GameObject exitPrefab, GameObject goldDoorPrefab, GameObject blueDoorPrefab, GameObject redDoorPrefab, GameObject enemyPrefab, GameObject boosterPrefab ) { string json = upload.LevelJson.Replace("\\", ""); Dungeon dungeon = JsonUtility.FromJson <Dungeon>(json); Addable[] addables = dungeon.Addables; Floor[] floors = dungeon.Floors; StartingValues startingValues = dungeon.StartingValues; infoTarget.Init(stage, upload.LevelName, sceneOnExit); for (int i = 0; i < floors.Length; i++) { GameObject floor = GameObject.Instantiate(floorPrefab, floorsParent.transform); floor.transform.localPosition = Vector3.zero; floor.SetActive(false); Tile[] tiles = floor.GetComponentsInChildren <Tile>(true); int[] indices = floors[i].Indices; for (int j = 0; j < indices.Length; j++) { int index = indices[j]; Tile current = tiles[j]; if (index != NO_ELEMENT) { Addable addable = addables[index]; GameObject instantiated = null; if (addable.AddableType == AddableType.STATIC) { StaticData data = addable.StaticData; switch (data.TileType) { case TileType.WALL: instantiated = GameObject.Instantiate(wallPrefab, current.transform); break; case TileType.UP_STAIRS: instantiated = GameObject.Instantiate(upstairsPrefab, current.transform); break; case TileType.DOWN_STAIRS: instantiated = GameObject.Instantiate(downstairsPrefab, current.transform); break; case TileType.GOLD_KEY: instantiated = GameObject.Instantiate(goldKeyPrefab, current.transform); break; case TileType.BLUE_KEY: instantiated = GameObject.Instantiate(blueKeyPrefab, current.transform); break; case TileType.RED_KEY: instantiated = GameObject.Instantiate(redKeyPrefab, current.transform); break; case TileType.PLAYER: instantiated = GameObject.Instantiate(playerPrefab, current.transform); instantiated.GetComponent <Player>().Init(startingValues); floor.SetActive(true); break; case TileType.EXIT: instantiated = GameObject.Instantiate(exitPrefab, current.transform); instantiated.GetComponent <Exit>().Init(sceneOnVictory); break; case TileType.GOLD_DOOR: instantiated = GameObject.Instantiate(goldDoorPrefab, current.transform); break; case TileType.BLUE_DOOR: instantiated = GameObject.Instantiate(blueDoorPrefab, current.transform); break; case TileType.RED_DOOR: instantiated = GameObject.Instantiate(redDoorPrefab, current.transform); break; default: Util.Assert(false, "Unhandled type: {0}", data.TileType); break; } } else if (addable.AddableType == AddableType.ENEMY) { EnemyData data = addable.EnemyData; instantiated = GameObject.Instantiate(enemyPrefab, current.transform); instantiated.GetComponent <Stats>().Init(data.Life, data.Power, data.Defense, data.Stars); instantiated.GetComponentInChildren <SpriteRenderer>().sprite = SpriteList.GetEnemy(data.SpriteID); } else if (addable.AddableType == AddableType.BOOSTER) { BoosterData data = addable.BoosterData; instantiated = GameObject.Instantiate(boosterPrefab, current.transform); instantiated.GetComponent <Pickup>().Init(data.StatToBoost, data.AmountBoosted); instantiated.GetComponentInChildren <SpriteRenderer>().sprite = SpriteList.GetBooster(data.SpriteID); } else { Util.Assert(false, "Unhandled type: {0}", addable.GetType()); } } } } }
public void Init(Dungeon dungeon) { Addable[] addables = dungeon.Addables; int playerIndex = PLACEHOLDER_PLAYER_INDEX; // find player index for (int i = 0; i < addables.Length && playerIndex == PLACEHOLDER_PLAYER_INDEX; i++) { Addable addable = addables[i]; if (addable.AddableType == AddableType.STATIC && addable.StaticData.TileType == TileType.PLAYER) { playerIndex = i; } } // find floor with player int[] startingFloor = null; Floor[] dungeonFloors = dungeon.Floors; for (int i = 0; i < dungeonFloors.Length && startingFloor == null; i++) { int[] indices = dungeonFloors[i].Indices; for (int j = 0; j < indices.Length && startingFloor == null; j++) { int index = indices[j]; if (index == playerIndex) { startingFloor = indices; } } } // Who submits an empty level??? if (startingFloor != null) { // setup preview, startingFloor length should match preview for (int i = 0; i < startingFloor.Length; i++) { PreviewElement element = elements[i]; int index = startingFloor[i]; element.IsVisible = (index != SerializationUtil.NO_ELEMENT); if (index != SerializationUtil.NO_ELEMENT) { Addable addable = addables[index]; Sprite sprite = null; switch (addable.AddableType) { case AddableType.STATIC: sprite = SpriteList.GetStatic(addable.StaticData.TileType); break; case AddableType.BOOSTER: sprite = SpriteList.GetBooster(addable.BoosterData.SpriteID); break; case AddableType.ENEMY: sprite = SpriteList.GetEnemy(addable.EnemyData.SpriteID); break; } element.Sprite = sprite; } } } }