From e2f5f180896110056aafb875cff055aa8633b4fa Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Wed, 29 Jan 2025 02:23:44 +0000 Subject: [PATCH] when enemy jumps or drops without needing to move horizontally to reach platform, tell enemy to stay still so we don't risk falling off platform --- fcore/enemy-behaviour.sml | 64 ++++++++++++++++++++++++--------------- fcore/player.sml | 4 --- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/fcore/enemy-behaviour.sml b/fcore/enemy-behaviour.sml index 53f763b..bf81fe5 100644 --- a/fcore/enemy-behaviour.sml +++ b/fcore/enemy-behaviour.sml @@ -176,10 +176,16 @@ struct isBetween (platX, ecx, platFinishX) then (* can jump from same position enemy is at *) - case eyAxis of - ON_GROUND => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc - | FALLING => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc - | _ => acc + let + (* since we want to jump vertically and not risk falling off by + * jumping + moving either left or right, make enemy stay still *) + val acc = EnemyPatch.W_X_AXIS STAY_STILL :: acc + in + case eyAxis of + ON_GROUND => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc + | FALLING => EnemyPatch.W_Y_AXIS (JUMPING 0) :: acc + | _ => acc + end else (* have to travel either left or right before jumping *) if ecx < platX then @@ -219,10 +225,14 @@ struct isBetween (platX, ecx, platFinishX) then (* can jump from same position enemy is at *) - case eyAxis of - ON_GROUND => EnemyPatch.W_Y_AXIS DROP_BELOW_PLATFORM :: acc - | FALLING => EnemyPatch.W_Y_AXIS DROP_BELOW_PLATFORM :: acc - | _ => acc + let + val acc = EnemyPatch.W_X_AXIS STAY_STILL :: acc + in + case eyAxis of + ON_GROUND => EnemyPatch.W_Y_AXIS DROP_BELOW_PLATFORM :: acc + | FALLING => EnemyPatch.W_Y_AXIS DROP_BELOW_PLATFORM :: acc + | _ => acc + end else (* have to travel either left or right before jumping *) if ecx < platX then @@ -420,24 +430,28 @@ struct (* if only one side in x direction overlaps with platform, * then move enemy left/right to make them fully overlap with platform *) fun getHorizontalLandingPatches (enemy, nextPlatform, acc) = - let - val {x = px, width = pw, ...} = nextPlatform - val pfx = px + pw - - val {x = ex, ...} = enemy - val efx = ex + Constants.enemySize - in - if isBetween (px, ex, pfx) andalso isBetween (px, efx, pfx) then + case #xAxis enemy of + STAY_STILL => acc - else - let - val startDiff = abs (px - ex) - val endDiff = abs (pfx - efx) - in - if startDiff > endDiff then EnemyPatch.W_X_AXIS MOVE_LEFT :: acc - else EnemyPatch.W_X_AXIS MOVE_RIGHT :: acc - end - end + | _ => + let + val {x = px, width = pw, ...} = nextPlatform + val pfx = px + pw + + val {x = ex, ...} = enemy + val efx = ex + Constants.enemySize + in + if isBetween (px, ex, pfx) andalso isBetween (px, efx, pfx) then + acc + else + let + val startDiff = abs (px - ex) + val endDiff = abs (pfx - efx) + in + if startDiff > endDiff then EnemyPatch.W_X_AXIS MOVE_LEFT :: acc + else EnemyPatch.W_X_AXIS MOVE_RIGHT :: acc + end + end fun getFallingPatches (enemy, newPlatformID, platforms, acc) = let diff --git a/fcore/player.sml b/fcore/player.sml index a14f355..52c9019 100644 --- a/fcore/player.sml +++ b/fcore/player.sml @@ -320,10 +320,6 @@ struct let val player = #player game - val _ = print - ("(playerX, playerY) = (" ^ Int.toString (#x player) ^ ", " - ^ Int.toString (#y player) ^ ")\n") - val patches = getProjectilePatches player val player = PlayerPatch.withPatches (player, patches)