Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 크래프톤 정글 게임 랩
- 게임 독학
- 유니티 체스
- unity 강의
- unity
- unity 개발
- node.js
- 유니티 스토리
- 오픽 im1 5일
- Unity Chess
- 유니티 대화
- ETRI 연구연수
- 유니티 독학
- unity 설치
- unity 공부
- 유니티 퀘스트
- 크래프톤 정글 게임랩
- 크래프톤 정글랩
- 오픽 im1
- Unity 독학
- 유니티 미연시
- 크래프톤 게임 정글 랩
- 크래프톤
- unity 게임
- protobuf 란?
- 유니티
- unity 게임 개발
- protobuf란
- 게임 개발
- 게임 개발 독학
Archives
- Today
- Total
하참이의 아이디어노트
유니티로 2D 체스 만들기 (3) 본문
https://www.youtube.com/watch?v=MLF9bOBCeqg&list=PL72bFQgYwe2t_T4ha7nJWc0OduWANpqz9&index=5
글 작성에 앞서 이 주제로 작성하게 되는 글들은 전부 위 영상을 참고해서 작성하였음을 미리 밝힙니다.
지난 글에서는 체스말 프리팹을 만들어보았습니다. 이번 시간에는 게임이 시작되었을 때 체스말들을 보드에 세팅하고 스프라이트를 변경하는 코드를 배워보도록 합시다.
지난번에 만들었던 Chessman의 코드에 다음과 같은 메소드들을 추가하여 수정합니다.
public int GetXBoard()
{
return xBoard;
}
public int GetYBoard()
{
return yBoard;
}
public void SetXBoard(int x)
{
this.xBoard = x;
}
public void SetYBoard(int y)
{
this.yBoard = y;
}
체스말의 위치(포지션)을 나타내는 xBoard와 yBoard를 반환하는 간단한 메소드입니다. public을 사용하지 않고 프로퍼티를 사용해 반환하는 이유는 은닉성을 향상시켜 다른 클래스에서 포지션 값을 함부로 바꾸지 못하게 하고, 값을 변경할 경우 오류 로그 메세지로 디버깅과정에서 어디서 오류가 났는지를 알 수 있게 합니다.
전체 수정된 코드는 다음과 같습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chessman : MonoBehaviour
{
// References
public GameObject controller;
public GameObject movePlate;
// Positions
private int xBoard = -1;
private int yBoard = -1;
// Variable to keep track of "black" player of "white" player
private string player;
// References for all the sptrites that the chesspiece can be
public Sprite black_queen, black_knight, black_bishop, black_king, black_rook, black_pawn;
public Sprite white_queen, white_knight, white_bishop, white_king, white_rook, white_pawn;
public void Activate()
{
controller = GameObject.FindGameObjectWithTag("GameController");
// take the instantiated location and adjust the transform
SetCoords();
switch(this.name)
{
case "black_queen": this.GetComponent<SpriteRenderer>().sprite = black_queen; break;
case "black_knight": this.GetComponent<SpriteRenderer>().sprite = black_knight; break;
case "black_bishop": this.GetComponent<SpriteRenderer>().sprite = black_bishop; break;
case "black_king": this.GetComponent<SpriteRenderer>().sprite = black_king; break;
case "black_rook": this.GetComponent<SpriteRenderer>().sprite = black_rook; break;
case "black_pawn": this.GetComponent<SpriteRenderer>().sprite = black_pawn; break;
case "white_queen": this.GetComponent<SpriteRenderer>().sprite = white_queen; break;
case "white_knight": this.GetComponent<SpriteRenderer>().sprite = white_knight; break;
case "white_bishop": this.GetComponent<SpriteRenderer>().sprite = white_bishop; break;
case "white_king": this.GetComponent<SpriteRenderer>().sprite = white_king; break;
case "white_rook": this.GetComponent<SpriteRenderer>().sprite = white_rook; break;
case "white_pawn": this.GetComponent<SpriteRenderer>().sprite = white_pawn; break;
}
}
public void SetCoords()
{
float x = xBoard;
float y = yBoard;
x *= 0.66f;
y *= 0.66f;
x += -2.3f;
y += -2.3f;
this.transform.position = new Vector3(x, y, -1.0f);
}
public int GetXBoard()
{
return xBoard;
}
public int GetYBoard()
{
return yBoard;
}
public void SetXBoard(int x)
{
this.xBoard = x;
}
public void SetYBoard(int y)
{
this.yBoard = y;
}
}
Gmae.cs의 코드를 다음과 같이 수정합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Game : MonoBehaviour
{
public GameObject chesspiece;
// Positions and team for each chesspiece
private GameObject[,] positions = new GameObject[8, 8];
private GameObject[] playerBlack = new GameObject[16];
private GameObject[] playerWhite = new GameObject[16];
private string currentPlayer = "white";
private bool gameOver = false;
void Start()
{
playerWhite = new GameObject[] { Create("white_rook", 0, 0), Create("white_knight", 1, 0),
Create("white_bishop", 2, 0), Create("white_queen", 3, 0), Create("white_king", 4, 0),
Create("white_bishop", 5, 0), Create("white_knight", 6, 0), Create("white_rook", 7, 0),
Create("white_pawn", 0, 1), Create("white_pawn", 1, 1), Create("white_pawn", 2, 1),
Create("white_pawn", 3, 1), Create("white_pawn", 4, 1), Create("white_pawn", 5, 1),
Create("white_pawn", 6, 1), Create("white_pawn", 7, 1) };
playerBlack = new GameObject[] { Create("black_rook", 0, 7), Create("black_knight", 1, 7),
Create("black_bishop", 2, 7), Create("black_queen", 3, 7), Create("black_king", 4, 7),
Create("black_bishop", 5, 7), Create("black_knight", 6, 7), Create("black_rook", 7, 7),
Create("black_pawn", 0, 6), Create("black_pawn", 1, 6), Create("black_pawn", 2, 6),
Create("black_pawn", 3, 6), Create("black_pawn", 4, 6), Create("black_pawn", 5, 6),
Create("black_pawn", 6, 6), Create("black_pawn", 7, 6) };
// Set all piece positions on the position board
for (int i = 0; i < playerBlack.Length; i++)
{
SetPosition(playerBlack[i]);
SetPosition(playerWhite[i]);
}
}
public GameObject Create(string name, int x, int y)
{
GameObject obj = Instantiate(chesspiece, new Vector3(0, 0, 1), Quaternion.identity);
Chessman cm = obj.GetComponent<Chessman>();
cm.name = name;
cm.SetXBoard(x);
cm.SetYBoard(y);
cm.Activate();
return obj;
}
public void SetPosition(GameObject obj)
{
Chessman cm = obj.GetComponent<Chessman>();
positions[cm.GetXBoard(), cm.GetYBoard()] = obj;
}
}
코드에 대한 설명은 다음과 같습니다.
positions 변수는 8*8 체스판의 말 위에 올라가 있는 체스말, 또는 체스말의 이동경로(movePlate)를 나타내기 위해 사용할 위치 변수로 사용하는 GameObject 변수입니다.
playerBlack, playerWhite는 흑백 각각의 플레이어가 가지고 있는 체스 말들을 참조하기 위하여 사용하는 변수입니다.
currentPlayer는 체스 차례를 나타내기 위한 String 변수입니다.
체크메이트 상태가 되면 게임이 오버되고 말을 움직이지 못하기 때문에 gameOver 변수로 조절을 합니다.
게임이 시작되면 호출되는 유니티 이벤트 메서드인 strat() 입니다. 게임이 시작되면 각 좌표에 맞는 체스말을 Create 메소드로 생성합니다.
또한 for문을 사용하여 Game 메소드에서도 체스말의 포지션을 세팅합니다.
Create() 메소드는 체스 말의 이름과 체스보드상의 좌표를 받아 해당 위치에 Instantiate를 사용하여 체스말 프리팹을 생성합니다. 또한 해당 프리팹에 체스보드 좌표, 이름을 할당한 뒤 반환합니다.
SetPosition() 메소드는 체스말 오브젝트를 받아 chessman 컴포넌트의 속성을 읽은 뒤 해당 체스말의 위치를 변경합니다.
이제 체스말이 잘 생성되는지 확인하기 위하여 지난번에 생성했던 Chesspiece를 Hierachy창에서 제거합니다.
다음엔 프리팹화 한 Chesspiece를 Controller의 Game 컴포넌트의 Chesspiece 오브젝트로 드래그 한다.
게임 재생버튼을 눌러 잘 생성되는지 확인해봅니다.
이번시간에서는 게임매니저와 프리팹을 다루어 체스말을 생성하는 단계까지 구현해보았습니다. 긴 글 읽어주셔서 감사합니다!!
'Unity > Unity Chess' 카테고리의 다른 글
유니티로 2D 체스 만들기 (5) (0) | 2024.03.20 |
---|---|
유니티로 2D 체스 만들기 (4) (0) | 2024.03.18 |
유니티로 2D 체스 만들기 (2) (0) | 2024.03.16 |
유니티로 2D 체스 만들기 (1) (0) | 2024.03.16 |
유니티로 2D 체스 만들기 (0) (0) | 2024.03.16 |