|
1 | | -namespace kojac { |
2 | | - export type CursorMode = "free" | "burdened"; |
3 | | - |
4 | | - const maxCursorSpeed = 120 / 1000; // pixels/milli |
5 | | - const startCursorSpeed = 10 / 1000; // |
6 | | - const cursorSpeedInc = 10 / 1000; // |
7 | | - const shiftGearsAt = 1000; // millis |
8 | | - |
9 | | - export class Cursor extends Component { |
10 | | - cursorMode: CursorMode; |
11 | | - kel0: Kelpie; |
12 | | - kel1: Kelpie; |
13 | | - disabled: boolean; |
14 | | - moveStartMs: number; // millis at move start |
15 | | - cursorSpeed: number; // pixels/milli |
16 | | - |
17 | | - public get x() { return this.kel0.x; } |
18 | | - public get y() { return this.kel0.y; } |
19 | | - public set x(v: number) { |
20 | | - this.kel0.x = v; |
21 | | - this.kel1.x = v; |
22 | | - } |
23 | | - public set y(v: number) { |
24 | | - this.kel0.y = v; |
25 | | - this.kel1.y = v; |
26 | | - } |
27 | | - |
28 | | - constructor(stage: Stage) { |
29 | | - super(stage, "cursor"); |
30 | | - this.kel0 = new Kelpie(icons.get("cursor")); |
31 | | - this.kel1 = new Kelpie(icons.get("carry")); |
32 | | - this.kel0.invisible = true; |
33 | | - this.kel1.invisible = true; |
34 | | - this.kel0.z = 1000; |
35 | | - this.kel1.z = 1000; |
36 | | - this.kel0.data["kind"] = "cursor"; |
37 | | - this.kel0.data["component"] = this; |
38 | | - this.setCursorMode("free"); |
39 | | - this.moveStartMs = 0; |
40 | | - this.cursorSpeed = 0; |
41 | | - } |
42 | | - |
43 | | - public setCursorMode(mode: CursorMode) { |
44 | | - this.cursorMode = mode; |
45 | | - this.kel0.invisible = mode !== "free"; |
46 | | - this.kel1.invisible = mode !== "burdened"; |
47 | | - } |
48 | | - |
49 | | - public moveTo(x: number, y: number) { |
50 | | - if (this.disabled) { return; } |
51 | | - this.x = x; |
52 | | - this.y = y; |
53 | | - } |
54 | | - |
55 | | - public disable() { |
56 | | - this.disabled = true; |
57 | | - this.kel0.invisible = true; |
58 | | - this.kel1.invisible = true; |
59 | | - } |
60 | | - |
61 | | - public enable() { |
62 | | - this.disabled = false; |
63 | | - this.setCursorMode(this.cursorMode); |
64 | | - } |
65 | | - |
66 | | - getAllOverlapping() { |
67 | | - return util.getAllOverlapping(this.kel0) |
68 | | - .filter(spr => util.pointInSprite(spr, this.x, this.y)) |
69 | | - .sort((a, b) => b.z - a.z); |
70 | | - } |
71 | | - |
72 | | - handleAPressed() { |
73 | | - if (this.disabled) { return; } |
74 | | - const overlaps = this.getAllOverlapping(); |
75 | | - if (!overlaps.length) { |
76 | | - // Click the canvas. |
77 | | - this.stage.notify("cursor:canvasClick", { x: this.x, y: this.y }); |
78 | | - return; |
79 | | - } |
80 | | - { // Click a button? |
81 | | - const buttons = (overlaps |
82 | | - .filter(value => value.data["kind"] === "button") |
83 | | - .map(value => value.data["component"]) as Button[]) |
84 | | - .filter(value => value.clickable()); |
85 | | - const button = buttons.shift(); |
86 | | - if (button) { |
87 | | - this.stage.notify("cursor:buttonClick", { button, x: this.x, y: this.y }); |
88 | | - return; |
89 | | - } |
90 | | - } |
91 | | - { |
92 | | - // Click a character? |
93 | | - const chars = overlaps |
94 | | - .filter(value => value.data["kind"] === "character") |
95 | | - .map(value => value.data["component"]) as Character[]; |
96 | | - const char = chars.shift(); |
97 | | - if (char) { |
98 | | - this.stage.notify("cursor:characterClick", { char, x: this.x, y: this.y }); |
99 | | - return; |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - |
104 | | - handleBPressed() { |
105 | | - if (this.disabled) { return; } |
106 | | - this.stage.notify("cursor:cancel", { x: this.x, y: this.y }); |
107 | | - } |
108 | | - |
109 | | - update(dt: number) { |
110 | | - if (this.disabled) { return; } |
111 | | - let x = 0; |
112 | | - let y = 0; |
113 | | - if (controller.up.isPressed()) { |
114 | | - y -= 1; |
115 | | - } |
116 | | - if (controller.down.isPressed()) { |
117 | | - y += 1; |
118 | | - } |
119 | | - if (controller.left.isPressed()) { |
120 | | - x -= 1; |
121 | | - } |
122 | | - if (controller.right.isPressed()) { |
123 | | - x += 1; |
124 | | - } |
125 | | - if (x || y) { |
126 | | - const t = control.millis(); |
127 | | - if (t + shiftGearsAt > this.moveStartMs) { |
128 | | - this.moveStartMs = t; |
129 | | - this.cursorSpeed += cursorSpeedInc; |
130 | | - this.cursorSpeed = Math.min(this.cursorSpeed, maxCursorSpeed); |
131 | | - } |
132 | | - this.x += x * this.cursorSpeed * dt; |
133 | | - this.y += y * this.cursorSpeed * dt; |
134 | | - this.stage.notify("cursor:moved", { x: this.x, y: this.y }); |
135 | | - } else { |
136 | | - this.moveStartMs = control.millis(); |
137 | | - this.cursorSpeed = startCursorSpeed; |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - notify(event: string, parm: any) { |
142 | | - if (event === "save") { |
143 | | - const savedGame = parm as SavedGame; |
144 | | - savedGame.cursor = { x: this.x, y: this.y }; |
145 | | - } else if (event === "load") { |
146 | | - const savedGame = parm as SavedGame; |
147 | | - if (savedGame.cursor) { |
148 | | - this.x = savedGame.cursor.x; |
149 | | - this.y = savedGame.cursor.y; |
150 | | - } |
151 | | - this.disabled = false; |
152 | | - this.setCursorMode(this.cursorMode); |
153 | | - } |
154 | | - } |
155 | | - } |
| 1 | +{ |
| 2 | + "name": "kojac", |
| 3 | + "description": "", |
| 4 | + "dependencies": { |
| 5 | + "device": "*", |
| 6 | + "arcade-text": "github:microsoft/arcade-text#v1.3.0" |
| 7 | + }, |
| 8 | + "files": [ |
| 9 | + "main.ts", |
| 10 | + "README.md", |
| 11 | + "extras.ts", |
| 12 | + "button.ts", |
| 13 | + "camera.ts", |
| 14 | + "component.ts", |
| 15 | + "assets.ts", |
| 16 | + "cursor.ts", |
| 17 | + "character.ts", |
| 18 | + "menu.ts", |
| 19 | + "stage.ts", |
| 20 | + "language.ts", |
| 21 | + "physics.ts", |
| 22 | + "characters.ts", |
| 23 | + "library.ts", |
| 24 | + "program.ts", |
| 25 | + "app.ts", |
| 26 | + "vec2.ts", |
| 27 | + "worldStage.ts", |
| 28 | + "kodeStage.ts", |
| 29 | + "kelpie.ts" |
| 30 | + ], |
| 31 | + "testFiles": [], |
| 32 | + "targetVersions": { |
| 33 | + "target": "1.3.30", |
| 34 | + "targetId": "arcade" |
| 35 | + }, |
| 36 | + "supportedTargets": [ |
| 37 | + "arcade" |
| 38 | + ], |
| 39 | + "preferredEditor": "tsprj", |
| 40 | + "disableTargetTemplateFiles": true |
156 | 41 | } |
0 commit comments