From 05cc1d3a46b664226fe26e77efd29fa382bf727e Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 19 Dec 2024 03:08:26 +0000 Subject: [PATCH] begin coding enemy --- cat | 0 fcore/enemy.sml | 71 +++++++++++++++++++++++++++++++++++++++++++ fcore/game-type.sml | 14 +++++++++ fcore/game-update.sml | 5 ++- oms.mlb | 1 + shell/gl-draw.sml | 3 ++ 6 files changed, 93 insertions(+), 1 deletion(-) delete mode 100644 cat create mode 100644 fcore/enemy.sml diff --git a/cat b/cat deleted file mode 100644 index e69de29..0000000 diff --git a/fcore/enemy.sml b/fcore/enemy.sml new file mode 100644 index 0000000..a3c690a --- /dev/null +++ b/fcore/enemy.sml @@ -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 diff --git a/fcore/game-type.sml b/fcore/game-type.sml index fbf1e6b..60dbee5 100644 --- a/fcore/game-type.sml +++ b/fcore/game-type.sml @@ -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 diff --git a/fcore/game-update.sml b/fcore/game-update.sml index a9ccb75..ac1a644 100644 --- a/fcore/game-update.sml +++ b/fcore/game-update.sml @@ -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 diff --git a/oms.mlb b/oms.mlb index 100947f..54ec3af 100644 --- a/oms.mlb +++ b/oms.mlb @@ -11,6 +11,7 @@ end fcore/wall.sml fcore/platform.sml +fcore/enemy.sml fcore/game-type.sml fcore/player.sml diff --git a/shell/gl-draw.sml b/shell/gl-draw.sml index 328a9f5..0c36191 100644 --- a/shell/gl-draw.sml +++ b/shell/gl-draw.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]