allow player to have a different width/height that is not a perfect square, (now we have Constants.playerWidth and Constants.playerHeight, having deleted the previous size in Constants.playerSize)

This commit is contained in:
2025-02-18 12:31:22 +00:00
parent e62493ce56
commit d3200745d0
7 changed files with 270 additions and 88 deletions

View File

@@ -6,10 +6,13 @@ struct
val worldHeightReal: Real32.real = 1080.0
(* constants for player *)
val playerSize = 32
val playerSizeReal: Real32.real = 32.0
val halfPlayerSize = 16
val halfPlayerSizeReal: Real32.real = 16.0
val playerWidth = 32
val playerHeight = 40
val playerWidthReal: Real32.real = 32.0
val playerHeightReal: Real32.real = 40.0
val halfPlayerWidthReal: Real32.real = 16.0
val halfPlayerHeightReal: Real32.real = 20.0
val movePlayerBy = 5
(* player timing values *)

View File

@@ -3,35 +3,6 @@ struct
open EnemyType
open EntityType
(* if player is attacking, does enemy collide with attack? *)
fun isCollidingWithPlayerAttack (player: PlayerType.player, enemy: enemy) =
let
val {x = px, y = py, facing, mainAttack, ...} = player
val pSize = Constants.playerSize
val {x = ex, y = ey, ...} = enemy
val eSize = Constants.enemySize
in
case mainAttack of
PlayerType.MAIN_ATTACKING {length, ...} =>
(case facing of
FACING_RIGHT =>
let
val px = px + pSize
in
Collision.isCollidingPlus
(px, py, length, pSize, ex, ey, eSize, eSize)
end
| FACING_LEFT =>
let
val px = px - length
in
Collision.isCollidingPlus
(px, py, length, pSize, ex, ey, eSize, eSize)
end)
| _ => false
end
fun canWalkAhead (x, y, wallTree, platformTree) =
let
val y = y + Constants.enemySize - 5
@@ -351,8 +322,8 @@ struct
fun isInFollowRange (player, enemy) =
let
val {x = px, y = py, ...} = player
val pfx = px + Constants.playerSize
val pfy = py + Constants.playerSize
val pfx = px + Constants.playerWidth
val pfy = py + Constants.playerHeight
val range = 199

View File

@@ -3,7 +3,8 @@ sig
type t
type patch
val entitySize: int
val entityWidth: int
val entityHeight: int
(* constants for physics *)
val moveBy: int
@@ -76,9 +77,9 @@ struct
fun standingOnArea (x, y, tree) =
let
val y = y + Fn.entitySize - 1
val y = y + Fn.entityHeight - 1
val width = Fn.entitySize
val width = Fn.entityWidth
val height = Platform.platHeight
val ww = Constants.worldWidth
@@ -89,9 +90,9 @@ struct
fun standingOnAreaID (x, y, tree) =
let
val y = y + Fn.entitySize - 1
val y = y + Fn.entityHeight - 1
val width = Fn.entitySize
val width = Fn.entityWidth
val height = Platform.platHeight + 2
in
@@ -100,7 +101,9 @@ struct
fun getWallPatches (x, y, walls, wallTree, acc) =
let
val size = Fn.entitySize
val entityWidth = Fn.entityWidth
val entityHeight = Fn.entityHeight
val moveBy = Fn.moveBy
val ww = Constants.worldWidth
val wh = Constants.worldHeight
@@ -126,13 +129,14 @@ struct
(* check collision with wall to the right *)
val acc =
let
val rightWallID = QuadTree.getItemID (x + size - 1, y, 1, 1, wallTree)
val rightWallID = QuadTree.getItemID
(x + entityWidth - 1, y, 1, 1, wallTree)
in
if rightWallID <> ~1 then
let
val {x = wallX, ...} = Vector.sub (walls, rightWallID - 1)
val newX = wallX - size
val newX = wallX - entityWidth
in
Fn.W_X newX :: acc
end
@@ -142,13 +146,13 @@ struct
(* check collision with wall below *)
val downWallID = QuadTree.getItemID
(x + moveBy + 1, y + size, 1, 1, wallTree)
(x + moveBy + 1, y + entityHeight, 1, 1, wallTree)
in
if downWallID <> ~1 then
let
val {y = wallY, ...} = Vector.sub (walls, downWallID - 1)
val newY = wallY - size
val newY = wallY - entityHeight
in
Fn.W_Y_AXIS ON_GROUND :: Fn.W_Y newY :: acc
end
@@ -164,7 +168,9 @@ struct
val y = Fn.getY input
val yAxis = Fn.getYAxis input
val size = Fn.entitySize
val ew = Fn.entityWidth
val eh = Fn.entityHeight
val ww = Constants.worldWidth
val wh = Constants.worldHeight
@@ -187,7 +193,7 @@ struct
val {y = platY, ...}: Platform.t =
Vector.sub (platforms, standPlatID - 1)
val newY = platY - Fn.entitySize
val newY = platY - eh
val acc = Fn.W_Y_AXIS ON_GROUND :: Fn.W_Y newY :: acc
in
acc
@@ -204,10 +210,8 @@ struct
* then set new yAxis to FALLING
* so we do not drop below any platforms again
* *)
if QuadTree.hasCollisionAt (x, y, size, size, ~1, platformTree) then
acc
else
Fn.W_Y_AXIS FALLING :: acc
if QuadTree.hasCollisionAt (x, y, ew, eh, ~1, platformTree) then acc
else Fn.W_Y_AXIS FALLING :: acc
| _ => acc
val acc = getWallPatches (x, y, walls, wallTree, acc)
@@ -222,7 +226,8 @@ structure PlayerPhysics =
type t = PlayerType.player
type patch = PlayerPatch.player_patch
val entitySize = Constants.playerSize
val entityWidth = Constants.playerWidth
val entityHeight = Constants.playerHeight
(* constants for physics *)
val moveBy = Constants.movePlayerBy
@@ -248,7 +253,8 @@ structure EnemyPhysics =
type t = EnemyType.enemy
type patch = EnemyPatch.enemy_patch
val entitySize = Constants.enemySize
val entityWidth = Constants.enemySize
val entityHeight = Constants.enemySize
(* constants for physics *)
val moveBy = Constants.moveEnemyBy

