This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockstile.ts
129 lines (113 loc) · 4.28 KB
/
blockstile.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//% weight=1000 color="#442255" icon="\uf45c"
//% groups='["Tiles", "Events", "Conditions", "Actions"]'
//% blockGap=8
namespace TileWorld {
let myWorld = new TileWorld();
/**
* Set the map for placing tiles in the scene
* @param map
*/
//% blockId=TWsettilemap block="set tile map to %map=tilemap_image_picker"
//% group="Tiles"
export function setTileMap(map: Image) {
myWorld.setMap(map)
}
/**
* Set the background tile for sprites
* @param color
*/
//% group="Tiles"
//% blockId=TWsetbackgroundtile block="set background tile to %color=colorindexpicker"
export function setBackgroundTile(code: number) {
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
* @param img
*/
//% blockId=TWsettile block="set %index=colorindexpicker to fixed sprite %img=tile_image_picker with $kind=spritekind""
//% group="Tiles"
export function setTile(code: number, image: Image, kind: number) {
myWorld.addTiles(code, image)
}
/**
* Set an image as a sprite at the given index. Sprites should be a 16x16 image
* @param index
* @param img
*/
//% blockId=TWsetsprite block="set %index=colorindexpicker to movable sprite %img=tile_image_picker with $kind=spritekind"
//% group="Tiles"
export function addTileSprite(code: number, image: Image, kind: number) {
myWorld.addTileSprites(code, image, kind)
}
/**
* Move sprite with buttons
* @param color
*/
//% group="Tiles"
//% blockId=TWmoveButtons block="move $kind=spritekind with buttons"
export function moveWithButtons(kind: number) {
let sprites = game.currentScene().spritesByKind[kind].sprites()
sprites.forEach((s) => {
if (s instanceof TileSprite) {
bindToController(<TileSprite>s)}
}
)
}
// notifications
/**
* Act on a sprite that is resting on a tile
* @param body code to execute
*/
//% group="Events" color="#444488"
//% blockId=TWontilestationary block="on $tile of kind $kind=spritekind at rest"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileStationary(kind: number, h: (tile: TileSprite) => void) {
myWorld.onTileStationary(kind, h);
}
/**
* Sprite is at center of tile and received request to move
* @param body code to execute
*/
//% group="Events" color="#444488"
//% blockId=TWontilearrived block="on $tile of kind $kind=spritekind move $direction"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileArrived(kind: number, h: (tile: TileSprite, direction: TileDir) => void) {
myWorld.onTileArrived(kind, h)
}
/**
* Sprite has just move into (entered) a tile
* @param body code to execute
*/
//% group="Events" color="#444488"
//% blockId=TWontiletransition block="enter $tile of kind $kind=spritekind"
//% blockAllowMultiple=1 draggableParameters="reporter"
export function onTileTransition(kind: number, h: (tile: TileSprite) => void) {
myWorld.onTileTransition(kind, h)
}
// checks
/**
* Check if a direction is one of several values.
*/
//% group="Conditions" color="#448844"
//% blockId=TWisoneof block="is %dir=variables_get(direction) one of %c1 %c2 || %c3"
//% inlineInputMode=inline direction.shadow=tiledir
export function isOneOf(dir: number, c1: TileDir, c2: TileDir = 0xff, c3: TileDir = 0xff) {
myWorld.isOneOf(dir, c1, c2, c3)
}
/**
* Check if a direction is not one of several values.
*/
//% group="Conditions" color="#448844"
//% blockId=TWisnotoneof block="is %dir=variables_get(direction) not one of %c1 %c2 || %c3"
//% inlineInputMode=inline direction.shadow=tiledir
export function isNotOneOf(dir: number, c1: TileDir, c2: TileDir = 0xff, c3: TileDir = 0xff) {
myWorld.isNotOneOf(dir, c1, c2, c3)
}
}