kgc2012 온라인 게임을 위한 게임 오브젝트 설계
TRANSCRIPT
박성준 ([email protected])
Korea Games Conference KGC 2012
게임 + 오브젝트
오브젝트?
게임?
• 플레이어, 몬스터 등 캐릭터
• 바위, 나무, 건물 등 배경 오브젝트
• 함정, 회복아이템 등 트리거
• 미사일, 수류탄, 파편 등
• 카메라, 광원 등
• 캐릭터가 서있는 지형
오브젝
트
캐릭터
지형
프랍
트리거
광원
카메라
발사체
레벨
게임 오브젝트!
오브젝트
오브젝트
오브젝트
오브젝
트
캐릭터
지형
프랍
트리거
광원
카메라
발사체
레벨
상호작용
게임 = 게임 오브젝트 사이의 상호작용
온라인 게임
게임 오브젝트 중심 개발
• 게임 오브젝트들 사이의 이벤트
• 이동 / 공격 / 피격
• 아이템 떨어뜨리기 / 줍기
• 등 게임 내적인 이벤트
컴포넌트 + 게임 오브젝트
컴포넌트 기반 설계 구조
온라인 게임은 컴포넌트 기반 설계를 해야 한다!
알과 닭 객체를 계층 구조로 만든다고 가정해봅시다..
슈퍼닭을 만듭시다! 슈퍼닭은 날아다닙니다!
그렇다면 컴포넌트 기반 설계 방식이라면?!
온라인 게임 + 컴포넌트 설계 = 행복한 개발
컴포넌트 설계는 좋지만..
• 의존적인 컴포넌트 구분!
• 메시지 통신을 하지 말자!
• 그렇지만 의존성은 피하자!
• 스크립트 친화적 구현!
의존적인 컴포넌트?
Controller
• 의존적인 컴포넌트는 컨트롤러의 속성을 가진다 • 다른 컴포넌트에 접근하는걸 고려하여 디자인 • 컴포넌트와는 다르게 활성화/비활성화 존재
Entity
AAA Controller
AAA Component
BBB Component
BBB Controller
Owner Entity가 가진 다른 컴포넌트와 컨트롤러에 접근 가능!
메시지 통신을 하지 말자!
분산되는 로직 처리
= 응집력이 떨어진다
순서대로 로직 처리가 힘듦
= 작업 스트레스 증가
1. 액션 단위로 로직 처리 2. 액션객체가 컴포넌트들에 접근
3. 게임 오브젝트는 액션들을 관리
액션
죄송합니다.. 시간에 쫓겨 급해서 Boost 전처리기 메타를 썼습니다..
전처리기 메타 코드가 만들어내는 코드
Action의 데이터
위에서 정의한 Action데이터로 virtual void Do( 데이터 ) = 0; 순수 가상 함수가 만들어진다
GetOwner()를 이용하여 Action이 실행된 Entity를 얻어올 수 있다
액션의 구현
Entity
AAA Action
AAA Component
BBB Component
AAA Controller
Owner Entity가 가진 다른 컴포넌트와 컨트롤러에 접근 가능!
DoAction 호출
생성
접근
Entity
Entity Handler DoAction 함수를 가진다
다른 객체에 접근을 위해..
Controller와 Action의 Owner로 노출
다른 Entity / 또는 외부에서 접근
Entity:: Update
Controller
Entity:: Update Action
Component System:: Update
업데이트 순서
Server Object
Client Object
Server Object
Server Object
Client Object
Client Object
Server
Client
Client Object
Client Object
Client Object
Client
NPC1
NPC1
NPC1
Player1
Player1 Player1
NPC2
NPC2 NPC2
게임 오브젝트와 네트워크 통신
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Client Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Client Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Client Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Client Base
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Client Base
Client Base
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Server Object
Server Object
Server Object
NPC1 Player1 NPC2
Server Base
ID : 0 ID : 1 ID : 2
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Client Base
Client Base
Player1
Client Object
Client Object
Client Object
NPC1 NPC2
ID : 0 ID : 1 ID : 2
Entity가 받은 메시지는 어떻게?
Action과 Message를 연결 Message가 ServerObject/ClientObject에 도착하면 연결된 Action 실행
Network Action
Manager
Server/ Client Object
Network - OnRecv
Action
생성 함수
메시지에서 사용할 데이터 형식 메시지 타입
선언한 순서로 메시지ID 할당
버퍼에 데이터 넣기
버퍼에서 데이터 꺼내기
메시지ID + 데이터 타입
네트워크로 받은 메시지 버퍼
메시지를 받은 엔터티 액션 실행을 위한
함수포인터
ServerObject와 연결 ClientObject와 연결
메시지와 Action의 연결 선언
메시지와 Action의 연결 정의
메시지와 Action의 데이터가 같아야만 컴파일이 된다
액션과 네트워크 통신의 연결
게임 오브젝트와 스크립트
lua_state
컨트롤러 테이블 컨트롤러 테이블 컨트롤러 테이블
Controller Table
컨트롤러 테이블 컨트롤러 테이블 컨트롤러 테이블
Component Table
컨트롤러 테이블 컨트롤러 테이블 컨트롤러 테이블 Instance
Table Lua Thread
컨트롤러와 스크립트의 연결
Entity
Controller Script
Manager
Controller 정보 lua_state에 입력
Entity별로 lua table과 lua thread 생성
Controller Instance lua table 생성하여 Controller userdata와 연결
C++
AAA Controller Instance1
AAA Controller
AAA Controller Instance3
AAA Controller Instance2
AAA Controller Instance4
Lua
AAA Controller Instance1
AAA Controller Instance2
AAA Controller Instance3
AAA Controller Instance4
Class
Instance
table
table
AAA Controller Instance1
AAA Controller
userdata
meta table
AAA Controller Instance1
AAA Controller Instance1
AAA Controller Instance1
부모 클래스와 자식 클래스 모두 연결할 경우?
C++
AAA Controller
Lua
AAA Controller Instance
상속
BBB Controller
BBB Controller Instance
AAA Controller
AAA Controller Instance
meta table
BBB Controller
BBB Controller Instance
BBB Controller Instance AAA
Controller Instance
meta table
스크립트와 연결
스크립트 함수 실행
C++ 함수 실행
스크립트에서 컴포넌트 접근
Entity
Component Script
Manager
Component 정보 lua_state에 입력
C++
AAA Component Instance1
AAA Component
AAA Component Instance3
AAA Component Instance2
AAA Component Instance4
Lua
Class
Instance
table AAA Component
userdata AAA
Component Instance1
AAA Component Instance2
AAA Component Instance3
AAA Component Instance4
Entity
Entity Wrapper
Entity
Controller Script
Manager
Controller 정보 lua_state에 입력
Entity별로 lua table과 lua thread 생성
Controller Instance lua table 생성하여 Controller userdata와 연결
Controller의 owner로 EntityWrapper 생성 후 Controller Instance lua table에 “owner” Set
KGC 2011 에서…
강연이 끝난 후..
당신껀 멀티 쓰레딩이 안되자나?!
천재 꽃 미남 슈퍼 프로그래머 트위터 : @BlindRendererKR 프로필 :
지금은 멀티쓰레딩도 가능합니다
Game Loop
Animation Physics Rendering Audio
어떻게?!
Game Loop
Set up
Thread0
Thread1 Thread2
Thread3
Process Result
• Controller는 Owner가 가지고 있는 것만 접근
-> Owner의 Component / Controller
• 다른 Entity에는 DoAction으로 할 일 전달
• Entity별로 Controller들 업데이트
Entity별로 Action Queue 처리
Component별로 업데이트
현재 구조를 보면…
• Entity는 다른 Entity에 직접 접근 X
• Component는 다른 Component에 직접 접근 X
• Entity별로 Controller들 업데이트
Entity별로 Action Queue 처리
Component별로 업데이트
다시 정리해보면
Entity:: Update
Controller
Entity:: Update Action
Component System:: Update
업데이트 순서
Game Loop
Set up
Thread0 Thread1 Thread2 Thread3
Set up
Thread0 Thread1 Thread2 Thread3
Set up
Thread0 Thread1 Thread2 Thread3
Process Result
Controller
Action
Component
감사합니다
Special Thanks to : Dragonfly SS팀 일동/ 게임개발포에버 필진 일동 / 민철님 / 퐆삼촌 / 조진현님 / 김정우님 / 오즈형 / 레아형 / 민근형 / 티스형 / Hybrid님 / 창희님 / 대마왕님 / banhae님 / 정균님 / 스티브 / 바레로 박사님 / 김토마님 / Cagetu님 / 달땡땡님 / 퐁퐁퐁님 / 진짜님 / 박PD님 / 비토님 / 어제 같이 술 마신 분들 / 트친분들
박성준 mail : [email protected] twitter : @kgun86