javascript - Basic AI in Node.js/Socket.IO -
i'm creating small game in node.js/socket.io , need advice on creating ai. code have below real quick example came it's fast player doesn't see enemy move on client-side. on right lines approach or there better way should doing it?
thanks!
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}); };
that's pretty ai! randomizing interval between movements easy, common technique things this. im curious , love try whatever making! 1 thing aware of though, make sure ai not too good.
one other thing implement in code have ai "aim" point away target. eg:
var move = function(targetx + randomx, targety + randomy)
you can use position of target before moved predict heading.
var xchange = (targetx2 - targetx1)/(timeinterval1); var ychange = (targety2 - targety1)/(timeinterval1); var move = function(targetx + xchange * timeinterval2, targety + ychange * timeinterval2)
where timeinterval1 time interval between 2 of targets positions , timeinterval2 time interval between current position , next position.
the key not making ai hard player though. ;)
Comments
Post a Comment