-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (82 loc) · 3.21 KB
/
app.js
File metadata and controls
104 lines (82 loc) · 3.21 KB
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
import { createScene } from './src/js/scene';
import { createAvatar, createAvatarText } from './src/js/avatar';
import { addKeyboardControls, addMouseControls } from './src/js/control';
import { listenForUpdates } from './src/js/network';
import { playerAvatars, targetPos } from './src/js/state';
import { initChat } from './src/js/textChat';
import * as utils from './src/js/utils';
import * as playerUtils from './src/js/playerUtils'
import * as THREE from 'three';
import WebGL from 'three/addons/capabilities/WebGL.js';
const socket = io("http://localhost:3000");
if (WebGL.isWebGL2Available())
{
initSpace();
initChat(socket);
}
else
{
document.body.appendChild(WebGL.getErrorMessage());
}
let isAnimating = true;
function initSpace()
{
const { scene, camera, renderer, sun } = createScene();
const coordsDisplay = utils.displayCoords();
const avatar = createAvatar();
avatar.add(camera);
camera.position.set(0, 4, -5);
// Prompt for the username
const username = prompt("Enter your username:", "Anonymous");
const userHexColor = avatar.material.color.getHex();
const userColor = `#${userHexColor.toString(16).padStart(6, '0')}`;
socket.emit('setUserData', { username, color: userColor, hp: 100, maxHP: 100 } );
createAvatarText(scene, avatar, username, userColor, 1);
const currentPlayer = {
avatar: avatar,
hp: 100,
maxHP: 100
};
playerUtils.updatePlayerHealth(scene, currentPlayer, 0, true);
const keyControls = addKeyboardControls(avatar);
addMouseControls(avatar, scene, socket, userColor);
// listen for socket events
listenForUpdates(socket, scene, avatar, currentPlayer);
let lastPos = new THREE.Vector3();
let lastRot = new THREE.Euler();
function animateLoop()
{
if (!isAnimating) return;
keyControls();
coordsDisplay.update(avatar.position, avatar.rotation);
utils.checkSunCollision(scene, currentPlayer, sun.position, true);
playerUtils.updateCurrentPlayerPos(lastPos, lastRot, avatar, socket, currentPlayer);
Object.keys(playerAvatars).forEach(playerId => {
if (targetPos[playerId])
{
utils.checkSunCollision(scene, playerAvatars[playerId], sun.position);
const currentAvatar = playerAvatars[playerId].avatar;
const newPos = targetPos[playerId].position;
const newRot = targetPos[playerId].rotation;
// interpolate curr pos to target pos
currentAvatar.position.lerp(newPos, 0.1);
// interpolate curr rot to target rot
const targetRotation = new THREE.Euler(newRot._x, newRot._y, newRot._z);
currentAvatar.quaternion.slerp(new THREE.Quaternion().setFromEuler(targetRotation), 0.1);
}
})
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animateLoop);
utils.handleResize(renderer, camera);
const colorPoints = new utils.createColorPoints(2000, 500); // 2000 points spread over 500 units in space
scene.add(colorPoints);
}
export function pauseAnimation()
{
isAnimating = false;
}
export function resumeAnimation()
{
isAnimating = true;
}