diff --git a/css/main.css b/css/main.css index 9035555..25cc8c7 100644 --- a/css/main.css +++ b/css/main.css @@ -295,6 +295,14 @@ margin-top: 5px; } +.item_item_spaten3 { + background: url(../img/items.png) -59px -149px; + width: 30px; + background-size: 476px; + margin-left: 5px; + margin-top: 5px; +} + .item_weapon_stick1 { background: url(../img/items.png) -150px -91px; width: 30px; @@ -303,7 +311,7 @@ margin-top: 5px; } -.item_item_bone1 { +.item_item_bone0 { background: url(../img/items.png) -356px -29px; width: 30px; background-size: 476px; diff --git a/js/main.js b/js/main.js index b47dd63..fd5ee65 100644 --- a/js/main.js +++ b/js/main.js @@ -39,7 +39,8 @@ var tooltiptexts = { "item_weapon_sword4": "Schwert: Schaden erhöht 500", "item_weapon_sword5": "Schwert: Schaden erhöht 600", "item_weapon_stick1": "Stock: Schaden +20", - "item_item_bone1": "Knochen: Schaden +5", + "item_item_bone0": "Ganz toller Knochen!", + "item_item_spaten3": "Spaten: Schneller graben", "item_consumable_fish1": "Erhöht deine Lebenspunkte um 20", "item_consumable_fish2": "Erhöht deine Lebenspunkte um 10", "item_item_dimond1": "Verkaufe den Diamand für 10 Siegpunkte! Bewegungsgeschwindigkeit verlangsamt!", @@ -363,13 +364,9 @@ $(document).ready(function () { } $(document).keydown(function (event) { - //console.log(event.which) - 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; //console.log(key); - if (key == 37 || key == 38 || key == 39 || key == 40) + if (key == 37 || key == 38 || key == 39 || key == 40 || key == 71) event.preventDefault(); if (!keys[key]) { @@ -379,13 +376,19 @@ $(document).ready(function () { socket.emit("keyDown", key); } keys[key] = true; + return; } + //console.log(event.which) + if (event.ctrlKey == true && (event.which == '61' || event.which == '107' || event.which == '173' || event.which == '109' || event.which == '187' || event.which == '189')) { + event.preventDefault(); + } + }); $(document).keyup(function (event) { var key = event.which; socket.emit("keyUp", key); - if (key == 69) { + if (key == 69 || key == 71) { $("#animationDiv").remove(); } keys[key] = false; diff --git a/server.js b/server.js index 918e749..cca3bc6 100644 --- a/server.js +++ b/server.js @@ -148,9 +148,11 @@ var itemAttrs = { lvl: 1, dmg: 20 }, - "item_item_bone1": { + "item_item_bone0": { + lvl: 0, + }, + "item_item_spaten3": { lvl: 1, - dmg: 5 }, "item_consumable_fish1": { lvl: 3, @@ -165,7 +167,7 @@ var itemAttrs = { changeMovementSpeed: -0.5 }, "item_item_woods1": { - lvl: 0, + lvl: 1, //changeMovementSpeed: -0.5 }, } @@ -188,8 +190,8 @@ io.on('connection', function (socket) { x: 10, y: 10, mvnt: defaultMvnt, - lookingLeft : 0, - lookingUp : 0, + lookingLeft: 0, + lookingUp: 0, special: {}, mode: 0, //0 = ship //1 foot slots: { @@ -201,8 +203,8 @@ io.on('connection', function (socket) { tool: null, item: null }, - hp : 80, - socketId : socket.id + hp: 80, + socketId: socket.id }; var player = allPlayers[socket.id]; io.sockets.emit("newPlayer", { @@ -214,7 +216,7 @@ io.on('connection', function (socket) { for (var i in player.slots) { if (player.slots[i]) { let nearFieldCoords = getNearEmptyField(Math.floor(player["x"] / 32), Math.floor(player["y"] / 32)); - if(nearFieldCoords) { + if (nearFieldCoords) { let newObj = { mapY: nearFieldCoords["y"], mapX: nearFieldCoords["x"], @@ -241,7 +243,7 @@ io.on('connection', function (socket) { socket.on('keyUp', function (key) { allPlayers[socket.id]["keys"][key] = false; - if (key == 69) { //space + if (key == 69 || key == 71) { //space oder G player["action"] = false; if (playerActionTimer) { clearTimeout(playerActionTimer); @@ -253,147 +255,187 @@ io.on('connection', function (socket) { socket.on('keyDown', function (key) { allPlayers[socket.id]["keys"][key] = true; - if(key == 32 && allPlayers[socket.id]["mode"] != 0) { //Space HIT SOMEONE + if (key == 32 && allPlayers[socket.id]["mode"] != 0) { //Space HIT SOMEONE //if(allPlayers[socket.id]["mode"] == 1) { // On land - if(!allPlayers[socket.id]["hitPending"]) { - io.sockets.emit("showHitAnimation", socket.id); - allPlayers[socket.id]["hitPending"] = true; - let allPlayersWithCollision = checkHitWithOtherPlayers(socket.id); - for(var i=0;i= 1) { //its nearly land - player["mode"] = 1; - player["special"]["modechange"] = true; - } - } else { - if (groundUnder <= 1) { //its water land - player["mode"] = 0; - player["special"]["modechange"] = true; - } - }; - //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") { - let cutTime = 2000; //2 Secs - if (player["slots"]["tool"] && itemAttrs[player["slots"]["tool"]] && itemAttrs[player["slots"]["tool"]]["reduceTreeCutSpeed"] > 0) { - cutTime -= itemAttrs[player["slots"]["tool"]]["reduceTreeCutSpeed"]; - playerActionTimer = setTimeout(function () { + if (key == 69) {// E - if (Math.random() > 0.75) { - utilMap[obj["mapY"]][obj["mapX"]] = "item_item_woods1"; - obj["newName"] = "item_item_woods1"; - io.sockets.emit("replaceUtil", obj); - } else { - utilMap[obj["mapY"]][obj["mapX"]] = ""; - io.sockets.emit("removeUtil", obj); - } - - socket.emit("stopAnimation", cutTime); - - }, cutTime); - socket.emit("animation", cutTime); - } else { - socket.emit("displayMsg", "Baum kann nur mit einer Axt gefällt werden!"); - - //can not cut tree + if (player["mode"] == 0) { //ship + if (groundUnder >= 1) { //its nearly land + player["mode"] = 1; + player["special"]["modechange"] = true; } - } else if (obj["name"] == "treasure-closed") { - playerActionTimer = setTimeout(function () { - utilMap[obj["mapY"]][obj["mapX"]] = "treasure-open"; - obj["newName"] = "treasure-open"; - io.sockets.emit("replaceUtil", obj); - socket.emit("stopAnimation", 2000); + } else { + if (groundUnder <= 1) { //its water land + player["mode"] = 0; + player["special"]["modechange"] = true; + } + }; + //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") { + let cutTime = 2000; //2 Secs + if (player["slots"]["tool"] && itemAttrs[player["slots"]["tool"]] && itemAttrs[player["slots"]["tool"]]["reduceTreeCutSpeed"] > 0) { + cutTime -= itemAttrs[player["slots"]["tool"]]["reduceTreeCutSpeed"]; + playerActionTimer = setTimeout(function () { - //Drop a random Item - let randomItem = getRandomItem(2, 5); - let nearFieldCoords = getNearEmptyField(obj["mapX"], obj["mapY"]); + if (Math.random() > 0.75) { + utilMap[obj["mapY"]][obj["mapX"]] = "item_item_woods1"; + obj["newName"] = "item_item_woods1"; + io.sockets.emit("replaceUtil", obj); + } else { + utilMap[obj["mapY"]][obj["mapX"]] = ""; + io.sockets.emit("removeUtil", obj); + } - utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = randomItem; - let newObj = { - mapY: nearFieldCoords["y"], - mapX: nearFieldCoords["x"], - newName: randomItem + socket.emit("stopAnimation", cutTime); + + }, cutTime); + socket.emit("animation", cutTime); + } else { + socket.emit("displayMsg", "Baum kann nur mit einer Axt gefällt werden!"); + + //can not cut tree } - io.sockets.emit("dropItem", newObj); - }, 2000); //2 Secs - socket.emit("animation", 2000); - } else if (obj["name"] == "fass") { - playerActionTimer = setTimeout(function () { + } else if (obj["name"] == "treasure-closed") { + playerActionTimer = setTimeout(function () { + utilMap[obj["mapY"]][obj["mapX"]] = "treasure-open"; + obj["newName"] = "treasure-open"; + io.sockets.emit("replaceUtil", obj); + socket.emit("stopAnimation", 2000); + + //Drop a random Item + let randomItem = getRandomItem(2, 5); + let nearFieldCoords = getNearEmptyField(obj["mapX"], obj["mapY"]); + + utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = randomItem; + let newObj = { + mapY: nearFieldCoords["y"], + mapX: nearFieldCoords["x"], + newName: randomItem + } + io.sockets.emit("dropItem", newObj); + }, 2000); //2 Secs + socket.emit("animation", 2000); + } else if (obj["name"] == "fass") { + playerActionTimer = setTimeout(function () { + utilMap[obj["mapY"]][obj["mapX"]] = ""; + io.sockets.emit("removeUtil", obj); + socket.emit("stopAnimation", 500); + + //Drop a random Item + let randomItem = getRandomItem(1, 2); + let nearFieldCoords = getNearEmptyField(obj["mapX"], obj["mapY"]); + + utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = randomItem; + let newObj = { + mapY: nearFieldCoords["y"], + mapX: nearFieldCoords["x"], + newName: randomItem + } + io.sockets.emit("dropItem", newObj); + }, 500); //2 Secs + socket.emit("animation", 500); + } else if (obj["name"].split("_")[0] == "item") { + let itemName = obj["name"]; utilMap[obj["mapY"]][obj["mapX"]] = ""; io.sockets.emit("removeUtil", obj); - socket.emit("stopAnimation", 500); - //Drop a random Item - let randomItem = getRandomItem(1, 2); - let nearFieldCoords = getNearEmptyField(obj["mapX"], obj["mapY"]); - - utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = randomItem; - let newObj = { - mapY: nearFieldCoords["y"], - mapX: nearFieldCoords["x"], - newName: randomItem + let slotArt = obj["name"].split("_")[1]; + if (allPlayers[socket.id]["slots"][slotArt]) { //Slot is not empty so drop it + let nearFieldCoords = getNearEmptyField(Math.floor(player["x"] / 32), Math.floor(player["y"] / 32)); + let newObj = { + mapY: nearFieldCoords["y"], + mapX: nearFieldCoords["x"], + newName: allPlayers[socket.id]["slots"][slotArt] + } + utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = newObj["newName"]; + io.sockets.emit("dropItem", newObj); + //console.log("drop", newObj) } - io.sockets.emit("dropItem", newObj); - }, 500); //2 Secs - socket.emit("animation", 500); - } else if (obj["name"].split("_")[0] == "item") { - let itemName = obj["name"]; - utilMap[obj["mapY"]][obj["mapX"]] = ""; - io.sockets.emit("removeUtil", obj); - let slotArt = obj["name"].split("_")[1]; - if (allPlayers[socket.id]["slots"][slotArt]) { //Slot is not empty so drop it - let nearFieldCoords = getNearEmptyField(Math.floor(player["x"] / 32), Math.floor(player["y"] / 32)); - let newObj = { - mapY: nearFieldCoords["y"], - mapX: nearFieldCoords["x"], - newName: allPlayers[socket.id]["slots"][slotArt] + console.log(slotArt) + if (slotArt == "consumable") { + let hp = itemAttrs[obj["name"]]["hp"]; + allPlayers[socket.id]["hp"] += hp; + let toMutch = 0; + if (allPlayers[socket.id]["hp"] > 100) { + toMutch = allPlayers[socket.id]["hp"] - 100; + } + if (allPlayers[socket.id]["hp"] > 100) { + allPlayers[socket.id]["hp"] = 100; + } + socket.emit("displayMsg", "Lebenspunkte +" + (hp - toMutch)); + socket.emit("hpChange", allPlayers[socket.id]["hp"]); + } else { + allPlayers[socket.id]["slots"][slotArt] = itemName; + socket.emit("addItemToInventar", itemName); } - utilMap[nearFieldCoords["y"]][nearFieldCoords["x"]] = newObj["newName"]; - io.sockets.emit("dropItem", newObj); - //console.log("drop", newObj) } - console.log(slotArt) - if(slotArt == "consumable") { - let hp = itemAttrs[obj["name"]]["hp"]; - allPlayers[socket.id]["hp"] += hp; - let toMutch = 0; - if(allPlayers[socket.id]["hp"]>100) { - toMutch = allPlayers[socket.id]["hp"]-100; - } - if(allPlayers[socket.id]["hp"] > 100) { - allPlayers[socket.id]["hp"] = 100; - } - socket.emit("displayMsg", "Lebenspunkte +"+(hp-toMutch)); - socket.emit("hpChange", allPlayers[socket.id]["hp"]); - } else { - allPlayers[socket.id]["slots"][slotArt] = itemName; - socket.emit("addItemToInventar", itemName); - } + io.sockets.emit("objAction", obj); } - - io.sockets.emit("objAction", obj); } player["action"] = true; }); @@ -419,32 +461,33 @@ function getNearEmptyField(x, y) { function calcDmgAndHitPlayer(player1, player2) { let dmg = 1; - dmg += parseFloat(player1["slots"]["weapon"] ? itemAttrs[player1["slots"]["weapon"]]["dmg"] : 0 ); + dmg += parseFloat(player1["slots"]["weapon"] ? itemAttrs[player1["slots"]["weapon"]]["dmg"] : 0); let armor = 1; - for(var k in player2["slots"]) { - if(k == "head" || k=="chest" || k == "legs") { - armor+= parseFloat(player1["slots"][k] ? itemAttrs[player1["slots"][k]]["armor"] : 0); + for (var k in player2["slots"]) { + if (k == "head" || k == "chest" || k == "legs") { + armor += parseFloat(player1["slots"][k] ? itemAttrs[player1["slots"][k]]["armor"] : 0); } } - let hitDmg = dmg * (armor/100) * 100 + let hitDmg = dmg * (armor / 100) * 100 player2["hp"] -= hitDmg; - if(player2["hp"] < 0) { + if (player2["hp"] < 0) { player2["hp"] = 0; } - io.sockets.emit("newPlayerHp", { socketId : player2["socketId"], hp : player2["hp"]}) + io.sockets.emit("newPlayerHp", { socketId: player2["socketId"], hp: player2["hp"] }) console.log(hitDmg, dmg, armor, player1, player2) } function getRandomItem(minLvl, maxLvl) { //console.log("rnd", items.length, getRandomNumber(0, items.length), items) - //return items[32] //fish + // return items[32] //Spaten + // return items[32] //fish while (1) { let item = items[getRandomNumber(0, items.length)]; console.log(item) if (item && (typeof (maxLvl) == "undefined" || typeof (minLvl) == "undefined" || (itemAttrs[item]["lvl"] >= minLvl && itemAttrs[item]["lvl"] <= maxLvl))) { - + return item; } } @@ -496,7 +539,7 @@ setInterval(function () { for (var id in allPlayers) { var player = allPlayers[id]; for (var key in player["keys"]) { - if (player["keys"][key] && player["hp"] >0) { //if key is pressed + if (player["keys"][key] && player["hp"] > 0) { //if key is pressed var tx = 0; var ty = 0; @@ -617,26 +660,26 @@ function getRandomNumber(min, max) { //min (inclusive) and max (exclusive) function checkHitWithOtherPlayers(socketId) { let playersWithCollision = []; let mainPlayer = allPlayers[socketId]; - for(var sid in allPlayers) { - if(sid != socketId) { //Dont check collision with self + for (var sid in allPlayers) { + if (sid != socketId) { //Dont check collision with self let targetPlayer = allPlayers[sid]; let bottom = mainPlayer.keys["40"]; let left = mainPlayer.keys["37"]; //console.log(mainPlayer.x, mainPlayer.y, 28, targetPlayer.x, targetPlayer.y, 8) - if(isCircleCircleCollision(mainPlayer.x, mainPlayer.y, 28, targetPlayer.x, targetPlayer.y, 8)) { + if (isCircleCircleCollision(mainPlayer.x, mainPlayer.y, 28, targetPlayer.x, targetPlayer.y, 8)) { let hit = false; - console.log(mainPlayer.lookingLeft, mainPlayer.lookingUp , mainPlayer.x, mainPlayer.y, targetPlayer.x, targetPlayer.y) - if(mainPlayer.lookingLeft == 1 && mainPlayer.x >= targetPlayer.x) { + console.log(mainPlayer.lookingLeft, mainPlayer.lookingUp, mainPlayer.x, mainPlayer.y, targetPlayer.x, targetPlayer.y) + if (mainPlayer.lookingLeft == 1 && mainPlayer.x >= targetPlayer.x) { hit = true; - } else if(mainPlayer.lookingLeft == -1 && mainPlayer.x <= targetPlayer.x) { + } else if (mainPlayer.lookingLeft == -1 && mainPlayer.x <= targetPlayer.x) { hit = true; - } else if(mainPlayer.lookingUp == 1 && mainPlayer.y >= targetPlayer.y) { + } else if (mainPlayer.lookingUp == 1 && mainPlayer.y >= targetPlayer.y) { hit = true; - } else if(mainPlayer.lookingUp == -1 && mainPlayer.y <= targetPlayer.y) { + } else if (mainPlayer.lookingUp == -1 && mainPlayer.y <= targetPlayer.y) { hit = true; - } + } - if(hit) { + if (hit) { playersWithCollision.push(sid); } }