-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterPlugin.js
267 lines (221 loc) · 8.7 KB
/
CharacterPlugin.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { Emotions } from '../../Constants.js';
import { CustomerQueue } from './CustomerQueue.js';
import { HungrySentences, VerifySentences } from './Sentences.js';
export class CharacterPlugin {
constructor(spriteKey, character) {
this.character = character; // Der Charakter wird von außen übergeben
this.queue = CustomerQueue.getInstance();
this.spriteKey = spriteKey
this.spriteGraphics = null;
this.bubbleGraphics = null;
this.bubbleTextGraphics = null;
this.statementText = ''
this.position = null;
this.targetX = null;
this.middleware = {}; // Hooks für Phasen
// this.addMiddleware('onRecognizeHunger', 'before', async (character) => {
// console.log("+++++++++++++++++++++++++++++++")
// character.setThinking(HungrySentences[Math.floor(Math.random()*HungrySentences.length)].emoji)
// });
this.addMiddleware('onEnjoyAndEvaluate', 'before', async (character) => {
console.log("+++++++++++++++++++++++++++++++")
character.setThinking(VerifySentences[Math.floor(Math.random()*VerifySentences.length)].emoji)
});
this.phase = this.startPhase('onEntering');
this.phaseStartTime = null;
}
// Methode zum Hinzufügen von Hooks für Phasen
addMiddleware(phase, timing, middlewareFunc) {
const key = `${phase}_${timing}`; // Beispiel: "onPaying_before"
if (!this.middleware[key]) {
this.middleware[key] = [];
}
this.middleware[key].push(middlewareFunc);
}
// Methode zum Ausführen von Middleware
async executeMiddleware(phase, timing) {
const key = `${phase}_${timing}`; // Beispiel: "onPaying_before"
if (this.middleware[key]) {
for (const middlewareFunc of this.middleware[key]) {
await middlewareFunc(this); // Middleware erhält den Charakter
}
}
}
// Startet eine Phase und integriert Hooks
async startPhase(phase) {
console.log(`Starting phase: ${phase}`);
await this.executeMiddleware(phase, 'before'); // Middleware vor der Phase
if (this[phase]) {
this.phaseStartTime = Date.now();
this.phase = phase
await this[phase](); // Hauptlogik der Phase
}
await this.executeMiddleware(phase, 'after'); // Middleware nach der Phase
}
// Überprüft, wie lange der Charakter bereits in der Phase verweilt
hasPhaseTimeElapsed(duration) {
const elapsed = Date.now() - this.phaseStartTime;
return elapsed >= duration;
}
hasReachedTargetX() {
const tolerance = 100;
return Math.abs(this.targetX - this.position.x) <= tolerance;
}
setTargetX(targetX) {
this.targetX = targetX;
}
moveToTargetX(speed) {
if (this.position.x > this.targetX) speed *= -1
this.position.x += speed;
if (this.spriteGraphics) this.spriteGraphics.setFlipX(this.position.x > this.targetX)
}
setThinking(text) {
if (text) {
this.statementText = text;
}
}
setEmotion(emotion) {
if (emotion) {
this.character.emotion = emotion;
}
}
setWishes(items) {
if (items) {
this.character.wishList = items;
}
}
showWishes() {
this.statementText = this.character.wishList.map(item => item.emoji).join(' ');
}
showPurchasedItems() {
this.statementText = this.character.purchasedItems.map(item => item.emoji).join(' ');
}
update() {
if (!this.spriteGraphics) return
if (this.phase && this[this.phase]) {
console.log("ff", this.phase)
this[this.phase]();
}
}
render(scene, delta) {
if (!this.spriteGraphics) {
this.spriteGraphics = scene.add.sprite(this.position.x, this.position.y, this.spriteKey);
}
if (this.bubbleTextGraphics && this.statementText) {
this.bubbleTextGraphics.setText(this.statementText);
}
if (!this.bubbleGraphics) {
this.bubbleGraphics = scene.add.image(this.spriteGraphics.x + 20, this.spriteGraphics.y - 250, 'bubble').setOrigin(0.5).setScale(0.5);
this.bubbleTextGraphics = scene.add.text(this.bubbleGraphics.x, this.bubbleGraphics.y - 40, '', {
fontSize: '40px',
fill: '#000',
align: 'center',
padding: {
left: 0,
right: 0,
top: 6,
bottom: 6,
},
}).setOrigin(0.5);
}
this.bubbleGraphics.visible = this.statementText.length > 0
this.bubbleTextGraphics.visible = this.statementText.length > 0
this.spriteGraphics.setPosition(this.position.x, this.position.y);
this.bubbleGraphics.setPosition(this.position.x + 50, this.position.y - 240);
this.bubbleTextGraphics.setPosition(this.bubbleGraphics.x + 10, this.bubbleGraphics.y - 35);
// Falls der Kunde den Bildschirm verlässt, wird die Position nicht weiter aktualisiert
if (this.position.x > 3000) {
console.log(`${this.character.firstName} hat den Bildschirm verlassen.`);
this.destroy();
}
}
onEntering = function () {
console.log(`${this.character.firstName}: "Ich laufe hier lang."`);
// Stoppt vor dem Imbisswagen
if (!this.hasReachedTargetX()) {
this.moveToTargetX(3)
this.setEmotion(Emotions.HUNGRY)
} else {
this.startPhase('onRecognizeHunger');
}
};
// Phase: Hunger erkennen
onRecognizeHunger = function () {
console.log(`${this.character.firstName}: „Ich habe Hunger, was will ich essen?“`);
if (this.hasPhaseTimeElapsed(200)) {
this.startPhase('onCheckOptions');
}
};
// Phase: Optionen prüfen
onCheckOptions = function () {
console.log(`${this.character.firstName}: „Gibt es hier etwas, das mir schmeckt?“`);
if (this.hasPhaseTimeElapsed(300)) {
this.startPhase('onEvaluateQueue');
}
};
// Phase: Schlange bewerten
onEvaluateQueue = function () {
console.log(`${this.character.firstName}: „Wie lang ist die Schlange? Habe ich die Zeit und Geduld?“`);
if (this.hasPhaseTimeElapsed(250)) {
this.startPhase('onEvaluatePricePerformance');
}
};
// Phase: Preis-Leistungs-Abgleich
onEvaluatePricePerformance = function () {
console.log(`${this.character.firstName}: „Ist es das wert? Was kostet es?“`);
if (this.hasPhaseTimeElapsed(250)) {
this.startPhase('onMakeDecision');
}
};
// Phase: Entscheidung treffen
onMakeDecision = function () {
console.log(`${this.character.firstName}: „Das nehme ich / Ich warte hier.“`);
if (this.hasPhaseTimeElapsed(180)) {
if (!this.queue.contains(this)) {
this.queue.enqueue(this);
}
this.startPhase('onWaitingInQueue');
}
};
onWaitingInQueue = function () {
console.log(`${this.character.firstName}: „Warten! Ich stelle mich hinten an.“`);
// Stoppt vor dem Imbisswagen
if (!this.hasReachedTargetX()) {
this.moveToTargetX(3)
this.setEmotion(Emotions.HUNGRY)
} else {
if (this.queue.isFirst(this)) {
this.startPhase('onOrderAndPay'); // Wechselt in die nächste Phase
}
}
};
// Phase: Bestellen und zahlen
onOrderAndPay = function () {
console.log(`${this.character.firstName}: „Wie läuft der Kauf ab?“`);
this.spriteGraphics.setFlipX(false);
if (this.hasPhaseTimeElapsed(600)) {
this.startPhase('onEnjoyAndEvaluate');
}
};
// Phase: Genießen und bewerten
onEnjoyAndEvaluate = function () {
console.log(`${this.character.firstName}: „Hat es sich gelohnt?“`);
if (this.hasPhaseTimeElapsed(500)) {
console.log(`${this.character.firstName} hat alle Phasen durchlaufen.`);
if (this.queue.contains(this)) {
this.queue.dequeue(this);
}
this.startPhase('onLeaving');
}
};
onLeaving = function () {
console.log(`${this.character.firstName}: "Ich gehe jetzt nach Hause."`);
this.setEmotion(Emotions.HAPPY)
this.position.x += 4;
};
destroy() {
this.spriteGraphics.destroy(); // Entfernt den Sprite, falls nötig
this.bubbleGraphics.destroy();
this.bubbleTextGraphics.destroy();
}
}