C#/텍스트 게임 "Shelter"
3일차. 스크린 관리
홍삼맛
2023. 11. 8. 12:32
📌 준비
로비, 메인, 인벤토리, 장비관리 등등, 앞으로 화면을 출력하고 제어할 클래스 구현
📌 개발
📄 ScreenManager.cs
public class ScreenManager
{
// 스크린 타입에 맞게 화면 출력
public void DisplayScreen(ScreenType screenType)
{
IScreen screen = screenType switch
{
ScreenType.Main => new ScreenMain(),
ScreenType.MyInfo => new ScreenMyInfo(),
ScreenType.Inventory => new ScreenInventory(),
ScreenType.Equipment => new ScreenEquipment(),
_ => new ScreenMain()
};
screen.DrawScreen();
}
}
// 스크린 타입
public enum ScreenType
{
Main,
MyInfo,
Inventory,
Equipment,
}
// 키 조작 타입
public enum Command
{
Exit,
Interact,
Nothing,
MoveTop,
MoveBottom,
}
- DisplayScreen
- ScreenType을 통해, 해당 타입에 맞는 스크린 클래스 생성 그리고 해당 스크린 클래스로 화면 출력
📄 DrawScreen (Method)
// ScreenEquipment 클래스, 화면 출력 매서드
public void DrawScreen()
{
do
{
Console.Clear();
Console.Write("[인벤토리] - ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("장착 관리");
Console.WriteLine();
Console.ResetColor();
// 아이템 목록 표시
GameManager.player.DisplayEquipmentList(currentItemIdx);
Console.WriteLine();
Console.WriteLine("[방향키 ↑ ↓: 위 아래로 이동] [Enter: 아이템 장착] [Esc: 인벤토리로 돌아가기]");
}
while (ManageInput());
}
- DrawScreen
- do-while 구문으로 입력할 때마다 화면 출력
- 특정 명령어 키 입력시 ManageInput 에서 bool check로 화면 탈출
📄 ManageInput (Method)
// ScreenEquipment 클래스, 콘솔 조작 관리 메서드
public bool ManageInput()
{
var key = Console.ReadKey();
var commands = key.Key switch
{
ConsoleKey.UpArrow => Command.MoveTop,
ConsoleKey.DownArrow => Command.MoveBottom,
ConsoleKey.Enter => Command.Interact,
ConsoleKey.Escape => Command.Exit,
_ => Command.Nothing
};
// 입력 받은 명령키에 따라 기능 작동
OnCommand(commands);
// 화면 나가기
return commands != Command.Exit;
}
- ManageInput
- 명령 받은 조작 키에 따라 Command 타입 정의
- 정의된 명령어를 클래스 내의 OnCommand 메서드를 통해 기능 작동