Skip to content

Commit

Permalink
Feature/fix player spawn (#61)
Browse files Browse the repository at this point in the history
* update window.input interface

* update spawn method to use window.input.trySpawn()

* fix bug
  • Loading branch information
Cazka authored Dec 17, 2022
1 parent a02472d commit 5b68c69
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/apis/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Player extends EventEmitter {
#mouseLock = false;
#mouseCanvasPos = new Vector(0, 0);
#mousePos = new Vector(0, 0);

#username = _window.localStorage.name;
#gamemode = _window.localStorage.gamemode;
#level = 1;
#tank = 'Tank';
Expand Down Expand Up @@ -74,6 +76,14 @@ class Player extends EventEmitter {
},
});

// username
_window.input.trySpawn = new Proxy(_window.input.trySpawn, {
apply: (target, thisArg, args) => {
this.#username = args[0];
return Reflect.apply(target, thisArg, args);
},
});

// tank and level event listener
CanvasKit.hookCtx('fillText', (target, thisArg, args) => {
const text = args[0];
Expand Down Expand Up @@ -151,16 +161,12 @@ class Player extends EventEmitter {
gamepad.connected = value;
}

async spawn(name: string, attempts: number = 0): Promise<void> {
if (!this.#isDead) return;

if (name !== undefined) (document.getElementById('textInput') as HTMLInputElement).value = name;

await input.keyPress(13);

await sleep(250);
async spawn(name: string = this.#username): Promise<void> {
if (!this.#isDead) {
return;
}

await this.spawn(name, attempts + 1);
_window.input.trySpawn(name);
}

async upgrade_stat(id: number, level: number): Promise<void> {
Expand Down
13 changes: 5 additions & 8 deletions src/core/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
interface Input {
mouse: (x: number, y: number) => void;
blur: () => void;
execute: (v: string) => void;
get_convar: (key: string) => null | string;
keyDown: (key: string | number) => void;
keyUp: (key: string | number) => void;
blur: () => void;
wheel: Function;
prevent_right_click: (value: boolean) => void;
flushInputHooks: Function;
mouse: (x: number, y: number) => void;
set_convar: (key: string, value: string) => boolean;
get_convar: (key: string) => null | string;
execute: (v: string) => void;
print_convar_help: () => void;
should_prevent_unload: () => boolean;
trySpawn: (username: string) => void;
}

declare var input: Input;
Expand Down

0 comments on commit 5b68c69

Please sign in to comment.