forked from delaford/game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelaford.vue
329 lines (289 loc) · 7.45 KB
/
Delaford.vue
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<template>
<div id="app">
<!-- Login screen -->
<div
v-show="!loaded || game.exit"
class="wrapper login__screen">
<AudioMainMenu />
<div
v-if="screen === 'server-down'"
class="bg server__down">
The game server is down. Please check the website for more information.
</div>
<div
v-else
class="bg">
<div
v-if="screen === 'register'"
class="register">
To register an account, please visit <a href="https://delaford.com/register">this page</a> to get started and then come back.
</div>
<div
v-if="screen === 'login'"
class="login">
<img
class="logo"
src="./assets/logo.png"
alt="Logo">
<Login/>
</div>
<div v-if="screen === 'main'">
<img
class="logo"
src="./assets/logo.png"
alt="Logo">
<div class="button_group">
<button
class="login"
@click="screen = 'login'">
Login
</button>
<button
class="register"
@click="screen = 'register'">
Register
</button>
</div>
</div>
</div>
</div>
<!-- Game wrapper -->
<div
v-show="loaded && game.map"
class="wrapper"
@click.right="nothing">
<div class="left">
<!-- Main canvas -->
<GameCanvas :game="game" />
<!-- Chatbox -->
<Chatbox :game="game" />
</div>
<div
class="right"
@click="sidebarClicked">
<!-- Player overview -->
<Info :game="game" />
<!-- Slots (Stats, Wear, Inventory, etc.) -->
<Slots
ref="sidebarSlots"
:game="game" />
</div>
<context-menu :game="game"/>
</div>
<!-- End Game wrapper -->
</div>
</template>
<script>
// Vue components
import config from 'root/config';
import GameCanvas from './components/GameCanvas.vue';
import Chatbox from './components/Chatbox.vue';
import Slots from './components/Slots.vue';
import Info from './components/Info.vue';
// Sub Vue components
import ContextMenu from './components/sub/ContextMenu.vue';
import AudioMainMenu from './components/sub/AudioMainMenu.vue';
import Login from './components/ui/Login.vue';
// Core assets
import Client from './core/client';
import Engine from './core/engine';
import bus from './core/utilities/bus';
import Event from './core/player/events';
export default {
name: 'Delaford',
components: {
GameCanvas, Chatbox, Info, Slots, ContextMenu, Login, AudioMainMenu,
},
data() {
return {
config,
loaded: false,
game: { exit: true },
screen: 'login',
};
},
/**
* WebSocket event handler
*/
created() {
const context = this;
// Reload window upon Socket close
window.ws.onclose = () => setTimeout(() => window.location.reload(), 1000);
window.ws.onmessage = (evt) => {
const data = JSON.parse(evt.data);
const eventName = data.event;
const canRefresh = ['world', 'player', 'item'].some(e => eventName.split(':').includes(e));
// Did the game canvas change that we need
// to refresh the first context action?
if (data && eventName && canRefresh) {
bus.$emit('canvas:reset-context-menu');
}
if (eventName !== undefined) {
if (!Event[eventName]) {
bus.$emit(eventName, data);
} else {
Event[eventName](data, context);
}
} else {
console.log(data);
}
};
// On server connection error,
// show the appropriate screen
window.ws.onerror = () => {
this.screen = 'server-down';
};
bus.$on('show-sidebar', this.showSidebar);
// On logout, let's do a few things...
bus.$on('player:logout', this.logout);
bus.$on('go:main', this.cancelLogin);
},
methods: {
/**
* Logout player
*/
logout() {
this.screen = 'login';
this.game = { exit: true };
this.$refs.sidebarSlots.selected = 1;
},
/**
* Cancel login
*/
cancelLogin() {
this.screen = 'main';
},
/**
* Player movement, do something
*/
playerMovement(data) {
if (this.game.player.uuid === data.uuid) {
this.game.map.player = data;
this.game.player = data;
if (data.inventory.slots) {
this.game.map.player.inventory = data.inventory.slots;
this.game.player.inventory = data.inventory.slots;
}
} else {
const playerIndex = this.game.map.players.findIndex(p => p.uuid === data.uuid);
this.game.map.players[playerIndex] = data;
}
},
/**
* On NPC movement, update NPCs
*/
npcMovement(data) {
if (this.game.npcs) {
this.game.map.npcs = data;
this.game.npcs = data;
}
},
/**
* Start the whole game
*/
async startGame(data) {
// Stop the main menu music
bus.$emit('music:stop');
// Start the Client
this.game = new Client(data);
await this.game.buildMap();
// Start game engine
const engine = new Engine(this.game);
engine.start();
// Focus on game.
setTimeout(() => {
window.focusOnGame();
}, 250);
// Clear login procedure
bus.$emit('login:done');
// Show the game canvas
this.loaded = true;
// Set screen to 'game' for chatbox reset
this.screen = 'game';
},
/**
* A click-handler event that does nothing, really.
*
* @param {MouseEvent} event The mouse event
*/
nothing(event) {
// Make right-click system for
// rest of the game view.
event.preventDefault();
},
sidebarClicked() {
bus.$emit('contextmenu:close');
},
showSidebar(selectedSlot) {
this.$refs.sidebarSlots.selected = selectedSlot;
},
},
};
</script>
<style lang="scss" scoped>
#app {
font-family: "Roboto Slab", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
flex-direction: column;
img.logo {
margin-bottom: 1em;
}
div.login__screen {
width: 692px;
position: relative;
border: 5px solid #ababab;
box-sizing: border-box;
display: flex;
height: 505px;
align-content: center;
justify-content: center;
background-image: url("./assets/bg-screen.png");
div.server__down {
font-size: 0.85em;
}
div.bg {
background-color: rgba(0, 0, 0, 0.5);
padding: 1em 0;
border-radius: 5px;
display: inline-flex;
justify-content: space-around;
}
div.button_group {
width: 100%;
display: inline-flex;
justify-content: space-around;
button {
background: #dedede;
border: 2px solid darken(#dedede, 10%);
font-size: 1.5em;
cursor: pointer;
}
}
}
div.wrapper {
background-color: #ababab;
padding: 5px;
display: grid;
grid-template-columns: 512px auto;
border-radius: 5px;
div.right {
display: flex;
flex-direction: column;
position: relative;
justify-content: flex-end;
div.content {
background-color: #c7c7c7;
height: 100px;
font-size: 12px;
}
}
}
}
</style>