Hey, I've been working on a project for awhile that uses tile based movement, in other words non-fluid movement. I'm working on a second enemy, beyond the basic move up and down or left and right script. This one is simply meant to pursue the closest player. (Note, this is done in multiplayer.) Here's what I have thus far:
func movement(ex, why):
Checks if there are players in the server, put in to stop the issue of this script running before the server was up.
if server.players.empty() == false:
Holds the player you're looking to pursue.
var player2pursue
Holder for the value of the poorly constructed distance formula later on.
var resultofroot = 0
For each player in the server.
for player in server.players:
The players are dictionaries, which have a locx and a locy, which are drawn from save files.
var player2check = Vector2(server.players[player]["locx"], server.players[player]["locy"])
If the type hasn't been established yet, this will trip, making sure there's no error due to having no value.
if typeof(player2pursue) != TYPE_VECTOR2:
player2pursue = player2check
The previously mentioned distance formula, which should compare the distance of one player to another, until the nearest player is selected.
if resultofroot < sqrt(((pow((player2check.x - player2pursue.x), 2))) - (pow((player2check.y - player2pursue.y), 2))):
resultofroot = sqrt(((pow((player2check.x - player2pursue.x), 2))) - (pow((player2check.y - player2pursue.y), 2)))
player2pursue = player2check
Holds the tile to move to.
var tile2move
I use a pair of tilesets to track movement, the used cells are all possible moves.
for tile in tile2mask.get_used_cells():
Same as the other, this checks to make sure there's no null.
if typeof(tile2move) != TYPE_VECTOR2:
tile2move = tile
_This is where I think I messed up. Another distance formula to try to find the distance between the selected tile and the tile in which the player resides. If this one is closer than the previous, this becomes the previous.)
else:
if sqrt(((pow((tile2mask.world_to_map(player2pursue).x - tile2move.x), 2))) - (pow((tile2mask.world_to_map(player2pursue).y - tile2move.y), 2))) > sqrt(((pow((tile2mask.world_to_map(player2pursue).x - tile.x), 2))) - (pow((tile2mask.world_to_map(player2pursue).y - tile.y), 2))):
tile2move = tile
position = tile2mask.map_to_world(tile2move)