Skip to content

Commit 5b68c69

Browse files
authored
Feature/fix player spawn (#61)
* update window.input interface * update spawn method to use window.input.trySpawn() * fix bug
1 parent a02472d commit 5b68c69

2 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/apis/player.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Player extends EventEmitter {
1515
#mouseLock = false;
1616
#mouseCanvasPos = new Vector(0, 0);
1717
#mousePos = new Vector(0, 0);
18+
19+
#username = _window.localStorage.name;
1820
#gamemode = _window.localStorage.gamemode;
1921
#level = 1;
2022
#tank = 'Tank';
@@ -74,6 +76,14 @@ class Player extends EventEmitter {
7476
},
7577
});
7678

79+
// username
80+
_window.input.trySpawn = new Proxy(_window.input.trySpawn, {
81+
apply: (target, thisArg, args) => {
82+
this.#username = args[0];
83+
return Reflect.apply(target, thisArg, args);
84+
},
85+
});
86+
7787
// tank and level event listener
7888
CanvasKit.hookCtx('fillText', (target, thisArg, args) => {
7989
const text = args[0];
@@ -151,16 +161,12 @@ class Player extends EventEmitter {
151161
gamepad.connected = value;
152162
}
153163

154-
async spawn(name: string, attempts: number = 0): Promise<void> {
155-
if (!this.#isDead) return;
156-
157-
if (name !== undefined) (document.getElementById('textInput') as HTMLInputElement).value = name;
158-
159-
await input.keyPress(13);
160-
161-
await sleep(250);
164+
async spawn(name: string = this.#username): Promise<void> {
165+
if (!this.#isDead) {
166+
return;
167+
}
162168

163-
await this.spawn(name, attempts + 1);
169+
_window.input.trySpawn(name);
164170
}
165171

166172
async upgrade_stat(id: number, level: number): Promise<void> {

src/core/globals.d.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
interface Input {
2-
mouse: (x: number, y: number) => void;
2+
blur: () => void;
3+
execute: (v: string) => void;
4+
get_convar: (key: string) => null | string;
35
keyDown: (key: string | number) => void;
46
keyUp: (key: string | number) => void;
5-
blur: () => void;
6-
wheel: Function;
7-
prevent_right_click: (value: boolean) => void;
8-
flushInputHooks: Function;
7+
mouse: (x: number, y: number) => void;
98
set_convar: (key: string, value: string) => boolean;
10-
get_convar: (key: string) => null | string;
11-
execute: (v: string) => void;
12-
print_convar_help: () => void;
139
should_prevent_unload: () => boolean;
10+
trySpawn: (username: string) => void;
1411
}
1512

1613
declare var input: Input;

0 commit comments

Comments
 (0)