From d0d93780fb15534870d9d0e54a26b0da3de49ee8 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Tue, 11 Feb 2025 19:38:02 +0000 Subject: [PATCH] initial implementation of bat's update function (todo: make bat oscillate up and down while it also moves in x axis, and also code bat's collisions with player, adding to falling list if necessary, or being filtered by player's attack) --- fcore/constants.sml | 1 + fcore/enemy/enemy-behaviour.sml | 59 +++++++++++++++++++++++++++++++++ fcore/enemy/enemy-patch.sml | 15 +++++++++ fcore/game-type.sml | 4 +-- 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/fcore/constants.sml b/fcore/constants.sml index bea9dab..9e64e40 100644 --- a/fcore/constants.sml +++ b/fcore/constants.sml @@ -30,6 +30,7 @@ struct val enemySize = 35 val enemySizeReal: Real32.real = 35.0 val moveEnemyBy = 3 + val batRestLimit = 155 val moveProjectileBy = 11 end diff --git a/fcore/enemy/enemy-behaviour.sml b/fcore/enemy/enemy-behaviour.sml index b250360..8abeec1 100644 --- a/fcore/enemy/enemy-behaviour.sml +++ b/fcore/enemy/enemy-behaviour.sml @@ -499,6 +499,55 @@ struct end end + fun updateStraightBat + (player, enemy, walls, wallTree, projectileTree, enemyList, fallingList) = + let + val {x, y, batRest, ...} = enemy + val size = Constants.enemySize + in + if QuadTree.hasCollisionAt (x, y, size, size, ~1, wallTree) then + (* has collision with wall *) + let + val enemy = + if batRest >= Constants.batRestLimit then + (* make enemy move in opposite direction *) + case #xAxis enemy of + MOVE_RIGHT => + EnemyPatch.withPatches + ( enemy + , [ EnemyPatch.W_X_AXIS MOVE_LEFT + , EnemyPatch.W_X (x - (Constants.moveEnemyBy * 9)) + ] + ) + | MOVE_LEFT => + EnemyPatch.withPatches + ( enemy + , [ EnemyPatch.W_X_AXIS MOVE_RIGHT + , EnemyPatch.W_X (x + (Constants.moveEnemyBy * 9)) + ] + ) + | _ => enemy + else + (* keep resting until we hit rest limit *) + EnemyPatch.withPatch (enemy, EnemyPatch.W_BAT_REST (batRest + 1)) + in + (enemy :: enemyList, fallingList) + end + else + (* no collision, so continue moving in direction *) + let + val patches = + case #xAxis enemy of + MOVE_RIGHT => [EnemyPatch.W_X (x + Constants.moveEnemyBy)] + | MOVE_LEFT => [EnemyPatch.W_X (x - Constants.moveEnemyBy)] + | STAY_STILL => [] + val patches = EnemyPatch.W_BAT_REST 0 :: patches + val enemy = EnemyPatch.withPatches (enemy, patches) + in + (enemy :: enemyList, fallingList) + end + end + fun updateEnemyState ( enemy , projectiles @@ -541,5 +590,15 @@ struct , enemyList , fallingList ) + | STRAIGHT_BAT => + updateStraightBat + ( player + , enemy + , walls + , wallTree + , projectileTree + , enemyList + , fallingList + ) end end diff --git a/fcore/enemy/enemy-patch.sml b/fcore/enemy/enemy-patch.sml index 5ed6b58..91faac2 100644 --- a/fcore/enemy/enemy-patch.sml +++ b/fcore/enemy/enemy-patch.sml @@ -8,6 +8,7 @@ sig | W_Y_AXIS of GameType.y_axis | W_PLAT_ID of int | W_NEXT_PLAT_ID of int + | W_BAT_REST of int val withPatch: GameType.enemy * enemy_patch -> GameType.enemy @@ -24,6 +25,7 @@ struct | W_Y_AXIS of GameType.y_axis | W_PLAT_ID of int | W_NEXT_PLAT_ID of int + | W_BAT_REST of int fun mkEnemy (id, health, x, y, xAxis, yAxis, variant, platID, nextPlatID, batRest) = @@ -136,6 +138,19 @@ struct , nextPlatID , batRest ) + | W_BAT_REST batRest => + mkEnemy + ( id + , health + , x + , y + , xAxis + , yAxis + , variant + , platID + , nextPlatID + , batRest + ) end fun withPatches (enemy, lst) = diff --git a/fcore/game-type.sml b/fcore/game-type.sml index 7a4a0db..2dcf138 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -217,9 +217,9 @@ struct , x = 751 , y = 555 , health = 1 - , xAxis = STAY_STILL + , xAxis = MOVE_RIGHT , yAxis = FALLING - , variant = EnemyVariants.FOLLOW_SLIME + , variant = EnemyVariants.STRAIGHT_BAT , platID = ~1 , nextPlatID = ~1 , batRest = 0