From 2004001d9b2bd931966929cfcf2f03f8ec4702d4 Mon Sep 17 00:00:00 2001 From: rofl256 Date: Sun, 25 Jun 2017 21:45:40 +0200 Subject: [PATCH] add more action --- js/main.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- server.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 94 insertions(+), 7 deletions(-) diff --git a/js/main.js b/js/main.js index 73a4cc8..c55ce5b 100644 --- a/js/main.js +++ b/js/main.js @@ -3,7 +3,7 @@ var socket = io(); socket.on('connect', function () { console.log("connect!"); - socket.emit("getmap", 100, 100); + socket.emit("getmap"); }); socket.on('getmap', function (conent) { @@ -22,10 +22,52 @@ socket.on('getmap', function (conent) { for(var w=0;w'); + var utilTitle = utilMap[w][h]; + if(utilTitle!="") { + domMap.append(''); } } } }); + +socket.on('setpos', function (allPlayers) { + for(var player in allPlayers) { + //console.log(player); + } +}); + +$(document).ready(function() { + var viewportmeta = document.querySelector('meta[name="viewport"]'); + viewportmeta.content = 'user-scalable=NO, width=device-width, initial-scale=1.0'; + window.scrollTo(0, 0); + var keys = {}; + + $(document).keydown(function(event) { + if (event.ctrlKey==true && (event.which == '61' || event.which == '107' || event.which == '173' || event.which == '109' || event.which == '187' || event.which == '189' ) ) { + event.preventDefault(); + } + var key = event.which; + if(key==37||key==38||key==39||key==40) + event.preventDefault(); + if(!keys[key]) { + console.log("down"); + socket.emit("keyDown", key); + keys[key] = true; + } + }); + + $( document ).keyup(function( event ) { + var key = event.which; + console.log("up"); + socket.emit("keyUp", key ); + keys[key] = false; + }); + + $(window).bind('mousewheel DOMMouseScroll', function (event) { + if (event.ctrlKey == true) { + event.preventDefault(); + } + }); + +}); + diff --git a/server.js b/server.js index 2881f2e..40ac913 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,8 @@ var PORT = 8080; var gameMode = true; -var allSockets = []; +var allSockets = {}; +var allPlayers = {}; var userCnt = 0; var express = require('express'); @@ -10,6 +11,7 @@ app.use(express.static(__dirname + '/')); var server = require('http').Server(app); var io = require('socket.io')(server); var map = require("./s_map.js"); +var newmap = map.generateMap(100,100); server.listen(PORT); @@ -19,16 +21,59 @@ io.on('connection', function(socket){ userCnt++; console.log("New user", socket.id); allSockets[socket.id] = socket; + allPlayers[socket.id] = { + keys : {}, + x : 10, + y : 10 + }; socket.on('disconnect', function () { userCnt--; + delete allSockets[socket.id]; + delete allPlayers[socket.id]; console.log("User Disconnected:", socket.id); }); - socket.on('getmap', function (width, hight) { - var newmap = map.generateMap(width,hight); + socket.on('getmap', function () { + //console.log("getmap",newmap); socket.emit("getmap", newmap); }); + + socket.on('keyUp', function (key) { + allPlayers[socket.id]["keys"][key] = false; + }); + + socket.on('keyDown', function (key) { + console.log(allPlayers); + allPlayers[socket.id]["keys"][key] = true; + }); + + }); + +setInterval(function() { + for(var id in allPlayers) { + var player = allPlayers[id]; + for(var key in player["keys"]) { + if(player["keys"][key]) { //if key is pressed + var tx = 0; + var ty = 0; + var mvnt = 2; + if(key==37) { //left + tx = -1 * mvnt; + } else if(key==38) { //top + ty = -1 * mvnt; + } else if(key==39) { //right + tx = mvnt; + } else if(key==40) { //bottom + ty = mvnt; + } + player["x"] = player["x"]+tx; + player["y"] = player["y"]+ty; + } + } + } + io.sockets.emit("setpos", allPlayers); +},100); \ No newline at end of file