View File

@@ -60,10 +60,10 @@ struct
MAIN_ATTACKING {length, ...} =>
let
open EntityType
val height = Constants.playerSize
val height = Constants.playerHeight
val x =
(case facing of
FACING_RIGHT => x + Constants.playerSize
FACING_RIGHT => x + Constants.playerWidth
| FACING_LEFT => x - length)
val (defeatedList, enemyMap) = PlayerAttackEnemy.foldRegion

View File

@@ -0,0 +1,193 @@
structure PlayerSprite =
struct
fun lerp (startX, startY, drawWidth, drawHeight, windowWidth, windowHeight) : Real32.real vector =
let
val endY = windowHeight - startY
val startY = windowHeight - (startY + drawHeight)
val endX = startX + drawWidth
val windowHeight = windowHeight / 2.0
val windowWidth = windowWidth / 2.0
in
#[ (((startX * (1.0 - 0.3125)) + (endX * 0.3125)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.3125)) + (endX * 0.3125)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.25)) + (endX * 0.25)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.25)) + (endX * 0.25)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.25)) + (endX * 0.25)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.3125)) + (endX * 0.3125)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.6875)) + (endX * 0.6875)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.6875)) + (endX * 0.6875)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.75)) + (endX * 0.75)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.75)) + (endX * 0.75)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.75)) + (endX * 0.75)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.6875)) + (endX * 0.6875)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.0)) + (endY * 0.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.25)) + (endY * 0.25)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.25)) + (endY * 0.25)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.25)) + (endY * 0.25)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.9375)) + (endX * 0.9375)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 1.0)) + (endX * 1.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.9375)) + (endY * 0.9375)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0)) + (endX * 0.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0)) + (endX * 0.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0)) + (endX * 0.0)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 0.1875)) + (endY * 0.1875)) / windowHeight) - 1.0,
0.0,
0.0,
0.0,
(((startX * (1.0 - 0.0625)) + (endX * 0.0625)) / windowWidth) - 1.0,
(((startY * (1.0 - 1.0)) + (endY * 1.0)) / windowHeight) - 1.0,
0.0,
0.0,
0.0
]
end
end

View File

