Skip to content
This repository has been archived by the owner on Nov 8, 2019. It is now read-only.

Commit

Permalink
add some plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
tballmsft committed Sep 26, 2019
1 parent 060a256 commit 53af990
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
15 changes: 4 additions & 11 deletions blockstile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ namespace TileWorld {
myWorld.setBackgroundTile(code)
}

// TODO: can we unify the two below?
// idea - a sprite is stationary or movable?

// set $code $image $kind $movable?
// $code -> $kind, regardless of $movable
// only movable sprites get events

/**
* Set an image as a tile at the given index. Tiles should be a 16x16 image
* @param index
Expand All @@ -42,7 +35,7 @@ namespace TileWorld {
//% inlineInputMode=inline
export function addSprite(code: number, image: Image, kk: KindKind, kind: number) {
if (kk == KindKind.Fixed)
myWorld.addTiles(code, image)
myWorld.addTiles(code, image, kind)
else
myWorld.addTileSprites(code, image, kind)
}
Expand Down Expand Up @@ -71,7 +64,7 @@ namespace TileWorld {
//% group="Events" color="#444488"
//% blockId=TWontilestationary block="on change around $kind=spritekind at $tile"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileStationary(kind: number, h: (tile: TileSprite) => void) {
export function onTileStationary(kind: number, h: (tile: TileWorld.TileSprite) => void) {
myWorld.onTileStationary(kind, h);
}

Expand All @@ -82,7 +75,7 @@ namespace TileWorld {
//% group="Events" color="#444488"
//% blockId=TWontilearrived block="on request of $kind=spritekind at $tile to move $direction"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileArrived(kind: number, h: (tile: TileSprite, direction: TileDir) => void) {
export function onTileArrived(kind: number, h: (tile: TileWorld.TileSprite, direction: TileDir) => void) {
myWorld.onTileArrived(kind, h)
}

Expand All @@ -93,7 +86,7 @@ namespace TileWorld {
//% group="Events" color="#444488"
//% blockId=TWontiletransition block="on $kind=spritekind moved into $tile"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileTransition(kind: number, h: (tile: TileSprite) => void) {
export function onTileTransition(kind: number, h: (tile: TileWorld.TileSprite) => void) {
myWorld.onTileTransition(kind, h)
}

Expand Down
62 changes: 29 additions & 33 deletions tileworld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,22 +354,13 @@ namespace TileWorld {
this.backgroundTile = backgroundTile
}

addTiles(code: number, art: Image) {
addTiles(code: number, art: Image, kind: number) {
let tiles = scene.getTilesByType(code)
this.codeToKind[code] = kind;
scene.setTile(code, art);
}

private initHandlers(kind: number) {
if (!this.stationaryHandlers[kind]) this.stationaryHandlers[kind] = []
if (!this.arrivalHandlers[kind]) this.arrivalHandlers[kind] = []
if (!this.transitionHandlers[kind]) this.transitionHandlers[kind] = []
}
private hookupHandlers(s: TileSprite) {
this.hookupArrival(s)
this.hookupStationary(s)
this.hookupTransition(s)
}
addTileSprites(code: number, art:Image, kind: number = 0) {
addTileSprites(code: number, art:Image, kind: number) {
let tiles = scene.getTilesByType(code)
scene.setTile(code, art);
this.sprites[code] = []
Expand All @@ -391,47 +382,52 @@ namespace TileWorld {
}
}

private setKind(code: number, kind: number) {
if (this.spriteCodes.find(c => c == code)) {
this.sprites[code].forEach((s) => { s.setKind(kind) })
}
}

private hookupStationary(s: TileSprite) {
let process = (s: TileSprite) =>
this.stationaryHandlers[s.kind()].forEach((h) => tryCatch(h, s));
s.onTileStationary(process);
}
onTileStationary(kind: number, h: (ts: TileSprite) => void) {
if (!this.stationaryHandlers[kind]) {
this.stationaryHandlers[kind] = []
}
this.stationaryHandlers[kind].push(h);
}

private hookupArrival(s: TileSprite) {
let process = (s: TileSprite, d: TileDir) =>
this.arrivalHandlers[s.kind()].forEach((h) => tryCatchTileDir(h, s, d));
s.onTileArrived(process);
}
onTileArrived(kind: number, h: (ts: TileSprite, d: TileDir) => void) {
if (!this.arrivalHandlers[kind]) {
this.arrivalHandlers[kind] = [];
}
this.arrivalHandlers[kind].push(h);
}

private hookupTransition(s: TileSprite) {
let process = (s: TileSprite, c: number, r: number) =>
this.transitionHandlers[s.kind()].forEach((h) => tryCatchColRow(h, s, c, r));
s.onTileTransition(process);
}
onTileTransition(kind: number, h: (ts: TileSprite, r: number, c: number) => void) {
if (!this.transitionHandlers[kind]) {
this.transitionHandlers[kind] = []; }
this.transitionHandlers[kind].push(h);
}

private initHandlers(kind: number) {
if (!this.stationaryHandlers[kind]) this.stationaryHandlers[kind] = []
if (!this.arrivalHandlers[kind]) this.arrivalHandlers[kind] = []
if (!this.transitionHandlers[kind]) this.transitionHandlers[kind] = []
}
private hookupHandlers(s: TileSprite) {
this.hookupArrival(s)
this.hookupStationary(s)
this.hookupTransition(s)
}
private hookupStationary(s: TileSprite) {
let process = (s: TileSprite) =>
this.stationaryHandlers[s.kind()].forEach((h) => tryCatch(h, s));
s.onTileStationary(process);
}
private hookupArrival(s: TileSprite) {
let process = (s: TileSprite, d: TileDir) =>
this.arrivalHandlers[s.kind()].forEach((h) => tryCatchTileDir(h, s, d));
s.onTileArrived(process);
}
private hookupTransition(s: TileSprite) {
let process = (s: TileSprite, c: number, r: number) =>
this.transitionHandlers[s.kind()].forEach((h) => tryCatchColRow(h, s, c, r));
s.onTileTransition(process);
}

isOneOf(d: TileDir, c1: TileDir, c2: TileDir = 0xff, c3: TileDir = 0xff) {
this.check(d == c1 || (c2 != 0xff && d == c2) || (c3 != 0xff && d==c3) )
}
Expand Down

0 comments on commit 53af990

Please sign in to comment.