mirror of
https://github.com/MartinKral/Slash-The-Hordes
synced 2026-02-14 12:22:42 +00:00
added temp enemy mover
This commit is contained in:
60
assets/Scripts/Game/Input/KeyboardInput.ts
Normal file
60
assets/Scripts/Game/Input/KeyboardInput.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { EventKeyboard, Input, input, KeyCode, Vec2 } from "cc";
|
||||
import { IInput } from "./IInput";
|
||||
|
||||
export class KeyboardInput implements IInput {
|
||||
private xAxis = 0;
|
||||
private yAxis = 0;
|
||||
|
||||
private up: KeyCode;
|
||||
private down: KeyCode;
|
||||
private left: KeyCode;
|
||||
private right: KeyCode;
|
||||
|
||||
public constructor(up: KeyCode, down: KeyCode, left: KeyCode, right: KeyCode) {
|
||||
this.up = up;
|
||||
this.down = down;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
|
||||
input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
|
||||
input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
|
||||
}
|
||||
|
||||
public getAxis(): Vec2 {
|
||||
return new Vec2(this.xAxis, this.yAxis).normalize();
|
||||
}
|
||||
|
||||
private onKeyDown(event: EventKeyboard): void {
|
||||
switch (event.keyCode) {
|
||||
case this.up:
|
||||
this.yAxis += 1;
|
||||
break;
|
||||
case this.down:
|
||||
this.yAxis += -1;
|
||||
break;
|
||||
case this.left:
|
||||
this.xAxis += -1;
|
||||
break;
|
||||
case this.right:
|
||||
this.xAxis += 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private onKeyUp(event: EventKeyboard): void {
|
||||
switch (event.keyCode) {
|
||||
case this.up:
|
||||
this.yAxis -= 1;
|
||||
break;
|
||||
case this.down:
|
||||
this.yAxis -= -1;
|
||||
break;
|
||||
case this.left:
|
||||
this.xAxis -= -1;
|
||||
break;
|
||||
case this.right:
|
||||
this.xAxis -= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user