TAB 2021 겨울방학 학회활동 [슬기로운 코딩생활]
4주차 TIL입니다.
작성자 : 38기_이병헌
4주차 - 되돌리기, 리셋 기능 만들기, 이동 횟수 표시
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Board_controller : MonoBehaviour
{
public static int block_count, move_count; //블럭의 개수, 움직인 횟수
float[] block_pos; //블럭의 좌표 배열
// Start is called before the first frame update
void Start()
{
block_count = transform.childCount; //자식 오브젝트의 개수로 블럭 개수 저장
block_pos = new float[block_count];
for (int i = 0; i < block_count; i++)
{
if(transform.GetChild(i).name.Substring(0,1) == "X") //블럭의 이름의 앞글자에 따라 x, y좌표 저장
{
block_pos[i] = transform.GetChild(i).transform.position.x;
}
else
{
block_pos[i] = transform.GetChild(i).transform.position.y;
}
}
}
// Update is called once per frame
void Update()
{
}
public void resetButton()
{
for (int i = 0; i < block_count; i++)
{
if (transform.GetChild(i).name.Substring(0, 1) == "X") //블럭의 이름의 앞글자에 따라 x, y좌표 저장
{
transform.GetChild(i).transform.position = new Vector2(block_pos[i], transform.GetChild(i).transform.position.y);
}
else
{
transform.GetChild(i).transform.position = new Vector2(transform.GetChild(i).transform.position.x, block_pos[i]);
}
}
}
}
보드의 스크립트를 만들고 블럭들에 자식으로 접근하여 좌표를 배열에 저장한다.
리셋 버튼이 눌리면 저장된 좌표로 블럭들을 이동시킨다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Board_controller : MonoBehaviour
{
public static int move_count; //움직인 횟수
public static GameObject block_before; //선택 블럭
public static Vector2 block_vec; //블럭의 이전 위치
Text moves; //텍스트
// Start is called before the first frame update
void Start()
{
move_count = 0;
moves = GameObject.Find("Moves").GetComponent<Text>(); //Moves 텍스트 변수 적용
}
// Update is called once per frame
void Update()
{
moves.text = "Moves" + move_count.ToString(); //Moves 횟수 업데이트
}
public void backButton()
{
if (block_before) //선택 된 블럭이 있을시
{
transform.Find(block_before.name).transform.position = block_vec; //블럭 원위치
move_count -= 1; //이동횟수 - 1
block_before = null; //선택 블럭 초기화
}
}
}
마지막으로 옮겼던 블럭을 블럭 스크립트에서 가져와서 저장한 후, 되돌리기 버튼을 누르면 원위치 시킨다.
움직인 횟수를 텍스트로 나타내어 준다.
public void checkResult()
{
if (block_before)
{
if (transform.Find(block_before.name).transform.position.x >= 300) //블럭이 출구를 통과하면 종료화면 표시
{
end_screen.SetActive(true);
if (move_count == minimum_moves) //이동횟수에 따라 문구 변화
{
result.text = "Perfect!!";
}
else
{
result.text = "Win!";
}
}
}
}
게임을 클리어하면 결과화면이 나오도록 한다. 최소이동횟수로 클리어시 다른 문구가 나온다.
'21 - 2학기 > 슬기로운 코딩생활' 카테고리의 다른 글
[TIL]Flutter 개발하기(Navigator,Drawer,ListView) (0) | 2022.01.26 |
---|---|
[TIL] 바닐라JS와 비교 및 리액트 기초 문법 (0) | 2022.01.26 |
[TIL]4주차:기존 앱 개선하기 (0) | 2022.01.25 |
[TIL]3주차 바닐라JS(3)_momentum 클론코딩 (0) | 2022.01.19 |
[TIL] 유니티2d로 게임 개발 3주차 (0) | 2022.01.19 |