-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainScene.js
156 lines (123 loc) · 5.38 KB
/
MainScene.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
import { Character } from './Character.js';
// import { setupDebug } from './debug.js';
import { World } from './World.js';
import { EventCharacterManager } from './event_character.js'; // Import der EventCharacterManager-Klasse
import { CustomerQueue } from './CustomerQueue.js';
export class MainScene extends Phaser.Scene {
constructor() {
super({ key: 'MainScene' });
this.world = World.getInstance();
this.customerSchedule = [];
this.customers = [];
this.dateText = null;
this.clockText = null;
this.wealthText = null;
this.customerPlugins = [];
this.dayCycleOverlay = null; // Overlay für Tag-Nacht-Zyklus
this.eventManager = null; // EventCharacterManager-Instanz
}
preload() {
this.loadAsyncPlugins();
this.load.image('customer1', './img/characters/mensch1.png');
this.load.image('customer2', './img/characters/mensch2.png');
this.load.image('customer3', './img/characters/mensch3.png');
this.load.image('customer4', './img/characters/mensch4.png');
this.load.image('bubble', './img/characters/bubble.png');
this.load.image(this.world.getFoodStall().getImage(), this.world.getFoodStall().getImage())
this.load.image('imbiss', this.world.getLocation().getImage());
this.eventManager = new EventCharacterManager(this); // Initialisiere den EventCharacterManager
this.eventManager.preload();
this.queue = CustomerQueue.getInstance(this);
}
async loadAsyncPlugins() {
const pluginPaths = [
'./plugins/customers/Plugin1.js',
'./plugins/customers/Plugin2.js',
'./plugins/customers/Plugin3.js',
'./plugins/customers/Plugin4.js'
];
for (const pathOn of pluginPaths) {
const module = await import(pathOn);
this.customerPlugins.push(module);
}
}
create() {
this.setupScene();
this.setupTopBar();
// setupDebug(this);
// Tag-Nacht-Overlay hinzufügen
this.dayCycleOverlay = this.add.rectangle(0, 0, this.scale.width, this.scale.height, 0x000000)
.setOrigin(0)
.setDepth(5) // Über Hintergrund, aber unter anderen Objekten
.setAlpha(this.world.getCurrentAlpha()); // Initialer Helligkeitswert
this.world.startClock();
// Listener für Mitternacht hinzufügen
this.world.events.subscribe('midnight', () => {
console.log('Mitternacht erreicht, Szene wechseln!');
this.world.stopClock();
this.world.events.emit('stop_game');
});
this.customerSchedule = this.world.getLocation().generateCustomerSchedule();
this.eventManager.startRandomEventSpawner(); // Starte die zufällige Generierung von Events
}
update(time, delta) {
this.updateClockText();
this.adjustDayCycle(); // Helligkeit basierend auf der Tageszeit anpassen
this.customers.forEach((customerOn) => {
customerOn.update();
customerOn.render(this, delta);
});
this.eventManager.update(delta); // Aktualisiere die EventCharacter
}
setupScene() {
this.add.image(0, 0, 'imbiss')
.setOrigin(0)
.setDisplaySize(this.scale.width, this.scale.height);
this.add.image(this.scale.width / 2, this.scale.height / 2, this.world.getFoodStall().getImage())
.setOrigin(0.5)
.setScale(0.5);
}
setupTopBar() {
const barHeight = 43;
this.add.rectangle(0, 0, this.scale.width, barHeight, 0xffffff)
.setOrigin(0)
.setDepth(10);
this.dateText = this.add.text(20, 10, `📅 ${this.world.getFullDateAndTime()}`, {
fontSize: '26px',
fill: '#000',
}).setDepth(11);
this.wealthText = this.add.text(this.scale.width - 20, 10, `💰 ${this.world.getWealth().toFixed(2)}€`, {
fontSize: '26px',
fill: '#000',
}).setOrigin(1, 0).setDepth(11);
}
getFormattedDateAndTime() {
const dayOfWeek = this.world.getDayOfWeek();
const formattedDate = this.world.getFormattedDate();
const formattedTime = this.world.getFormattedTime();
return `${dayOfWeek}, ${formattedDate} - ${formattedTime}`;
}
updateClockText() {
this.dateText.setText(`📅 ${this.getFormattedDateAndTime()}`);
this.checkSpawnProbability();
}
adjustDayCycle() {
const currentAlpha = this.world.getCurrentAlpha();
this.dayCycleOverlay.setAlpha(currentAlpha);
}
checkSpawnProbability() {
if (this.customerSchedule.length > 0 && this.customerSchedule[0].time === this.world.getTimeInMinutes()) {
const nextCustomer = this.customerSchedule.shift();
this.spawnCustomer(nextCustomer);
}
}
spawnCustomer() {
const customerIndex = Phaser.Math.Between(1, 4);
const spriteKey = `customer${customerIndex}`;
const pluginIndex = Phaser.Math.Between(0, this.customerPlugins.length - 1);
const character = this.customerPlugins[pluginIndex].default(spriteKey, new Character('Alice', 'Smith', 25));
character.position = { x: -70, y: this.scale.height - 150 };
character.setTargetX(this.queue.calcLastPositionX());
this.customers.push(character);
}
}