kaplay 3000.1.17

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.
Binary file
Binary file
Binary file
@@ -0,0 +1,9 @@
1
+ declare module "*.mp3" {
2
+ const value: Uint8Array
3
+ export default value
4
+ }
5
+
6
+ declare module "*.png" {
7
+ const value: string
8
+ export default value
9
+ }
Binary file
package/src/assets.ts ADDED
@@ -0,0 +1,131 @@
1
+ import {
2
+ Event,
3
+ } from "./utils"
4
+
5
+ export class Asset<D> {
6
+ loaded: boolean = false
7
+ data: D | null = null
8
+ error: Error | null = null
9
+ private onLoadEvents: Event<[D]> = new Event()
10
+ private onErrorEvents: Event<[Error]> = new Event()
11
+ private onFinishEvents: Event<[]> = new Event()
12
+ constructor(loader: Promise<D>) {
13
+ loader.then((data) => {
14
+ this.data = data
15
+ this.onLoadEvents.trigger(data)
16
+ }).catch((err) => {
17
+ this.error = err
18
+ if (this.onErrorEvents.numListeners() > 0) {
19
+ this.onErrorEvents.trigger(err)
20
+ } else {
21
+ throw err
22
+ }
23
+ }).finally(() => {
24
+ this.onFinishEvents.trigger()
25
+ this.loaded = true
26
+ })
27
+ }
28
+ static loaded<D>(data: D): Asset<D> {
29
+ const asset = new Asset(Promise.resolve(data)) as Asset<D>
30
+ asset.data = data
31
+ asset.loaded = true
32
+ return asset
33
+ }
34
+ onLoad(action: (data: D) => void) {
35
+ if (this.loaded && this.data) {
36
+ action(this.data)
37
+ } else {
38
+ this.onLoadEvents.add(action)
39
+ }
40
+ return this
41
+ }
42
+ onError(action: (err: Error) => void) {
43
+ if (this.loaded && this.error) {
44
+ action(this.error)
45
+ } else {
46
+ this.onErrorEvents.add(action)
47
+ }
48
+ return this
49
+ }
50
+ onFinish(action: () => void) {
51
+ if (this.loaded) {
52
+ action()
53
+ } else {
54
+ this.onFinishEvents.add(action)
55
+ }
56
+ return this
57
+ }
58
+ then(action: (data: D) => void): Asset<D> {
59
+ return this.onLoad(action)
60
+ }
61
+ catch(action: (err: Error) => void): Asset<D> {
62
+ return this.onError(action)
63
+ }
64
+ finally(action: () => void): Asset<D> {
65
+ return this.onFinish(action)
66
+ }
67
+ }
68
+
69
+ export class AssetBucket<D> {
70
+ assets: Map<string, Asset<D>> = new Map()
71
+ lastUID: number = 0
72
+ add(name: string | null, loader: Promise<D>): Asset<D> {
73
+ // if user don't provide a name we use a generated one
74
+ const id = name ?? (this.lastUID++ + "")
75
+ const asset = new Asset(loader)
76
+ this.assets.set(id, asset)
77
+ return asset
78
+ }
79
+ addLoaded(name: string | null, data: D): Asset<D> {
80
+ const id = name ?? (this.lastUID++ + "")
81
+ const asset = Asset.loaded(data)
82
+ this.assets.set(id, asset)
83
+ return asset
84
+ }
85
+ get(handle: string): Asset<D> | void {
86
+ return this.assets.get(handle)
87
+ }
88
+ progress(): number {
89
+ if (this.assets.size === 0) {
90
+ return 1
91
+ }
92
+ let loaded = 0
93
+ this.assets.forEach((asset) => {
94
+ if (asset.loaded) {
95
+ loaded++
96
+ }
97
+ })
98
+ return loaded / this.assets.size
99
+ }
100
+ }
101
+
102
+ export function fetchURL(url: string) {
103
+ return fetch(url)
104
+ .then((res) => {
105
+ if (!res.ok) throw new Error(`Failed to fetch "${url}"`)
106
+ return res
107
+ })
108
+ }
109
+
110
+ export function fetchJSON(path: string) {
111
+ return fetchURL(path).then((res) => res.json())
112
+ }
113
+
114
+ export function fetchText(path: string) {
115
+ return fetchURL(path).then((res) => res.text())
116
+ }
117
+
118
+ export function fetchArrayBuffer(path: string) {
119
+ return fetchURL(path).then((res) => res.arrayBuffer())
120
+ }
121
+
122
+ // wrapper around image loader to get a Promise
123
+ export function loadImg(src: string): Promise<HTMLImageElement> {
124
+ const img = new Image()
125
+ img.crossOrigin = "anonymous"
126
+ img.src = src
127
+ return new Promise<HTMLImageElement>((resolve, reject) => {
128
+ img.onload = () => resolve(img)
129
+ img.onerror = () => reject(new Error(`Failed to load image from "${src}"`))
130
+ })
131
+ }
package/src/easings.ts ADDED
@@ -0,0 +1,94 @@
1
+ // https://easings.net/
2
+ const c1 = 1.70158
3
+ const c2 = c1 * 1.525
4
+ const c3 = c1 + 1
5
+ const c4 = (2 * Math.PI) / 3
6
+ const c5 = (2 * Math.PI) / 4.5
7
+
8
+ const easings = {
9
+ linear: (x) => x,
10
+ easeInSine: (x) => 1 - Math.cos((x * Math.PI) / 2),
11
+ easeOutSine: (x) => Math.sin((x * Math.PI) / 2),
12
+ easeInOutSine: (x) => -(Math.cos(Math.PI * x) - 1) / 2,
13
+ easeInQuad: (x) => x * x,
14
+ easeOutQuad: (x) => 1 - (1 - x) * (1 - x),
15
+ easeInOutQuad: (x) => x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2,
16
+ easeInCubic: (x) => x * x * x,
17
+ easeOutCubic: (x) => 1 - Math.pow(1 - x, 3),
18
+ easeInOutCubic: (x) => x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2,
19
+ easeInQuart: (x) => x * x * x * x,
20
+ easeOutQuart: (x) => 1 - Math.pow(1 - x, 4),
21
+ easeInOutQuart: (x) => x < 0.5 ? 8 * x * x * x * x : 1 - Math.pow(-2 * x + 2, 4) / 2,
22
+ easeInQuint: (x) => x * x * x * x * x,
23
+ easeOutQuint: (x) => 1 - Math.pow(1 - x, 5),
24
+ easeInOutQuint: (x) => x < 0.5 ? 16 * x * x * x * x * x : 1 - Math.pow(-2 * x + 2, 5) / 2,
25
+ easeInExpo: (x) => x === 0 ? 0 : Math.pow(2, 10 * x - 10),
26
+ easeOutExpo: (x) => x === 1 ? 1 : 1 - Math.pow(2, -10 * x),
27
+ easeInOutExpo: (x) => {
28
+ return x === 0
29
+ ? 0
30
+ : x === 1
31
+ ? 1
32
+ : x < 0.5
33
+ ? Math.pow(2, 20 * x - 10) / 2
34
+ : (2 - Math.pow(2, -20 * x + 10)) / 2
35
+ },
36
+ easeInCirc: (x) => 1 - Math.sqrt(1 - Math.pow(x, 2)),
37
+ easeOutCirc: (x) => Math.sqrt(1 - Math.pow(x - 1, 2)),
38
+ easeInOutCirc: (x) => {
39
+ return x < 0.5
40
+ ? (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2
41
+ : (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2
42
+ },
43
+ easeInBack: (x) => c3 * x * x * x - c1 * x * x,
44
+ easeOutBack: (x) => 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2),
45
+ easeInOutBack: (x) => {
46
+ return x < 0.5
47
+ ? (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2
48
+ : (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2
49
+ },
50
+ easeInElastic: (x) => {
51
+ return x === 0
52
+ ? 0
53
+ : x === 1
54
+ ? 1
55
+ : -Math.pow(2, 10 * x - 10) * Math.sin((x * 10 - 10.75) * c4)
56
+ },
57
+ easeOutElastic: (x) => {
58
+ return x === 0
59
+ ? 0
60
+ : x === 1
61
+ ? 1
62
+ : Math.pow(2, -10 * x) * Math.sin((x * 10 - 0.75) * c4) + 1
63
+ },
64
+ easeInOutElastic: (x) => {
65
+ return x === 0
66
+ ? 0
67
+ : x === 1
68
+ ? 1
69
+ : x < 0.5
70
+ ? -(Math.pow(2, 20 * x - 10) * Math.sin((20 * x - 11.125) * c5)) / 2
71
+ : (Math.pow(2, -20 * x + 10) * Math.sin((20 * x - 11.125) * c5)) / 2 + 1
72
+ },
73
+ easeInBounce: (x) => 1 - easings.easeOutBounce(1 - x),
74
+ easeOutBounce: (x) => {
75
+ const n1 = 7.5625
76
+ const d1 = 2.75
77
+ if (x < 1 / d1) {
78
+ return n1 * x * x
79
+ } else if (x < 2 / d1) {
80
+ return n1 * (x -= 1.5 / d1) * x + 0.75
81
+ } else if (x < 2.5 / d1) {
82
+ return n1 * (x -= 2.25 / d1) * x + 0.9375
83
+ } else {
84
+ return n1 * (x -= 2.625 / d1) * x + 0.984375
85
+ }
86
+ },
87
+ easeInOutBounce: (x) => {
88
+ return x < 0.5
89
+ ? (1 - easings.easeOutBounce(1 - 2 * x)) / 2
90
+ : (1 + easings.easeOutBounce(2 * x - 1)) / 2
91
+ },
92
+ }
93
+
94
+ export default easings
@@ -0,0 +1,111 @@
1
+ {
2
+ "Joy-Con L+R (STANDARD GAMEPAD Vendor: 057e Product: 200e)": {
3
+ "buttons": {
4
+ "0": "south",
5
+ "1": "east",
6
+ "2": "west",
7
+ "3": "north",
8
+ "4": "lshoulder",
9
+ "5": "rshoulder",
10
+ "6": "ltrigger",
11
+ "7": "rtrigger",
12
+ "8": "select",
13
+ "9": "start",
14
+ "10": "lstick",
15
+ "11": "rstick",
16
+ "12": "dpad-up",
17
+ "13": "dpad-down",
18
+ "14": "dpad-left",
19
+ "15": "dpad-right",
20
+ "16": "home",
21
+ "17": "capture"
22
+ },
23
+ "sticks": {
24
+ "left": { "x": 0, "y": 1 },
25
+ "right": { "x": 2, "y": 3 }
26
+ }
27
+ },
28
+ "Joy-Con (L) (STANDARD GAMEPAD Vendor: 057e Product: 2006)": {
29
+ "buttons": {
30
+ "0": "south",
31
+ "1": "east",
32
+ "2": "west",
33
+ "3": "north",
34
+ "4": "lshoulder",
35
+ "5": "rshoulder",
36
+ "9": "select",
37
+ "10": "lstick",
38
+ "16": "start"
39
+ },
40
+ "sticks": {
41
+ "left": { "x": 0, "y": 1 }
42
+ }
43
+ },
44
+ "Joy-Con (R) (STANDARD GAMEPAD Vendor: 057e Product: 2007)": {
45
+ "buttons": {
46
+ "0": "south",
47
+ "1": "east",
48
+ "2": "west",
49
+ "3": "north",
50
+ "4": "lshoulder",
51
+ "5": "rshoulder",
52
+ "9": "start",
53
+ "10": "lstick",
54
+ "16": "select"
55
+ },
56
+ "sticks": {
57
+ "left": { "x": 0, "y": 1 }
58
+ }
59
+ },
60
+ "Pro Controller (STANDARD GAMEPAD Vendor: 057e Product: 2009)": {
61
+ "buttons": {
62
+ "0": "south",
63
+ "1": "east",
64
+ "2": "west",
65
+ "3": "north",
66
+ "4": "lshoulder",
67
+ "5": "rshoulder",
68
+ "6": "ltrigger",
69
+ "7": "rtrigger",
70
+ "8": "select",
71
+ "9": "start",
72
+ "10": "lstick",
73
+ "11": "rstick",
74
+ "12": "dpad-up",
75
+ "13": "dpad-down",
76
+ "14": "dpad-left",
77
+ "15": "dpad-right",
78
+ "16": "home",
79
+ "17": "capture"
80
+ },
81
+ "sticks": {
82
+ "left": { "x": 0, "y": 1 },
83
+ "right": { "x": 2, "y": 3 }
84
+ }
85
+ },
86
+ "default": {
87
+ "buttons": {
88
+ "0": "south",
89
+ "1": "east",
90
+ "2": "west",
91
+ "3": "north",
92
+ "4": "lshoulder",
93
+ "5": "rshoulder",
94
+ "6": "ltrigger",
95
+ "7": "rtrigger",
96
+ "8": "select",
97
+ "9": "start",
98
+ "10": "lstick",
99
+ "11": "rstick",
100
+ "12": "dpad-up",
101
+ "13": "dpad-down",
102
+ "14": "dpad-left",
103
+ "15": "dpad-right",
104
+ "16": "home"
105
+ },
106
+ "sticks": {
107
+ "left": { "x": 0, "y": 1 },
108
+ "right": { "x": 2, "y": 3 }
109
+ }
110
+ }
111
+ }