-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.ts
171 lines (139 loc) · 4.66 KB
/
Game.ts
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
import {
app,
dialog,
BrowserWindow,
ipcMain,
IpcMainInvokeEvent,
} from 'electron';
import ElectronClient from './client/ElectronClient';
import Player from '@civ-clone/core-player/Player';
import SimpleAIClient from '@civ-clone/simple-ai-client/SimpleAIClient';
import TransferObject from './client/TransferObject';
import { instance as clientRegistryInstance } from '@civ-clone/core-client/ClientRegistry';
import { instance as engine } from '@civ-clone/core-engine/Engine';
import { instance as playerRegistryInstance } from '@civ-clone/core-player/PlayerRegistry';
export interface IGame {
start(): void;
}
export class Game implements IGame {
#ready: Promise<void>;
#window: BrowserWindow | undefined;
constructor() {
this.#ready = app.whenReady().then((): void => this.createWindow());
app.on('window-all-closed', (): void => app.quit());
ipcMain.handle('start', () => {
this.bindEvents();
this.configure();
this.start();
});
ipcMain.handle('setOption', (event, { name, value }) => {
this.sendData('notification', `setting ${name} to ${value}`);
engine.setOption(name, value);
});
ipcMain.handle('quit', () => app.quit());
}
private createWindow(): void {
this.#window = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
contextIsolation: true,
// enableRemoteModule: false,
preload: `${__dirname}/view/js/preload.js`,
},
});
this.#window.loadURL(`file://${__dirname}/view/html/index.html`);
this.#window.on('close', (event) => {
const response = dialog.showMessageBoxSync(this.#window!, {
type: 'question',
buttons: ['Yes', 'No'],
title: 'Confirm',
message: 'Are you sure you want to quit?',
});
if (response === 1) {
event.preventDefault();
}
});
// this.#window.webContents.openDevTools();
}
private bindEvents(): void {
this.sendData('notification', `binding events`);
engine.on('engine:initialise', (): void =>
this.sendData('notification', `initialising...`)
);
engine.on('engine:plugins:load:success', (packageName: string): void =>
this.sendData('notification', `loaded plugin: ${packageName}`)
);
engine.on('engine:plugins-loaded', (): void =>
this.sendData('notification', `plugins loaded`)
);
engine.on('engine:start', (): void =>
this.sendData('notification', `starting...`)
);
engine.on('world:generate-start-tiles', (): void =>
this.sendData('notification', `generating start tiles...`)
);
engine.on('world:built', (): void =>
this.sendData('notification', `world built`)
);
engine.on('game:start', (): void =>
this.sendData('notification', `game start`)
);
engine.on('turn:start', (turn): void =>
this.sendData('notification', `turn start ${turn}`)
);
engine.on('player:turn-start', (player): void =>
this.sendData(
'notification',
`player turn-start: ${player.civilization().constructor.name}`
)
);
}
private configure(): void {
// engine.setOption('debug', true);
engine.setOption('height', 60);
engine.setOption('width', 80);
}
private receiveData(
channel: string,
handler: (...args: any[]) => void
): void {
ipcMain.handle(channel, (event: IpcMainInvokeEvent, ...args: any[]): void =>
handler(...args)
);
}
private sendData(channel: string, payload: any): void {
(this.#window as BrowserWindow).webContents.send(channel, payload);
}
start(): void {
this.#ready.then((): void => {
engine.on('engine:start', (): void => {
new Array(parseInt(engine.option('players'), 10))
.fill(0)
.forEach((value: 0, i: number) => {
const player = new Player(),
// TODO: This is pretty basic.
client =
i === 0
? new ElectronClient(
player,
(channel: string, payload: TransferObject) =>
this.sendData(channel, payload),
(
channel: string,
handler: (...args: any[]) => void
): void => this.receiveData(channel, handler)
)
: new SimpleAIClient(player);
playerRegistryInstance.register(player);
clientRegistryInstance.register(client);
this.sendData('notification', `generating world...`);
});
});
engine.setOption('height', 50);
engine.setOption('width', 80);
engine.start();
});
}
}
export default Game;