a little bit of refactoring
This commit is contained in:
@@ -3,14 +3,31 @@ struct
|
|||||||
val size = 35
|
val size = 35
|
||||||
val realSize = 35.0
|
val realSize = 35.0
|
||||||
|
|
||||||
|
(* called when filtering enemies,
|
||||||
|
* to adjust enemy data on collision with projectile *)
|
||||||
|
fun onCollisionWithProjectile (enemy, projectileTree, acc) =
|
||||||
|
let
|
||||||
|
val {x, y, health, id} = enemy
|
||||||
|
val hasCollision = QuadTree.hasCollisionAt
|
||||||
|
(x, y, size, size, 0, 0, 1920, 1080, ~1, projectileTree)
|
||||||
|
in
|
||||||
|
if hasCollision then
|
||||||
|
if health = 1 then
|
||||||
|
(* filter out if decrementing health by one = 0 *)
|
||||||
|
acc
|
||||||
|
else
|
||||||
|
{health = health - 1, x = x, y = y, id = id} :: acc
|
||||||
|
else
|
||||||
|
enemy :: acc
|
||||||
|
end
|
||||||
|
|
||||||
fun helpGenerateTree (pos, enemyVec, acc) =
|
fun helpGenerateTree (pos, enemyVec, acc) =
|
||||||
if pos = Vector.length enemyVec then
|
if pos = Vector.length enemyVec then
|
||||||
acc
|
acc
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val {id, x, y, health = _} = Vector.sub (enemyVec, pos)
|
val {id, x, y, health = _} = Vector.sub (enemyVec, pos)
|
||||||
val acc = QuadTree.insert
|
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, id, acc)
|
||||||
(x, y, size, size, 0, 0, 1920, 1080, id, acc)
|
|
||||||
in
|
in
|
||||||
helpGenerateTree (pos + 1, enemyVec, acc)
|
helpGenerateTree (pos + 1, enemyVec, acc)
|
||||||
end
|
end
|
||||||
@@ -24,12 +41,9 @@ struct
|
|||||||
val enemy = Vector.sub (vec, mid)
|
val enemy = Vector.sub (vec, mid)
|
||||||
val {id = curNum, x = _, y = _, health = _} = enemy
|
val {id = curNum, x = _, y = _, health = _} = enemy
|
||||||
in
|
in
|
||||||
if curNum = findNum then
|
if curNum = findNum then enemy
|
||||||
enemy
|
else if curNum < findNum then helpFind (findNum, vec, mid + 1, high)
|
||||||
else if curNum < findNum then
|
else helpFind (findNum, vec, low, mid - 1)
|
||||||
helpFind (findNum, vec, mid + 1, high)
|
|
||||||
else
|
|
||||||
helpFind (findNum, vec, low, mid - 1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
fun find (findNum, vec) =
|
fun find (findNum, vec) =
|
||||||
@@ -84,6 +98,6 @@ struct
|
|||||||
getDrawVecLoop (pos + 1, enemies, width, height, acc)
|
getDrawVecLoop (pos + 1, enemies, width, height, acc)
|
||||||
end
|
end
|
||||||
|
|
||||||
fun getDrawVec (enemies, width, height) =
|
fun getDrawVec (enemies, width, height) =
|
||||||
getDrawVecLoop (0, enemies, width, height, [])
|
getDrawVecLoop (0, enemies, width, height, [])
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -68,28 +68,10 @@ struct
|
|||||||
let
|
let
|
||||||
val enemy = Vector.sub (enemies, pos)
|
val enemy = Vector.sub (enemies, pos)
|
||||||
val acc =
|
val acc =
|
||||||
if exists (#id enemy, collisions) then
|
if exists (#id enemy, collisions) then (* filter out *) acc
|
||||||
(* filter out *)
|
else Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
|
||||||
acc
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val {x, y, health, id} = enemy
|
|
||||||
val eSize = Enemy.size
|
|
||||||
val hasCollision = QuadTree.hasCollisionAt
|
|
||||||
(x, y, eSize, eSize, 0, 0, 1920, 1080, ~1, projectileTree)
|
|
||||||
in
|
|
||||||
if hasCollision then
|
|
||||||
if health = 1 then
|
|
||||||
(* filter out if decrementing health by one = 0 *)
|
|
||||||
acc
|
|
||||||
else
|
|
||||||
{health = health - 1, x = x, y = y, id = id} :: acc
|
|
||||||
else
|
|
||||||
enemy :: acc
|
|
||||||
end
|
|
||||||
in
|
in
|
||||||
filterEnemyAttacked
|
filterEnemyAttacked (pos - 1, collisions, enemies, projectileTree, acc)
|
||||||
(pos - 1, collisions, enemies, projectileTree, acc)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(* filter enemy projectiles when player is not attacking *)
|
(* filter enemy projectiles when player is not attacking *)
|
||||||
@@ -99,20 +81,7 @@ struct
|
|||||||
else
|
else
|
||||||
let
|
let
|
||||||
val enemy = Vector.sub (enemies, pos)
|
val enemy = Vector.sub (enemies, pos)
|
||||||
val {x, y, health, id} = enemy
|
val acc = Enemy.onCollisionWithProjectile (enemy, projectileTree, acc)
|
||||||
val eSize = Enemy.size
|
|
||||||
val hasCollision = QuadTree.hasCollisionAt
|
|
||||||
(x, y, eSize, eSize, 0, 0, 1920, 1080, ~1, projectileTree)
|
|
||||||
|
|
||||||
val acc =
|
|
||||||
if hasCollision then
|
|
||||||
if health = 1 then
|
|
||||||
(* filter out if decrementing health by one = 0 *)
|
|
||||||
acc
|
|
||||||
else
|
|
||||||
{health = health - 1, x = x, y = y, id = id} :: acc
|
|
||||||
else
|
|
||||||
enemy :: acc
|
|
||||||
in
|
in
|
||||||
filterEnemyProjectiles (pos - 1, enemies, projectileTree, acc)
|
filterEnemyProjectiles (pos - 1, enemies, projectileTree, acc)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1008,60 +1008,4 @@ struct
|
|||||||
(x, y, 0, enemies, width, height, hratio, xOffset, 0.0, [])
|
(x, y, 0, enemies, width, height, hratio, xOffset, 0.0, [])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
fun helpGetProjectileVec
|
|
||||||
(pos, projectiles, width, height, ratio, xOffset, yOffset, acc) =
|
|
||||||
if pos = Vector.length projectiles then
|
|
||||||
Vector.concat acc
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val {x, y, ...} = Vector.sub (projectiles, pos)
|
|
||||||
|
|
||||||
val x = Real32.fromInt x * ratio + xOffset
|
|
||||||
val y = Real32.fromInt y * ratio + yOffset
|
|
||||||
|
|
||||||
val defeatedSize = defeatedSize * ratio
|
|
||||||
|
|
||||||
val vec = Field.lerp
|
|
||||||
(x, y, defeatedSize, defeatedSize, width, height, 0.3, 0.9, 0.3, 1.0)
|
|
||||||
val acc = vec :: acc
|
|
||||||
in
|
|
||||||
helpGetProjectileVec
|
|
||||||
(pos + 1, projectiles, width, height, ratio, xOffset, yOffset, acc)
|
|
||||||
end
|
|
||||||
|
|
||||||
fun getProjectileVec (player: player, width, height) =
|
|
||||||
let
|
|
||||||
val {x, y, projectiles, ...} = player
|
|
||||||
|
|
||||||
val wratio = width / 1920.0
|
|
||||||
val hratio = height / 1080.0
|
|
||||||
in
|
|
||||||
if wratio < hratio then
|
|
||||||
let
|
|
||||||
val scale = 1080.0 * wratio
|
|
||||||
val yOffset =
|
|
||||||
if height > scale then (height - scale) / 2.0
|
|
||||||
else if height < scale then (scale - height) / 2.0
|
|
||||||
else 0.0
|
|
||||||
|
|
||||||
val xOffset = 0.0
|
|
||||||
in
|
|
||||||
helpGetProjectileVec
|
|
||||||
(0, projectiles, width, height, wratio, xOffset, yOffset, [])
|
|
||||||
end
|
|
||||||
else
|
|
||||||
let
|
|
||||||
val scale = 1920.0 * hratio
|
|
||||||
val xOffset =
|
|
||||||
if width > scale then (width - scale) / 2.0
|
|
||||||
else if width < scale then (scale - width) / 2.0
|
|
||||||
else 0.0
|
|
||||||
|
|
||||||
val yOffset = 0.0
|
|
||||||
in
|
|
||||||
helpGetProjectileVec
|
|
||||||
(0, projectiles, width, height, hratio, xOffset, yOffset, [])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
structure Projectile =
|
structure Projectile =
|
||||||
struct
|
struct
|
||||||
|
val projectileSize = 9.0
|
||||||
|
val projectileSizeInt = 9
|
||||||
|
|
||||||
fun helpGenerateTree (pos, projectiles, acc) =
|
fun helpGenerateTree (pos, projectiles, acc) =
|
||||||
if pos = Vector.length projectiles then
|
if pos = Vector.length projectiles then
|
||||||
acc
|
acc
|
||||||
else
|
else
|
||||||
let
|
let
|
||||||
val size = Player.defeatedSizeInt
|
val size = projectileSizeInt
|
||||||
|
|
||||||
val {x, y, facing = _} = Vector.sub (projectiles, pos)
|
val {x, y, facing = _} = Vector.sub (projectiles, pos)
|
||||||
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, pos, acc)
|
val acc = QuadTree.insert (x, y, size, size, 0, 0, 1920, 1080, pos, acc)
|
||||||
@@ -15,4 +18,60 @@ struct
|
|||||||
|
|
||||||
fun generateTree projectiles =
|
fun generateTree projectiles =
|
||||||
helpGenerateTree (0, projectiles, QuadTree.empty)
|
helpGenerateTree (0, projectiles, QuadTree.empty)
|
||||||
|
|
||||||
|
fun helpGetProjectileVec
|
||||||
|
(pos, projectiles, width, height, ratio, xOffset, yOffset, acc) =
|
||||||
|
if pos = Vector.length projectiles then
|
||||||
|
Vector.concat acc
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val {x, y, ...} = Vector.sub (projectiles, pos)
|
||||||
|
|
||||||
|
val x = Real32.fromInt x * ratio + xOffset
|
||||||
|
val y = Real32.fromInt y * ratio + yOffset
|
||||||
|
|
||||||
|
val size = projectileSize * ratio
|
||||||
|
|
||||||
|
val vec = Field.lerp
|
||||||
|
(x, y, size, size, width, height, 0.3, 0.9, 0.3, 1.0)
|
||||||
|
val acc = vec :: acc
|
||||||
|
in
|
||||||
|
helpGetProjectileVec
|
||||||
|
(pos + 1, projectiles, width, height, ratio, xOffset, yOffset, acc)
|
||||||
|
end
|
||||||
|
|
||||||
|
fun getProjectileVec (player: GameType.player, width, height) =
|
||||||
|
let
|
||||||
|
val {projectiles, ...} = player
|
||||||
|
|
||||||
|
val wratio = width / 1920.0
|
||||||
|
val hratio = height / 1080.0
|
||||||
|
in
|
||||||
|
if wratio < hratio then
|
||||||
|
let
|
||||||
|
val scale = 1080.0 * wratio
|
||||||
|
val yOffset =
|
||||||
|
if height > scale then (height - scale) / 2.0
|
||||||
|
else if height < scale then (scale - height) / 2.0
|
||||||
|
else 0.0
|
||||||
|
|
||||||
|
val xOffset = 0.0
|
||||||
|
in
|
||||||
|
helpGetProjectileVec
|
||||||
|
(0, projectiles, width, height, wratio, xOffset, yOffset, [])
|
||||||
|
end
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val scale = 1920.0 * hratio
|
||||||
|
val xOffset =
|
||||||
|
if width > scale then (width - scale) / 2.0
|
||||||
|
else if width < scale then (scale - width) / 2.0
|
||||||
|
else 0.0
|
||||||
|
|
||||||
|
val yOffset = 0.0
|
||||||
|
in
|
||||||
|
helpGetProjectileVec
|
||||||
|
(0, projectiles, width, height, hratio, xOffset, yOffset, [])
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -247,8 +247,8 @@ struct
|
|||||||
|
|
||||||
(* temp *)
|
(* temp *)
|
||||||
val pelletVec = Player.getPelletVec (#player game, width, height)
|
val pelletVec = Player.getPelletVec (#player game, width, height)
|
||||||
val projectileVec =
|
val projectileVec =
|
||||||
Player.getProjectileVec (#player game, width, height)
|
Projectile.getProjectileVec (#player game, width, height)
|
||||||
val fieldVec = Vector.concat [pelletVec, projectileVec, fieldVec]
|
val fieldVec = Vector.concat [pelletVec, projectileVec, fieldVec]
|
||||||
|
|
||||||
val shellState = uploadWall (shellState, wallVec)
|
val shellState = uploadWall (shellState, wallVec)
|
||||||
|
|||||||
Reference in New Issue
Block a user