pxt-common-packages 9.4.3 → 9.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/built/common-sim.d.ts +10 -2
  2. package/built/common-sim.js +86 -1
  3. package/libs/azureiot/built/debug/binary.js +461 -461
  4. package/libs/color/built/debug/binary.js +8 -8
  5. package/libs/color/colors.ts +11 -0
  6. package/libs/color-sensor/built/debug/binary.js +8 -8
  7. package/libs/controller/built/debug/binary.js +9043 -7832
  8. package/libs/controller---none/built/debug/binary.js +9022 -7811
  9. package/libs/datalogger/built/debug/binary.js +63 -63
  10. package/libs/edge-connector/built/debug/binary.js +8 -8
  11. package/libs/esp32/built/debug/binary.js +462 -462
  12. package/libs/game/_locales/game-strings.json +21 -0
  13. package/libs/game/built/debug/binary.js +8935 -7724
  14. package/libs/game/docs/reference/scene/set-tile-at.md +2 -2
  15. package/libs/game/docs/reference/scene/tile-at-location-equals.md +1 -1
  16. package/libs/game/docs/reference/sprites/sprite/ax.md +34 -3
  17. package/libs/game/docs/reference/sprites/sprite/ay.md +34 -3
  18. package/libs/game/docs/reference/sprites/sprite/fx.md +123 -0
  19. package/libs/game/docs/reference/sprites/sprite/fy.md +123 -0
  20. package/libs/game/docs/reference/sprites.md +2 -0
  21. package/libs/game/hitbox.ts +40 -22
  22. package/libs/game/renderText.ts +74 -11
  23. package/libs/game/sprite.ts +244 -16
  24. package/libs/game/spritesay.ts +206 -53
  25. package/libs/lcd/built/debug/binary.js +8 -8
  26. package/libs/light-spectrum-sensor/built/debug/binary.js +8 -8
  27. package/libs/lora/built/debug/binary.js +8 -8
  28. package/libs/matrix-keypad/built/debug/binary.js +8 -8
  29. package/libs/mqtt/built/debug/binary.js +176 -176
  30. package/libs/net/built/debug/binary.js +176 -176
  31. package/libs/net-game/built/debug/binary.js +10832 -9617
  32. package/libs/palette/built/debug/binary.js +8934 -7723
  33. package/libs/pixel/built/debug/binary.js +8 -8
  34. package/libs/power/built/debug/binary.js +8 -8
  35. package/libs/proximity/built/debug/binary.js +9 -9
  36. package/libs/radio/built/debug/binary.js +8 -8
  37. package/libs/radio-broadcast/built/debug/binary.js +8 -8
  38. package/libs/rotary-encoder/built/debug/binary.js +8 -8
  39. package/libs/screen/built/debug/binary.js +50 -50
  40. package/libs/screen/image.cpp +14 -4
  41. package/libs/screen/image.ts +5 -4
  42. package/libs/screen/sim/image.ts +15 -3
  43. package/libs/servo/built/debug/binary.js +8 -8
  44. package/libs/sprite-scaling/README.md +3 -0
  45. package/libs/sprite-scaling/_locales/sprite-scaling-jsdoc-strings.json +1 -0
  46. package/libs/sprite-scaling/_locales/sprite-scaling-strings.json +9 -0
  47. package/libs/sprite-scaling/built/debug/binary.js +40047 -0
  48. package/libs/sprite-scaling/pxt.json +16 -0
  49. package/libs/sprite-scaling/scaling.ts +111 -0
  50. package/libs/sprite-scaling/targetoverrides.ts +1 -0
  51. package/libs/sprite-scaling/test.ts +0 -0
  52. package/libs/storyboard/built/debug/binary.js +8934 -7723
  53. package/libs/text-to-speech/pxt.json +14 -0
  54. package/libs/text-to-speech/shims.d.ts +47 -0
  55. package/libs/text-to-speech/sim/tts.ts +72 -0
  56. package/libs/text-to-speech/tts.ts +70 -0
  57. package/package.json +1 -1
@@ -3,15 +3,15 @@
3
3
  Set a tile at a location in the tilemap.
4
4
 
5
5
  ```sig
6
- tiles.setTileAt(null, tiles.getTileLocation(0, 0))
6
+ tiles.setTileAt(tiles.getTileLocation(0, 0), null)
7
7
  ```
8
8
 
