-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtilemap_path.ts
More file actions
214 lines (199 loc) · 7.18 KB
/
tilemap_path.ts
File metadata and controls
214 lines (199 loc) · 7.18 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//% color="#00FF42"
namespace TilemapPath {
/**
* Define variables that are part of the namespace
*/
let _sprites_to_stop: Sprite[] = [];
let _sprites_are_following: Sprite[] = [];
let _finish_callback: (sprite: Sprite) => void;
/**
* Create a TilemapPath object, which will hold an array of tilemap locations.
*/
export class TilemapPath {
/**
* Define variables that are part of the class
*/
private tilemap_path: tiles.Location[] = [];
public a_star_path: tiles.Location[][] = undefined;
/**
* Make a tilemap path.
* @param tilemap_path: An array of locations that a sprite will go to.
*/
constructor(tilemap_path: tiles.Location[] = []) {
this.tilemap_path = tilemap_path;
this._calculate_path();
}
/**
* Calculates the paths.
*/
//% hidden
_calculate_path() {
this.a_star_path = [];
for (let index = 0; index < this.tilemap_path.length - 1; index += 1) {
let from_loc: tiles.Location = this.tilemap_path[index];
let to_loc: tiles.Location = this.tilemap_path[index + 1];
// tiles.setTileAt(from_loc, sprites.castle.tileDarkGrass1);
// tiles.setTileAt(to_loc, sprites.castle.tileDarkGrass2);
// pause(1000);
// tiles.setTileAt(from_loc, sprites.castle.tileGrass2);
// tiles.setTileAt(to_loc, sprites.castle.tileGrass2);
let path = scene.aStar(from_loc, to_loc);
// mySprite.say("" + path);
this.a_star_path.push(path);
}
}
/**
* Set the tilemap path.
* @param tilemap_path: An array of locations that a sprite will go to.
*/
set_path(tilemap_path: tiles.Location[]) {
this.tilemap_path = tilemap_path;
this._calculate_path();
}
/**
* Get the tilemap path.
* @return: An array of locations that a sprite will go to.
*/
get_path(): tiles.Location[] {
return this.tilemap_path;
}
}
/**
* Converts a screen coordinate to a tilemap location.
* Definitely did not copy from the tilemap extension.
* @param value: The value (either X or Y coordinate)
*/
function screenCoordinateToTile(value: number): number {
const tm = game.currentScene().tileMap;
if (!tm) return value >> 4;
return value >> tm.scale;
}
/**
* Find the location of a sprite.
* Definitely did not copy from the tilemap extension.
* @param sprite: The sprite.
*/
function locationOfSprite(sprite: Sprite): tiles.Location {
return tiles.getTileLocation(
screenCoordinateToTile(sprite.x),
screenCoordinateToTile(sprite.y)
);
}
/**
* Create a new TilemapPath object
* @param path: A list of locations to go to in order.
*/
//% block="create path $path"
//% path.shadow="lists_create_with"
//% blockSetVariable=path
//% weight=100
export function create_path(path: tiles.Location[]): TilemapPath {
return new TilemapPath(path)
}
/**
* Set the path of the TilemapPath object
* @param tmpath: The TilemapPath to use.
* @param path: A list of locations to go to in order.
*/
//% block="$tmpath set path to $path"
//% tmpath.shadow="variables_get"
//% tmpath.defl="path"
//% path.shadow="lists_create_with"
//% weight=98
export function set_path(tmpath: TilemapPath.TilemapPath, path: tiles.Location[]) {
tmpath.set_path(path);
}
/**
* Get the path of the TilemapPath object
* @param tmpath: The TilemapPath to use.
* @return: A list of locations to go to in order.
*/
//% block="$tmpath get path"
//% tmpath.shadow="variables_get"
//% tmpath.defl="path"
//% weight=99
export function get_path(tmpath: TilemapPath.TilemapPath): tiles.Location[] {
return tmpath.get_path();
}
/**
* Have a sprite follow a TilemapPath
* @param sprite: The sprite that will follow the path.
* @param path: The TilemapPath that will be followed.
* @param speed: How fast the sprite should follow the path. Defaults to 100.
*/
//% block="sprite $sprite follow path $path || at speed $speed"
//% sprite.shadow="variables_get"
//% sprite.defl="mySprite"
//% path.shadow="variables_get"
//% path.defl="path"
//% speed.defl=100
//% expandableArgumentMode="enabled"
//% weight=90
export function follow_path(sprite: Sprite, path: TilemapPath, speed: number = 100) {
_sprites_are_following.push(sprite);
if (path.get_path().length > 0) {
scene.followPath(sprite, scene.aStar(locationOfSprite(sprite), path.get_path()[0]), speed);
}
for (let index = 0; index < path.a_star_path.length; index += 1) {
let inner_path: tiles.Location[] = path.a_star_path[index];
sprite.data["_tilemap_path_curr_segment"] = index;
scene.followPath(sprite, inner_path, speed);
wait_till_finish_path(sprite);
let sprite_index = _sprites_to_stop.indexOf(sprite);
if (sprite_index != -1) {
_sprites_to_stop.splice(sprite_index, 1);
_sprites_are_following.splice(_sprites_are_following.indexOf(sprite), 1);
return;
}
}
if (_finish_callback) {
_finish_callback(sprite);
}
_sprites_are_following.splice(_sprites_are_following.indexOf(sprite), 1);
}
/**
* Wait until a sprite finishes following it's path.
* This returns immediimmediately if not following a path.
* @param sprite: The sprite to wait for.
*/
//% hidden
function wait_till_finish_path(sprite: Sprite) {
while (scene.spriteIsFollowingPath(sprite)) {
pause(0);
}
}
/**
* Get if a sprite is following a tilemap_path
* @param sprite: The sprite to check.
*/
//% block="is $sprite following path"
//% sprite.shadow="variables_get"
//% sprite.defl="mySprite"
//% weight=80
export function is_sprite_following_path(sprite: Sprite): boolean {
return _sprites_are_following.indexOf(sprite) != -1;
}
/**
* Have a sprite stop following a TilemapPath
* @param sprite: The sprite that will stop following the path.
*/
//% block="sprite $sprite stop following path"
//% sprite.shadow="variables_get"
//% sprite.defl="mySprite"
//% weight=70
export function stop_follow_path(sprite: Sprite) {
if (_sprites_are_following.indexOf(sprite) != -1) {
_sprites_to_stop.push(sprite);
scene.followPath(sprite, null);
}
}
/**
* Event handlers can have arguments too. You can refer to them using $NAME.
*/
//% block="on sprite $sprite finishes tilemap path"
//% draggableParameters="reporter"
//% weight=60
export function on_sprite_finishes_path(handler: (sprite: Sprite) => void) {
_finish_callback = handler;
}
}