begin coding enemy
This commit is contained in:
71
fcore/enemy.sml
Normal file
71
fcore/enemy.sml
Normal file
@@ -0,0 +1,71 @@
|
||||
structure Enemy =
|
||||
struct
|
||||
val size = 35
|
||||
val realSize = 35.0
|
||||
|
||||
fun helpGenerateTree (pos, enemyVec, acc) =
|
||||
if pos = Vector.length enemyVec then
|
||||
acc
|
||||
else
|
||||
let
|
||||
val {id, x, y, health = _} = Vector.sub (enemyVec, pos)
|
||||
val acc = QuadTree.insert
|
||||
(x, y, size, size, 0, 0, 1920, 1080, id, acc)
|
||||
in
|
||||
helpGenerateTree (pos + 1, enemyVec, acc)
|
||||
end
|
||||
|
||||
fun generateTree enemyVec = helpGenerateTree (0, enemyVec, QuadTree.empty)
|
||||
|
||||
fun helpGetDrawVec ({x, y, id = _, health = _}, width, height) =
|
||||
let
|
||||
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 x = Real32.fromInt x * wratio
|
||||
val y = Real32.fromInt y * wratio + yOffset
|
||||
|
||||
val realSize = realSize * wratio
|
||||
in
|
||||
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0)
|
||||
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 x = Real32.fromInt x * hratio + xOffset
|
||||
val y = Real32.fromInt y * hratio
|
||||
|
||||
val realSize = realSize * hratio
|
||||
in
|
||||
Block.lerp (x, y, realSize, realSize, width, height, 0.5, 0.5, 1.0)
|
||||
end
|
||||
end
|
||||
|
||||
fun getDrawVecLoop (pos, enemies, width, height, acc) =
|
||||
if pos = Vector.length enemies then
|
||||
Vector.concat acc
|
||||
else
|
||||
let
|
||||
val e = Vector.sub (enemies, pos)
|
||||
val hd = helpGetDrawVec (e, width, height)
|
||||
val acc = hd :: acc
|
||||
in
|
||||
getDrawVecLoop (pos + 1, enemies, width, height, acc)
|
||||
end
|
||||
|
||||
fun getDrawVec (enemies, width, height) =
|
||||
getDrawVecLoop (0, enemies, width, height, [])
|
||||
end
|
||||
@@ -22,12 +22,16 @@ sig
|
||||
, jumpPressed: bool
|
||||
}
|
||||
|
||||
type enemy = {id: int, health: int, x: int, y: int}
|
||||
|
||||
type game_type =
|
||||
{ player: player
|
||||
, walls: wall vector
|
||||
, wallTree: QuadTree.t
|
||||
, platforms: platform vector
|
||||
, platformTree: QuadTree.t
|
||||
, enemies: enemy vector
|
||||
, enemyTree: QuadTree.t
|
||||
}
|
||||
|
||||
val initial: game_type
|
||||
@@ -58,12 +62,16 @@ struct
|
||||
, jumpPressed: bool
|
||||
}
|
||||
|
||||
type enemy = {id: int, health: int, x: int, y: int}
|
||||
|
||||
type game_type =
|
||||
{ player: player
|
||||
, walls: wall vector
|
||||
, wallTree: QuadTree.t
|
||||
, platforms: platform vector
|
||||
, platformTree: QuadTree.t
|
||||
, enemies: enemy vector
|
||||
, enemyTree: QuadTree.t
|
||||
}
|
||||
|
||||
val initial: game_type =
|
||||
@@ -86,12 +94,18 @@ struct
|
||||
val plat1 = {id = 1, x = 155, y = 911, width = 155}
|
||||
val platforms = Vector.fromList [plat1]
|
||||
val platformTree = Platform.generateTree platforms
|
||||
|
||||
val enemy1 = {id = 1, x = 300, y = 945, health = 5}
|
||||
val enemies = Vector.fromList [enemy1]
|
||||
val enemyTree = Enemy.generateTree enemies
|
||||
in
|
||||
{ player = player
|
||||
, walls = walls
|
||||
, wallTree = wallTree
|
||||
, platforms = platforms
|
||||
, platformTree = platformTree
|
||||
, enemies = enemies
|
||||
, enemyTree = enemyTree
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,8 @@ structure GameUpdate =
|
||||
struct
|
||||
fun update (game, input) =
|
||||
let
|
||||
val {player, walls, wallTree, platforms, platformTree} = game
|
||||
val {player, walls, wallTree, platforms, platformTree, enemies, enemyTree} =
|
||||
game
|
||||
val player = Player.move (game, input)
|
||||
in
|
||||
{ player = player
|
||||
@@ -10,6 +11,8 @@ struct
|
||||
, wallTree = wallTree
|
||||
, platforms = platforms
|
||||
, platformTree = platformTree
|
||||
, enemies = enemies
|
||||
, enemyTree = enemyTree
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
1
oms.mlb
1
oms.mlb
@@ -11,6 +11,7 @@ end
|
||||
|
||||
fcore/wall.sml
|
||||
fcore/platform.sml
|
||||
fcore/enemy.sml
|
||||
fcore/game-type.sml
|
||||
|
||||
fcore/player.sml
|
||||
|
||||
@@ -160,6 +160,9 @@ struct
|
||||
val game = GameUpdate.update (game, input)
|
||||
|
||||
val playerVec = Player.getDrawVec (#player game, width, height)
|
||||
val enemyVec = Enemy.getDrawVec (#enemies game, width, height)
|
||||
val playerVec = Vector.concat [playerVec, enemyVec]
|
||||
|
||||
val wallVec = Wall.getDrawVec (#walls game, width, height)
|
||||
val platVec = Platform.getDrawVec (#platforms game, width, height)
|
||||
val wallVec = Vector.concat [wallVec, platVec]
|
||||
|
||||
Reference in New Issue
Block a user