File size: 1,860 Bytes
670a607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Actor } from "./actor";
export class Player extends Actor {
  private keyW: Phaser.Input.Keyboard.Key;
  private keyA: Phaser.Input.Keyboard.Key;
  private keyS: Phaser.Input.Keyboard.Key;
  private keyD: Phaser.Input.Keyboard.Key;

  constructor(scene: Phaser.Scene, x: number, y: number) {
    super(scene, x, y, "Brendan");

    this.setName("Brendan");

    // Keys
    this.initKeyboard();

    // PHYSICS
    this.getBody().setSize(14, 16);
    this.getBody().setOffset(0, 5);

    // ANIMATIONS
    this.initAnimations();
  }

  update(): void {
    this.getBody().setVelocity(0);

    var pressed_flag = false;
    if (this.keyW.enabled && this.keyW?.isDown) {
      this.getBody().setVelocityY(-110);
      this.anims.play(this.name + "-walk-up", true);
      pressed_flag = true;
    }

    if (this.keyA.enabled && this.keyA?.isDown) {
      // this.getBody().setOffset(48, 15);
      this.getBody().setVelocityX(-110);
      this.anims.play(this.name + "-walk-left", true);
      pressed_flag = true;
    }

    if (this.keyS.enabled && this.keyS?.isDown) {
      this.getBody().setVelocityY(110);
      this.anims.play(this.name + "-walk-down", true);
      pressed_flag = true;
    }

    if (this.keyD.enabled && this.keyD?.isDown) {
      this.getBody().setVelocityX(110);
      this.anims.play(this.name + "-walk-right", true);
      // this.getBody().setOffset(15, 15);
      pressed_flag = true;
    }

    if (!pressed_flag && this.anims.isPlaying) {
      this.anims.setCurrentFrame(this.anims.currentAnim!.frames[0]);
    }
    this.depth = this.y + 0.5 * this.height;
  }

  initKeyboard(): void {
    this.keyW = this.scene.input.keyboard!.addKey("W");
    this.keyA = this.scene.input.keyboard!.addKey("A");
    this.keyS = this.scene.input.keyboard!.addKey("S");
    this.keyD = this.scene.input.keyboard!.addKey("D");
  }
}