9
9
  You can set a tile at a specific column and row in the tilemap using a tile location object. Specify a tile to set in the tilemap and a location for it.
10
10
 
11
11
  ## Parameters
12
12
 
13
- * **tile**: the to set in the tilemap.
14
13
  * **loc**: a tile location in the tilemap.
14
+ * **tile**: the to set in the tilemap.
15
15
 
16
16
  ## Example #example
17
17
 
@@ -3,7 +3,7 @@
3
3
  Check if a location in the tilemap contains the tile you're looking for.
4
4
 
5
5
  ```sig
6
- tiles.tileAtLocationEquals(location, null)
6
+ tiles.tileAtLocationEquals(tiles.getTileLocation(0, 0), null)
7
7
  ```
8
8
 
9
9
  ## Parameters
@@ -36,13 +36,15 @@ mySprite.ax = 0
36
36
 
37
37
  * **value**: the new horizontal acceleration for the sprite in pixels per second, per second.
38
38
 
39
- ## Sprite acceleration
39
+ ## Sprite horizontal acceleration
40
40
 
41
- Acceleration of a sprite makes it speed up or slow down. The value for acceleration determines how quickly the speed of the sprite will change. A positive value makes the sprite speed up as it moves. A negative value causes the sprite to slow down while it moves.
41
+ The value for acceleration determines how quickly the speed of the sprite will change. Acceleration of a sprite makes it speed up in a direction toward the right or left. A positive value causes the sprite's speed in the `right` direction to increase. A negative value causes the sprite's speed in the `left` direction to increase.
42
+
43
+ Acceleration can be used as an opposing force too. If a sprite is travelling toward the right at a certain speed and a negative horizontal acceleration is applied, the sprite will keep moving to the right but it will slow down. When the sprite's speed reaches `0`, the sprite will begin to move to the left.
42
44
 
43
45
  ### ~ hint
44
46
 
45
- **How speed changes**
47
+ #### How speed changes
46
48
 
47
49
  Speed, or velocity, is how much distance an object moves during some period of time. Distance in your game is measured in pixels so the speed of a sprite is in _pixels per second_. Speed is changed by _accelerating_ an object. In your game, a sprite is accelerated by some amount of change in speed per second. So, acceleration is measured in _pixels per second per second_.
48
50
 
@@ -131,6 +133,35 @@ game.onUpdateInterval(1000, function () {
131
133
  })
132
134
  ```
133
135
 
136
+ ### Opposite force #ex3
137
+
138
+ Make a sprite move from the left side of the screen to the right side. Apply acceleration in the opposite direction to slow the sprite down and send back to the left.
139
+
140
+ ```blocks
141
+ let mySprite = sprites.create(img`
142
+ . . . . . . . . . . . . . . . .
143
+ . . . . . . . . . . . . . . . .
144
+ . . . . . 5 5 5 5 5 5 . . . . .
145
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
146
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
147
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
148
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
149
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
150
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
151
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
152
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
153
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
154
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
155
+ . . . . . 5 5 5 5 5 5 . . . . .
156
+ . . . . . . . . . . . . . . . .
157
+ . . . . . . . . . . . . . . . .
158
+ `, SpriteKind.Player)
159
+ mySprite.setStayInScreen(true)
160
+ mySprite.left = 0
161
+ mySprite.vx = 100
162
+ mySprite.ax = -35
163
+ ```
164
+
134
165
  ## See also #seealso
135
166
 
136
167
  [ay](/reference/sprites/sprite/ay),
@@ -36,13 +36,16 @@ mySprite.ay = 0
36
36
 
37
37
  * **value**: the new vertical acceleration for the sprite in pixels per second, per second.
38
38
 
39
- ## Sprite acceleration
39
+ ## Sprite vertical acceleration
40
+
41
+ The value for acceleration determines how quickly the speed of the sprite will change. Acceleration of a sprite makes it speed up in a direction toward the bottom or top. A positive value causes the sprite's speed in the `bottom` direction to increase. A negative value causes the sprite's speed in the `top` direction to increase.
42
+
43
+ Acceleration can be used as an opposing force too. If a sprite is travelling toward the bottom at a certain speed and a negative vertical acceleration is applied, the sprite will keep moving to the bottom but it will slow down. When the sprite's speed reaches `0`, the sprite will begin to move to the top.
40
44
 
41
- Acceleration of a sprite makes it speed up or slow down. The value for acceleration determines how quickly the speed of the sprite will change. A positive value makes the sprite speed up as it moves. A negative value causes the sprite to slow down while it moves.
42
45
 
43
46
  ### ~ hint
44
47
 
45
- **How speed changes**
48
+ #### How speed changes
46
49
 
47
50
  Speed, or velocity, is how much distance an object moves during some period of time. Distance in your game is measured in pixels so the speed of a sprite is in _pixels per second_. Speed is changed by _accelerating_ an object. In your game, a sprite is accelerated by some amount of change in speed per second. So, acceleration is measured in _pixels per second per second_.
48
51
 
@@ -129,6 +132,34 @@ game.onUpdateInterval(1000, function () {
129
132
  interval += 1
130
133
  })
131
134
  ```
