我正在制作一个socket.io游戏,而存储项目符号数据对我来说已成为一个问题。如何将项目符号对象放在项目符号对象列表的内部,即项目对象的内部,所有主要对象的列表的内部。
服务器代码:
#include <iostream>
#include <string>
using namespace std;
bool is_isomorphic(const string& input1, const string& input2)
{
if (input1.length() != input2.length()) {
return false;
}
char map[256]{};
bool used[256]{};
for (size_t i = 0; i < input1.length(); i++) {
unsigned char val1 = input1[i];
if (!map[val1]) {
unsigned char val2 = input2[i];
if (used[val2]) {
return false;
}
map[val1] = input2[i];
used[val2] = true;
} else
if (map[val1] != input2[i]) {
return false;
}
}
return true;
}
int main() {
cout << is_isomorphic("abcd", "aabb") << endl;
cout << is_isomorphic("abcdb", "zerte") << endl;
return 0;
}
客户代码:
var Player = function(id) {
var self = {
canshot: false,
bullets: []
}
}
socket.on('shoot', function(data){
if(player.canshot){
console.log("FIre")
player.canshot = false
player.bullets.push({
xV:data.xVel,
yV:data.yVel,
x:data.x,
y:data.y
});
time = 1000/player.fireRate
setTimeout(() => {
player.canshot = true;
}, time);
}
});
//Sends data in loop
bullets:player.bullets,
canshot:player.canshot
如果要查看所有代码,可以在这里查看:https://repl.it/@Helixable/FireAway
答案 0 :(得分:1)
按照我在评论中表达的道理,可以将您的代码重构为如下代码,以帮助您解决此嵌套问题:
// Player Class
function Player(id)
{
this.id = id;
this.canshot = false;
this.bullets = [];
this.fireRate = 100; // whatever
|
// Bullet class
function Bullet(player, xV,yV,x,y)
{
this.player = player;
this.xV = xV;
this.yV = yV;
this.x = x;
this.y = y;
}
// other code
socket.on('shoot', function(data){
if(player.canshot){
console.log("FIre")
player.canshot = false
var bullet = new Bullet(player, data.xVel, data.yVel, data.x, data.y);
player.bullets.push(bullet);
time = 1000/player.fireRate
setTimeout(() => {
player.canshot = true;
}, time);
}
});
现在每个子弹都可以自己处理,因为它知道它属于哪个玩家,并且您可以避免所有这些玩家和子弹的嵌套