first playable version released

This commit is contained in:
VisualGMQ 2022-07-31 23:51:12 +08:00
parent b829b49acb
commit 66b58aa71d
8 changed files with 116 additions and 8 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ game/shader
game/*.dll game/*.dll
game/*.dll.a game/*.dll.a
game/*.exe game/*.exe
*.zip

View File

@ -16,6 +16,8 @@ _M.RoleColliBox = {
x = 9, x = 9,
y = 0, y = 0,
} }
_M.MonsterBirthInterval = 1
_M.MonsterBirthInitNum = 1
_M.Invincible = 1 _M.Invincible = 1
_M.BulletColliBox = { _M.BulletColliBox = {
x = 10, x = 10,

View File

@ -4,6 +4,15 @@ local _M = {}
---@type Texture ---@type Texture
_M.Texture = nil _M.Texture = nil
---@type Texture
_M.RestartHintTexture = nil
---@type Texture
_M.StartHintTexture = nil
---@type boolean
_M.IsStartGame = false
---@type TileSheet ---@type TileSheet
_M.Tilesheet = nil _M.Tilesheet = nil
@ -16,4 +25,8 @@ _M.BulletList = {}
---@type table<Entity> ---@type table<Entity>
_M.MonsterList = {} _M.MonsterList = {}
_M.MonsterBirthNum = 0
_M.MonsterBirthCountDown = 0
return _M return _M

View File

@ -447,10 +447,7 @@ function _M.CreatePlayer(pos)
local entity = _M.CreateEntity("player") 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.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.CreateImageComponent(content.Tilesheet, 0, 0))
entity:SetComponent(_M.CreateControllerComponent())
entity:SetComponent(_M.CreateRolePropComponent(constants.PlayerInfo.hp, constants.PlayerInfo.velocity)) 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.CreateDirectionComponent(0))
entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox)) entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox))
entity:SetComponent(_M.CreateInvincibleComponent(constants.Invincible)) 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.CreateHpShowComponent(hazel.CreateSize(constants.MonsetHpBarInfo.width, constants.MonsetHpBarInfo.height)))
entity:SetComponent(_M.CreateDirectionComponent(5)) entity:SetComponent(_M.CreateDirectionComponent(5))
entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox)) entity:SetComponent(_M.CreateColliBoxComponent(constants.RoleColliBox))
entity:SetComponent(_M.CreateAIComponent())
return entity return entity
end end

View File

@ -143,12 +143,76 @@ local function collisionDeal()
end end
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() function GameStart()
hazel.SetWindowIcon("resources/icon.png") hazel.SetWindowIcon("resources/icon.png")
content.Texture = hazel.LoadTexture("resources/tilesheet.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.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() hazel.HideCursor()
generateFloors() generateFloors()
@ -165,10 +229,42 @@ function GameLoop()
content.PlayerEntity:Update() content.PlayerEntity:Update()
updateBullet() updateBullet()
collisionDeal() 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() drawCurosr()
end end
function GameQuit() function GameQuit()
hazel.ShowCursor() hazel.ShowCursor()
hazel.DestroyTexture(content.Texture) hazel.DestroyTexture(content.Texture)
hazel.DestroyTexture(content.RestartHintTexture)
hazel.DestroyTexture(content.StartHintTexture)
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 678 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 645 B

View File

@ -9,7 +9,7 @@ function _M.Len(p)
end end
---@return Point ---@return Point
---@param p ---@param p Point
function _M.Normalize(p) function _M.Normalize(p)
local l = _M.Len(p) local l = _M.Len(p)
return hazel.CreatePos(p.x / l, p.y / l) return hazel.CreatePos(p.x / l, p.y / l)