John Horton Conway, 1937년 12월 26일 ~ 2020년 4월 11일
표로 정리하면 다음과 같습니다.
현 세대 | 인접 세포 갯수 | 다음 세대 |
---|---|---|
LIVE | 0 | DEAD |
LIVE | 1 | DEAD |
LIVE | 2 | LIVE |
LIVE | 3 | LIVE |
LIVE | 4 | DEAD |
LIVE | 5 | DEAD |
LIVE | 6 | DEAD |
LIVE | 7 | DEAD |
LIVE | 8 | DEAD |
DEAD | 0 | DEAD |
DEAD | 1 | DEAD |
DEAD | 2 | DEAD |
DEAD | 3 | LIVE |
DEAD | 4 | DEAD |
DEAD | 5 | DEAD |
DEAD | 6 | DEAD |
DEAD | 7 | DEAD |
DEAD | 8 | DEAD |
전체 코드는 여기를 참조 하세요.
https://github.com/q_lazzarus/lifegame
먼저 phaser 의 scene 시스템을 이해해야 합니다.
typescript1class MainScene extends Phaser.Scene {2 constructor() {3 super(SCENE_KEY);4 }56 public init(): void {7 // init8 }910 public preload(): void {11 // preload12 }1314 public create(): void {15 // create16 }1718 public update(): void {19 // update20 }21}
각 메쏘드는 다음의 내용을 정의 합니다.
메쏘드명 | 역할 |
---|---|
init | scene 초기화 |
preload | 에셋 (이미지/오디오 등) 호출 |
create | 게임 오브젝트 생성시 |
update | 게임 기동시 tick 단위 업데이트시 호출 |
실행 순서는 정리하자면 다음과 같습니다.
scene 메쏘드 실행 순서
typescript1 public create(): void {2 this.generation = 1; // 현재 세대를 정의3 Array.from(Array(this.stageSize).keys()).forEach(index => {4 // 각 픽셀단위로 cell 클래스를 생성하였습니다.5 // 해당 셀은 위치값과 live/dead 를 표기하는 값만 가집니다.6 this.cells.push(new Cell(this.numberToPosition(index)));7 });89 // this.nextGeneration 메쏘드를 타이머로 반복하게 실행하도록 하였습니다.10 this.timer = this.time.addEvent({11 delay: GameConfig.DELAY_INTERVAL,12 callback: this.nextGeneration,13 callbackScope: this,14 loop: true15 });16 }
graphics property 는 프리젠테이션 레이어입니다.
이러면 코드상에서는 조작하는 부분과 UI 그리는 부분을 따로 제어할 수 있습니다.
typescript1 public update(): void {2 // 그려진 모든 버퍼를 지웁니다.3 this.graphics?.clear();4 this.cells.forEach(cell => {5 // 생존한 cell 들만 그려줍니다.6 cell.active && this.graphics?.fillRectShape(cell)7 });8 }
typescript1 private nextGeneration(): void {2 this.generation++; // 현재 세대를 정의3 Array.from(Array(this.stageSize).keys()).forEach(index => {4 // 이전 세대에서 살아남았는지5 const active = this.cells[index].active;6 // 주변 세포들이 존재하는지 판단해서 점수를 매깁니다.7 const score = this.neighborScore(index);89 // 점수에 따른 세포들의 생존 유무10 if (active && (2 > score || 3 < score)) {11 this.cells[index].active = false;12 } else if (!active && 3 == score) {13 this.cells[index].active = true;14 }15 });16 }
우와 대박
오늘도 고생하셨습니다.