Hi, peeps,
This whole week I've been making a roguelike. Everything went fine and swell, until……I had to make the enemies chase the 'main character'. I've tried three ways of doing this…WAY IExplanation : I used point_direction to detect where the character is.**********var dir, dir2, temp;///////////////////////////////////////////////////////////////////////////////temp = 1;while (temp <= movesmax) do { //movesmax = amount dir2 = point_direction(prey.x, prey.y, x, y); //of moves per turn.if dir2 > 0 and dir2 < 90 then dir = 7; //prey - object the if dir2 = 90 then dir = 8; //monster is chasing.if dir2 > 90 and dir2 < 180 then dir = 1;if dir2 = 180 then dir = 2;if dir2 > 180 and dir2 < 270 then dir = 3;if dir2 = 270 then dir = 4;if dir2 > 270 and dir2 < 360 then dir = 5;if dir2 = 0 then dir = 6;switch(dir) {//hor and vercase 4: if (col_check('up')) {self.y -= sprite_height; temp +=1; }case 8: if (col_check('down')){self.y += sprite_height; temp+=1;} case 6: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case 2: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase 1: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case 3: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case 7: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; }case 5: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;**********Result:enemy makes strange turns and after a long walk finally reaches the character. Not good enough - he must move directly towards the character.**********WAY IIExplanation:using x and y of enemy to find the prey's coordinates.**********var dir, temp;///////////////////////////////////////////////////////////////////////////////temp = 1;while (temp <= movesmax) do {if prey.x > x+8 and prey.x < sight and prey.y = y then dir = 2;if prey.x < x-8 and prey.x > -sight and prey.y = y then dir = 6;if prey.y > y+8 and prey.y < sight and prey.x = x then dir = 8;if prey.y < y-8 and prey.y > -sight and prey.x = x then dir = 4;//diagonalif prey.x > x+8 and prey.x < sight and prey.y > y+8 and prey.y < sight then dir = 1;if prey.x > x+8 and prey.x < sight and prey.y < y-8 and prey.y > -sight then dir = 3;if prey.x < x-8 and prey.x > -sight and prey.y < y-8 and prey.y > -sight then dir = 5;if prey.x < x-8 and prey.x > -sight and prey.y > y+8 and prey.y < sight then dir = 7;switch(dir) {//hor and vercase 4: if (col_check('up')) {self.y -= sprite_height;temp +=1; }case 8: if (col_check('down')) {self.y += sprite_height;temp+=1;}case 6: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case 2: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase 1: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case 3: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case 7: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; }case 5: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;********Result:same as above. Sometimes it even starts walking to the completely opposite direction it should actually walk to. wtf…********WAY IIIExplanation:checking which of the squares around the enemy is closest to the prey, and then moving to it.********var v1, v2, v3, v4, v5, v6, v7, v8, temp, dir;///////////////////////////////////////////////////////////////////////////////temp = 1;while(temp <= movesmax) do {v1 = point_distance(x-8, y-8, prey.x, prey.y);v2 = point_distance(x, y-8, prey.x, prey.y);v3 = point_distance(x+8, y-8, prey.x, prey.y);v4 = point_distance(x-8, y, prey.x, prey.y);v5 = point_distance(x+8, y, prey.x, prey.y);v6 = point_distance(x-8, y+8, prey.x, prey.y);v7 = point_distance(x, y+8, prey.x, prey.y);v8 = point_distance(x+8, y+8, prey.x, prey.y);//mindir = min(v1, v2, v3, v4, v5, v6, v7, v8);switch(dir) {//hor and vercase v2: if (col_check('up')) {self.y -= sprite_height; temp +=1; }case v7: if (col_check('down')) {self.y+=sprite_height;temp+=1;}case v4: if (col_check('left')) {self.x -= sprite_height; temp +=1; }case v5: if (col_check('right')){self.x += sprite_height; temp +=1; }//diagonalcase v3: if (col_check('up')) and (col_check('right')){self.x += sprite_height; self.y -= sprite_height; temp +=1; }case v8: if (col_check('down')) and (col_check('right')){self.x += sprite_height; self.y += sprite_height; temp +=1; }case v1: if (col_check('left')) and (col_check('up')) {self.x -= sprite_height; self.y -= sprite_height; temp +=1; }case v6: if (col_check('left')) and (col_check('down')) {self.x -= sprite_height; self.y += sprite_height; temp +=1; } } }if temp = movesmax then c_e = true;********Result : same as above,just a little more accurate. (col_check checks if the argumented square is free)Could you please show me what's wrong with my codes? :/ I just can't find it…P.S.I WOULD have posted this in the forums, but user registration is forbidden…
Um can you make it easyeir so you and me can understand it. I am a global var freak and that is too hard even to you. Are you making a RPG game if so I don't use that musch scripting and still it more easyer than that.