2025-01-18 22:18:05 +00:00
|
|
|
structure EnemyBehaviour =
|
2025-01-14 11:50:27 +00:00
|
|
|
struct
|
|
|
|
|
open GameType
|
|
|
|
|
|
|
|
|
|
fun canWalkAhead (x, y, wallTree, platformTree) =
|
|
|
|
|
let
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
|
|
|
|
|
val searchWidth = Constants.enemySize
|
|
|
|
|
|
|
|
|
|
val y = y + Constants.enemySize - 5
|
|
|
|
|
val searchHeight = 10
|
|
|
|
|
in
|
|
|
|
|
QuadTree.hasCollisionAt
|
|
|
|
|
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, wallTree)
|
|
|
|
|
orelse
|
|
|
|
|
QuadTree.hasCollisionAt
|
|
|
|
|
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree)
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-17 11:49:34 +00:00
|
|
|
(* same function takes either wallTree or platformTree and returns true
|
|
|
|
|
* if standing on tree.
|
|
|
|
|
* Function is monomorphic in the sense that wallTree and platformTree
|
|
|
|
|
* are both same type (no generics/parametric polymorphism).
|
|
|
|
|
* *)
|
2025-01-21 23:54:53 +00:00
|
|
|
fun standingOnArea (enemy: enemy, tree) =
|
2025-01-17 11:43:49 +00:00
|
|
|
let
|
|
|
|
|
val {x = ex, y = ey, ...} = enemy
|
|
|
|
|
|
|
|
|
|
val ey = ey + Constants.enemySize - 1
|
|
|
|
|
|
|
|
|
|
val width = Constants.enemySize
|
2025-01-20 02:38:22 +00:00
|
|
|
val height = Platform.platHeight
|
2025-01-17 11:43:49 +00:00
|
|
|
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
in
|
2025-01-17 11:49:34 +00:00
|
|
|
QuadTree.hasCollisionAt (ex, ey, width, height, 0, 0, ww, wh, ~1, tree)
|
2025-01-17 11:43:49 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-14 11:50:27 +00:00
|
|
|
fun getPatrollPatches (enemy: enemy, wallTree, platformTree, acc) =
|
|
|
|
|
let
|
|
|
|
|
(* This function is meant to check
|
|
|
|
|
* if enemy should switch the horizontal direction
|
|
|
|
|
* if the enemy is patrolling.
|
|
|
|
|
*
|
|
|
|
|
* Algorithm:
|
|
|
|
|
* 1. Check if enemy there is a wall ahead of the enemy
|
|
|
|
|
* in the direction the enemy is walking.
|
|
|
|
|
* 1.1. If there is a wall, then invert the direction.
|
|
|
|
|
*
|
|
|
|
|
* 2. If there is no wall, check if there is space to
|
|
|
|
|
* walk ahead on, such that enemy will not fall
|
|
|
|
|
* if enemy continues to walk.
|
|
|
|
|
* 2.1. If continuing to walk will cause the enemy to fall,
|
|
|
|
|
* then invert the direction.
|
|
|
|
|
*
|
|
|
|
|
* 3. Else, do not invert direction and simply return given list.
|
|
|
|
|
* *)
|
|
|
|
|
|
|
|
|
|
val {x, y, xAxis, ...} = enemy
|
|
|
|
|
in
|
|
|
|
|
case xAxis of
|
|
|
|
|
MOVE_LEFT =>
|
|
|
|
|
let
|
|
|
|
|
(* search to see if there is wall on left side *)
|
|
|
|
|
val searchStartX = x - Constants.moveEnemyBy
|
|
|
|
|
val searchWidth = Constants.enemySize
|
|
|
|
|
val searchHeight = Constants.enemySize - 5
|
|
|
|
|
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
|
|
|
|
|
val hasWallAhead = QuadTree.hasCollisionAt
|
|
|
|
|
( searchStartX
|
|
|
|
|
, y
|
|
|
|
|
, searchWidth
|
|
|
|
|
, searchHeight
|
|
|
|
|
, 0
|
|
|
|
|
, 0
|
|
|
|
|
, ww
|
|
|
|
|
, wh
|
|
|
|
|
, ~1
|
|
|
|
|
, wallTree
|
|
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
if
|
|
|
|
|
hasWallAhead
|
|
|
|
|
then EnemyPatch.W_X_AXIS MOVE_RIGHT :: acc
|
|
|
|
|
else (* invert direction if moving further left
|
|
|
|
|
* will result in falling down *) if
|
|
|
|
|
canWalkAhead (searchStartX, y, wallTree, platformTree)
|
|
|
|
|
then acc
|
|
|
|
|
else EnemyPatch.W_X_AXIS MOVE_RIGHT :: acc
|
|
|
|
|
end
|
|
|
|
|
| MOVE_RIGHT =>
|
|
|
|
|
let
|
|
|
|
|
(* enemy's x field is top left coordinate
|
|
|
|
|
* but we want to check top * right coordinate,
|
|
|
|
|
* so add enemySize *)
|
|
|
|
|
val searchStartX = x + Constants.enemySize + Constants.moveEnemyBy
|
|
|
|
|
val searchWidth = Constants.enemySize
|
|
|
|
|
val searchHeight = Constants.enemySize - 5
|
|
|
|
|
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
|
|
|
|
|
val hasWallAhead = QuadTree.hasCollisionAt
|
|
|
|
|
( searchStartX
|
|
|
|
|
, y
|
|
|
|
|
, searchWidth
|
|
|
|
|
, searchHeight
|
|
|
|
|
, 0
|
|
|
|
|
, 0
|
|
|
|
|
, ww
|
|
|
|
|
, wh
|
|
|
|
|
, ~1
|
|
|
|
|
, wallTree
|
|
|
|
|
)
|
|
|
|
|
in
|
|
|
|
|
if
|
|
|
|
|
hasWallAhead
|
|
|
|
|
then EnemyPatch.W_X_AXIS MOVE_LEFT :: acc
|
|
|
|
|
else (* invert direction if moving further right
|
|
|
|
|
* will result in falling down *) if
|
|
|
|
|
canWalkAhead (searchStartX, y, wallTree, platformTree)
|
|
|
|
|
then acc
|
|
|
|
|
else EnemyPatch.W_X_AXIS MOVE_LEFT :: acc
|
|
|
|
|
end
|
|
|
|
|
| STAY_STILL => acc
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
(* pathfinding *)
|
2025-01-18 22:13:16 +00:00
|
|
|
fun isBetween (p1, check, p2) = check >= p1 andalso check <= p2
|
|
|
|
|
|
2025-01-17 13:18:02 +00:00
|
|
|
fun getHighestPlatform (collisions, platforms, highestY, highestID) =
|
|
|
|
|
case collisions of
|
|
|
|
|
id :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val {y = platY, ...} = Platform.find (id, platforms)
|
|
|
|
|
in
|
|
|
|
|
(* platY < highestY is correct because lowest number = highest
|
|
|
|
|
* in * this case *)
|
|
|
|
|
if platY < highestY then getHighestPlatform (tl, platforms, platY, id)
|
|
|
|
|
else getHighestPlatform (tl, platforms, highestY, highestID)
|
|
|
|
|
end
|
|
|
|
|
| [] => highestID
|
|
|
|
|
|
2025-01-18 00:25:27 +00:00
|
|
|
fun getPlatformBelowPlayer (player, platformTree, platforms) =
|
2025-01-17 12:29:07 +00:00
|
|
|
let
|
2025-01-17 13:18:02 +00:00
|
|
|
val {x, y, ...} = player
|
|
|
|
|
|
|
|
|
|
val searchWidth = Constants.playerSize
|
|
|
|
|
val searchHeight = Constants.worldHeight - y
|
|
|
|
|
|
|
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
|
|
|
|
|
val collisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree)
|
|
|
|
|
in
|
|
|
|
|
getHighestPlatform (collisions, platforms, wh, ~1)
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-18 00:25:27 +00:00
|
|
|
fun getPlatformBelowEnemy (enemy: enemy, platformTree, platforms) =
|
|
|
|
|
let
|
|
|
|
|
val {x, y, ...} = enemy
|
|
|
|
|
|
|
|
|
|
val searchWidth = Constants.enemySize
|
|
|
|
|
val searchHeight = Constants.worldHeight - y
|
|
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val y = y + Constants.enemySize
|
|
|
|
|
|
2025-01-18 00:25:27 +00:00
|
|
|
val ww = Constants.worldWidth
|
|
|
|
|
val wh = Constants.worldHeight
|
|
|
|
|
|
|
|
|
|
val collisions = QuadTree.getCollisions
|
|
|
|
|
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree)
|
|
|
|
|
in
|
|
|
|
|
getHighestPlatform (collisions, platforms, wh, ~1)
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
fun canJump (nextPlatform, platformTree, enemy) =
|
2025-01-17 13:18:02 +00:00
|
|
|
let
|
2025-01-21 23:54:53 +00:00
|
|
|
val {x = platX, y = platY, width = platW, ...} = nextPlatform
|
|
|
|
|
val platFinishX = platX + platW
|
2025-01-18 00:25:27 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val {x = eX, y = ey, yAxis = eyAxis, ...} = enemy
|
2025-01-23 01:06:16 +00:00
|
|
|
val ey = ey + Constants.enemySize
|
2025-01-18 00:25:27 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val standingOnPlat = standingOnArea (enemy, platformTree)
|
|
|
|
|
in
|
|
|
|
|
isBetween (platX, eX, platFinishX) andalso standingOnPlat
|
2025-01-23 01:06:16 +00:00
|
|
|
andalso (ey > platY andalso ey >= platY)
|
2025-01-21 23:54:53 +00:00
|
|
|
end
|
2025-01-17 12:29:07 +00:00
|
|
|
|
2025-01-23 00:01:15 +00:00
|
|
|
fun canDrop (nextPlatform, platformTree, enemy) =
|
|
|
|
|
let
|
|
|
|
|
val {x = platX, y = platY, width = platW, ...} = nextPlatform
|
|
|
|
|
val platFinishX = platX + platW
|
|
|
|
|
|
|
|
|
|
val {x = eX, y = ey, yAxis = eyAxis, ...} = enemy
|
|
|
|
|
|
|
|
|
|
val standingOnPlat = standingOnArea (enemy, platformTree)
|
|
|
|
|
in
|
|
|
|
|
isBetween (platX, eX, platFinishX) andalso standingOnPlat
|
|
|
|
|
andalso ey < platY
|
|
|
|
|
end
|
|
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
(* get patches to help enemy move to nextPlatformID *)
|
|
|
|
|
fun getPathToNextPlatform
|
|
|
|
|
(nextPlatformID, platforms, platformTree, enemy, eID, pID, acc) =
|
|
|
|
|
let
|
|
|
|
|
val nextPlatform = Platform.find (nextPlatformID, platforms)
|
2025-01-17 12:29:07 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val {x = eX, y = ey, yAxis = eyAxis, ...} = enemy
|
2025-01-17 12:29:07 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val canJump = canJump (nextPlatform, platformTree, enemy)
|
2025-01-23 00:01:15 +00:00
|
|
|
val canDrop = canDrop (nextPlatform, platformTree, enemy)
|
2025-01-21 23:54:53 +00:00
|
|
|
in
|
|
|
|
|
if canJump then
|
|
|
|
|
case eyAxis of
|
|
|
|
|
ON_GROUND => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc
|
|
|
|
|
| FALLING => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc
|
|
|
|
|
| _ => acc
|
2025-01-23 00:01:15 +00:00
|
|
|
else if canDrop then
|
|
|
|
|
EnemyPatch.W_Y_AXIS DROP_BELOW_PLATFORM :: acc
|
2025-01-21 23:54:53 +00:00
|
|
|
else
|
|
|
|
|
acc
|
|
|
|
|
end
|
2025-01-17 13:18:02 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
fun canJumpOnPlatform (player, platforms, enemy: enemy, platformTree, acc) =
|
|
|
|
|
let
|
|
|
|
|
(* todo: possibly get pID and eID of player/enemy in a different way *)
|
|
|
|
|
val pID = getPlatformBelowPlayer (player, platformTree, platforms)
|
|
|
|
|
val eID = getPlatformBelowEnemy (enemy, platformTree, platforms)
|
2025-01-17 13:18:02 +00:00
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val bestPath = PathFinding.start (pID, eID, platforms, platformTree)
|
2025-01-17 13:18:02 +00:00
|
|
|
in
|
2025-01-21 23:54:53 +00:00
|
|
|
if eID = pID then
|
|
|
|
|
EnemyPatch.W_Y_AXIS FALLING :: acc
|
|
|
|
|
else
|
|
|
|
|
case bestPath of
|
|
|
|
|
nextPlatformID :: _ =>
|
|
|
|
|
getPathToNextPlatform
|
|
|
|
|
(nextPlatformID, platforms, platformTree, enemy, eID, pID, acc)
|
|
|
|
|
| [] => acc
|
2025-01-17 12:29:07 +00:00
|
|
|
end
|
|
|
|
|
|
2025-01-17 13:18:02 +00:00
|
|
|
fun getFollowPatches
|
|
|
|
|
(player: player, enemy, wallTree, platformTree, platforms, acc) =
|
2025-01-17 11:43:49 +00:00
|
|
|
let
|
|
|
|
|
val {x = px, y = py, ...} = player
|
|
|
|
|
val {x = ex, y = ey, yAxis = eyAxis, ...} = enemy
|
|
|
|
|
|
|
|
|
|
val xAxis = if px < ex then MOVE_LEFT else MOVE_RIGHT
|
|
|
|
|
|
2025-01-21 23:54:53 +00:00
|
|
|
val acc = canJumpOnPlatform (player, platforms, enemy, platformTree, acc)
|
|
|
|
|
|
2025-01-17 11:43:49 +00:00
|
|
|
in
|
2025-01-18 00:25:27 +00:00
|
|
|
EnemyPatch.W_X_AXIS STAY_STILL :: acc
|
2025-01-17 11:43:49 +00:00
|
|
|
end
|
2025-01-14 11:50:27 +00:00
|
|
|
|
2025-01-16 08:15:29 +00:00
|
|
|
fun getVariantPatches
|
|
|
|
|
(enemy, walls, wallTree, platforms, platformTree, player, acc) =
|
2025-01-14 11:50:27 +00:00
|
|
|
let
|
|
|
|
|
open EnemyVariants
|
|
|
|
|
in
|
|
|
|
|
case #variant enemy of
|
|
|
|
|
PATROL_SLIME => getPatrollPatches (enemy, wallTree, platformTree, acc)
|
2025-01-17 11:43:49 +00:00
|
|
|
| FOLLOW_SIME =>
|
2025-01-17 13:18:02 +00:00
|
|
|
getFollowPatches
|
|
|
|
|
(player, enemy, wallTree, platformTree, platforms, acc)
|
2025-01-14 11:50:27 +00:00
|
|
|
end
|
|
|
|
|
end
|