| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- class InputHandler {
- constructor() {
- this.keys = {
- a: false,
- A: false,
- d: false,
- D: false,
- w: false,
- W: false,
- s: false,
- S: false,
- Shift: false,
- ArrowLeft: false,
- ArrowRight: false,
- ArrowUp: false,
- ArrowDown: false
- };
-
- this.init();
- }
-
- init() {
- window.addEventListener('keydown', (e) => {
- if (this.keys.hasOwnProperty(e.key)) {
- e.preventDefault();
- this.keys[e.key] = true;
- }
- });
-
- window.addEventListener('keyup', (e) => {
- if (this.keys.hasOwnProperty(e.key)) {
- e.preventDefault();
- this.keys[e.key] = false;
- }
- });
- }
-
- isMovingLeft() {
- return this.keys.a || this.keys.A || this.keys.ArrowLeft;
- }
-
- isMovingRight() {
- return this.keys.d || this.keys.D || this.keys.ArrowRight;
- }
-
- isJumping() {
- return this.keys.w || this.keys.W || this.keys.ArrowUp;
- }
-
- isShiftPressed() {
- return this.keys.Shift;
- }
-
- isCrouching() {
- return this.keys.s || this.keys.S || this.keys.ArrowDown;
- }
- }
- const input = new InputHandler();
|