public virtual void SetActiveState(State _state, bool _isActive) { if (_isActive) { SetState(_state); } if (state != State.INVISIBLE && !gameObject.active) { gameObject.SetActive(true); } switch (state) { case State.INVISIBLE: gameObject.SetActive(false); break; case State.IDLE: case State.GROWSMALL: case State.GROWBIG: case State.BOUNCE: if (state == State.GROWBIG) { size = Size.BIG; } else if (state == State.GROWSMALL) { size = Size.SMALL; } if (_isActive) { PlayAnimation(state); } else { SetState(State.IDLE); } break; case State.MOVING: curStep = 0; mvmVelocity = oldMvmVelocity = Vector2.zero; movingPath = _isActive ? movingPath : null; // set z depth Vector3 pos = GetPosition(); pos.z = ZDepth.GetDepth(_isActive ? ZDepth.Layer.BALLONTOP : ZDepth.Layer.BALL); SetPosition(pos); // play anim idle if (_isActive) { PlayAnimation(State.IDLE); } else { SetActiveState(State.IDLE, true); } break; } }
// ========================================================== PRIVATE FUNC ========================================================== private void CreateBoard() { Camera cam = Camera.main; SpriteRenderer sr = GetComponent <SpriteRenderer>(); // set width, height of Board Background (following ratio vs camera's height) rectBoardSize.width = rectBoardSize.height = cam.orthographicSize * 2 * GameConfig.boardScale; transform.localScale = new Vector3(rectBoardSize.width / sr.bounds.size.x, rectBoardSize.height / sr.bounds.size.y, 1.0f); // set position of Board Background Vector3 pos = transform.position; pos.x = cam.transform.position.x; pos.y = (cam.transform.position.y - cam.orthographicSize) + rectBoardSize.height / 2.0f; transform.position = pos; // top-left position of Board rectBoardSize.x = sr.bounds.min.x; rectBoardSize.y = sr.bounds.max.y; // set width, height of Top Zone SpriteRenderer topPanelSr = topPanelZone.GetComponent <SpriteRenderer>(); topPanelZone.transform.localScale = new Vector3(rectBoardSize.width / topPanelSr.bounds.size.x, (cam.orthographicSize * 2 - rectBoardSize.height - 0.2f) / topPanelSr.bounds.size.y, 1.0f); pos.y = (cam.transform.position.y + cam.orthographicSize) - (cam.orthographicSize * 2 - rectBoardSize.height) / 2.0f; // set pos of Top Zone topPanelZone.transform.position = pos; TopPanelMgr.Instance.SetPositionBallPanel(Camera.main.WorldToScreenPoint(topPanelZone.transform.position)); TopPanelMgr.Instance.SetPositionScoreZone(Camera.main.WorldToScreenPoint(new Vector3(topPanelSr.bounds.min.x, topPanelSr.bounds.center.y, 0.0f))); TopPanelMgr.Instance.SetPositionHighScoreZone(Camera.main.WorldToScreenPoint(new Vector3(topPanelSr.bounds.max.x, topPanelSr.bounds.center.y, 0.0f))); // generate tiles tileSize = rectBoardSize.width / (float)GameConfig.boardDimension; for (int i = 0; i < Mathf.Pow(GameConfig.boardDimension, 2); i++) { GameObject genTile = GameObject.Instantiate(prefTile); SpriteRenderer tileSr = genTile.GetComponent <SpriteRenderer>(); genTile.name = "tile_" + i.ToString(); // set size Vector3 tileScale = new Vector3((tileSize - GameConfig.tileOffset) / tileSr.bounds.size.x, (tileSize - GameConfig.tileOffset) / tileSr.bounds.size.y, 1.0f); genTile.transform.localScale = tileScale; // set position int r = (i / GameConfig.boardDimension); int c = (i % GameConfig.boardDimension); Vector3 tilePos = new Vector3(rectBoardSize.x + (c + 0.5f) * tileSize, rectBoardSize.y - (r + 0.5f) * tileSize, ZDepth.GetDepth(ZDepth.Layer.TILE)); genTile.transform.position = tilePos; // set parent genTile.transform.parent = transform; // store tiles lTiles.Add(genTile.GetComponent <Tile>()); } }
// ========================================================== PUBLIC FUNC ========================================================== public void Init() { // get list of tiles on board List <Tile> lTiles = BoardMgr.Instance.GetListTiles(); // gen ball objects for (int i = 0; i < lTiles.Count; i++) { Tile tile = lTiles[i]; GameObject genBall = GameObject.Instantiate(prefBall, transform); Ball compBall = genBall.GetComponent <Ball>(); SpriteRenderer srBall = compBall.GetSpriteRenderer(); genBall.name = "ball_" + i.ToString(); // set position Vector3 posBall = compBall.GetPosition(); compBall.SetPosition(new Vector3(tile.GetPosition().x, tile.GetPosition().y, ZDepth.GetDepth(ZDepth.Layer.BALL))); // set scale if (ballSize == 0) { SpriteRenderer tileSr = tile.GetSpriteRenderer(); ballSize = tileSr.bounds.size.x * GameConfig.ballScale; ballSize = Mathf.Min(ballSize / srBall.bounds.size.x, ballSize / srBall.bounds.size.y); } compBall.SetLocalScale(new Vector3(ballSize, ballSize, 1.0f)); // init Ball component compBall.Init(i, Ball.Type.GHOST, Ball.State.INVISIBLE); // store ball lBalls.Add(compBall); lEmptyTiles.Add(i); // default all slots available to spawn } }
void Start() { // set z for bg transform.position = new Vector3(transform.position.x, transform.position.y, ZDepth.GetDepth(ZDepth.Layer.TILEBG)); }