make actions possible and make forest removeable

This commit is contained in:
rofl256 2017-07-25 01:49:02 +02:00
parent 33ddb4316e
commit 4addf2696a
3 changed files with 98 additions and 27 deletions

View File

@ -24,21 +24,6 @@ socket.on('getmap', function (conent) {
domMap.empty();
// for(var w=0;w<cssMap.length;w++) {
// for(var h=0;h<cssMap[w].length;h++) {
// domMap.append('<img style="top:'+(w*32)+'px; left:'+(h*32)+'px; z-index:1;" src="./img/plain.png" i="'+w+'-'+h+'" class="sprite '+cssMap[w][h]+'">');
// }
// }
// for(var w=0;w<utilMap.length;w++) {
// for(var h=0;h<utilMap[w].length;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+'">');
// }
// }
// }
var allPlayers = conent["allPlayers"];
console.log(allPlayers);
for(var id in allPlayers) {
@ -46,6 +31,20 @@ socket.on('getmap', function (conent) {
}
});
socket.on('objAction', function (obj) {
console.log(obj);
var mapX = obj["mapX"]*32;
var mapY = obj["mapY"]*32;
$(".overlay").remove();
$("#main").append('<div class="overlay" id="" style="background: rgba(206, 0, 104, 0.45); width:32px; height:32px; z-index:2; position:absolute; top:'+mapY+'px; left:'+mapX+'px;"></div>');
});
socket.on('removeUtil', function (obj) {
console.log(obj);
$("."+obj["mapY"]+'-'+obj["mapX"]+'.'+obj["name"]).remove();
});
function addNewPlayer(player, id) {
if($("#"+id).length==0) {
var modeClass = "default_ship";
@ -112,7 +111,7 @@ socket.on('setpos', function (allPlayers) {
domMap.append('<img style="top:'+(y*32)+'px; left:'+(x*32)+'px; z-index:1;" src="./img/plain.png" i="'+y+'-'+x+'" class="'+y+'-'+x+' sprite '+cssMap[y][x]+'">');
var utilTitle = utilMap[y][x];
if(utilTitle!="") {
domMap.append('<img style="top:'+(y*32)+'px; left:'+(x*32)+'px; z-index:2;" src="./img/plain.png" i="'+utilTitle+'" class="'+y+'-'+x+' spriteUtils '+utilTitle+'">');
domMap.append('<img style="top:'+((y*32)-5)+'px; left:'+(x*32)+'px; z-index:2;" src="./img/plain.png" i="'+utilTitle+'" class="'+y+'-'+x+' spriteUtils '+utilTitle+'">');
}
if(!visibleMap[x]) {
visibleMap[x] = [];
@ -198,6 +197,7 @@ $(document).ready(function() {
event.preventDefault();
}
var key = event.which;
console.log(key);
if(key==37||key==38||key==39||key==40)
event.preventDefault();
if(!keys[key]) {
@ -214,6 +214,9 @@ $(document).ready(function() {
$( document ).keyup(function( event ) {
var key = event.which;
socket.emit("keyUp", key );
if(key==32) {
$(".overlay").remove();
}
keys[key] = false;
});

View File

@ -24,7 +24,7 @@ module.exports = {
for(var h=0;h<height;h++) {
map[w][h] = "0";
utilMap[w][h] = "";
if(Math.random()<0.0006)
if(Math.random()<0.0009)
map[w][h] = "#";
}
}
@ -127,7 +127,7 @@ module.exports = {
}
}
for(var w=0;w<width;w++) {
for(var w=0;w<width;w++) { //fill forest
for(var h=0;h<height;h++) {
if(map[w][h] == "6") {
if((utilMap[w-1][h]=="forest" && utilMap[w+1][h]=="forest") || (utilMap[w][h-1]=="forest" && utilMap[w][h+1]=="forest")) {

View File

@ -25,6 +25,7 @@ server.listen(PORT);
console.log("Pirate running on port:"+PORT);
io.on('connection', function(socket){
var playerActionTimer = null;
userCnt++;
console.log("New user", socket.id);
allSockets[socket.id] = socket;
@ -55,6 +56,13 @@ io.on('connection', function(socket){
socket.on('keyUp', function (key) {
allPlayers[socket.id]["keys"][key] = false;
if(key==32) { //space
player["action"] = false;
if(playerActionTimer) {
clearTimeout(playerActionTimer);
playerActionTimer = null;
}
}
});
socket.on('keyDown', function (key) {
@ -74,11 +82,69 @@ io.on('connection', function(socket){
player["special"]["modechange"] = true;
}
};
console.log(player["mode"],groundUnder)
console.log(player["mode"],groundUnder);
var obj = getNearestObject(player["x"], player["y"]);
if(obj) {
obj["socketId"] = socket.id;
if(playerActionTimer) {
clearTimeout(playerActionTimer);
playerActionTimer = null;
}
if(obj["name"] == "forest") {
playerActionTimer = setTimeout(function() {
utilMap[obj["mapY"]][obj["mapX"]] = "";
io.sockets.emit("removeUtil", obj);
console.log("REMOVEEEEE", obj);
}, 2000); //2 Secs
}
io.sockets.emit("objAction", obj);
}
player["action"] = true;
});
});
function getNearestObject(x, y) {
var mapX = Math.floor(x/32);
var mapY = Math.floor(y/32);
var obj = null;
if(utilMap[mapY][mapX]!="") {
obj = {
mapX : mapX,
mapY : mapY,
name : utilMap[mapY][mapX]
}
} else {
var rX = x%32;
var rY = y%32;
var sX = 0;
var sY = 0;
if(rX>6 && rX<26) { //He stands in the middle of the field (left-><-right)
if(rY<16) { //go Up
sY = -1;
} else { //go Down
sY = 1;
}
} else if(rY>6 && rY<26) { //He stands in the middle of the field (up-><-down)
if(rX<16) { //go left
sX = -1;
} else { //go right
sX = 1;
}
}
if(utilMap[mapY+sY][mapX+sX]!="") {
obj = {
mapY : mapY+sY,
mapX : mapX+sX,
name : utilMap[mapY+sY][mapX+sX]
}
}
}
return obj;
}
setInterval(function() {
for(var id in allPlayers) {
@ -99,14 +165,16 @@ setInterval(function() {
ty = mvnt;
}
var newX = player["x"]+tx;
var newY = player["y"]+ty;
var groundUnderPlayer = getGroundUnderPlayer(newX, newY);
if(checkPlayerPosition(newX, newY, groundUnderPlayer, player)) {
player["x"] = newX;
player["y"] = newY;
player["mvnt"] = getPlayerMvnt(player, groundUnderPlayer);
if(!player["action"]) {
var newX = player["x"]+tx;
var newY = player["y"]+ty;
var groundUnderPlayer = getGroundUnderPlayer(newX, newY);
if(checkPlayerPosition(newX, newY, groundUnderPlayer, player)) {
player["x"] = newX;
player["y"] = newY;
player["mvnt"] = getPlayerMvnt(player, groundUnderPlayer);
}
}
}
}