-
Notifications
You must be signed in to change notification settings - Fork 47
Description
I'm new to game development so the first thing I did was find a few tutorials, but most seemed to be unity and pygame focused. I found the basic idea was something like this per stackoverflow:
dir.x = player.x - enemy.x;
dir.y = player.y - enemy.y;
hyp = sqrt(dir.x*dir.x + dir.y*dir.y);
dir.x /= hyp;
dir.y /= hyp;
enemy.x += dir.x*speed;
enemy.y += dir.y*speed;
I tried to write the Haskell equivalent like:
moveTowardsPlayer :: Position -> Position -> Position
moveTowardsPlayer playerPos'@(Position pv@(V2 pX pY)) aiPos@(Position aiv@(V2 aiX aiY)) = do
-- from https://stackoverflow.com/a/2625107
-- float xDistance = playerX-enemyX;
let xDistance = pX - aiX
-- float yDistance = playerY-enemyY;
yDistance = pY - aiY
-- float hypotenuse = sqrt((xDistance * xDistance) + (yDistance * yDistance));
hypotenuse = sqrt ((xDistance * xDistance) + (yDistance * yDistance))
timeFactor = 0.1
in if hypotenuse < 400
then
let yPos = timeFactor * 200 * (yDistance / hypotenuse)
xPos = timeFactor * 200 * (xDistance / hypotenuse)
in Position (V2 xPos yPos)
else aiPosMy understanding is that written like this you wouldn't use triggerEvery like some apecs code uses. I also observed that most other gamedev tutorials seemed to not have something like triggerEvery and it seems like a big difference when trying to map gamedev tutorials to apecs. Any other concepts like this that are different in apecs than most gamedev tutorials?
But that doesn't work and the enemy moves away from the player sort of? I also started trying to debug it by writing out the position the enemy moves to but that hasn't been enough.
Finally I just thought I'd write out my problem in an issue and provide a reproducible repo that demonstrates my issue: