/// <summary> /// 縦方向に移動できるかチェック /// </summary> /// <param name="data"></param> /// <param name="moveY"></param> /// <returns></returns> private bool CheckMovingVertical(TamaData data, int moveY) { if (data == null) { return(false); } int newY = data.Y + moveY; // 半分浮いているなら縦に移動可能 if (data.Half) { return(true); } // 枠のチェック if ((newY < 0) || (newY >= setting.rowsCount)) { return(false); } // フィールドのチェック if (GetTamaFromField(data.X, newY) != null) { return(false); } return(true); }
/// <summary> /// たまをキャッシュ /// </summary> public void InitTamaCache(PlayFieldSetting setting, Transform transform) { if (tamaCache != null) { return; } var count = setting.columnsCount * setting.rowsCount + 4; tamaCache = new Stack <TamaData>(count); for (int i = 0; i < count; ++i) { var s = GameObject.Instantiate <SpriteRenderer>(tamaPrefab); //s.sprite = atlas.GetSprite("l01"); s.transform.parent = transform; s.transform.localPosition = Vector3.zero; s.transform.localScale = Vector3.one; s.enabled = false; var e = s.transform.GetChild(0).gameObject.GetComponent <SpriteRenderer>(); var tama = new TamaData() { Sprite = s, Spark = e }; tamaCache.Push(tama); } }
/// <summary> /// たまのペアを回転 /// </summary> /// <param name="main"></param> /// <param name="sub"></param> /// <param name="cw">true:時計回り</param> private void RotateTamaPair(TamaData main, TamaData sub, bool cw) { (int rotX, int rotY) = CalculateRotateOffset(main, sub, cw); int subX = main.X + rotX; int subY = main.Y + rotY; // フィールドが空ではない場合 if (!CheckFieldBlank(subX, subY)) { // メインが動かせるかチェック if (CheckMove(main, -rotX, -rotY)) { subX = main.X + rotX; subY = main.Y + rotY; } // 動かせなくて縦なら上下入れ替え else if (main.X == sub.X) { int x = main.X; int mainY = sub.Y; subY = main.Y; SetTamaPosition(main, x, mainY); SetTamaPosition(sub, x, subY); return; } } // サブの位置を確定 SetTamaPosition(sub, subX, subY); }
/// <summary> /// たまをキャッシュに戻す /// </summary> /// <param name="data"></param> public void ReleaseTama(TamaData data) { data.Sprite.enabled = false; data.Spark.enabled = false; tamaCache.Push(data); }
/// <summary> /// たまを1つ生成 /// </summary> /// <returns></returns> private TamaData GenerateTama() { TamaData data = tamaCache.Pop(); data.Color = (ColorType)tamaRand.Next(4); data.Half = false; return(data); }
/// <summary> /// たまを右に動かす /// </summary> /// <param name="data"></param> /// <param name="x"></param> private void MoveTamaRight(TamaData data, int x = 1) { if (data == null) { return; } SetTamaPosition(data, data.X + x, data.Y); }
private (int, int) CalculateRotateOffset(TamaData main, TamaData sub, bool cw) { // サブのオフセット int x = sub.X - main.X; int y = sub.Y - main.Y; // 回転後のオフセット return(cw ? (y, -x) : (-y, x)); }
/// <summary> /// こうげきだまを1つ生成 /// </summary> /// <param name="color"></param> /// <returns></returns> public TamaData GenerateAttackTama(ColorType color) { TamaData data = tamaCache.Pop(); data.Color = color; data.Half = false; data.State = TamaStateType.Small; SetupTamaSprite(data); return(data); }
/// <summary> /// たまのペアを上に動かす /// </summary> /// <param name="main"></param> /// <param name="sub"></param> /// <param name="y"></param> /// <returns>動かしたらtrue</returns> private bool MoveTamaPairUp(TamaData main, TamaData sub) { if (!CheckMovingVertical(main, 1) || !CheckMovingVertical(sub, 1)) { return(false); } MoveTamaUp(main); MoveTamaUp(sub); return(true); }
/// <summary> /// たまのペアを右に動かす /// </summary> /// <param name="main"></param> /// <param name="sub"></param> /// <param name="x"></param> /// <returns>動かしたらtrue</returns> private bool MoveTamaPairRight(TamaData main, TamaData sub, int x = 1) { if (!CheckMovingHorizontal(main, 1) || !CheckMovingHorizontal(sub, 1)) { return(false); } MoveTamaRight(main, x); MoveTamaRight(sub, x); return(true); }
/// <summary> /// 指定方向に動かせるなら動かす /// </summary> /// <param name="main"></param> /// <param name="moveX"></param> /// <param name="moveY"></param> /// <returns></returns> private bool CheckMove(TamaData main, int moveX, int moveY) { int x = main.X + moveX; int y = main.Y + moveY; if (GetTamaFromField(x, y) != null) { return(false); } // 動かせるなら動かす SetTamaPosition(main, x, y); return(true); }
/// <summary> /// たまの見た目を設定 /// </summary> /// <param name="data"></param> /// <param name="col"></param> /// <param name="row"></param> private void SetupTamaSprite(TamaData data) { int index = (int)data.Color; if (data.State == TamaStateType.Small) { index += 4; } data.Sprite.sprite = tamaSprites[index]; data.Sprite.enabled = true; //data.Sprite.transform.localPosition = new Vector3(setting.cellSize * col, setting.cellSize * row, 0); data.Sprite.transform.localScale = Vector3.one; data.Spark.enabled = false; }
/// <summary> /// たまの位置を設定 /// </summary> private void SetTamaPosition(TamaData data, int col, int row) { if (data == null) { return; } data.X = col; data.Y = row; float y = (float)row; if (data.Half) { y += 0.5f; // 半分浮いている } data.Sprite.transform.localPosition = new Vector3(setting.cellSize * col, setting.cellSize * y, 0); }
/// <summary> /// 横方向に移動できるかどうかチェックする /// </summary> /// <param name="data"></param> /// <param name="moveX"></param> /// <returns></returns> private bool CheckMovingHorizontal(TamaData data, int moveX) { int newX = data.X + moveX; // 枠のチェック if ((newX < 0) || (newX >= setting.columnsCount)) { return(false); } // フィールドのチェック if (GetTamaFromField(newX, data.Y) != null) { return(false); } return(true); }
/// <summary> /// たまを下に動かす /// </summary> /// <param name="data"></param> /// <param name="useHalf">半分ずつ動かすかどうか</param> private void MoveTamaDown(TamaData data, bool useHalf = true) { if (data == null) { return; } // 半分浮いてる場合 if (data.Half) { data.Half = false; SetTamaPosition(data, data.X, data.Y); } else { data.Half = useHalf; SetTamaPosition(data, data.X, data.Y - 1); } }
/// <summary> /// たまを上に動かす /// </summary> /// <param name="data"></param> /// <param name="useHalf">半分ずつ動かすかどうか</param> private void MoveTamaUp(TamaData data, bool useHalf = true) { if (data == null) { return; } if (data.Half || !useHalf) { // 半分浮いてるか、半分ずつ動かさないなら一つ上にあげる data.Half = false; SetTamaPosition(data, data.X, data.Y + 1); } else { // 半分浮かせる data.Half = true; SetTamaPosition(data, data.X, data.Y); } }
/// <summary> /// フィールドのたまを落下させる /// </summary> private bool DropTamaInFIeld() { bool falling = false; for (int x = 0; x < setting.columnsCount; ++x) { for (int y = 0; y < setting.rowsCount; ++y) { //TamaData data = tamaField[y, x]; TamaData data = GetTamaFromField(x, y); if ((data != null) && (CheckMovingVertical(data, -1))) { RemoveTamaFromField(x, y); MoveTamaDown(data); PutTamaToField(data); falling = true; } } } return(falling); }
/// <summary> /// たまを現在の座標に設置 /// </summary> /// <param name="data"></param> private void PutTamaToField(TamaData data) { PutTamaToField(data.X, data.Y, data); }
/// <summary> /// 接続をチェック /// </summary> /// <param name="data"></param> /// <param name="group"></param> /// <param name="found"></param> /// <returns>連結されたものある場合true</returns> private bool CheckTamaFieldLink(TamaData data, int group, bool found = false) { // 隣 (int x, int y, bool b)[] nexts =
/// <summary> /// フィールドのたまを設置 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="data"></param> private void PutTamaToField(int x, int y, TamaData data) { tamaField[y, x] = data; }