Unityでぷよぷよを作ってみた 5
スポンサードリンク
ゴーストの表示
手順は以下の通り
- ゴーストの位置を実体と同じにする。
- 2つのゴーストをY座標でソートする。
- それぞれ、実体のある位置からハードドロップした位置を求める(ハードドロップの方法はテトリスと同じ)
- 位置が重なった場合、2で上にあった方を上にずらす。
public class PairGhost { private readonly List<Ghost> ghosts; public PairGhost() { ghosts = new List<Ghost>(); } public void Add(Ghost ghost) { ghosts.Add(ghost); } public void Move() { ghosts.ForEach(g => g.Move()); //Y座標でソート var ordered = ghosts.OrderBy(g => g.IntY).ToList<Ghost>(); //ハードドロップ ordered.ForEach(g => g.HardDrop()); //位置が重なった場合、上にあった方をずらす if (ordered[0].IntX == ordered[1].IntX) { ordered[1].transform.position += Vector3.up; } } public void Destory() { ghosts.ForEach(g => g.Destroy()); } }
public class Ghost : MonoBehaviour { private Puyo puyo; private SingleMover mover; public int IntX { get => Mathf.RoundToInt(transform.position.x); } public int IntY { get => Mathf.RoundToInt(transform.position.y); } public void Setup(Puyo puyo, Field field) { this.puyo = puyo; mover.Setup(field); } //実体と同じ位置に移動 public void Move() { transform.position = puyo.transform.position; } public void HardDrop() { mover.HardDrop(); } public void Destroy() { Destroy(gameObject); } private void Awake() { mover = GetComponent<SingleMover>(); } }