-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.js
110 lines (96 loc) · 3.04 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/** @type {import ("../typings/phaser")}*/
class Player {
constructor(scene, x, y, spriteName, name) {
this.name = name;
this.scene = scene;
this.sprite = scene.physics.add.sprite(x, y, spriteName);
this.sprite.setSize(20, 20);
this.sprite.setDepth(2);
this.sprite.setCollideWorldBounds(true);
this.invItems = scene.physics.add.group();
this.speed = gameSettings.playerSpeed;
this.itemMax = 20;
this.invCollider;
this.invItems.removeCallback = this.itemRemoveCallback.bind(this);
}
plusInvItem(playerSprite, item) {
this.scene.sound.add('sound_Item').play({
volume : 0.35,
});
if (this.invItems.getLength() < this.itemMax) {
this.scene.items.remove(item);
item.name = "" + this.invItems.getLength();
item.setSize(10, 10);
//item.removeAllListeners();
this.invItems.add(item);
}
}
invManager() {
if (this.invCollider.active == true) {
var preX = this.sprite.x;
var preY = this.sprite.y;
for (var n = 1; n <= this.invItems.getLength(); n++) {
var temp = this.invItems.getLastNth(n, true);
temp.setVelocity((preX - temp.x) * 20, (preY - temp.y) * 20);
preX = temp.x;
preY = temp.y;
}
}
}
moveManager(up, down, left, right) {
this.sprite.setVelocity(0);
if (left.isDown) {
this.sprite.setVelocityX(-this.speed);
this.sprite.play(this.name + "_left_anim", true);
} else if (right.isDown) {
this.sprite.setVelocityX(this.speed);
this.sprite.play(this.name + "_right_anim", true);
}
if (down.isDown) {
this.sprite.setVelocityY(this.speed);
this.sprite.play(this.name + "_down_anim", true);
} else if (up.isDown) {
this.sprite.setVelocityY(-this.speed);
this.sprite.play(this.name + "_up_anim", true);
}
}
plusSpeedUp(playerSprite, speedup) {
this.scene.sound.add('sound_speedUp').play({
volume : 0.35,
});
this.speed += 20;
speedup.destroy();
}
itemPopUp(playerSprite, item) { // p1 items * p2 sprite => call p1
this.invCollider.active = false;
// console.log(this.invCollider)
for (var k = 1; k <= this.invItems.getLength(); k++) {
var pItem = this.invItems.getFirstNth(k, true);
if (item == pItem) {
// pItem.setRandomPosition(0, 0, game.config.width, game.config.height);
this.invItems.remove(pItem);
if (!this.invItems.contains(item)) {
break;
}
}
}
this.invCollider.active = true;
}
itemRemoveCallback(item) {
var toX = Math.random() * game.config.width;
var toY = Math.random() * game.config.height;
var delX = toX - item.x;
var delY = toY - item.y;
var sec = 1;
item.setVelocity(delX / sec, delY / sec);
item.setAngularVelocity(1000);
this.scene.time.delayedCall(1000 * sec, this.setTo.bind(this), [item, toX, toY]);
}
setTo(item, toX, toY) {
item.setVelocity(0);
item.setRotation(0);
item.setPosition(toX, toY);
this.scene.items.add(item);
item.setCollideWorldBounds(true);
}
}