From 66b58aa71dcd02ae80d7fa9b0c21eeefa34c83c1 Mon Sep 17 00:00:00 2001 From: VisualGMQ <2142587070@qq.com> Date: Sun, 31 Jul 2022 23:51:12 +0800 Subject: [PATCH] first playable version released --- .gitignore | 3 +- game/constants.lua | 2 + game/content.lua | 13 +++++ game/ecs.lua | 4 -- game/main.lua | 100 ++++++++++++++++++++++++++++++++- game/resources/RestartHint.png | Bin 0 -> 678 bytes game/resources/StartHint.png | Bin 0 -> 645 bytes game/vmath.lua | 2 +- 8 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 game/resources/RestartHint.png create mode 100644 game/resources/StartHint.png diff --git a/.gitignore b/.gitignore index ef79c2c..f2bfc24 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ compile_flags.txt game/shader game/*.dll game/*.dll.a -game/*.exe \ No newline at end of file +game/*.exe +*.zip \ No newline at end of file diff --git a/game/constants.lua b/game/constants.lua index 86c211e..4a625a3 100644 --- a/game/constants.lua +++ b/game/constants.lua @@ -16,6 +16,8 @@ _M.RoleColliBox = { x = 9, y = 0, } +_M.MonsterBirthInterval = 1 +_M.MonsterBirthInitNum = 1 _M.Invincible = 1 _M.BulletColliBox = { x = 10, diff --git a/game/content.lua b/game/content.lua index 926aa2c..5ee6eef 100644 --- a/game/content.lua +++ b/game/content.lua @@ -4,6 +4,15 @@ local _M = {} ---@type Texture _M.Texture = nil +---@type Texture +_M.RestartHintTexture = nil + +---@type Texture +_M.StartHintTexture = nil + +---@type boolean +_M.IsStartGame = false + ---@type TileSheet _M.Tilesheet = nil @@ -16,4 +25,8 @@ _M.BulletList = {} ---@type table _M.MonsterList = {} +_M.MonsterBirthNum = 0 + +_M.MonsterBirthCountDown = 0 + return _M \ No newline at end of file diff --git a/game/ecs.lua b/game/ecs.lua index e13c37d..b3b62f6 100644 --- a/game/ecs.lua +++ b/game/ecs.lua @@ -447,10 +447,7 @@ function _M.CreatePlayer(pos) local entity = _M.CreateEntity("player") entity:SetComponent(_M.CreateTransformComponent(hazel.CreatePos(pos.x, pos.y), hazel.CreateSize(constants.TileSize, constants.TileSize), Flip.Vertical)) entity:SetComponent(_M.CreateImageComponent(content.Tilesheet, 0, 0)) - entity:SetComponent(_M.CreateControllerComponent()) entity:SetComponent(_M.CreateRolePropComponent(constants.PlayerInfo.hp, constants.PlayerInfo.velocity)) - entity:SetComponent(_M.CreateHpShowComponent(hazel.CreateSize(constants.PlayerHpBarInfo.width, constants.PlayerHpBarInfo.height))) - entity:SetComponent(_M.CreateGunComponent(constants.BulletInfo.damage, constants.BulletInfo.velocity)) entity:SetComponent(_M.CreateDirectionComponent(0)) entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox)) entity:SetComponent(_M.CreateInvincibleComponent(constants.Invincible)) @@ -467,7 +464,6 @@ function _M.CreateMonster(pos) entity:SetComponent(_M.CreateHpShowComponent(hazel.CreateSize(constants.MonsetHpBarInfo.width, constants.MonsetHpBarInfo.height))) entity:SetComponent(_M.CreateDirectionComponent(5)) entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox)) - entity:SetComponent(_M.CreateAIComponent()) return entity end diff --git a/game/main.lua b/game/main.lua index fd419eb..8573271 100644 --- a/game/main.lua +++ b/game/main.lua @@ -143,12 +143,76 @@ local function collisionDeal() end end +local function showRestartHint() + ---@type Point + local canvaSize = hazel.GetCanvaSize() + local drawW = content.RestartHintTexture.w * 2 + local drawH = content.RestartHintTexture.h * 2 + local dstRect = hazel.CreateRect((canvaSize.x - drawW) / 2, + (canvaSize.y - drawH) / 2, + drawW, drawH) + hazel.Renderer.DrawTexture(content.RestartHintTexture, nil, dstRect, hazel.Flip.None) +end + +local function showStartHint() + ---@type Point + local canvaSize = hazel.GetCanvaSize() + local drawW = content.StartHintTexture.w * 2 + local drawH = content.StartHintTexture.h * 2 + local dstRect = hazel.CreateRect((canvaSize.x - drawW) / 2, + (canvaSize.y - drawH) / 2, + drawW, drawH) + hazel.Renderer.DrawTexture(content.StartHintTexture, nil, dstRect, hazel.Flip.None) +end + +local function initGame() + content.BulletList = {} + content.MonsterList = {} + content.PlayerEntity = ECS.CreatePlayer(hazel.CreatePos(constants.TileSize * 16, constants.TileSize * 13)) + content.MonsterBirthNum = constants.MonsterBirthInitNum + content.IsStartGame = false +end + +local function generateMonster() + if content.MonsterBirthCountDown > 0 then + content.MonsterBirthCountDown = content.MonsterBirthCountDown - hazel.Time.GetElapseTime() + return + end + local dir = math.random(1, 4) + ---@type Point + local pos = hazel.CreatePos(0, 0) + ---@type Point + local canvaSize = hazel.GetCanvaSize() + if dir == 1 or dir == 3 then + pos.y = math.random(-constants.TileSize, canvaSize.y + constants.TileSize) + if dir == 1 then + pos.x = -constants.TileSize + else + pos.x = canvaSize.x + constants.TileSize + end + else + pos.x = math.random(-constants.TileSize, canvaSize.x + constants.TileSize) + if dir == 2 then + pos.y = -constants.TileSize + else + pos.y = canvaSize.y + constants.TileSize + end + end + ---@type Entity + local monster = ECS.CreateMonster(pos) + monster:SetComponent(ECS.CreateAIComponent()) + table.insert(content.MonsterList, monster) + content.MonsterBirthCountDown = constants.MonsterBirthInterval +end + function GameStart() hazel.SetWindowIcon("resources/icon.png") content.Texture = hazel.LoadTexture("resources/tilesheet.png") + content.RestartHintTexture = hazel.LoadTexture("resources/RestartHint.png") + content.StartHintTexture = hazel.LoadTexture("resources/StartHint.png") content.Tilesheet = hazel.CreateTileSheet(content.Texture, 3, 10) - content.PlayerEntity = ECS.CreatePlayer(hazel.CreatePos(constants.TileSize * 10, constants.TileSize * 10)) - table.insert(content.MonsterList, ECS.CreateMonster(hazel.CreatePos(500, 500))) + + initGame() hazel.HideCursor() generateFloors() @@ -165,10 +229,42 @@ function GameLoop() content.PlayerEntity:Update() updateBullet() collisionDeal() + + ---@type RolePropComponent + local playerRoleInfo = content.PlayerEntity:GetComponent(ECS.ComponentType.RoleProp) + if not playerRoleInfo or playerRoleInfo.hp <= 0 then + showRestartHint() + if hazel.GetKeyState(hazel.Key.R) == hazel.InputState.Press then + initGame() + end + end + + if not content.IsStartGame then + showStartHint() + if hazel.GetMouseButtonState(hazel.MouseButton.Left) == hazel.InputState.Press then + content.IsStartGame = true + content.PlayerEntity:SetComponent(ECS.CreateControllerComponent()) + content.PlayerEntity:SetComponent(ECS.CreateGunComponent(constants.BulletInfo.damage, constants.BulletInfo.velocity)) + content.PlayerEntity:SetComponent(ECS.CreateHpShowComponent(hazel.CreateSize(constants.PlayerHpBarInfo.width, constants.PlayerHpBarInfo.height))) + + for _, v in pairs(content.MonsterList) do + v:SetComponent(ECS.CreateAIComponent()) + end + end + end + + if content.IsStartGame then + for i = 0, content.MonsterBirthNum do + generateMonster() + end + end + drawCurosr() end function GameQuit() hazel.ShowCursor() hazel.DestroyTexture(content.Texture) + hazel.DestroyTexture(content.RestartHintTexture) + hazel.DestroyTexture(content.StartHintTexture) end diff --git a/game/resources/RestartHint.png b/game/resources/RestartHint.png new file mode 100644 index 0000000000000000000000000000000000000000..b7afbfc1a1cedd19152685dfa1a6999d424833cd GIT binary patch literal 678 zcmV;X0$KfuP)Px%U`a$lRA@u(n%i!~APhvC|NrPzw2Kr`V9prlA~k*KLk)OtCh6|B@B98GeztAf zN(regb*0{^t;f&S)E*+WExdA$+hbkRql#i(;btTeJ=?|W@JxG%;HP^#_-Y?Nzh}Z8 zNvH?3Mx2>T)gIT9KH7d%aA&5u6A!^kYU)r@JRbwEYNMKrd@vdqst>1UHJoN=Jt6>9 zY$~`4Muc3aN7X0Ui`1xdA_FXY$h-kiZu~r2sfmy!j630|5zm@{iayex6ZkBiSElkY z*bcr(*#6}W;D}5FaET{-lA8Ah_+@@2Z0Kb*`VxG`RHw#Jld}>>>L?C+yATYd#>GJQ zRHh=FDN~X?oEO`XMhY{!0Y(#3fpmO4JAtDh#b#HA|KO6j#nU ztC-qeoUDPPE~cxbPrXC)fxjhs{WxkHmH12EG_ah)1~ zqj1$c$~fdx=FG1`J2!x1L??(YH!}{Ez*lMF;#dEUt+*U~J)YG1ZPx%JxN4CRA@u(TES7oFbqt};97x>QUZB~64DX0!j%!m83e{-OS`f&lMCj@+{Ttx zo0W7hZripE#?Ls8V}KyofD3pAE7xn!NDLmVJoEhex##-vcBAvrC_;0s;l`>F!^@HJ zqGvN2*?9{Gry@v65lm}rd=GMVTnsB_t*|24_vLALneRW)@cTT^e;!?yWqyXR@(Rux7Aj`&w>Vhf@Dj}yF}_DcDzn$Ru4)aYY3j>V#lp?js%l2n7Maq) znp*800p^RHYhhhkr7udB9-_tZko ziSvmTCR*@`i}El|y;BDm~+k3E@+lQ7sW7 zFL?DbRk{R8jwu3j9*Sjzn&xS_tXC9FQ5pU$M{5