-- Define enemy speed
local enemySpeed = 150
-- Initialize enemy position within the update function
local enemyPosition
-- Function to calculate distance between two points
local function distance_squared(p1, p2)
local dx = p1.x - p2.x
local dy = p1.y - p2.y
return dx * dx + dy * dy
end
-- Function to calculate path using A* algorithm
local function find_path(start, goal)
-- Placeholder implementation of A* algorithm
-- Replace this with your A* implementation
return {start, goal}
function update(self, dt)
-- Initialize enemy position within the update function
if not enemyPosition then
enemyPosition = go.get_position()
end
-- Get player position
local playerPosition = go.get_position("player")
-- Calculate path to player using A* algorithm
local path = find_path(enemyPosition, playerPosition)
-- If path is empty, return
if #path <= 1 then
return
end
-- Get next waypoint from the path
local nextWaypoint = path[2]
-- Calculate direction vector towards the next waypoint
local direction = vmath.normalize(nextWaypoint - enemyPosition)
-- Update enemy position towards the next waypoint
enemyPosition = enemyPosition + direction * enemySpeed * dt
-- Set enemy position
go.set_position(enemyPosition)
end
ERROR:SCRIPT: enemy/enemy.script:28: Instance player not found
stack traceback:
[C] in function get_position
enemy/enemy.script:28: in function <enemy/enemy.script:21>