135
+ ### Opposite force #ex3
136
+
137
+ Make a sprite move from the top of the screen to the bottom. Apply acceleration in the opposite direction to slow the sprite down and send back to the top.
138
+
139
+ ```blocks
140
+ let mySprite = sprites.create(img`
141
+ . . . . . . . . . . . . . . . .
142
+ . . . . . . . . . . . . . . . .
143
+ . . . . . 5 5 5 5 5 5 . . . . .
144
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
145
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
146
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
147
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
148
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
149
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
150
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
151
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
152
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
153
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
154
+ . . . . . 5 5 5 5 5 5 . . . . .
155
+ . . . . . . . . . . . . . . . .
156
+ . . . . . . . . . . . . . . . .
157
+ `, SpriteKind.Player)
158
+ mySprite.setStayInScreen(true)
159
+ mySprite.top = 0
160
+ mySprite.vy = 80
161
+ mySprite.ay = -35
162
+ ```
132
163
 
133
164
  ## See also #seealso
134
165
 
@@ -0,0 +1,123 @@
1
+ # fy (property)
2
+
3
+ Get or set the friction opposing a sprite's motion in the vertical direction.
4
+
5
+ ## Get
6
+
7
+ Get the vertical friction on the sprite.
8
+
9
+ ```block
10
+ let mySprite: Sprite = null
11
+
12
+ let horzAccel = mySprite.fy
13
+ ```
14
+
15
+ ```typescript-ignorelet
16
+ horzAccel = mySprite.fy
17
+ ```
18
+
19
+ ### Returns
20
+
21
+ * a [number](/types/number) that is the current vertical friction of the sprite.
22
+
23
+ ## Set
24
+
25
+ Set the vertical friction for the sprite.
26
+
27
+ ```block
28
+ let mySprite: Sprite = null
29
+
30
+ mySprite.fy = 0
31
+ ```
32
+
33
+ ```typescript-ignore
34
+ mySprite.fy = 0
35
+ ```
36
+
37
+ ### Parameter
38
+
39
+ * **value**: the new vertical friction opposing the sprite's motion in pixels per second, per second.
40
+
41
+ ## Sprite vertical friction
42
+
43
+ Friction is an opposing force against the motion of a sprite. If a sprite has a vertical velocity (`vy`), it's friction will slow the sprite down until it's vertical velocity becomes `0`. This is similar to setting an opposite vertical acceleration but the opposite acceleration goes away once the sprite stops.
44
+
45
+ ## Examples #example
46
+
47
+ ### Measure stopping time #ex1
48
+
49
+ Move the sprite from top to bottom across the screen. Use the correct amount of friction (`fy`) to make the sprite stop at `3` seconds.
50
+
51
+ ```blocks
52
+ let stopTime = 0
53
+ let counting = true
54
+ let mySprite = sprites.create(img`
55
+ . . . . . . . . . . . . . . . .
56
+ . . . . . . . . . . . . . . . .
57
+ . . . . . 5 5 5 5 5 5 . . . . .
58
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
59
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
60
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
61
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
62
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
63
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
64
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
65
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
66
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
67
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
68
+ . . . . . 5 5 5 5 5 5 . . . . .
69
+ . . . . . . . . . . . . . . . .
70
+ . . . . . . . . . . . . . . . .
71
+ `, SpriteKind.Player)
72
+ mySprite.setStayInScreen(true)
73
+ mySprite.top = 0
74
+ mySprite.fy = 20
75
+ mySprite.vy = 60
76
+ game.onUpdateInterval(100, function () {
77
+ if (counting) {
78
+ stopTime += 0.1
79
+ if (mySprite.vy == 0) {
80
+ counting = false
81
+ mySprite.sayText("" + stopTime + "sec", 5000, false)
82
+ }
83
+ }
84
+ })
85
+ ```
86
+
87
+ ### Skipppng sprite #ex2
88
+
89
+ Make a sprite move from the top to the bottom. Make it skip to the right once every second.
90
+
91
+ ```blocks
92
+ let mySprite = sprites.create(img`
93
+ . . . . . . . . . . . . . . . .
94
+ . . . . . . . . . . . . . . . .
95
+ . . . . . 5 5 5 5 5 5 . . . . .
96
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
97
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
98
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
99
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
100
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
101
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
102
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
103
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
104
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
105
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
106
+ . . . . . 5 5 5 5 5 5 . . . . .
107
+ . . . . . . . . . . . . . . . .
108
+ . . . . . . . . . . . . . . . .
109
+ `, SpriteKind.Player)
110
+ mySprite.setStayInScreen(true)
111
+ mySprite.top = 0
112
+ mySprite.fy = 60
113
+ mySprite.vy = 60
114
+ for (let index = 0; index < 3; index++) {
115
+ mySprite.vy = 60
116
+ pause(1000)
117
+ }
118
+ ```
119
+
120
+ ## See also #seealso
121
+
122
+ [fx](/reference/sprites/sprite/fx),
123
+ [ay](/reference/sprites/sprite/ay)
@@ -0,0 +1,123 @@
1
+ # fy (property)
2
+
3
+ Get or set the friction opposing a sprite's motion in the horizontal direction.
4
+
5
+ ## Get
6
+
7
+ Get the horizontal friction on the sprite.
8
+
9
+ ```block
10
+ let mySprite: Sprite = null
11
+
12
+ let horzAccel = mySprite.fx
13
+ ```
14
+
15
+ ```typescript-ignorelet
16
+ horzAccel = mySprite.fx
17
+ ```
18
+
19
+ ### Returns
20
+
21
+ * a [number](/types/number) that is the current horizontal friction of the sprite.
22
+
23
+ ## Set
24
+
25
+ Set the horizontal friction for the sprite.
26
+
27
+ ```block
28
+ let mySprite: Sprite = null
29
+
30
+ mySprite.fx = 0
31
+ ```
32
+
33
+ ```typescript-ignore
34
+ mySprite.fx = 0
35
+ ```
36
+
37
+ ### Parameter
38
+
39
+ * **value**: the new horizontal friction opposing the sprite's motion in pixels per second, per second.
40
+
41
+ ## Sprite horizontal friction
42
+
43
+ Friction is an opposing force against the motion of a sprite. If a sprite has a horizontal velocity (`vx`), it's friction will slow the sprite down until it's horizontal velocity becomes `0`. This is similar to setting an opposite horizontal acceleration but the opposite acceleration goes away once the sprite stops.
44
+
45
+ ## Examples #example
46
+
47
+ ### Measure stopping time #ex1
48
+
49
+ Move the sprite from left to right across the screen. Use the correct amount of friction (`fx`) to make the sprite stop at `3` seconds.
50
+
51
+ ```blocks
52
+ let stopTime = 0
53
+ let counting = true
54
+ let mySprite = sprites.create(img`
55
+ . . . . . . . . . . . . . . . .
56
+ . . . . . . . . . . . . . . . .
57
+ . . . . . 5 5 5 5 5 5 . . . . .
58
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
59
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
60
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
61
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
62
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
63
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
64
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
65
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
66
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
67
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
68
+ . . . . . 5 5 5 5 5 5 . . . . .
69
+ . . . . . . . . . . . . . . . .
70
+ . . . . . . . . . . . . . . . .
71
+ `, SpriteKind.Player)
72
+ mySprite.setStayInScreen(true)
73
+ mySprite.left = 0
74
+ mySprite.fx = 30
75
+ mySprite.vx = 90
76
+ game.onUpdateInterval(100, function () {
77
+ if (counting) {
78
+ stopTime += 0.1
79
+ if (mySprite.vx == 0) {
80
+ counting = false
81
+ mySprite.sayText("" + stopTime + "sec", 5000, false)
82
+ }
83
+ }
84
+ })
85
+ ```
86
+
87
+ ### Skipppng sprite #ex2
88
+
89
+ Make a sprite move from the left to the right. Make it skip to the right once every second.
90
+
91
+ ```blocks
92
+ let mySprite = sprites.create(img`
93
+ . . . . . . . . . . . . . . . .
94
+ . . . . . . . . . . . . . . . .
95
+ . . . . . 5 5 5 5 5 5 . . . . .
96
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
97
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
98
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
99
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
100
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
101
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
102
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
103
+ . . 5 5 5 5 5 5 5 5 5 5 5 5 . .
104
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
105
+ . . . 5 5 5 5 5 5 5 5 5 5 . . .
106
+ . . . . . 5 5 5 5 5 5 . . . . .
107
+ . . . . . . . . . . . . . . . .
108
+ . . . . . . . . . . . . . . . .
109
+ `, SpriteKind.Player)
110
+ mySprite.setStayInScreen(true)
111
+ mySprite.left = 0
112
+ mySprite.fx = 80
113
+ mySprite.vx = 80
114
+ for (let index = 0; index < 3; index++) {
115
+ mySprite.vx = 80
116
+ pause(1000)
117
+ }
118
+ ```
119
+
120
+ ## See also #seealso
121
+
122
+ [fy](/reference/sprites/sprite/fy),
123
+ [ax](/reference/sprites/sprite/ax)
@@ -58,6 +58,8 @@ sprites.onOverlap(0, 0, function (sprite, otherSprite) {})
58
58
  * [**vy- velocity y**](/reference/sprites/sprite/vy)
59
59
  * [**ax - acceleration x**](/reference/sprites/sprite/ax)
60
60
  * [**ay - acceleration y**](/reference/sprites/sprite/ay)
61
+ * [**fx - friction x**](/reference/sprites/sprite/fx)
62
+ * [**fy - friction y**](/reference/sprites/sprite/fy)
61
63
 
62
64
  ### Image and Attributes
63
65
 
@@ -1,21 +1,19 @@
1
1
  namespace game {
2
2
  export class Hitbox {
3
- img: Image;
4
- rev: number;
3
+ hash: Fx8;
5
4
  parent: Sprite;
6
5
  ox: Fx8;
7
6
  oy: Fx8;
8
7
  width: Fx8;
9
8
  height: Fx8;
10
9
 
11
- constructor(parent: Sprite, width: number, height: number, ox: number, oy: number) {
12
- this.img = parent.image;
13
- this.rev = parent.image.revision();
10
+ constructor(parent: Sprite, width: Fx8, height: Fx8, ox: Fx8, oy: Fx8) {
11
+ this.hash = parent.calcDimensionalHash();
14
12
  this.parent = parent;
15
- this.width = Fx8(width);
16
- this.height = Fx8(height);
17
- this.ox = Fx8(ox);
18
- this.oy = Fx8(oy);
13
+ this.width = width;
14
+ this.height = height;
15
+ this.ox = ox;
16
+ this.oy = oy;
19
17
  }
20
18
 
21
19
  get left() {
@@ -41,7 +39,23 @@ namespace game {
41
39
  }
42
40
 
43
41
  isValid() {
44
- return this.img === this.parent.image && this.rev === this.parent.image.revision();
42
+ return this.hash === this.parent.calcDimensionalHash();
43
+ }
44
+
45
+ contains(x: Fx8, y: Fx8): boolean {
46
+ return (x >= this.left) && (x <= this.right) && (y >= this.top) && (y <= this.bottom);
47
+ }
48
+
49
+ overlapsWith(other: Hitbox): boolean {
50
+ if (this.contains(other.left, other.top)) return true;
51
+ if (this.contains(other.left, other.bottom)) return true;
52
+ if (this.contains(other.right, other.top)) return true;
53
+ if (this.contains(other.right, other.bottom)) return true;
54
+ if (other.contains(this.left, this.top)) return true;
55
+ if (other.contains(this.left, this.bottom)) return true;
56
+ if (other.contains(this.right, this.top)) return true;
57
+ if (other.contains(this.right, this.bottom)) return true;
58
+ return false;
45
59
  }
46
60
  }
47
61
 
@@ -51,24 +65,28 @@ namespace game {
51
65
  return s._hitbox;
52
66
 
53
67
  const i = s.image;
54
- let minX = i.width;
55
- let minY = i.height;
56
- let maxX = 0;
57
- let maxY = 0;
68
+ let minX = Fx8(i.width);
69
+ let minY = Fx8(i.height);
70
+ let maxX = Fx.zeroFx8;
71
+ let maxY = Fx.zeroFx8;
58
72
 
59
- for (let c = 0; c < i.width; c++) {
60
- for (let r = 0; r < i.height; r++) {
73
+ for (let c = 0, fxc = Fx.zeroFx8; c < i.width; c++, fxc = Fx.add(fxc, Fx.oneFx8)) {
74
+ for (let r = 0, fxr = Fx.zeroFx8; r < i.height; r++, fxr = Fx.add(fxr, Fx.oneFx8)) {
61
75
  if (i.getPixel(c, r)) {
62
- minX = Math.min(minX, c);
63
- minY = Math.min(minY, r);
64
- maxX = Math.max(maxX, c);
65
- maxY = Math.max(maxY, r);
76
+ minX = Fx.min(minX, fxc);
77
+ minY = Fx.min(minY, fxr);
78
+ maxX = Fx.max(maxX, fxc);
79
+ maxY = Fx.max(maxY, fxr);
66
80
  }
67
81
  }
68
82
  }
69
83
 
70
- const width = maxX - minX + 1;
71
- const height = maxY - minY + 1;
84
+ minX = Fx.mul(minX, s._sx);
85
+ minY = Fx.mul(minY, s._sy);
86
+ maxX = Fx.mul(maxX, s._sx);
87
+ maxY = Fx.mul(maxY, s._sy);
88
+ const width = Fx.add(Fx.sub(maxX, minX), Fx.oneFx8);
89
+ const height = Fx.add(Fx.sub(maxY, minY), Fx.oneFx8);
72
90
 
73
91
  return new Hitbox(s, width, height, minX, minY);
74
92
  }
@@ -61,6 +61,8 @@ namespace sprites {
61
61
  }
62
62
 
63
63
  calculatePartialHeight(startLine: number, lengthToDraw: number) {
64
+ if (this.linebreaks.length === 0) return this.font.charHeight;
65
+
64
66
  let current = 0;
65
67
 
66
68
  for (let i = startLine; i < this.linebreaks.length + 1; i++) {
@@ -93,9 +95,9 @@ namespace sprites {
93
95
  return total;
94
96
  }
95
97
 
96
- protected lineEnd(index: number) {
97
- const prevEnd = index > 0 ? this.linebreaks[index - 1] : 0;
98
- let end = index < this.linebreaks.length ? this.linebreaks[index] : this.text.length;
98
+ lineEnd(lineIndex: number) {
99
+ const prevEnd = lineIndex > 0 ? this.linebreaks[lineIndex - 1] : 0;
100
+ let end = lineIndex < this.linebreaks.length ? this.linebreaks[lineIndex] : this.text.length;
99
101
  let didMove = false;
100
102
 
101
103
  // Trim trailing whitespace
@@ -115,8 +117,8 @@ namespace sprites {
115
117
  return didMove ? end + 1 : end;
116
118
  }
117
119
 
118
- protected lineStart(index: number) {
119
- let start = index > 0 ? this.linebreaks[index - 1] : 0;
120
+ lineStart(lineIndex: number) {
121
+ let start = lineIndex > 0 ? this.linebreaks[lineIndex - 1] : 0;
120
122
 
121
123
  // Trim leading whitespace
122
124
  while (start < this.text.length) {
@@ -133,6 +135,29 @@ namespace sprites {
133
135
 
134
136
  return start;
135
137
  }
138
+
139
+ widthOfLine(lineIndex: number, fullTextOffset?: number) {
140
+ if (fullTextOffset != undefined) {
141
+ return (Math.min(this.lineEnd(lineIndex), fullTextOffset + 1) - this.lineStart(lineIndex)) * this.font.charWidth;
142
+ }
143
+ return (this.lineEnd(lineIndex) - this.lineStart(lineIndex)) * this.font.charWidth;
144
+ }
145
+
146
+ widthOfLines(lineStartIndex: number, lineEndIndex: number, offset?: number) {
147
+ if (this.linebreaks.length === 0) return this.widthOfLine(0, offset);
148
+
149
+ let width = 0;
150
+ let fullTextOffset: number;
151
+ for (let i = lineStartIndex; i < Math.min(lineEndIndex, this.linebreaks.length + 1); i++) {
152
+ if (offset != undefined) {
153
+ fullTextOffset = this.lineStart(i) + offset;
154
+ offset -= this.lineEnd(i) - this.lineStart(i);
155
+ }
156
+ if (fullTextOffset !== undefined && this.lineStart(i) > fullTextOffset) break;
157
+ width = Math.max(width, this.widthOfLine(i, fullTextOffset));
158
+ }
159
+ return width;
160
+ }
136
161
  }
137
162
 
138
163
 
@@ -203,6 +228,9 @@ namespace sprites {
203
228
  protected pageLine: number;
204
229
  protected timer: number;
205
230
  protected pauseMillis: number;
231
+ protected onTickCB: () => void;
232
+ protected onEndCB: () => void;
233
+ protected prevOffset: number;
206
234
 
207
235
  constructor(public text: RenderText, public height: number) {
208
236
  this.state = RenderTextAnimationState.Idle;
@@ -232,17 +260,21 @@ namespace sprites {
232
260
  }
233
261
 
234
262
  currentHeight() {
235
- const maxHeight = Math.min(
236
- Math.idiv(this.height, this.text.lineHeight()) + 1,
237
- this.text.linebreaks.length + 1 - this.pageLine
238
- ) * this.text.lineHeight();
263
+ const minHeight = this.text.lineHeight();
264
+ const maxHeight = Math.max(
265
+ Math.min(
266
+ Math.idiv(this.height, this.text.lineHeight()) + 1,
267
+ this.text.linebreaks.length + 1 - this.pageLine
268
+ ) * this.text.lineHeight(),
269
+ minHeight
270
+ );
239
271
 
240
272
 
241
273
  if (this.state === RenderTextAnimationState.Printing) {
242
- return Math.min(
274
+ return Math.max(Math.min(
243
275
  this.text.calculatePartialHeight(this.pageLine, this.currentOffset()),
244
276
  maxHeight
245
- );
277
+ ), minHeight)
246
278
  }
247
279
  else if (this.state === RenderTextAnimationState.Pausing) {
248
280
  return maxHeight
@@ -252,10 +284,34 @@ namespace sprites {
252
284
  }
253
285
  }
254
286
 
287
+ currentWidth() {
288
+ return this.text.widthOfLines(
289
+ this.pageLine,
290
+ this.pageLine + Math.idiv(this.currentHeight(), this.text.lineHeight()) + 1,
291
+ this.state === RenderTextAnimationState.Printing ? this.currentOffset() : undefined
292
+ );
293
+ }
294
+
255
295
  currentOffset() {
256
296
  return Math.idiv(control.millis() - this.timer, this.tickPeriod)
257
297
  }
258
298
 
299
+ isDone() {
300
+ return this.state === RenderTextAnimationState.Idle;
301
+ }
302
+
303
+ cancel() {
304
+ this.state = RenderTextAnimationState.Idle;
305
+ }
306
+
307
+ onCharacterPrinted(cb: () => void) {
308
+ this.onTickCB = cb;
309
+ }
310
+
311
+ onAnimationEnd(cb: () => void) {
312
+ this.onEndCB = cb;
313
+ }
314
+
259
315
  draw(canvas: Image, left: number, top: number, color: number) {
260
316
  if (this.state === RenderTextAnimationState.Idle) return;
261
317
  else if (this.state === RenderTextAnimationState.Printing) {
@@ -269,6 +325,10 @@ namespace sprites {
269
325
  this.pageLine + Math.idiv(this.height, this.text.lineHeight()) + 1
270
326
  );
271
327
 
328
+ if (this.onTickCB && this.prevOffset !== this.currentOffset()) {
329
+ this.onTickCB();
330
+ }
331
+
272
332
  if (pageFinished) {
273
333
  this.state = RenderTextAnimationState.Pausing;
274
334
  this.timer = this.pauseMillis
@@ -290,6 +350,7 @@ namespace sprites {
290
350
  this.pageLine += Math.idiv(this.height, this.text.lineHeight()) + 1;
291
351
  if (this.pageLine > this.text.linebreaks.length) {
292
352
  this.state = RenderTextAnimationState.Idle;
353
+ if (this.onEndCB) this.onEndCB();
293
354
  }
294
355
  else {
295
356
  this.state = RenderTextAnimationState.Printing;
@@ -297,6 +358,8 @@ namespace sprites {
297
358
  }
298
359
  }
299
360
  }
361
+
362
+ this.prevOffset = this.currentOffset();
300
363
  }
301
364
  }
302
365
  }