This repository was archived by the owner on Mar 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation.ts
More file actions
134 lines (121 loc) · 4.34 KB
/
foundation.ts
File metadata and controls
134 lines (121 loc) · 4.34 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
/**
* Foundation blocks
*/
//% groups='["Builders", "Util"]'
//% weight=60 color=#fcba03 icon="\uf0e3"
namespace Foundation {
/**
* Applies a preset flag list, in case you can't be asked to make your own.
* @param preset The Flag preset.
*/
//% block
//% group="Util"
export function applyFlagPreset(preset: sprites.flagPresets): SpriteFlag[] {
const builtFlags: SpriteFlag[] = [];
clearArray(builtFlags);
if (preset == sprites.flagPresets.DEFAULT) {
builtFlags.push(SpriteFlag.StayInScreen);
}
if (preset == sprites.flagPresets.COLLISIONLESS) {
builtFlags.push(SpriteFlag.Ghost);
builtFlags.push(SpriteFlag.GhostThroughSprites);
builtFlags.push(SpriteFlag.GhostThroughWalls);
}
if (preset == sprites.flagPresets.CLEARONWALL) {
builtFlags.push(SpriteFlag.DestroyOnWall);
builtFlags.push(SpriteFlag.AutoDestroy);
}
return builtFlags;
}
/**
* Clears an array of every element it contains.
* @param array The array to clear.
*/
//% block
//% group="Util"
export function clearArray(array: any[]) {
for (let value of array) {
array.removeElement(value);
}
}
/**
* Applies every flag in a SpriteFlag array to a Sprite.
* @param array The array of SpriteFlags to apply.
* @param sprite The Sprite to apply the flags to.
*/
//% block
//% group="Util"
export function applyFlagsToSprite(array: SpriteFlag[], sprite: Sprite) {
if (array != null) {
for (let value of array) {
sprite.setFlag(value, true);
}
}
}
/**
* Builds a Player from the given fields.
* @param img The Sprite image.
* @param kind The SpriteKind.
* @param flags The SpriteFlags to apply to the player.
* @param xSpeed The Sprite's X-Speed.
* @param ySpeed The Sprite's Y-Speed.
*/
//% block
//% group="Builders"
export function buildPlayer(img: Image, kind: number, flags: SpriteFlag[], xSpeed?: number, ySpeed?: number): Sprite {
const spriteFromBuilder: Sprite = sprites.create(img, kind);
applyFlagsToSprite(flags, spriteFromBuilder);
forever(function movementController() {
controller.moveSprite(spriteFromBuilder, xSpeed, ySpeed);
});
return spriteFromBuilder;
}
/**
* Builds an Enemy sprite from the given fields.
* @param img The Sprite image.
* @param kind The SpriteKind.
* @param flags The SpriteFlags to apply to the player.
* @param followTarget The target the Sprite follows.
* @param followSpeed The Sprite's speed while following the target.
* @param turnRate The rate of turning while following the target.
*/
//% block
//% group="Builders"
export function buildEnemy(img: Image, kind: number, flags: SpriteFlag[], followTarget?: Sprite, followSpeed?: number, turnRate?: number): Sprite {
const spriteFromBuilder: Sprite = sprites.create(img, kind);
applyFlagsToSprite(flags, spriteFromBuilder);
spriteFromBuilder.follow(followTarget, followSpeed, turnRate);
return spriteFromBuilder;
}
/**
* Builds a regular Sprite from the given fields.
* @param img The Sprite image.
* @param kind The SpriteKind.
* @param flags The SpriteFlags to apply to the player.
*/
//% block
//% group="Builders"
export function buildCommonSprite(img: Image, kind: number, flags: SpriteFlag[]): Sprite {
const spriteFromBuilder: Sprite = sprites.create(img, kind);
applyFlagsToSprite(flags, spriteFromBuilder);
return spriteFromBuilder;
}
sprites.onCreated(SpriteKind.Placeholder, function placeHoldersAreIntangible(sprite: Sprite) {
sprite.setFlag(SpriteFlag.Ghost, true);
sprite.setFlag(SpriteFlag.GhostThroughSprites, true);
sprite.setFlag(SpriteFlag.GhostThroughTiles, true);
sprite.setFlag(SpriteFlag.GhostThroughWalls, true);
});
}
namespace sprites {
export enum flagPresets {
DEFAULT,
COLLISIONLESS,
CLEARONWALL
}
}
namespace SpriteKind {
export let Particle = SpriteKind.create();
export let MenuElement = SpriteKind.create();
export let Placeholder = SpriteKind.create();
}