我正在 Node.js/Socket.IO 中创建一个小游戏,需要一些关于创建 AI 的建议。下面的代码是我想出的一个真正快速的示例,但它是如此之快,玩家甚至看不到敌人在客户端移动。我是否采用这种方法是正确的,还是有更好的方法我应该这样做?
谢谢!
var random;
setInterval(function() {
random = Math.round(Math.random() * 200);
move(random, random);
console.log("Moving player");
}, 10000)
var move = function(targetX, targetY) {
if (x < targetX) {
while (x < targetX) {
x++;
sendNewCoordinates(x, y);
}
} else if (x > targetX) {
while (x > targetX) {
x--;
sendNewCoordinates(x, y);
}
} else if (y < targetY) {
while (y < targetX) {
y++;
sendNewCoordinates(x, y);
}
} else if (y > targetY) {
while (y > targetX) {
y--;
sendNewCoordinates(x, y);
}
}
};
var sendNewCoordinates = function(newX, newY) {
socket.sockets.emit("move enemy", {x: newX, y: newY});
};