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 0000000..b7afbfc Binary files /dev/null and b/game/resources/RestartHint.png differ diff --git a/game/resources/StartHint.png b/game/resources/StartHint.png new file mode 100644 index 0000000..fde73ca Binary files /dev/null and b/game/resources/StartHint.png differ diff --git a/game/vmath.lua b/game/vmath.lua index d4605c5..ec054a8 100644 --- a/game/vmath.lua +++ b/game/vmath.lua @@ -9,7 +9,7 @@ function _M.Len(p) end ---@return Point ----@param p +---@param p Point function _M.Normalize(p) local l = _M.Len(p) return hazel.CreatePos(p.x / l, p.y / l)