From 78ab05a2624dc7763ca473ca881ea6e4f9890e4f Mon Sep 17 00:00:00 2001 From: leehoosgg Date: Thu, 21 Dec 2023 20:34:02 +0900 Subject: [PATCH] =?UTF-8?q?=ED=83=80=EC=9D=BC=20Interface,=20Class=20?= =?UTF-8?q?=EB=A1=9C=EC=9D=98=20=EC=BD=94=EB=93=9C=20=EC=9D=B4=EA=B4=80=20?= =?UTF-8?q?=EB=B0=8F=20=EC=97=90=EB=9F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit if 문 리팩토링시 map[y][x] 에서 메서드 인식 불가 map 생성 함수를 고쳐서 map[y][x] 가 클래스로 받아들여지도록 해야할듯 싶음 --- index.ts | 250 +++++++++++++++++++++++++++------- src/ch02/TrafficLight.enum.ts | 5 - 2 files changed, 199 insertions(+), 56 deletions(-) delete mode 100644 src/ch02/TrafficLight.enum.ts diff --git a/index.ts b/index.ts index 27aaa4a..e188893 100644 --- a/index.ts +++ b/index.ts @@ -2,7 +2,7 @@ const TILE_SIZE = 30; const FPS = 30; const SLEEP = 1000 / FPS; -enum Tile { +enum RawTile { AIR, FLUX, UNBREAKABLE, @@ -13,6 +13,150 @@ enum Tile { KEY2, LOCK2 } +// Tile Interface & Class +interface Tile { + isFlux(): boolean; + isUnbreakable(): boolean; + isStone(): boolean; + isFallingStone(): boolean; + isBox(): boolean; + isFallingBox(): boolean; + isKey1(): boolean; + isLock1(): boolean; + isKey2(): boolean; + isLock2(): boolean; +} + +class Flux implements Tile { + isFlux() { return true; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Unbreakable implements Tile { + isFlux() { return false; } + isUnbreakable() { return true; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Stone implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return true; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class FallingStone implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return true; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Box implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return true; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class FallingBox implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return true; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Key1 implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return true; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Lock1 implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return true; } + isKey2() { return false; } + isLock2() { return false; } +} + +class Key2 implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return true; } + isLock2() { return false; } +} + +class Lock2 implements Tile { + isFlux() { return false; } + isUnbreakable() { return false; } + isStone() { return false; } + isFallingStone() { return false; } + isBox() { return false; } + isFallingBox() { return false; } + isKey1() { return false; } + isLock1() { return false; } + isKey2() { return false; } + isLock2() { return true; } +} + enum RawInput { UP, DOWN, LEFT, RIGHT } @@ -60,7 +204,7 @@ class Down implements Input { let playerx = 1; let playery = 1; -const map: Tile[][] = [ +const map: RawTile[][] = [ [2, 2, 2, 2, 2, 2, 2, 2], [2, 3, 0, 1, 1, 2, 0, 2], [2, 4, 2, 6, 1, 2, 0, 2], @@ -71,51 +215,51 @@ const map: Tile[][] = [ const inputs: Input[] = []; -function remove(tile: Tile) { +function remove(tile: RawTile) { for (let y = 0; y < map.length; y++) { for (let x = 0; x < map[y].length; x++) { if (map[y][x] === tile) { - map[y][x] = Tile.AIR; + map[y][x] = RawTile.AIR; } } } } function moveToTile(newx: number, newy: number) { - map[playery][playerx] = Tile.AIR; - map[newy][newx] = Tile.PLAYER; + map[playery][playerx] = RawTile.AIR; + map[newy][newx] = RawTile.PLAYER; playerx = newx; playery = newy; } function moveHorizontal(dx: number) { - if (map[playery][playerx + dx] === Tile.FLUX - || map[playery][playerx + dx] === Tile.AIR) { + if (map[playery][playerx + dx] === RawTile.FLUX + || map[playery][playerx + dx] === RawTile.AIR) { moveToTile(playerx + dx, playery); - } else if ((map[playery][playerx + dx] === Tile.STONE - || map[playery][playerx + dx] === Tile.BOX) - && map[playery][playerx + dx + dx] === Tile.AIR - && map[playery + 1][playerx + dx] !== Tile.AIR) { + } else if ((map[playery][playerx + dx] === RawTile.STONE + || map[playery][playerx + dx] === RawTile.BOX) + && map[playery][playerx + dx + dx] === RawTile.AIR + && map[playery + 1][playerx + dx] !== RawTile.AIR) { map[playery][playerx + dx + dx] = map[playery][playerx + dx]; moveToTile(playerx + dx, playery); - } else if (map[playery][playerx + dx] === Tile.KEY1) { - remove(Tile.LOCK1); + } else if (map[playery][playerx + dx] === RawTile.KEY1) { + remove(RawTile.LOCK1); moveToTile(playerx + dx, playery); - } else if (map[playery][playerx + dx] === Tile.KEY2) { - remove(Tile.LOCK2); + } else if (map[playery][playerx + dx] === RawTile.KEY2) { + remove(RawTile.LOCK2); moveToTile(playerx + dx, playery); } } function moveVertical(dy: number) { - if (map[playery + dy][playerx] === Tile.FLUX - || map[playery + dy][playerx] === Tile.AIR) { + if (map[playery + dy][playerx] === RawTile.FLUX + || map[playery + dy][playerx] === RawTile.AIR) { moveToTile(playerx, playery + dy); - } else if (map[playery + dy][playerx] === Tile.KEY1) { - remove(Tile.LOCK1); + } else if (map[playery + dy][playerx] === RawTile.KEY1) { + remove(RawTile.LOCK1); moveToTile(playerx, playery + dy); - } else if (map[playery + dy][playerx] === Tile.KEY2) { - remove(Tile.LOCK2); + } else if (map[playery + dy][playerx] === RawTile.KEY2) { + remove(RawTile.LOCK2); moveToTile(playerx, playery + dy); } } @@ -135,18 +279,18 @@ function handleInputs() { function updateMap() { for (let y = map.length - 1; y >= 0; y--) { for (let x = 0; x < map[y].length; x++) { - if ((map[y][x] === Tile.STONE || map[y][x] === Tile.FALLING_STONE) - && map[y + 1][x] === Tile.AIR) { - map[y + 1][x] = Tile.FALLING_STONE; - map[y][x] = Tile.AIR; - } else if ((map[y][x] === Tile.BOX || map[y][x] === Tile.FALLING_BOX) - && map[y + 1][x] === Tile.AIR) { - map[y + 1][x] = Tile.FALLING_BOX; - map[y][x] = Tile.AIR; - } else if (map[y][x] === Tile.FALLING_STONE) { - map[y][x] = Tile.STONE; - } else if (map[y][x] === Tile.FALLING_BOX) { - map[y][x] = Tile.BOX; + if ((map[y][x] === RawTile.STONE || map[y][x] === RawTile.FALLING_STONE) + && map[y + 1][x] === RawTile.AIR) { + map[y + 1][x] = RawTile.FALLING_STONE; + map[y][x] = RawTile.AIR; + } else if ((map[y][x] === RawTile.BOX || map[y][x] === RawTile.FALLING_BOX) + && map[y + 1][x] === RawTile.AIR) { + map[y + 1][x] = RawTile.FALLING_BOX; + map[y][x] = RawTile.AIR; + } else if (map[y][x] === RawTile.FALLING_STONE) { + map[y][x] = RawTile.STONE; + } else if (map[y][x] === RawTile.FALLING_BOX) { + map[y][x] = RawTile.BOX; } } } @@ -168,27 +312,31 @@ function createGraphics() { function drawMap(g:CanvasRenderingContext2D) { for (let y = 0; y < map.length; y++) { for (let x = 0; x < map[y].length; x++) { - if (map[y][x] === Tile.FLUX) { - g.fillStyle = '#ccffcc'; - } else if (map[y][x] === Tile.UNBREAKABLE) { - g.fillStyle = '#999999'; - } else if (map[y][x] === Tile.STONE || map[y][x] === Tile.FALLING_STONE) { - g.fillStyle = '#0000cc'; - } else if (map[y][x] === Tile.BOX || map[y][x] === Tile.FALLING_BOX) { - g.fillStyle = '#8b4513'; - } else if (map[y][x] === Tile.KEY1 || map[y][x] === Tile.LOCK1) { - g.fillStyle = '#ffcc00'; - } else if (map[y][x] === Tile.KEY2 || map[y][x] === Tile.LOCK2) { - g.fillStyle = '#00ccff'; - } - - if (map[y][x] !== Tile.AIR && map[y][x] !== Tile.PLAYER) { - g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE); - } + colorOfTile(g, x, y); } } } +function colorOfTile(g: CanvasRenderingContext2D, x: number, y: number) { + if (map[y][x]) { + g.fillStyle = '#ccffcc'; + } else if (map[y][x] === RawTile.UNBREAKABLE) { + g.fillStyle = '#999999'; + } else if (map[y][x] === RawTile.STONE || map[y][x] === RawTile.FALLING_STONE) { + g.fillStyle = '#0000cc'; + } else if (map[y][x] === RawTile.BOX || map[y][x] === RawTile.FALLING_BOX) { + g.fillStyle = '#8b4513'; + } else if (map[y][x] === RawTile.KEY1 || map[y][x] === RawTile.LOCK1) { + g.fillStyle = '#ffcc00'; + } else if (map[y][x] === RawTile.KEY2 || map[y][x] === RawTile.LOCK2) { + g.fillStyle = '#00ccff'; + } + + if (map[y][x] !== RawTile.AIR && map[y][x] !== RawTile.PLAYER) { + g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE); + } +} + function drawPlayer(g:CanvasRenderingContext2D) { g.fillStyle = '#ff0000'; g.fillRect(playerx * TILE_SIZE, playery * TILE_SIZE, TILE_SIZE, TILE_SIZE); diff --git a/src/ch02/TrafficLight.enum.ts b/src/ch02/TrafficLight.enum.ts deleted file mode 100644 index 240d46c..0000000 --- a/src/ch02/TrafficLight.enum.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum TrafficLight { - RED, - YELLOW, - GREEN -}