add function to drop items from crates

This commit is contained in:
Raphael 2021-10-29 23:36:00 +02:00
parent c0d79755eb
commit c409b5e5e9
5 changed files with 207 additions and 81 deletions

View File

@ -54,6 +54,48 @@
transform-origin: 0; transform-origin: 0;
} }
/* Items */
.boots1 {
background: url(../img/items.png) 0px -95px;
width: 30px;
background-size: 500px;
margin-left: 5px;
margin-top: 5px;
}
.boots2 {
background: url(../img/items.png) -32px -95px;
width: 30px;
background-size: 500px;
margin-left: 5px;
margin-top: 5px;
}
.boots3 {
background: url(../img/items.png) -64px -95px;
width: 30px;
background-size: 500px;
margin-left: 5px;
margin-top: 5px;
}
.boots4 {
background: url(../img/items.png) -96px -95px;
width: 30px;
background-size: 500px;
margin-left: 5px;
margin-top: 5px;
}
.boots5 {
background: url(../img/items.png) -128px -95px;
width: 30px;
background-size: 500px;
margin-left: 5px;
margin-top: 5px;
}
/* Guy */
.guy1-0-0 { .guy1-0-0 {
background: url(../img/guy_sprite.png) 0px 0px; background: url(../img/guy_sprite.png) 0px 0px;
} }

BIN
img/items.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 662 KiB

View File

@ -77,6 +77,11 @@ socket.on('replaceUtil', function (obj) {
$("."+obj["mapY"]+'-'+obj["mapX"]+'.'+obj["name"]).removeClass(obj["name"]).addClass(obj["newName"]); $("."+obj["mapY"]+'-'+obj["mapX"]+'.'+obj["name"]).removeClass(obj["name"]).addClass(obj["newName"]);
}); });
socket.on('dropItem', function (obj) {
console.log(obj);
domMap.append('<img style="top:'+((obj["mapY"]*32))+'px; left:'+((obj["mapX"]*32))+'px; z-index:3; position:absolute;" src="./img/plain.png" i="'+obj["newName"]+'" class="'+obj["mapY"]+'-'+obj["mapX"]+' spriteItem spriteUtils '+obj["newName"]+'">');
});
function addNewPlayer(player, id) { function addNewPlayer(player, id) {
if($("#"+id).length==0) { if($("#"+id).length==0) {
var modeClass = "default_ship"; var modeClass = "default_ship";

View File

@ -137,17 +137,17 @@ module.exports = {
} }
} }
var treasureSet = false; var treasureCnt = 5;
var foundLand = true; var foundLand = true;
while (!treasureSet && foundLand) { while (treasureCnt>0 && foundLand) {
foundLand = false; foundLand = false;
for (var w = 0; w < width; w++) { //add chest for (var w = 0; w < width; w++) { //add chest
for (var h = 0; h < height; h++) { for (var h = 0; h < height; h++) {
if(map[w][h] == "6") { if(map[w][h] == "6") {
foundLand = true; foundLand = true;
} }
if (map[w][h] == "6" && Math.random() < 0.00001 && !treasureSet) { if (map[w][h] == "6" && Math.random() < 0.00001 && treasureCnt>0) {
treasureSet = true; treasureCnt--;
utilMap[w][h] = "treasure-closed"; utilMap[w][h] = "treasure-closed";
console.log(w, h, "treasure") console.log(w, h, "treasure")
} }

233
server.js
View File

@ -11,34 +11,78 @@ app.use(express.static(__dirname + '/'));
var server = require('http').Server(app); var server = require('http').Server(app);
var io = require('socket.io')(server); var io = require('socket.io')(server);
var map = require("./s_map.js"); var map = require("./s_map.js");
var mapWidth = 100; var mapWidth = 20;
var mapHeight = 100; var mapHeight = 20;
var maxX = mapWidth * 32; var maxX = mapWidth * 32;
var maxY = mapHeight * 32; var maxY = mapHeight * 32;
var newmap = map.generateMap(mapWidth,mapHeight); var newmap = map.generateMap(mapWidth, mapHeight);
var cssMap = newmap["cssMap"]; var cssMap = newmap["cssMap"];
var map = newmap["map"]; var map = newmap["map"];
var utilMap = newmap["utilMap"]; var utilMap = newmap["utilMap"];
var defaultMvnt = 2; var defaultMvnt = 2;
var items = [
"boots1",
"boots2",
"boots3",
"boots4",
"boots5",
];
var itemPlayerChanges = {
"boots1" : {
changeMovementLand : 1,
text : "Geschwindigkeit an Land +1"
},
"boots2" : {
changeMovementLand : 2,
text : "Geschwindigkeit an Land +2"
},
"boots3" : {
changeMovementLand : 3,
text : "Geschwindigkeit an Land +3"
},
"boots4" : {
changeMovementLand : 4,
text : "Geschwindigkeit an Land +4"
},
"boots5" : {
changeMovementLand : 5,
text : "Geschwindigkeit an Land +5"
},
}
server.listen(PORT); server.listen(PORT);
console.log("Pirate running on port:"+PORT); console.log("Pirate running on port:" + PORT);
io.on('connection', function(socket){ io.on('connection', function (socket) {
var playerActionTimer = null; var playerActionTimer = null;
userCnt++; userCnt++;
console.log("New user", socket.id); console.log("New user", socket.id);
allSockets[socket.id] = socket; allSockets[socket.id] = socket;
allPlayers[socket.id] = { allPlayers[socket.id] = {
keys : {}, keys: {},
x : 10, x: 10,
y : 10, y: 10,
mvnt : defaultMvnt, mvnt: defaultMvnt,
special : {}, special: {},
mode : 0 //0 = ship //1 foot mode: 0, //0 = ship //1 foot
items: {
slot1: null,
slot2: null,
slot3: null
}
}; };
var player = allPlayers[socket.id]; var player = allPlayers[socket.id];
io.sockets.emit("newPlayer", { player : allPlayers[socket.id], id : socket.id }); io.sockets.emit("newPlayer", {
player: allPlayers[socket.id],
id: socket.id,
items: {
slot1: null,
slot2: null,
slot3: null
}
});
socket.on('disconnect', function () { socket.on('disconnect', function () {
userCnt--; userCnt--;
@ -49,16 +93,16 @@ io.on('connection', function(socket){
}); });
socket.on('getmap', function () { socket.on('getmap', function () {
newmap["allPlayers"] = allPlayers; newmap["allPlayers"] = allPlayers;
socket.emit("getmap", newmap); socket.emit("getmap", newmap);
}); });
socket.on('keyUp', function (key) { socket.on('keyUp', function (key) {
allPlayers[socket.id]["keys"][key] = false; allPlayers[socket.id]["keys"][key] = false;
if(key==32) { //space if (key == 32) { //space
player["action"] = false; player["action"] = false;
if(playerActionTimer) { if (playerActionTimer) {
clearTimeout(playerActionTimer); clearTimeout(playerActionTimer);
playerActionTimer = null; playerActionTimer = null;
} }
@ -72,38 +116,51 @@ io.on('connection', function(socket){
socket.on('actionKey', function (key) { socket.on('actionKey', function (key) {
var groundUnder = getGroundUnderPlayer(player["x"], player["y"]); var groundUnder = getGroundUnderPlayer(player["x"], player["y"]);
if(player["mode"] == 0) { //ship if (player["mode"] == 0) { //ship
if(groundUnder>=1) { //its nearly land if (groundUnder >= 1) { //its nearly land
player["mode"] = 1; player["mode"] = 1;
player["special"]["modechange"] = true; player["special"]["modechange"] = true;
} }
} else { } else {
if(groundUnder<=1) { //its water land if (groundUnder <= 1) { //its water land
player["mode"] = 0; player["mode"] = 0;
player["special"]["modechange"] = true; player["special"]["modechange"] = true;
} }
}; };
console.log(player["mode"],groundUnder); console.log(player["mode"], groundUnder);
var obj = getNearestObject(player["x"], player["y"]); var obj = getNearestObject(player["x"], player["y"]);
if(obj) { if (obj) {
obj["socketId"] = socket.id; obj["socketId"] = socket.id;
if(playerActionTimer) { if (playerActionTimer) {
clearTimeout(playerActionTimer); clearTimeout(playerActionTimer);
playerActionTimer = null; playerActionTimer = null;
} }
if(obj["name"] == "forest") { if (obj["name"] == "forest") {
playerActionTimer = setTimeout(function() { playerActionTimer = setTimeout(function () {
utilMap[obj["mapY"]][obj["mapX"]] = ""; utilMap[obj["mapY"]][obj["mapX"]] = "";
io.sockets.emit("removeUtil", obj); io.sockets.emit("removeUtil", obj);
socket.emit("stopAnimation", 2000); socket.emit("stopAnimation", 2000);
}, 2000); //2 Secs }, 2000); //2 Secs
socket.emit("animation", 2000); socket.emit("animation", 2000);
} else if(obj["name"] == "treasure-closed") { } else if (obj["name"] == "treasure-closed") {
playerActionTimer = setTimeout(function() { playerActionTimer = setTimeout(function () {
utilMap[obj["mapY"]][obj["mapX"]] = "treasure-open"; utilMap[obj["mapY"]][obj["mapX"]] = "treasure-open";
obj["newName"] = "treasure-open"; obj["newName"] = "treasure-open";
io.sockets.emit("replaceUtil", obj); io.sockets.emit("replaceUtil", obj);
socket.emit("stopAnimation", 2000); socket.emit("stopAnimation", 2000);
let randomItem = getRandomItem();
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);
console.log("newObj", newObj)
}, 2000); //2 Secs }, 2000); //2 Secs
socket.emit("animation", 2000); socket.emit("animation", 2000);
} }
@ -111,43 +168,61 @@ io.on('connection', function(socket){
} }
player["action"] = true; player["action"] = true;
}); });
}); });
function getNearEmptyField(x, y) {
for(var i=-1;i<2;i++) {
for(var k=-1;k<2;k++) {
console.log("check",i, k, y+i, x+k, utilMap[y+i][x+k])
if(utilMap[y+i][x+k] == "") {
return {
y : y+i,
x : x+k
}
}
}
}
}
function getRandomItem() {
console.log("rnd", items.length, getRandomNumber(0, items.length), items)
return items[getRandomNumber(0, items.length)];
}
function getNearestObject(x, y) { function getNearestObject(x, y) {
var mapX = Math.floor(x/32); var mapX = Math.floor(x / 32);
var mapY = Math.floor(y/32); var mapY = Math.floor(y / 32);
var obj = null; var obj = null;
if(utilMap[mapY][mapX]!="") { if (utilMap[mapY][mapX] != "") {
obj = { obj = {
mapX : mapX, mapX: mapX,
mapY : mapY, mapY: mapY,
name : utilMap[mapY][mapX] name: utilMap[mapY][mapX]
} }
} else { } else {
var rX = x%32; var rX = x % 32;
var rY = y%32; var rY = y % 32;
var sX = 0; var sX = 0;
var sY = 0; var sY = 0;
if(rX>6 && rX<26) { //He stands in the middle of the field (left-><-right) if (rX > 6 && rX < 26) { //He stands in the middle of the field (left-><-right)
if(rY<16) { //go Up if (rY < 16) { //go Up
sY = -1; sY = -1;
} else { //go Down } else { //go Down
sY = 1; sY = 1;
} }
} else if(rY>6 && rY<26) { //He stands in the middle of the field (up-><-down) } else if (rY > 6 && rY < 26) { //He stands in the middle of the field (up-><-down)
if(rX<16) { //go left if (rX < 16) { //go left
sX = -1; sX = -1;
} else { //go right } else { //go right
sX = 1; sX = 1;
} }
} }
if(utilMap[mapY+sY][mapX+sX]!="") { if (utilMap[mapY + sY][mapX + sX] != "") {
obj = { obj = {
mapY : mapY+sY, mapY: mapY + sY,
mapX : mapX+sX, mapX: mapX + sX,
name : utilMap[mapY+sY][mapX+sX] name: utilMap[mapY + sY][mapX + sX]
} }
} }
} }
@ -156,31 +231,31 @@ function getNearestObject(x, y) {
} }
setInterval(function() { setInterval(function () {
for(var id in allPlayers) { for (var id in allPlayers) {
var player = allPlayers[id]; var player = allPlayers[id];
for(var key in player["keys"]) { for (var key in player["keys"]) {
if(player["keys"][key]) { //if key is pressed if (player["keys"][key]) { //if key is pressed
var tx = 0; var tx = 0;
var ty = 0; var ty = 0;
var mvnt = player["mvnt"]; var mvnt = player["mvnt"];
if(key==37) { //left if (key == 37) { //left
tx = -1 * mvnt; tx = -1 * mvnt;
} else if(key==38) { //top } else if (key == 38) { //top
ty = -1 * mvnt; ty = -1 * mvnt;
} else if(key==39) { //right } else if (key == 39) { //right
tx = mvnt; tx = mvnt;
} else if(key==40) { //bottom } else if (key == 40) { //bottom
ty = mvnt; ty = mvnt;
} }
if(!player["action"]) { if (!player["action"]) {
var newX = player["x"]+tx; var newX = player["x"] + tx;
var newY = player["y"]+ty; var newY = player["y"] + ty;
var groundUnderPlayer = getGroundUnderPlayer(newX, newY); var groundUnderPlayer = getGroundUnderPlayer(newX, newY);
if(checkPlayerPosition(newX, newY, groundUnderPlayer, player)) { if (checkPlayerPosition(newX, newY, groundUnderPlayer, player)) {
player["x"] = newX; player["x"] = newX;
player["y"] = newY; player["y"] = newY;
player["mvnt"] = getPlayerMvnt(player, groundUnderPlayer); player["mvnt"] = getPlayerMvnt(player, groundUnderPlayer);
@ -190,42 +265,42 @@ setInterval(function() {
} }
} }
io.sockets.emit("setpos", allPlayers); io.sockets.emit("setpos", allPlayers);
for(var id in allPlayers) { for (var id in allPlayers) {
allPlayers[id]["special"] = {}; allPlayers[id]["special"] = {};
} }
},20); }, 20);
function checkPlayerPosition(newX, newY, groundUnderPlayer, player) { function checkPlayerPosition(newX, newY, groundUnderPlayer, player) {
if(newX <0 || newY<0 || newX > maxX || newY > maxY) { if (newX < 0 || newY < 0 || newX > maxX || newY > maxY) {
return false; return false;
} }
if(player["mode"]==0) { //ship if (player["mode"] == 0) { //ship
if(groundUnderPlayer < 0 || groundUnderPlayer > 2) { //dont move with ship on land if (groundUnderPlayer < 0 || groundUnderPlayer > 2) { //dont move with ship on land
return false; return false;
} }
} else { } else {
if(groundUnderPlayer == 7) { //forrest so dont move if (groundUnderPlayer == 7) { //forrest so dont move
return false; return false;
} }
} }
return true; return true;
} }
function getGroundUnderPlayer(newX, newY) { //0 = water; 10 = hard ground function getGroundUnderPlayer(newX, newY) { //0 = water; 10 = hard ground
if(newX <0 || newY<0 || newX > maxX || newY > maxY) { if (newX < 0 || newY < 0 || newX > maxX || newY > maxY) {
return 0; return 0;
} }
var mapX = Math.floor(newX/32); var mapX = Math.floor(newX / 32);
var mapY = Math.floor(newY/32); var mapY = Math.floor(newY / 32);
var ground = cssMap[mapY][mapX]; var ground = cssMap[mapY][mapX];
var util = utilMap[mapY][mapX]; var util = utilMap[mapY][mapX];
if(util == "forest") { if (util == "forest") {
return 7; return 7;
} }
var groundSplit = ground.split("_"); var groundSplit = ground.split("_");
if(groundSplit.length != 2) { if (groundSplit.length != 2) {
if(ground == "water") { if (ground == "water") {
return 0; return 0;
} }
return 10; //Ground return 10; //Ground
@ -233,22 +308,26 @@ function getGroundUnderPlayer(newX, newY) { //0 = water; 10 = hard ground
return parseFloat(groundSplit[1]); return parseFloat(groundSplit[1]);
} }
function getPlayerMvnt(player, groundUnderPlayer) { function getPlayerMvnt(player, groundUnderPlayer) {
if(player["mode"]==0) { //ship if (player["mode"] == 0) { //ship
if(groundUnderPlayer===1) { if (groundUnderPlayer === 1) {
return 0.5; return 0.5;
} else if(groundUnderPlayer===2) { } else if (groundUnderPlayer === 2) {
return 0.1; return 0.1;
} }
} else { } else {
if(groundUnderPlayer===0) { if (groundUnderPlayer === 0) {
return 0.1; return 0.1;
} else if(groundUnderPlayer===1) { } else if (groundUnderPlayer === 1) {
return 0.5; return 0.5;
} else if(groundUnderPlayer===2) { } else if (groundUnderPlayer === 2) {
return 0.8; return 0.8;
} }
return 1.5; return 1.5;
} }
return defaultMvnt; return defaultMvnt;
}
function getRandomNumber(min, max) { //min (inclusive) and max (exclusive)
return Math.floor(Math.random() * (max - min + 1)) + min;
} }