@@ -103,10 +103,11 @@ struct
Vector.fromList acc
else
let
val diff =
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
val x = Real32.fromInt x + diff
val y = Real32.fromInt y + diff
val halfProjectileSize = Constants.projectileSize / 2.0
val diffX = Constants.halfPlayerWidthReal - halfProjectileSize
val diffY = Constants.halfPlayerHeightReal - halfProjectileSize
val x = Real32.fromInt x + diffX
val y = Real32.fromInt y + diffY
val {angle} = Vector.sub (defeteadEnemies, pos)
val angle = degreesToRadians angle
@@ -343,8 +344,8 @@ struct
* and then chose appropriate direction to recoil in *)
let
val {x, ...} = player
val pFinishX = x + Constants.playerSize
val pHalfW = Constants.playerSize div 2
val pFinishX = x + Constants.playerWidth
val pHalfW = Constants.playerWidth div 2
val pCentreX = x + pHalfW
in
case EnemyMap.get (enemyID, enemies) of
@@ -433,11 +434,12 @@ struct
| _ =>
let
val {x, y, ...} = player
val size = Constants.playerSize
val ew = Constants.playerWidth
val eh = Constants.playerHeight
val env = (enemies, player)
val state = []
val patches = FoldEnemies.foldRegion
(x, y, size, size, env, state, enemyTree)
(x, y, ew, eh, env, state, enemyTree)
in
PlayerPatch.withPatches (player, patches)
end
@@ -492,35 +494,36 @@ struct
(*** DRAWING FUNCTIONS ***)
(* block is placeholder asset *)
fun helpGetDrawVec (x, y, size, width, height, attacked, mainAttack) =
fun helpGetDrawVec
(x, y, realWidth, realHeight, width, height, attacked, mainAttack) =
case mainAttack of
MAIN_NOT_ATTACKING =>
(case attacked of
NOT_ATTACKED =>
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
| ATTACKED amt =>
if amt mod 5 = 0 then
Block.lerp (x, y, size, size, width, height, 0.9, 0.9, 0.9)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
else
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5))
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height))
| MAIN_THROWING =>
(case attacked of
NOT_ATTACKED =>
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
| ATTACKED amt =>
if amt mod 5 = 0 then
Block.lerp (x, y, size, size, width, height, 0.9, 0.9, 0.9)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
else
Block.lerp (x, y, size, size, width, height, 0.5, 0.5, 0.5))
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height))
| MAIN_ATTACKING _ =>
(case attacked of
NOT_ATTACKED =>
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
| ATTACKED amt =>
if amt mod 5 = 0 then
Block.lerp (x, y, size, size, width, height, 1.0, 0.9, 0.9)
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height)
else
Block.lerp (x, y, size, size, width, height, 1.0, 0.5, 0.5))
PlayerSprite.lerp (x, y, realWidth, realHeight, width, height))
fun getDrawVec (player: player, width, height) =
let
@@ -539,9 +542,11 @@ struct
val x = Real32.fromInt x * wratio
val y = Real32.fromInt y * wratio + yOffset
val realSize = Constants.playerSizeReal * wratio
val realWidth = Constants.playerWidthReal * wratio
val realHeight = Constants.playerHeightReal * wratio
in
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
helpGetDrawVec
(x, y, realWidth, realHeight, width, height, attacked, mainAttack)
end
else
let
@@ -554,9 +559,11 @@ struct
val x = Real32.fromInt x * hratio + xOffset
val y = Real32.fromInt y * hratio
val realSize = Constants.playerSizeReal * hratio
val realWidth = Constants.playerWidthReal * hratio
val realHeight = Constants.playerHeightReal * hratio
in
helpGetDrawVec (x, y, realSize, width, height, attacked, mainAttack)
helpGetDrawVec
(x, y, realWidth, realHeight, width, height, attacked, mainAttack)
end
end
@@ -569,7 +576,7 @@ struct
val hratio = height / Constants.worldHeightReal
val x =
case #facing player of
FACING_RIGHT => x + Constants.playerSize
FACING_RIGHT => x + Constants.playerWidth
| FACING_LEFT => x - length
in
if wratio < hratio then
@@ -584,7 +591,7 @@ struct
val y = Real32.fromInt y * wratio + yOffset
val realLength = Real32.fromInt length * wratio
val realSize = Constants.playerSizeReal * wratio
val realHeight = Constants.playerHeightReal * wratio
val {charge, ...} = player
val alpha = Real32.fromInt charge / 60.0
@@ -592,10 +599,10 @@ struct
case facing of
FACING_RIGHT =>
ChainEdgeRight.lerp
(x, y, realLength, realSize, width, height, 0.5, 0.5, 0.5)
(x, y, realLength, realHeight, width, height, 0.5, 0.5, 0.5)
| FACING_LEFT =>
ChainEdgeLeft.lerp
(x, y, realLength, realSize, width, height, 0.5, 0.5, 0.5)
(x, y, realLength, realHeight, width, height, 0.5, 0.5, 0.5)
end
else
let
@@ -609,7 +616,7 @@ struct
val y = Real32.fromInt y * hratio
val realLength = Real32.fromInt length * hratio
val realSize = Constants.playerSizeReal * hratio
val realHeight = Constants.playerHeightReal * hratio
val {charge, ...} = player
val alpha = Real32.fromInt charge / 60.0
@@ -617,10 +624,10 @@ struct
case facing of
FACING_RIGHT =>
ChainEdgeRight.lerp
(x, y, realLength, realSize, width, height, 0.5, 0.5, 0.5)
(x, y, realLength, realHeight, width, height, 0.5, 0.5, 0.5)
| FACING_LEFT =>
ChainEdgeLeft.lerp
(x, y, realLength, realSize, width, height, 0.5, 0.5, 0.5)
(x, y, realLength, realHeight, width, height, 0.5, 0.5, 0.5)
end
end
| _ => Vector.fromList []
@@ -692,10 +699,11 @@ struct
val {x, y, enemies, ...} = player
(* get centre (x, y) coordinates of player *)
val diff =
Constants.halfPlayerSizeReal - (Constants.projectileSize / 2.0)
val x = Real32.fromInt x + diff
val y = Real32.fromInt y + diff
val halfProjectileSize = Constants.projectileSize / 2.0
val diffX = Constants.halfPlayerWidthReal - halfProjectileSize
val diffY = Constants.halfPlayerHeightReal - halfProjectileSize
val x = Real32.fromInt x + diffX
val y = Real32.fromInt y + diffY
val wratio = width / Constants.worldWidthReal
val hratio = height / Constants.worldHeightReal

View File

@@ -17,6 +17,7 @@ ann
"allowVectorExps true"
in
fcore/block.sml
fcore/player/player-sprite.sml
fcore/field.sml
fcore/chain-edge.sml
end