Skip to content

Commit

Permalink
230403 | add five lines code chapter 06-02
Browse files Browse the repository at this point in the history
  • Loading branch information
wally-wally committed Apr 3, 2023
1 parent 15a57af commit 13de36f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 70 deletions.
28 changes: 28 additions & 0 deletions 17_tech_book/01_Five_Lines_of_Code/06장_데이터_보호/02.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const database = {
find: (to: string) => { return 12345 },
updateOne: (accountId: number, v: any) => {}
}

// bad
function accountDeposit(to: string, amount: number) {
let accountId = database.find(to);
database.updateOne(accountId, { $inc: { balance: amount } });
}

function accountTransfer(amount: number, from: string, to: string) {
accountDeposit(from, -amount);
accountDeposit(to, amount);
}

// good
class Account {
private deposit(to: string, amount: number) {
let accountId = database.find(to);
database.updateOne(accountId, { $inc: { balance: amount } });
}

transfer(amount: number, from: string, to: string) {
this.deposit(from, -amount);
this.deposit(to, amount);
}
}
36 changes: 36 additions & 0 deletions 17_tech_book/01_Five_Lines_of_Code/06장_데이터_보호/03.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// before
// let counter = 0;
// function incrementCounter() {
// counter++;
// }

// function main() {
// for (let i = 0; i < 20; i++) {
// incrementCounter();
// console.log(counter);
// }
// }

// after
class Counter {
private counter = 0;
getCounter() {
return this.counter;
}

setCounter(c: number) {
this.counter = c;
}
}

function incrementCounter(counter: Counter) {
counter.setCounter(counter.getCounter() + 1);
}

let counter = new Counter();
function main() {
for (let i = 0; i < 20; i++) {
incrementCounter(counter);
console.log(counter.getCounter());
}
}
153 changes: 83 additions & 70 deletions 17_tech_book/01_Five_Lines_of_Code/06장_데이터_보호/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ interface Tile {
isLock1(): boolean;
isLock2(): boolean;
draw(g: CanvasRenderingContext2D, x: number, y: number): void;
moveHorizontal(dx: number): void;
moveVertical(dy: number): void;
moveHorizontal(player: Player, dx: number): void;
moveVertical(player: Player, dy: number): void;
update(x: number, y: number): void;
getBlockOnTopState(): FallingState;
}
Expand All @@ -30,11 +30,11 @@ class Air implements Tile {
isLock1() { return false; }
isLock2() { return false; }
draw(g: CanvasRenderingContext2D, x: number, y: number) { }
moveHorizontal(dx: number) {
moveToTile(playerx + dx, playery);
moveHorizontal(player: Player, dx: number) {
player.move(dx, 0);
}
moveVertical(dy: number) {
moveToTile(playerx, playery + dy);
moveVertical(player: Player, dy: number) {
player.move(0, dy);
}
update(x: number, y: number) { }
getBlockOnTopState() { return new Falling(); }
Expand All @@ -48,11 +48,11 @@ class Flux implements Tile {
g.fillStyle = "#ccffcc";
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) {
moveToTile(playerx + dx, playery);
moveHorizontal(player: Player, dx: number) {
player.move(dx, 0);
}
moveVertical(dy: number) {
moveToTile(playerx, playery + dy);
moveVertical(player: Player, dy: number) {
player.move(0, dy);
}
update(x: number, y: number) { }
getBlockOnTopState() { return new Resting(); }
Expand All @@ -66,31 +66,31 @@ class Unbreakable implements Tile {
g.fillStyle = "#999999";
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) { }
moveVertical(dy: number) { }
moveHorizontal(player: Player, dx: number) { }
moveVertical(player: Player, dy: number) { }
update(x: number, y: number) { }
getBlockOnTopState() { return new Resting(); }
}

class Player implements Tile {
class PlayerTile implements Tile {
isAir() { return false; }
isLock1() { return false; }
isLock2() { return false; }
draw(g: CanvasRenderingContext2D, x: number, y: number) { }
moveHorizontal(dx: number) { }
moveVertical(dy: number) { }
moveHorizontal(player: Player, dx: number) { }
moveVertical(player: Player, dy: number) { }
update(x: number, y: number) { }
getBlockOnTopState() { return new Resting(); }
}

interface FallingState {
isFalling(): boolean;
moveHorizontal(tile: Tile, dx: number): void;
moveHorizontal(player: Player, tile: Tile, dx: number): void;
drop(tile: Tile, x: number, y: number): void;
}
class Falling {
isFalling() { return true; }
moveHorizontal(tile: Tile, dx: number) {
moveHorizontal(player: Player, tile: Tile, dx: number) {
}
drop(tile: Tile, x: number, y: number) {
map[y + 1][x] = tile;
Expand All @@ -99,12 +99,8 @@ class Falling {
}
class Resting {
isFalling() { return false; }
moveHorizontal(tile: Tile, dx: number) {
if (map[playery][playerx + dx + dx].isAir()
&& !map[playery + 1][playerx + dx].isAir()) {
map[playery][playerx + dx + dx] = tile;
moveToTile(playerx + dx, playery);
}
moveHorizontal(player: Player, tile: Tile, dx: number) {
player.pushHorizontal(tile, dx);
}
drop(tile: Tile, x: number, y: number) {
}
Expand All @@ -121,10 +117,10 @@ class Stone implements Tile {
g.fillStyle = "#0000cc";
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) {
this.fallStrategy.moveHorizontal(this, dx);
moveHorizontal(player: Player, dx: number) {
this.fallStrategy.moveHorizontal(player, this, dx);
}
moveVertical(dy: number) { }
moveVertical(player: Player, dy: number) { }
update(x: number, y: number) {
this.fallStrategy.update(this, x, y);
}
Expand All @@ -143,10 +139,10 @@ class Box implements Tile {
g.fillStyle = "#8b4513";
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) {
this.fallStrategy.moveHorizontal(this, dx);
moveHorizontal(player: Player, dx: number) {
this.fallStrategy.moveHorizontal(player, this, dx);
}
moveVertical(dy: number) { }
moveVertical(player: Player, dy: number) { }
update(x: number, y: number) {
this.fallStrategy.update(this, x, y);
}
Expand All @@ -162,13 +158,13 @@ class Key implements Tile {
this.keyConf.setColor(g);
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) {
moveHorizontal(player: Player, dx: number) {
this.keyConf.removeLock();
moveToTile(playerx + dx, playery);
player.move(dx, 0);
}
moveVertical(dy: number) {
moveVertical(player: Player, dy: number) {
this.keyConf.removeLock();
moveToTile(playerx, playery + dy);
player.move(0, dy);
}
update(x: number, y: number) { }
getBlockOnTopState() { return new Resting(); }
Expand All @@ -183,8 +179,8 @@ class Lock implements Tile {
this.keyConf.setColor(g);
g.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) { }
moveVertical(dy: number) { }
moveHorizontal(player: Player, dx: number) { }
moveVertical(player: Player, dy: number) { }
update(x: number, y: number) { }
getBlockOnTopState() { return new Resting(); }
}
Expand All @@ -193,43 +189,72 @@ class FallStrategy {
constructor(private falling: FallingState) { }
update(tile: Tile, x: number, y: number) {
this.falling = map[y + 1][x].getBlockOnTopState();
this.falling.drop(tile, x, y);
this.falling.drop(tile, x, y)
}
moveHorizontal(tile: Tile, dx: number) {
this.falling.moveHorizontal(tile, dx);
moveHorizontal(player: Player, tile: Tile, dx: number) {
this.falling.moveHorizontal(player, tile, dx);
}
}

interface Input {
handle(): void;
handle(player: Player): void;
}

class Right implements Input {
handle() {
map[playery][playerx + 1].moveHorizontal(1);
handle(player: Player) {
player.moveHorizontal(1);
}
}

class Left implements Input {
handle() {
map[playery][playerx - 1].moveHorizontal(-1);
handle(player: Player) {
player.moveHorizontal(-1);
}
}

class Up implements Input {
handle() {
map[playery - 1][playerx].moveVertical(-1);
handle(player: Player) {
player.moveVertical(-1);
}
}

class Down implements Input {
handle() {
map[playery + 1][playerx].moveVertical(1);
handle(player: Player) {
player.moveVertical(1);
}
}

let playerx = 1;
let playery = 1;
class Player {
private x = 1;
private y = 1;
draw(g: CanvasRenderingContext2D) {
g.fillStyle = "#ff0000";
g.fillRect(this.x * TILE_SIZE, this.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
moveHorizontal(dx: number) {
map[this.y][this.x + dx].moveHorizontal(this, dx);
}
moveVertical(dy: number) {
map[this.y + dy][this.x].moveVertical(this, dy);
}
move(dx: number, dy: number) {
this.moveToTile(this.x + dx, this.y + dy);
}
pushHorizontal(tile: Tile, dx: number) {
if (map[this.y][this.x + dx + dx].isAir()
&& !map[this.y + 1][this.x + dx].isAir()) {
map[this.y][this.x + dx + dx] = tile;
this.moveToTile(this.x + dx, this.y);
}
}
private moveToTile(newx: number, newy: number) {
map[this.y][this.x] = new Air();
map[newy][newx] = new PlayerTile();
this.x = newx;
this.y = newy;
}
}
let player = new Player();
let rawMap: RawTile[][] = [
[2, 2, 2, 2, 2, 2, 2, 2],
[2, 3, 0, 1, 1, 2, 0, 2],
Expand All @@ -245,7 +270,7 @@ function assertExhausted(x: never): never {
function transformTile(tile: RawTile) {
switch (tile) {
case RawTile.AIR: return new Air();
case RawTile.PLAYER: return new Player();
case RawTile.PLAYER: return new PlayerTile();
case RawTile.UNBREAKABLE: return new Unbreakable();
case RawTile.STONE: return new Stone(new Resting());
case RawTile.FALLING_STONE: return new Stone(new Falling());
Expand Down Expand Up @@ -308,22 +333,15 @@ class KeyConfiguration {
const YELLOW_KEY = new KeyConfiguration("#ffcc00", true, new RemoveLock1());
const BLUE_KEY = new KeyConfiguration("#00ccff", false, new RemoveLock2());

function moveToTile(newx: number, newy: number) {
map[playery][playerx] = new Air();
map[newy][newx] = new Player();
playerx = newx;
playery = newy;
}

function update() {
handleInputs();
function update(player: Player) {
handleInputs(player);
updateMap();
}

function handleInputs() {
function handleInputs(player: Player) {
while (inputs.length > 0) {
let input = inputs.pop();
input.handle();
input.handle(player);
}
}

Expand All @@ -342,10 +360,10 @@ function createGraphics() {
return g;
}

function draw() {
function draw(player: Player) {
let g = createGraphics();
drawMap(g);
drawPlayer(g);
player.draw(g);
}

function drawMap(g: CanvasRenderingContext2D) {
Expand All @@ -356,15 +374,10 @@ function drawMap(g: CanvasRenderingContext2D) {
}
}

function drawPlayer(g: CanvasRenderingContext2D) {
g.fillStyle = "#ff0000";
g.fillRect(playerx * TILE_SIZE, playery * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}

function gameLoop() {
let before = Date.now();
update();
draw();
update(player);
draw(player);
let after = Date.now();
let frameTime = after - before;
let sleep = SLEEP - frameTime;
Expand Down

0 comments on commit 13de36f

Please sign in to comment.