-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
73 lines (58 loc) · 2.24 KB
/
player.js
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
68
69
70
71
72
73
class Player extends Phaser.Physics.Arcade.Sprite{
constructor(scene ,x,y){
super(scene,x,y,"player");
scene.add.existing(this);
scene.physics.add.existing(this);
this.player_speed=600;
this.depth = 5;
this.scene = scene;
this.setInteractive();
this.setCollideWorldBounds(true);
self=this;
this.keyUp = scene.input.keyboard.addKey('W');
this.keyDown = scene.input.keyboard.addKey('S');
this.keyLeft = scene.input.keyboard.addKey('A');
this.keyRight = scene.input.keyboard.addKey('D');
scene.physics.add.collider(this, scene.bullets, function() {
alert("You're dead");
});
scene.input.on("pointermove", function(pointer) {
this.angle = Phaser.Math.RAD_TO_DEG * Phaser.Math.Angle.Between(this.x, this.y, pointer.x, pointer.y);
this.setAngle(this.angle);
}, this);
scene.input.keyboard.on('keydown-F', function() {
// player method shoot
var x = self.x;
var y = self.y;
var angle = self.angle;
self.scene.io.emit('new_bullet', {x: x, y: y, angle: angle});
//var a = new Shot(self.scene, self.x, self.y, self.angle);
});
this.old_x = this.x;
this.old_y = this.y;
this.old_angle = this.angle;
}
update () {
this.setVelocity(0, 0);
if (this.keyUp.isDown) {
this.setVelocityY(this.player_speed * -1);
}else if(this.keyDown.isDown){
this.setVelocityY(this.player_speed);
}
if (this.keyRight.isDown) {
this.setVelocityX(this.player_speed);
}else if(this.keyLeft.isDown){
this.setVelocityX(this.player_speed * -1);
}
var x = this.x;
var y = this.y;
var angle = this.angle;
if (x != this.old_x || y != this.old_y || angle != this.old_angle) {
// send socket to server
this.scene.io.emit('player_moved', {x: x, y: y, angle: angle});
this.old_x = x;
this.old_y = y;
this.old_angle = angle;
}
}
}