-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_character.js
87 lines (76 loc) · 2.73 KB
/
event_character.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
export class EventCharacter {
constructor(scene, x, y, spriteKey, direction) {
this.scene = scene;
this.sprite = scene.add.sprite(x, y, spriteKey);
this.sprite.setOrigin(0.5);
this.sprite.setScale(0.2); // Tiere deutlich kleiner machen
this.direction = direction; // "LEFT_TO_RIGHT" or "RIGHT_TO_LEFT"
this.speed = Phaser.Math.Between(300, 500); // Deutlich schnellere Geschwindigkeit
// Setze die korrekte Richtung des Sprites
if (this.direction === "LEFT_TO_RIGHT") {
this.sprite.setFlipX(false);
} else {
this.sprite.setFlipX(true);
}
}
updatePosition(delta) {
const speed = this.speed * (delta / 1000);
if (this.direction === "LEFT_TO_RIGHT") {
this.sprite.x += speed;
} else {
this.sprite.x -= speed;
}
}
isOutOfBounds() {
return (
this.sprite.x < -50 ||
this.sprite.x > this.scene.scale.width + 50
);
}
destroy() {
this.sprite.destroy();
}
}
export class EventCharacterManager {
constructor(scene) {
this.scene = scene;
this.activeEvents = [];
this.eventSprites = ["dog1", "dog2", "dog3", "pigeon", "sparrow"];
}
preload() {
// Lade alle Tier-Sprites vor
this.scene.load.image("dog1", "./img/pets/dog1.png");
this.scene.load.image("dog2", "./img/pets/dog2.png");
this.scene.load.image("dog3", "./img/pets/dog3.png");
this.scene.load.image("sparrow", "./img/pets/sparrow.png");
this.scene.load.image("pigeon", "./img/pets/pigeon.png");
}
spawnRandomEvent() {
const spriteKey = Phaser.Utils.Array.GetRandom(this.eventSprites);
const direction = Phaser.Math.Between(0, 1) === 0 ? "LEFT_TO_RIGHT" : "RIGHT_TO_LEFT";
const y = this.scene.scale.height - 50;
const x = direction === "LEFT_TO_RIGHT" ? -50 : this.scene.scale.width + 50;
const event = new EventCharacter(this.scene, x, y, spriteKey, direction);
this.activeEvents.push(event);
}
update(delta) {
this.activeEvents.forEach((event, index) => {
event.updatePosition(delta);
// Entferne Events, die den Bildschirm verlassen haben
if (event.isOutOfBounds()) {
event.destroy();
this.activeEvents.splice(index, 1);
}
});
}
startRandomEventSpawner() {
this.scene.time.addEvent({
delay: Phaser.Math.Between(3000, 7000), // Kürzere Zeitspanne für Events
callback: () => {
this.spawnRandomEvent();
},
callbackScope: this,
loop: true,
});
}
}