-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
232 lines (190 loc) · 5.82 KB
/
client.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
const CAMERA_HEIGHT = 600
const ZOOM_HEIGHT_MAX = 0 // The smaller the value the 'higher' you can see..
const ZOOM_HEIGHT_MIN = 500 // The larger the value the 'closer' you can see..
const CUTSCENE_STARTING_HEIGHT = 100
const game = new Game()
let tree
let rock
function preload () {
game.font = loadFont('./fonts/SourceSansPro-Black.otf')
tree = loadModel('./assets/tree.obj')
rock = loadModel('./assets/rock.obj')
}
function windowResized () {
resizeCanvas(windowWidth, windowHeight)
}
function setup () {
pixelDensity(2.0) // Default is 2.0
createCanvas(windowWidth, windowHeight, WEBGL)
textFont(game.font)
}
function draw () {
setupPlayer()
if (game.playing) {
background(100, 150, 100)
handleCamera()
drawPlayer()
drawPlayers()
drawReference()
if (keyIsDown(32) || mouseIsPressed) {
game.player.attacking = true
}
}
}
function keyPressed () {
if (!game.playing) return
if (keyCode === ENTER) {
if (!Chat.isChatFocused() && !Chat.isInputEmpty()) {
game.socket.emit('text', Chat.getInputText())
Chat.resetInput()
}
Chat.toggle()
}
}
function setupPlayer () {
if (game.creatingPlayer) {
game.player = new Player({
x: 0,
y: 0,
angle: 0,
size: 25,
health: 100,
name: game.playerName,
client: true
})
const url = getURL() // This way we can dynamically switch between localhost and external ips.
game.socket = io.connect(url, { // Make connection
reconnect: false,
autoconnect: false
})
game.socket.emit('new_player', {
x: game.player.x,
y: game.player.y,
name: game.player.name
})
game.socket.on('connect', function () {
console.log('Connected: ' + game.socket.connected)
})
listener()
game.creatingPlayer = false
game.playing = true
// fullscreen(true) // Set fullscreen on play.
}
}
function drawPlayer () {
game.player.draw()
game.player.handleMovement()
game.player.handleAttack()
}
function drawPlayers () {
const players = Object.values(game.players)
for (const player of players) {
player.draw()
}
}
function handleCamera () {
// Lerp zoom changed by mouse wheel.
game.currentZoom = lerp(game.currentZoom, game.zoom, 0.02)
// Lerp scene transition for a smooth effect.
game.cutSceneDropValue = lerp(game.cutSceneDropValue, CUTSCENE_STARTING_HEIGHT, 0.03)
const z = CAMERA_HEIGHT - game.currentZoom + CUTSCENE_STARTING_HEIGHT - game.cutSceneDropValue
camera(game.player.x, game.player.y, z, game.player.x, game.player.y, 0, 0, 1, 0)
}
function drawReference () {
push()
translate(0, -450, 0)
fill(50)
strokeWeight(1)
scale(50) // Scale offsets the rock for some reason?
model(rock)
pop()
push()
stroke(0)
fill(55, 120, 55)
scale(50)
stroke(0, 50, 0)
strokeWeight(3)
const spacing = 6
const amount = 5
rotateX(HALF_PI)
for (let x = -amount / 2; x < amount; x++) {
for (let z = -amount / 2; z < amount; z++) {
translate(x * spacing, 0, z * spacing)
fill(55, 120 - (x * 10), 55)
stroke(0, 50 - (x * 10), 0)
model(tree)
translate(-x * spacing, 0, -z * spacing)
}
}
pop()
}
function mouseWheel (event) {
if (game.zoom <= ZOOM_HEIGHT_MIN && game.zoom >= ZOOM_HEIGHT_MAX) game.zoom -= event.delta
game.zoom = Math.min(game.zoom, ZOOM_HEIGHT_MIN)
game.zoom = Math.max(game.zoom, ZOOM_HEIGHT_MAX)
}
function listener () {
game.socket.on('handshake', function (data) {
Chat.logChatMessage(`Connected to ${getURL()}`, false)
game.player.id = data
})
game.socket.on('players', function (data) {
const entries = Object.entries(data)
for (const [id, player] of entries) {
if (game.player == null) continue
if (id === game.player.id) continue
game.players[id] = new Player(player)
if (game.firstSetupDone) { Chat.logChatMessage(`Player ${game.players[id].name} has connected`) }
}
if (!game.firstSetupDone) { game.firstSetupDone = true }
})
game.socket.on('player_transforms', function (data) {
// The server does not care if the client is not ready for player transforms update.
// This is why we have to check if the length is 0 in case game.players is not ready.
if (Object.keys(game.players).length === 0) return
const entries = Object.entries(data)
for (const [id, player] of entries) {
if (game.player == null) continue
if (id === game.player.id) continue
const theplayer = game.players[id]
theplayer.x = player.x
theplayer.y = player.y
theplayer.angle = player.angle
}
})
game.socket.on('messages', function (data) {
if (data.id === game.player.id) {
Chat.logChatMessage(`${game.player.name}: ${data.text}`, true, true)
game.player.updateMessage(data.text)
} else {
// Should we delete the player at game.players[data.id] here? Or just check if its not undefined??
if (game.players[data.id] !== undefined) {
Chat.logChatMessage(`${game.players[data.id].name}: ${data.text}`)
game.players[data.id].updateMessage(data.text)
}
}
})
game.socket.on('player_disconnected', function (id) {
if (game.players[id] !== undefined) { Chat.logChatMessage(`Player ${game.players[id].name} has disconnected`) }
delete (game.players[id])
})
game.socket.on('disconnect', (reason) => {
// Server closed.
console.log(reason)
if (reason === 'transport close') {
location.reload()
}
})
}
setInterval(function () { // This is the client sending data to the server every 33 milliseconds. (Emit ourself (this client) to the server.)
if (game.sendData) {
game.sendData = false
if (game.player) { // Only emit if the client exists.
game.socket.emit('player_transform', {
x: game.player.x,
y: game.player.y,
angle: game.player.angle
})
}
}
}, 33)