diff options
| author | Eltrac <hi@guhub.cn> | 2025-03-12 15:10:16 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-03-12 15:10:16 +0800 |
| commit | 66b583bcfad32261c9cd44df8f13b39024c441a7 (patch) | |
| tree | 96ae736d2c9384b3937d1415a16b95cee7d3397f /main.lua | |
| parent | d34e8ea9f06ec4415827a00d83da2d9e45b0e519 (diff) | |
refactor: clearer player-related module division
- Remove logic.player module, move the into objects.player.
- Controller module does not deal with specific player movement. Instead, it calculates essential data and pass that to player methods
Diffstat (limited to 'main.lua')
| -rw-r--r-- | main.lua | 33 |
1 files changed, 24 insertions, 9 deletions
@@ -7,20 +7,23 @@ require "render.speaker" local map = require "logic.map" local controller = require "logic.controller" local translator = require "i18n.translator" -local player = require "logic.player" - function love.load() + -- load font local myFont = love.graphics.newFont("resources/fonts/MZPXflat.ttf", 17) love.graphics.setFont(myFont) + -- load sprite and initialize renderer sprite.load() - local Renderer = require "render.renderer" RenderManager = Renderer.init() + -- initialize game state initGame() + -- set world callbacks + G.world:setCallbacks(onContact) + speaker.speak(translator.T("welcome"), 2, true) end @@ -29,18 +32,18 @@ function love.draw() end function love.update(dt) - -- physics world move forward - G.world:update(dt) + -- game state updates + local gameStates = { G.world, G.currentRoom, G.player } + for _, state in ipairs(gameStates) do + state:update(dt) + end -- game logic updates - local updates = { player, map, controller, speaker } + local updates = { map, controller, speaker } for _, entity in ipairs(updates) do entity.update(dt) end - -- update objects in current room - G.currentRoom:update(dt) - -- check if game has ended if not G.player:isAlive() then initGame() @@ -53,3 +56,15 @@ end function love.keypressed(key) controller.keypressed(key) end + +function onContact(a, b, contact) + local playerBody = G.player.physicsBody + + -- Determine if one of the bodies belongs to the player + if a:getBody() == playerBody or b:getBody() == playerBody then + -- Identify the other body + -- and deal with player body contact + local otherBody = (a:getBody() == playerBody) and b:getBody() or a:getBody() + G.player:onContact(otherBody, contact) + end +end |
