Dungeon Raiders Proto 1 (match 3 puzzle unity)

Project 2023. 3. 17. 09:30
반응형

이미 던전 레이드와 비슷한 게임이 있음 
https://www.youtube.com/watch?v=Lkhaci9HcO4 

tile.aseprite
0.00MB

리소스 제작 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cell : MonoBehaviour
{
    public GameObject focusGo;
    public GameObject focusGo2;

    public bool isSelected = false;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Game : MonoBehaviour
{
    public GameObject cellPrefab;
    public int maxRow;
    public int maxCol;
    private Cell[,] cells;

    void Start()
    {
        this.cells = new Cell[this.maxRow, this.maxCol];

        for (int i = 0; i < maxRow; i++)
        {
            for (int j = 0; j < maxCol; j++)
            {
                var pos = new Vector2(j, -i);
                var go = Instantiate(this.cellPrefab, pos, Quaternion.identity);
                go.name = string.Format("({0},{1})", i, j);
                this.cells[i, j] = go.GetComponent<Cell>();
            }
        }
    }

    
    private bool isDown = false;
    private List<Cell> list = new List<Cell>();
    private Cell currentCell;

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            this.isDown = true;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            this.isDown = false;
            this.currentCell.focusGo.SetActive(false);
            this.currentCell.focusGo2.SetActive(false);
            this.currentCell = null;

            //3개 이상이면 블록 제거 
            if (list.Count >= 3)
            {
                foreach (var cell in this.list)
                    cell.gameObject.SetActive(false);
            }
            else 
            {
                Debug.Log("연결된 블록 갯수 부족");
                foreach (var cell in this.list)
                    cell.focusGo2.SetActive(false);
            }
            this.list.Clear();
        }

        if (this.isDown) {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            var hit = Physics2D.Raycast(ray.origin, ray.direction);
            if (hit.collider != null) {
                var cell = hit.collider.GetComponent<Cell>();

                if (this.currentCell != cell) {

                    if (this.currentCell != null) {
                        var prevCell = this.currentCell;
                        prevCell.focusGo.SetActive(false);
                    }

                    this.currentCell = cell;

                    if (!list.Contains(this.currentCell))
                    {
                        if (this.list.Count == 0)
                        {
                            Debug.Log("start");
                            list.Add(this.currentCell);
                            
                            this.currentCell.focusGo.SetActive(true);
                            this.currentCell.focusGo2.SetActive(true);
                            
                        }
                        else {
                            //마지막 선택된 셀과 인접해 있는가?
                            var dis = Vector3.Distance(list[list.Count - 1].transform.position, this.currentCell.transform.position);
                            Debug.Log(dis);
                            if (dis > 1.42f)
                            {
                                Debug.Log("인접해 있지 않음");
                                this.currentCell.focusGo.SetActive(true);
                            }
                            else {
                                this.list.Add(this.currentCell);
                                this.currentCell.focusGo.SetActive(true);
                                this.currentCell.focusGo2.SetActive(true);
                                Debug.Log("?");
                            }
                        }
                        
                    }
                    else
                    {
                        Debug.Log("이미 선택된 cell");
                        var idx = list.IndexOf(this.currentCell);
                        for (int i = idx + 1; i < list.Count; i++) {
                            list[i].focusGo.SetActive(false);
                            list[i].focusGo2.SetActive(false);
                        }
                        list.RemoveRange(idx+1, list.Count - 1 - idx);
                        this.currentCell.focusGo.SetActive(true);
                    }
                }
            }
        }

    }
}

 

참고 자료 : https://www.youtube.com/watch?v=dKYYQcbV0Bo&t=2 

 

반응형
: