kaplay 3001.0.0-alpha.1

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,132 @@
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.loaded = true
15
+ this.data = data
16
+ this.onLoadEvents.trigger(data)
17
+ }).catch((err) => {
18
+ this.error = err
19
+ if (this.onErrorEvents.numListeners() > 0) {
20
+ this.onErrorEvents.trigger(err)
21
+ } else {
22
+ throw err
23
+ }
24
+ }).finally(() => {
25
+ this.onFinishEvents.trigger()
26
+ this.loaded = true
27
+ })
28
+ }
29
+ static loaded<D>(data: D): Asset<D> {
30
+ const asset = new Asset(Promise.resolve(data)) as Asset<D>
31
+ asset.data = data
32
+ asset.loaded = true
33
+ return asset
34
+ }
35
+ onLoad(action: (data: D) => void) {
36
+ if (this.loaded && this.data) {
37
+ action(this.data)
38
+ } else {
39
+ this.onLoadEvents.add(action)
40
+ }
41
+ return this
42
+ }
43
+ onError(action: (err: Error) => void) {
44
+ if (this.loaded && this.error) {
45
+ action(this.error)
46
+ } else {
47
+ this.onErrorEvents.add(action)
48
+ }
49
+ return this
50
+ }
51
+ onFinish(action: () => void) {
52
+ if (this.loaded) {
53
+ action()
54
+ } else {
55
+ this.onFinishEvents.add(action)
56
+ }
57
+ return this
58
+ }
59
+ then(action: (data: D) => void): Asset<D> {
60
+ return this.onLoad(action)
61
+ }
62
+ catch(action: (err: Error) => void): Asset<D> {
63
+ return this.onError(action)
64
+ }
65
+ finally(action: () => void): Asset<D> {
66
+ return this.onFinish(action)
67
+ }
68
+ }
69
+
70
+ export class AssetBucket<D> {
71
+ assets: Map<string, Asset<D>> = new Map()
72
+ lastUID: number = 0
73
+ add(name: string | null, loader: Promise<D>): Asset<D> {
74
+ // if user don't provide a name we use a generated one
75
+ const id = name ?? (this.lastUID++ + "")
76
+ const asset = new Asset(loader)
77
+ this.assets.set(id, asset)
78
+ return asset
79
+ }
80
+ addLoaded(name: string | null, data: D): Asset<D> {
81
+ const id = name ?? (this.lastUID++ + "")
82
+ const asset = Asset.loaded(data)
83
+ this.assets.set(id, asset)
84
+ return asset
85
+ }
86
+ get(handle: string): Asset<D> | void {
87
+ return this.assets.get(handle)
88
+ }
89
+ progress(): number {
90
+ if (this.assets.size === 0) {
91
+ return 1
92
+ }
93
+ let loaded = 0
94
+ this.assets.forEach((asset) => {
95
+ if (asset.loaded) {
96
+ loaded++
97
+ }
98
+ })
99
+ return loaded / this.assets.size
100
+ }
101
+ }
102
+
103
+ export function fetchURL(url: string) {
104
+ return fetch(url)
105
+ .then((res) => {
106
+ if (!res.ok) throw new Error(`Failed to fetch "${url}"`)
107
+ return res
108
+ })
109
+ }
110
+
111
+ export function fetchJSON(path: string) {
112
+ return fetchURL(path).then((res) => res.json())
113
+ }
114
+
115
+ export function fetchText(path: string) {
116
+ return fetchURL(path).then((res) => res.text())
117
+ }
118
+
119
+ export function fetchArrayBuffer(path: string) {
120
+ return fetchURL(path).then((res) => res.arrayBuffer())
121
+ }
122
+
123
+ // wrapper around image loader to get a Promise
124
+ export function loadImg(src: string): Promise<HTMLImageElement> {
125
+ const img = new Image()
126
+ img.crossOrigin = "anonymous"
127
+ img.src = src
128
+ return new Promise<HTMLImageElement>((resolve, reject) => {
129
+ img.onload = () => resolve(img)
130
+ img.onerror = () => reject(new Error(`Failed to load image from "${src}"`))
131
+ })
132
+ }
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
+ }