restore working collision detection code to player's attack

This commit is contained in:
2025-08-29 10:14:28 +01:00
parent 22d43dc0c1
commit 1d1fed35cf
3 changed files with 62 additions and 54 deletions

View File

@@ -32,6 +32,10 @@ struct
val projectileDistance: Real32.real = 26.0
val projectileSizeInt = 18
val projectileHeight = 24
val projectileWidth = 96
val projectileOffsetY = playerHeight div 3
(* constants for enemy *)
val enemySize = 48
val enemySizeReal: Real32.real = 48.0

View File

@@ -52,63 +52,69 @@ struct
end)
fun helpAttackEnemies
(player, defeatedList, enemyMap, fallingMap, enemyTree, pos, boxes) =
if pos = Vector.length boxes then
let
val defeatedList = Vector.fromList defeatedList
val defeatedList = Vector.concat [defeatedList, #enemies player]
(projectileX, projectileY, enemyMap, enemyTree, fallingMap, player) =
let
val width = Constants.projectileWidth
val height = Constants.projectileHeight
val player =
PlayerPatch.withPatch (player, PlayerPatch.W_ENEMIES defeatedList)
in
(player, enemyMap, fallingMap)
end
else
let
val {x = px, y = py, ...} = player
val {x = bx, y = by} = Vector.sub (boxes, pos)
val (defeatedList, enemyMap) = PlayerAttackEnemy.foldRegion
(projectileX, projectileY, width, height, (), ([], enemyMap), enemyTree)
val x = px + bx
val y = py + by
val size = Whip.size
val fallingTree = FallingEnemies.generateTree fallingMap
val (defeatedList, fallingMap) = PlayerAttackFalling.foldRegion
( projectileX
, projectileY
, width
, height
, ()
, (defeatedList, fallingMap)
, fallingTree
)
val (defeatedList, enemyMap) = PlayerAttackEnemy.foldRegion
(x, y, size, size, (), (defeatedList, enemyMap), enemyTree)
val fallingTree = FallingEnemies.generateTree fallingMap
val (defeatedList, fallingMap) = PlayerAttackFalling.foldRegion
(x, y, size, size, (), (defeatedList, fallingMap), fallingTree)
in
helpAttackEnemies
( player
, defeatedList
, enemyMap
, fallingMap
, enemyTree
, pos + 1
, boxes
)
end
val defeatedList = Vector.fromList defeatedList
val player =
PlayerPatch.withPatch (player, PlayerPatch.W_ENEMIES defeatedList)
in
(player, enemyMap, fallingMap)
end
fun attackEnemies (player: PlayerType.player, enemyMap, enemyTree, fallingMap) =
let
open PlayerType
open EntityType
val {x, y, facing, mainAttack, ...} = player
in
case mainAttack of
MAIN_ATTACKING amt =>
let
open EntityType
val frame = amt div 2
val boxes =
case facing of
FACING_RIGHT => Vector.sub (Whip.rightFrames, frame)
| FACING_LEFT => Vector.sub (Whip.leftFrames, frame)
in
helpAttackEnemies
(player, [], enemyMap, fallingMap, enemyTree, 0, boxes)
end
MAIN_ATTACKING _ =>
(case facing of
FACING_RIGHT =>
let
val projectileX = x + Constants.playerWidth
val projectileY = y + Constants.projectileOffsetY
in
helpAttackEnemies
( projectileX
, projectileY
, enemyMap
, enemyTree
, fallingMap
, player
)
end
| FACING_LEFT =>
let
val projectileX = x - Constants.projectileWidth
val projectileY = y + Constants.projectileOffsetY
in
helpAttackEnemies
( projectileX
, projectileY
, enemyMap
, enemyTree
, fallingMap
, player
)
end)
| _ => (player, enemyMap, fallingMap)
end

View File

@@ -158,7 +158,7 @@ struct
(* adding playerWidth to x so that projectile starts from
* the rightmost pixel of the player *)
val projX = x + Constants.playerWidth
val projY = y + (Constants.playerHeight div 3)
val projY = y + Constants.projectileOffsetY
val projX = (Real32.fromInt projX + xOffset) * ratio
val projY = (Real32.fromInt projY + yOffset) * ratio
@@ -173,12 +173,10 @@ struct
val playerVec = PlayerAttackStandLeft.lerp
(rx, ry, 3.0, windowWidth, windowHeight)
(* subtracting playerHeight from x because projectile
* is the same size as the playerHeight, and it looks like
* the projectile starts from player's leftmost side this way.
* *)
val projX = x - Constants.playerHeight
val projY = y + (Constants.playerHeight div 3)
(* subtracting projectileWidth from x it looks like
* the projectile starts from player's leftmost side this way. *)
val projX = x - Constants.projectileWidth
val projY = y + Constants.projectileOffsetY
val projX = (Real32.fromInt projX + xOffset) * ratio
val projY = (Real32.fromInt projY + yOffset) * ratio