-
Notifications
You must be signed in to change notification settings - Fork 1
/
cursor.ts
181 lines (163 loc) · 6.47 KB
/
cursor.ts
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
namespace kodu {
export type VisualState = "free" | "burdened";
export class Cursor extends Component {
kel: Kelpie;
disabled: boolean;
visualState: VisualState;
public get x() { return this.kel.x; }
public get y() { return this.kel.y; }
public set x(v: number) { this.kel.x = v; }
public set y(v: number) { this.kel.y = v; }
public get pos(): Vec2 { return this.kel.pos; }
constructor(stage: Stage, private baseCursor: string) {
super(stage, "cursor");
this.kel = new Kelpie(icons.get(`cursor_free_${baseCursor}`));
this.kel.z = 1000;
this.kel.data["kind"] = "cursor";
this.kel.data["component"] = this;
this.setVisualState("free");
}
public moveTo(x: number, y: number) {
if (this.disabled) { return; }
this.x = x;
this.y = y;
}
public disable() {
this.disabled = true;
this.kel.invisible = true;
}
public enable() {
this.disabled = false;
this.kel.invisible = false;
this.setVisualState(this.visualState);
}
public setVisualState(state: VisualState) {
this.visualState = state;
this.kel.image = icons.get(`cursor_${state}_${this.baseCursor}`);
}
getAllOverlapping(): Kelpie[] {
return this.stage.radar.getOverlapping(this.kel);
}
handleAPressed() {
if (this.disabled) { return; }
const overlaps = this.getAllOverlapping();
if (!overlaps.length) {
// Click the canvas.
this.stage.notify("cursor:canvasClick", { x: this.x, y: this.y });
return;
}
{ // Click a button?
const buttons = (overlaps
.filter(value => value.data["kind"] === "button")
.map(value => value.data["component"]) as Button[])
.filter(value => value.clickable());
const button = buttons.shift();
if (button) {
this.stage.notify("cursor:buttonClick", { button, x: this.x, y: this.y });
return;
}
}
{
// Click a character?
const chars = overlaps
.filter(value => value.data["kind"] === "character")
.map(value => value.data["component"]) as Character[];
const char = chars.shift();
if (char) {
this.stage.notify("cursor:characterClick", { char, x: this.x, y: this.y });
return;
}
}
}
handleBPressed() {
if (this.disabled) { return; }
this.stage.notify("cursor:cancel", { x: this.x, y: this.y });
}
notify(event: string, parm: any) {
if (event === "save") {
const savedGame = parm as SavedGame;
savedGame.cursor = { x: this.x, y: this.y };
} else if (event === "load") {
const savedGame = parm as SavedGame;
if (savedGame.cursor) {
this.x = savedGame.cursor.x;
this.y = savedGame.cursor.y;
}
this.disabled = false;
this.setVisualState(this.visualState);
}
}
}
const maxCursorSpeed = 140 / 1000; // pixels/milli
const startCursorSpeed = 40 / 1000; //
const cursorSpeedInc = 20 / 1000; //
const shiftGearsAt = 50; // millis
export class WorldCursor extends Cursor {
moveStartMs: number; // millis at move start
cursorSpeed: number; // pixels/milli
constructor(stage: Stage, baseCursor: string) {
super(stage, baseCursor);
this.moveStartMs = 0;
this.cursorSpeed = 0;
}
update(dt: number) {
if (this.disabled) { return; }
let x = (controller.right.isPressed() ? 1 : 0) - (controller.left.isPressed() ? 1 : 0);
let y = (controller.down.isPressed() ? 1 : 0) - (controller.up.isPressed() ? 1 : 0);
if (x || y) {
const t = control.millis();
if (t > this.moveStartMs + shiftGearsAt) {
this.moveStartMs = t;
this.cursorSpeed += cursorSpeedInc;
this.cursorSpeed = Math.min(this.cursorSpeed, maxCursorSpeed);
}
this.x += x * this.cursorSpeed * dt;
this.y += y * this.cursorSpeed * dt;
this.stage.notify("cursor:moved", { x: this.x, y: this.y });
} else {
this.moveStartMs = control.millis();
this.cursorSpeed = startCursorSpeed;
}
}
}
export class StickyCursor extends Cursor {
pressed: boolean;
nextMs: number;
constructor(stage: Stage, baseCursor: string) {
super(stage, baseCursor);
this.kel.hitbox = new Hitbox(1, 1, 8, 10);
}
update(dt: number) {
if (this.disabled) { return; }
const wasPressed = this.pressed;
let x = (controller.right.isPressed() ? 1 : 0) - (controller.left.isPressed() ? 1 : 0);
let y = (controller.down.isPressed() ? 1 : 0) - (controller.up.isPressed() ? 1 : 0);
let dir: CardinalDirection = 0;
if (x > 0) dir |= CardinalDirection.East;
if (x < 0) dir |= CardinalDirection.West;
if (y > 0) dir |= CardinalDirection.South;
if (y < 0) dir |= CardinalDirection.North;
this.pressed = !!dir;
if (!this.pressed) { return; }
// TODO make a debouncer for this
const t = control.millis();
let exec = false;
if (!wasPressed) {
this.nextMs = t + INPUT_INITIAL_DELAY;
exec = true;
} else if (t >= this.nextMs) {
this.nextMs = t + INPUT_REPEAT_DELAY;
exec = true;
}
if (exec) {
const kel = this.stage.radar.getNearestInDirection(this.kel, dir, 0, 200);
if (kel) {
this.x = kel.x;
this.y = kel.y;
} else {
this.pressed = false;
}
}
}
}
}