diff --git a/js/input.js b/js/input.js index 194dd25..5745a7c 100644 --- a/js/input.js +++ b/js/input.js @@ -50,31 +50,31 @@ class Input { return; let oldDir=this.dir; this.dir=0; - if(this.keysDown.has('z')){ - if(this.keysDown.has('d')){ + if(this.keysDown.has('z') || this.keysDown.has('arrowup')){ + if(this.keysDown.has('d') || this.keysDown.has('arrowright')){ this.dir = 2; - }else if(this.keysDown.has('s')){ + }else if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){ this.dir = 0; - }else if(this.keysDown.has('q')){ + }else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){ this.dir = 8; }else{ this.dir = 1; } - }else if(this.keysDown.has('d')){ - if(this.keysDown.has('s')){ + }else if(this.keysDown.has('d') || this.keysDown.has('arrowright')){ + if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){ this.dir = 4; - }else if(this.keysDown.has('q')){ + }else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){ this.dir = 0; }else{ this.dir = 3; } - }else if(this.keysDown.has('s')){ - if(this.keysDown.has('q')){ + }else if(this.keysDown.has('s') || this.keysDown.has('arrowdown')){ + if(this.keysDown.has('q') || this.keysDown.has('arrowLeft')){ this.dir = 6; }else{ this.dir = 5; } - }else if(this.keysDown.has('q')){ + }else if(this.keysDown.has('q') || this.keysDown.has('arrowleft')){ this.dir = 7; } if(oldDir!=this.dir) diff --git a/server/node_modules/.package-lock.json b/server/node_modules/.package-lock.json index 1b909ed..7f2718c 100644 --- a/server/node_modules/.package-lock.json +++ b/server/node_modules/.package-lock.json @@ -616,6 +616,11 @@ "node": ">= 0.10" } }, + "node_modules/punycode": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", + "integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==" + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -861,6 +866,29 @@ "node": ">= 0.8" } }, + "node_modules/url": { + "version": "0.11.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz", + "integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==", + "dependencies": { + "punycode": "^1.4.1", + "qs": "^6.11.2" + } + }, + "node_modules/url/node_modules/qs": { + "version": "6.11.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz", + "integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==", + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/server/objects.js b/server/objects.js new file mode 100644 index 0000000..3b37c86 --- /dev/null +++ b/server/objects.js @@ -0,0 +1,3 @@ +let objects = {"squares":[{"x":1162,"y":115,"w":144,"h":125},{"x":120,"y":906,"w":1228,"h":21},{"x":127,"y":0,"w":1225,"h":14},{"x":297,"y":114,"w":78,"h":93},{"x":169,"y":243,"w":62,"h":250},{"x":313,"y":243,"w":62,"h":253},{"x":228,"y":257,"w":96,"h":222},{"x":792,"y":113,"w":176,"h":126},{"x":1017,"y":113,"w":143,"h":81},{"x":1160,"y":241,"w":143,"h":-129},{"x":1065,"y":372,"w":127,"h":124},{"x":697,"y":372,"w":319,"h":126},{"x":697,"y":498,"w":159,"h":111},{"x":697,"y":628,"w":160,"h":158},{"x":1001,"y":628,"w":191,"h":158},{"x":281,"y":629,"w":175,"h":156},{"x":0,"y":0,"w":137,"h":616},{"x":0,"y":615,"w":136,"h":310},{"x":1337,"y":1,"w":130,"h":581},{"x":1337,"y":573,"w":131,"h":356},{"x":586,"y":153,"w":15,"h":24},{"x":651,"y":153,"w":12,"h":24},{"x":700,"y":187,"w":12,"h":22},{"x":1020,"y":194,"w":10,"h":47},{"x":1019,"y":227,"w":56,"h":14},{"x":1101,"y":227,"w":62,"h":13},{"x":654,"y":128,"w":105,"h":17},{"x":748,"y":129,"w":12,"h":49},{"x":748,"y":193,"w":12,"h":47},{"x":654,"y":223,"w":105,"h":16},{"x":521,"y":129,"w":106,"h":14},{"x":521,"y":140,"w":13,"h":38},{"x":521,"y":194,"w":13,"h":46},{"x":521,"y":225,"w":108,"h":16}],"circles":[{"x":552,"y":163,"r":13.601470508735444},{"x":608,"y":190,"r":20.248456731316587},{"x":569,"y":212,"r":11.704699910719626},{"x":680,"y":213,"r":12.041594578792296},{"x":727,"y":164,"r":14.212670403551895}]}; + +exports.objects = objects \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index 27a3594..f507e4f 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.18.2", "socket.io": "^4.7.2", + "url": "^0.11.3", "ws": "^8.14.2" } }, @@ -626,6 +627,11 @@ "node": ">= 0.10" } }, + "node_modules/punycode": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", + "integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==" + }, "node_modules/qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -871,6 +877,29 @@ "node": ">= 0.8" } }, + "node_modules/url": { + "version": "0.11.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz", + "integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==", + "dependencies": { + "punycode": "^1.4.1", + "qs": "^6.11.2" + } + }, + "node_modules/url/node_modules/qs": { + "version": "6.11.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz", + "integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==", + "dependencies": { + "side-channel": "^1.0.4" + }, + "engines": { + "node": ">=0.6" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -1370,6 +1399,11 @@ "ipaddr.js": "1.9.1" } }, + "punycode": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-1.4.1.tgz", + "integrity": "sha512-jmYNElW7yvO7TV33CjSmvSiE2yco3bV2czu/OzDKdMNVZQWfxCblURLhf+47syQRBntjfLdd/H0egrzIG+oaFQ==" + }, "qs": { "version": "6.11.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz", @@ -1551,6 +1585,25 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==" }, + "url": { + "version": "0.11.3", + "resolved": "https://registry.npmjs.org/url/-/url-0.11.3.tgz", + "integrity": "sha512-6hxOLGfZASQK/cijlZnZJTq8OXAkt/3YGfQX45vvMYXpZoo8NdWZcY73K108Jf759lS1Bv/8wXnHDTSz17dSRw==", + "requires": { + "punycode": "^1.4.1", + "qs": "^6.11.2" + }, + "dependencies": { + "qs": { + "version": "6.11.2", + "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz", + "integrity": "sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==", + "requires": { + "side-channel": "^1.0.4" + } + } + } + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/server/package.json b/server/package.json index e4a2154..04ec167 100644 --- a/server/package.json +++ b/server/package.json @@ -12,6 +12,7 @@ "dependencies": { "express": "^4.18.2", "socket.io": "^4.7.2", + "url": "^0.11.3", "ws": "^8.14.2" } } diff --git a/server/server.js b/server/server.js index 9681c0c..7f40c2e 100644 --- a/server/server.js +++ b/server/server.js @@ -1,6 +1,8 @@ const express = require('express'); const http = require('http'); const WebSocket = require('ws'); +const url = require('url'); +const objectsModule = require('./objects'); const app = express(); const server = http.createServer(app); @@ -14,24 +16,57 @@ app.get('/', (req, res) => { res.send('Grand Tabernacle Auto VI'); }); -players.push({ - id: playerCount, - x: 100, - y: 100, - dir: 4, - visibleDir: 4, - name: playerCount.toString() + "joueur" - }) -playerCount++; +function norm(x1, x2, y1, y2){ + return Math.sqrt((x1-x2)**2 + (y1+y2)**2); +} -function createNewPlayer(socket){ +function circleCollide(x, y){ + objectsModule.objects.circles.forEach((circle)=>{ + if(norm(x, circle.x, y, circle.y)<=circle.radius){ + return true; + } + }) + return false; +} + +function squareCollide(x, y){ + objectsModule.objects.squares.forEach((square)=>{ + if(square.x <= x && x<=square.x+square.w && square.y<=y && y<=square.y+square.h){ + return true; + } + }) + return false; +} + +function generatePosition(){ + let mapWidth = 1000; + let mapHeight = 1000; + let count = 0; + + let x = Math.floor(Math.random()*mapWidth); + let y = Math.floor(Math.random()*mapHeight); + + while((circleCollide(x,y) || squareCollide(x,y))){ + x = Math.floor(Math.random()*mapWidth); + y = Math.floor(Math.random()*mapHeight); + count++; + if(count >= 1000){ + break; + } + } + return x, y; + +} + +function createNewPlayer(socket, name){ + let x, y = generatePosition(); var obj = { id: playerCount, - x: Math.floor(Math.random()*1000), - y: Math.floor(Math.random()*1000), + x: x, + y: y, dir: 0, visibleDir: 0, - name: playerCount.toString() + "joueur" + name: name } players.push(obj); playerCount++; @@ -53,9 +88,11 @@ function createNewPlayer(socket){ connections.add(socket); } -wss.on('connection', (socket) => { +wss.on('connection', (socket, req) => { //create new player, send informations to new player and broadcast new player for all - createNewPlayer(socket); + let username = url.parse(req.url, true).query.name; + console.log(username); + createNewPlayer(socket, username); socket.on('message', (message) => { message = JSON.parse(message); diff --git a/server/testsite/app.js b/server/testsite/app.js index 29b14c2..1454825 100644 --- a/server/testsite/app.js +++ b/server/testsite/app.js @@ -1,4 +1,4 @@ -const socket = new WebSocket('ws://localhost:8080'); //ws://129.151.227.50:9600 +const socket = new WebSocket('ws://localhost:8080?name=coucou'); //ws://129.151.227.50:9600 var time = new Date(); socket.addEventListener('open', (event) => { console.log('connecté');