add more action

This commit is contained in:
rofl256 2017-06-25 21:45:40 +02:00
parent ad21fc81a0
commit 2004001d9b
2 changed files with 94 additions and 7 deletions

View File

@ -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<utilMap.length;w++) {
for(var h=0;h<utilMap[w].length;h++) {
if(utilMap[w][h]!="") {
console.log("FLOWER");
domMap.append('<img style="top:'+(w*32)+'px; left:'+(h*32)+'px; z-index:2;" src="./img/plain.png" i="'+utilMap[w][h]+'" class="spriteUtils '+utilMap[w][h]+'">');
var utilTitle = utilMap[w][h];
if(utilTitle!="") {
domMap.append('<img style="top:'+(w*32)+'px; left:'+(h*32)+'px; z-index:2;" src="./img/plain.png" i="'+utilTitle+'" class="spriteUtils '+utilTitle+'">');
}
}
}
});
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();
}
});
});

View File

@ -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);