start implementing patches to follow player (basic; shouldn't use path finding like Dijkstra's algorithm)
This commit is contained in:
@@ -19,6 +19,25 @@ struct
|
|||||||
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree)
|
(x, y, searchWidth, searchHeight, 0, 0, ww, wh, ~1, platformTree)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
fun isOnGround (enemy, wallTree, platformTree) =
|
||||||
|
let
|
||||||
|
val {x = ex, y = ey, ...} = enemy
|
||||||
|
|
||||||
|
val ey = ey + Constants.enemySize - 1
|
||||||
|
|
||||||
|
val width = Constants.enemySize
|
||||||
|
val height = 2
|
||||||
|
|
||||||
|
val ww = Constants.worldWidth
|
||||||
|
val wh = Constants.worldHeight
|
||||||
|
in
|
||||||
|
QuadTree.hasCollisionAt
|
||||||
|
(ex, ey, width, height, 0, 0, ww, wh, ~1, wallTree)
|
||||||
|
orelse
|
||||||
|
QuadTree.hasCollisionAt
|
||||||
|
(ex, ey, width, height, 0, 0, ww, wh, ~1, platformTree)
|
||||||
|
end
|
||||||
|
|
||||||
fun getPatrollPatches (enemy: enemy, wallTree, platformTree, acc) =
|
fun getPatrollPatches (enemy: enemy, wallTree, platformTree, acc) =
|
||||||
let
|
let
|
||||||
(* This function is meant to check
|
(* This function is meant to check
|
||||||
@@ -111,6 +130,26 @@ struct
|
|||||||
| STAY_STILL => acc
|
| STAY_STILL => acc
|
||||||
end
|
end
|
||||||
|
|
||||||
|
(* pathfinding *)
|
||||||
|
fun getFollowPatches (player: player, enemy, wallTree, platformTree, acc) =
|
||||||
|
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
|
||||||
|
|
||||||
|
val isOnGround = isOnGround (enemy, wallTree, platformTree)
|
||||||
|
val yAxis =
|
||||||
|
if ey > py andalso isOnGround then
|
||||||
|
case eyAxis of
|
||||||
|
ON_GROUND => JUMPING 0
|
||||||
|
| FALLING => JUMPING 0
|
||||||
|
| _ => eyAxis
|
||||||
|
else
|
||||||
|
eyAxis
|
||||||
|
in
|
||||||
|
EnemyPatch.W_Y_AXIS yAxis :: EnemyPatch.W_X_AXIS xAxis :: acc
|
||||||
|
end
|
||||||
|
|
||||||
fun getVariantPatches
|
fun getVariantPatches
|
||||||
(enemy, walls, wallTree, platforms, platformTree, player, acc) =
|
(enemy, walls, wallTree, platforms, platformTree, player, acc) =
|
||||||
@@ -119,5 +158,7 @@ struct
|
|||||||
in
|
in
|
||||||
case #variant enemy of
|
case #variant enemy of
|
||||||
PATROL_SLIME => getPatrollPatches (enemy, wallTree, platformTree, acc)
|
PATROL_SLIME => getPatrollPatches (enemy, wallTree, platformTree, acc)
|
||||||
|
| FOLLOW_SIME =>
|
||||||
|
getFollowPatches (player, enemy, wallTree, platformTree, acc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
signature ENEMY_VARIANTS =
|
signature ENEMY_VARIANTS =
|
||||||
sig
|
sig
|
||||||
datatype t = PATROL_SLIME
|
datatype t = PATROL_SLIME | FOLLOW_SLIME
|
||||||
end
|
end
|
||||||
|
|
||||||
structure EnemyVariants: ENEMY_VARIANTS = struct datatype t = PATROL_SLIME end
|
structure EnemyVariants: ENEMY_VARIANTS =
|
||||||
|
struct datatype t = PATROL_SLIME | FOLLOW_SLIME end
|
||||||
|
|||||||
@@ -2,6 +2,12 @@ structure Enemy =
|
|||||||
struct
|
struct
|
||||||
open GameType
|
open GameType
|
||||||
|
|
||||||
|
fun withDefaultYAxis (enemy: enemy) =
|
||||||
|
case #yAxis enemy of
|
||||||
|
ON_GROUND =>
|
||||||
|
EnemyPatch.withPatch (enemy, EnemyPatch.W_Y_AXIS FALLING)
|
||||||
|
| _ => enemy
|
||||||
|
|
||||||
fun helpExists (pos, id, collisions) =
|
fun helpExists (pos, id, collisions) =
|
||||||
if pos = Vector.length collisions then
|
if pos = Vector.length collisions then
|
||||||
false
|
false
|
||||||
@@ -40,8 +46,12 @@ struct
|
|||||||
acc
|
acc
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val enemy =
|
val enemy = withDefaultYAxis enemy
|
||||||
EnemyPatch.withPatch (enemy, EnemyPatch.W_Y_AXIS FALLING)
|
|
||||||
|
(* get patches specific to this type of enemy *)
|
||||||
|
val patches = EnemyBehaviour.getVariantPatches
|
||||||
|
(enemy, walls, wallTree, platforms, platformTree, player, [])
|
||||||
|
val enemy = EnemyPatch.withPatches (enemy, patches)
|
||||||
|
|
||||||
val patches = EnemyPhysics.getPhysicsPatches enemy
|
val patches = EnemyPhysics.getPhysicsPatches enemy
|
||||||
val patches = EnemyPatch.W_HEALTH (health - 1) :: patches
|
val patches = EnemyPatch.W_HEALTH (health - 1) :: patches
|
||||||
@@ -49,18 +59,18 @@ struct
|
|||||||
|
|
||||||
val patches = EnemyPhysics.getEnvironmentPatches
|
val patches = EnemyPhysics.getEnvironmentPatches
|
||||||
(enemy, walls, wallTree, platforms, platformTree)
|
(enemy, walls, wallTree, platforms, platformTree)
|
||||||
|
|
||||||
(* get patches specific to this type of enemy *)
|
|
||||||
val patches = EnemyBehaviour.getVariantPatches
|
|
||||||
(enemy, walls, wallTree, platforms, platformTree, player, patches)
|
|
||||||
|
|
||||||
val enemy = EnemyPatch.withPatches (enemy, patches)
|
val enemy = EnemyPatch.withPatches (enemy, patches)
|
||||||
in
|
in
|
||||||
enemy :: acc
|
enemy :: acc
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val enemy = EnemyPatch.withPatch (enemy, EnemyPatch.W_Y_AXIS FALLING)
|
val enemy = withDefaultYAxis enemy
|
||||||
|
|
||||||
|
(* get patches specific to this type of enemy *)
|
||||||
|
val patches = EnemyBehaviour.getVariantPatches
|
||||||
|
(enemy, walls, wallTree, platforms, platformTree, player, [])
|
||||||
|
val enemy = EnemyPatch.withPatches (enemy, patches)
|
||||||
|
|
||||||
val patches = EnemyPhysics.getPhysicsPatches enemy
|
val patches = EnemyPhysics.getPhysicsPatches enemy
|
||||||
val enemy = EnemyPatch.withPatches (enemy, patches)
|
val enemy = EnemyPatch.withPatches (enemy, patches)
|
||||||
@@ -68,10 +78,6 @@ struct
|
|||||||
val patches = EnemyPhysics.getEnvironmentPatches
|
val patches = EnemyPhysics.getEnvironmentPatches
|
||||||
(enemy, walls, wallTree, platforms, platformTree)
|
(enemy, walls, wallTree, platforms, platformTree)
|
||||||
|
|
||||||
(* get patches specific to this type of enemy *)
|
|
||||||
val patches = EnemyBehaviour.getVariantPatches
|
|
||||||
(enemy, walls, wallTree, platforms, platformTree, player, patches)
|
|
||||||
|
|
||||||
val enemy = EnemyPatch.withPatches (enemy, patches)
|
val enemy = EnemyPatch.withPatches (enemy, patches)
|
||||||
in
|
in
|
||||||
enemy :: acc
|
enemy :: acc
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ struct
|
|||||||
, health = 1
|
, health = 1
|
||||||
, xAxis = MOVE_LEFT
|
, xAxis = MOVE_LEFT
|
||||||
, yAxis = FALLING
|
, yAxis = FALLING
|
||||||
, variant = EnemyVariants.PATROL_SLIME
|
, variant = EnemyVariants.FOLLOW_SLIME
|
||||||
}
|
}
|
||||||
val enemy2 =
|
val enemy2 =
|
||||||
{ id = 2
|
{ id = 2
|
||||||
@@ -192,7 +192,7 @@ struct
|
|||||||
, yAxis = FALLING
|
, yAxis = FALLING
|
||||||
, variant = EnemyVariants.PATROL_SLIME
|
, variant = EnemyVariants.PATROL_SLIME
|
||||||
}
|
}
|
||||||
val enemies = Vector.fromList [enemy1, enemy2, enemy3]
|
val enemies = Vector.fromList [enemy1]
|
||||||
in
|
in
|
||||||
{ player = player
|
{ player = player
|
||||||
, walls = walls
|
, walls = walls
|
||||||
|
|||||||
Reference in New Issue
Block a user