add player at land
This commit is contained in:
parent
a9bc9a295e
commit
7497cee4c7
35
js/main.js
35
js/main.js
@ -3,6 +3,8 @@ var socket = io();
|
|||||||
var cssMap, utilMap, domMap;
|
var cssMap, utilMap, domMap;
|
||||||
var ownId;
|
var ownId;
|
||||||
var visibleMap = [];
|
var visibleMap = [];
|
||||||
|
var range = 18; //Sichtweite
|
||||||
|
|
||||||
socket.on('connect', function () {
|
socket.on('connect', function () {
|
||||||
ownId = socket.io.engine.id;
|
ownId = socket.io.engine.id;
|
||||||
console.log("connect!");
|
console.log("connect!");
|
||||||
@ -46,7 +48,7 @@ socket.on('getmap', function (conent) {
|
|||||||
|
|
||||||
function addNewPlayer(player, id) {
|
function addNewPlayer(player, id) {
|
||||||
if($("#"+id).length==0)
|
if($("#"+id).length==0)
|
||||||
$("body").append('<div class="player" id="'+id+'" style="position:absolute; top:'+player["y"]+'px; left:'+player["x"]+'px; width:5px; height:5px; background:red; z-index:100;"><img class="default_ship" src="./img/default_ship.png"></div>');
|
$("body").append('<div class="player" id="'+id+'" style="position:absolute; top:'+player["y"]+'px; left:'+player["x"]+'px; width:7px; height:7px; z-index:100;"><img class="default_ship" src="./img/default_ship.png"></div>');
|
||||||
}
|
}
|
||||||
|
|
||||||
var oldmapX = null;
|
var oldmapX = null;
|
||||||
@ -56,8 +58,23 @@ socket.on('setpos', function (allPlayers) {
|
|||||||
window.requestAnimationFrame(function() {
|
window.requestAnimationFrame(function() {
|
||||||
for(var id in allPlayers) {
|
for(var id in allPlayers) {
|
||||||
var player = allPlayers[id];
|
var player = allPlayers[id];
|
||||||
|
|
||||||
var playerElement = $("#"+id);
|
var playerElement = $("#"+id);
|
||||||
|
|
||||||
|
for(var i in player["special"]) {
|
||||||
|
if(i == "modechange") {
|
||||||
|
if(player["mode"] == 0) { //ship
|
||||||
|
playerElement.find("img").attr("src","./img/default_ship.png");
|
||||||
|
playerElement.find("img").removeClass("guy");
|
||||||
|
playerElement.find("img").addClass("default_ship");
|
||||||
|
} else { //land
|
||||||
|
|
||||||
|
playerElement.find("img").removeClass("default_ship");
|
||||||
|
playerElement.find("img").addClass("guy");
|
||||||
|
playerElement.find("img").attr("src","./img/plain.png");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(playerElement.length==1) {
|
if(playerElement.length==1) {
|
||||||
var playerX = Math.floor(player["x"]);
|
var playerX = Math.floor(player["x"]);
|
||||||
var playerY = Math.floor(player["y"]);
|
var playerY = Math.floor(player["y"]);
|
||||||
@ -72,7 +89,6 @@ socket.on('setpos', function (allPlayers) {
|
|||||||
if(oldmapX != mapX || oldmapY != mapY) {
|
if(oldmapX != mapX || oldmapY != mapY) {
|
||||||
oldmapX = mapX;
|
oldmapX = mapX;
|
||||||
oldmapY = mapY;
|
oldmapY = mapY;
|
||||||
var range = 18;
|
|
||||||
|
|
||||||
for(var x=mapX-range;x<mapX+range;x++) {
|
for(var x=mapX-range;x<mapX+range;x++) {
|
||||||
if(x >= 0 && x < cssMap.length) {
|
if(x >= 0 && x < cssMap.length) {
|
||||||
@ -81,7 +97,6 @@ socket.on('setpos', function (allPlayers) {
|
|||||||
if(!visibleMap[x] || !visibleMap[x][y]) {
|
if(!visibleMap[x] || !visibleMap[x][y]) {
|
||||||
var dist = getCoordDist(x,y,mapX,mapY);
|
var dist = getCoordDist(x,y,mapX,mapY);
|
||||||
if(dist<=range) {
|
if(dist<=range) {
|
||||||
console.log("NEW",dist);
|
|
||||||
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]+'">');
|
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];
|
var utilTitle = utilMap[y][x];
|
||||||
if(utilTitle!="") {
|
if(utilTitle!="") {
|
||||||
@ -122,7 +137,10 @@ socket.on('setpos', function (allPlayers) {
|
|||||||
} else if(oldPosX < playerX) {
|
} else if(oldPosX < playerX) {
|
||||||
left2right = 1;
|
left2right = 1;
|
||||||
}
|
}
|
||||||
var angle = Math.atan2(top2Btn, left2right);
|
var angle = 0;
|
||||||
|
if(player["mode"] == 0) {
|
||||||
|
var angle = Math.atan2(top2Btn, left2right);
|
||||||
|
}
|
||||||
playerElement.css({"top":player["y"],"left":player["x"], "-webkit-transform" : "rotate("+angle+"rad)"});
|
playerElement.css({"top":player["y"],"left":player["x"], "-webkit-transform" : "rotate("+angle+"rad)"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,7 +177,12 @@ $(document).ready(function() {
|
|||||||
if(key==37||key==38||key==39||key==40)
|
if(key==37||key==38||key==39||key==40)
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if(!keys[key]) {
|
if(!keys[key]) {
|
||||||
socket.emit("keyDown", key);
|
|
||||||
|
if(key == 32) {
|
||||||
|
socket.emit("actionKey", key);
|
||||||
|
} else {
|
||||||
|
socket.emit("keyDown", key);
|
||||||
|
}
|
||||||
keys[key] = true;
|
keys[key] = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
29
server.js
29
server.js
@ -11,8 +11,8 @@ 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 = 200;
|
var mapWidth = 500;
|
||||||
var mapHeight = 200;
|
var mapHeight = 500;
|
||||||
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);
|
||||||
@ -33,8 +33,10 @@ io.on('connection', function(socket){
|
|||||||
x : 10,
|
x : 10,
|
||||||
y : 10,
|
y : 10,
|
||||||
mvnt : defaultMvnt,
|
mvnt : defaultMvnt,
|
||||||
mode : 0 //0 = ship
|
special : {},
|
||||||
|
mode : 0 //0 = ship //1 foot
|
||||||
};
|
};
|
||||||
|
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 });
|
||||||
|
|
||||||
socket.on('disconnect', function () {
|
socket.on('disconnect', function () {
|
||||||
@ -59,6 +61,21 @@ io.on('connection', function(socket){
|
|||||||
allPlayers[socket.id]["keys"][key] = true;
|
allPlayers[socket.id]["keys"][key] = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on('actionKey', function (key) {
|
||||||
|
var groundUnder = getGroundUnderPlayer(player["x"], player["y"]);
|
||||||
|
if(player["mode"] == 0) { //ship
|
||||||
|
if(groundUnder>=0) { //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)
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -81,6 +98,7 @@ setInterval(function() {
|
|||||||
} else if(key==40) { //bottom
|
} else if(key==40) { //bottom
|
||||||
ty = mvnt;
|
ty = mvnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
var newX = player["x"]+tx;
|
var newX = player["x"]+tx;
|
||||||
var newY = player["y"]+ty;
|
var newY = player["y"]+ty;
|
||||||
|
|
||||||
@ -94,6 +112,9 @@ setInterval(function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
io.sockets.emit("setpos", allPlayers);
|
io.sockets.emit("setpos", allPlayers);
|
||||||
|
for(var id in allPlayers) {
|
||||||
|
allPlayers[id]["special"] = {};
|
||||||
|
}
|
||||||
},20);
|
},20);
|
||||||
|
|
||||||
function checkPlayerPosition(newX, newY, groundUnderPlayer, player) {
|
function checkPlayerPosition(newX, newY, groundUnderPlayer, player) {
|
||||||
@ -143,7 +164,7 @@ function getPlayerMvnt(player, groundUnderPlayer) {
|
|||||||
} else if(groundUnderPlayer===2) {
|
} else if(groundUnderPlayer===2) {
|
||||||
return 0.8;
|
return 0.8;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1.5;
|
||||||
}
|
}
|
||||||
return defaultMvnt;
|
return defaultMvnt;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user