Implement pause state.
This commit is contained in:
parent
ab89785883
commit
bf1a3134a1
@ -446,6 +446,7 @@ Let's start with the base ~GameState~ class, it should keep the shared logic of
|
||||
up=false,
|
||||
down=false,
|
||||
jump=false,
|
||||
pause_key_was_released=false,
|
||||
}
|
||||
self.ended = false
|
||||
self.next = nil
|
||||
@ -458,6 +459,9 @@ Let's start with the base ~GameState~ class, it should keep the shared logic of
|
||||
self.input.up = love.keyboard.isDown("w", "up")
|
||||
self.input.down = love.keyboard.isDown("down", "s")
|
||||
self.input.jump = love.keyboard.isDown("space")
|
||||
if not (self.input.pause_key_was_released or self.input.start) then
|
||||
self.input.pause_key_was_released = true
|
||||
end
|
||||
end
|
||||
|
||||
function GameState:update(dt)
|
||||
@ -548,9 +552,15 @@ Also, it'll spawn a couple entities for enemies that will just move around the s
|
||||
self:updateItems(dt)
|
||||
self:updateEnemies(dt)
|
||||
self:updateChirps(dt)
|
||||
if self.input.pause_key_was_released and self.input.start then
|
||||
self.ended = true
|
||||
self.next = Pause:new(self)
|
||||
self.input.pause_key_was_released = false
|
||||
end
|
||||
end
|
||||
|
||||
function Play:draw()
|
||||
Play.__super.draw(self)
|
||||
love.graphics.setColor(0.2, 0.2, 0.2)
|
||||
self.map:draw()
|
||||
love.graphics.setColor(0, 0.8, 0.1)
|
||||
@ -560,13 +570,45 @@ Also, it'll spawn a couple entities for enemies that will just move around the s
|
||||
end
|
||||
end
|
||||
#+end_src
|
||||
|
||||
Let's define a ~Pause~ state and see how it connects to ~Play~, it'll need to keep the ~Play~ state instance so it can be resumed after unpausing:
|
||||
#+name: pauseState
|
||||
#+begin_src lua :tangle no
|
||||
local Pause = GameState:extend()
|
||||
|
||||
function Pause:init(play_state)
|
||||
Pause.__super.init(self)
|
||||
self.play_state = play_state
|
||||
end
|
||||
|
||||
function Pause:update(dt)
|
||||
Pause.__super.update(self, dt)
|
||||
if self.input.pause_key_was_released and self.input.start then
|
||||
self.input.pause_key_was_released = false
|
||||
self.ended = true
|
||||
self.play_state.ended = false
|
||||
self.next = self.play_state
|
||||
end
|
||||
end
|
||||
|
||||
function Pause:draw()
|
||||
Pause.__super.draw(self)
|
||||
local pause_str = "~ PAUSE ~"
|
||||
local font = love.graphics.getFont()
|
||||
local height = font:getHeight()
|
||||
local width = font:getWidth(pause_str)
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
love.graphics.print(pause_str, 320/2, 200/2, 0, 2, 2, width/2, height/2)
|
||||
end
|
||||
#+end_src
|
||||
#+begin_src lua :tangle states.lua :noweb yes
|
||||
<<playState>>
|
||||
<<titleState>>
|
||||
return {
|
||||
Play=Play,
|
||||
Title=Title,
|
||||
}
|
||||
<<pauseState>>
|
||||
<<playState>>
|
||||
<<titleState>>
|
||||
return {
|
||||
Play=Play,
|
||||
Title=Title,
|
||||
}
|
||||
#+end_src
|
||||
|
||||
#+begin_src lua
|
||||
@ -1320,8 +1362,13 @@ Need to check ~next_state.pos~ and resulting ~top_left_tile~ for this case.
|
||||
y = 184
|
||||
}
|
||||
|
||||
** TODO Implement game pause as ~Play~ substate
|
||||
** DONE Implement game pause as ~Play~ substate
|
||||
DEADLINE: <2024-09-24 mar>
|
||||
:LOGBOOK:
|
||||
CLOCK: [2024-10-02 mié 14:40]--[2024-10-02 mié 15:01] => 0:21
|
||||
:END:
|
||||
|
||||
It's kinda working but I need to debounce it.
|
||||
|
||||
** TODO replace hardcoded player initial position (180, 100) with marker/point from Tiled map
|
||||
|
||||
|
36
states.lua
36
states.lua
@ -15,6 +15,7 @@ function GameState:init()
|
||||
up=false,
|
||||
down=false,
|
||||
jump=false,
|
||||
pause_key_was_released=false,
|
||||
}
|
||||
self.ended = false
|
||||
self.next = nil
|
||||
@ -27,6 +28,9 @@ function GameState:updateInput(dt)
|
||||
self.input.up = love.keyboard.isDown("w", "up")
|
||||
self.input.down = love.keyboard.isDown("down", "s")
|
||||
self.input.jump = love.keyboard.isDown("space")
|
||||
if not (self.input.pause_key_was_released or self.input.start) then
|
||||
self.input.pause_key_was_released = true
|
||||
end
|
||||
end
|
||||
|
||||
function GameState:update(dt)
|
||||
@ -36,6 +40,32 @@ end
|
||||
function GameState:draw()
|
||||
end
|
||||
|
||||
local Pause = GameState:extend()
|
||||
|
||||
function Pause:init(play_state)
|
||||
Pause.__super.init(self)
|
||||
self.play_state = play_state
|
||||
end
|
||||
|
||||
function Pause:update(dt)
|
||||
Pause.__super.update(self, dt)
|
||||
if self.input.pause_key_was_released and self.input.start then
|
||||
self.input.pause_key_was_released = false
|
||||
self.ended = true
|
||||
self.play_state.ended = false
|
||||
self.next = self.play_state
|
||||
end
|
||||
end
|
||||
|
||||
function Pause:draw()
|
||||
Pause.__super.draw(self)
|
||||
local pause_str = "~ PAUSE ~"
|
||||
local font = love.graphics.getFont()
|
||||
local height = font:getHeight()
|
||||
local width = font:getWidth(pause_str)
|
||||
love.graphics.setColor(1, 1, 1)
|
||||
love.graphics.print(pause_str, 320/2, 200/2, 0, 2, 2, width/2, height/2)
|
||||
end
|
||||
local Play = GameState:extend()
|
||||
|
||||
function Play:init()
|
||||
@ -83,9 +113,15 @@ function Play:update(dt)
|
||||
self:updateItems(dt)
|
||||
self:updateEnemies(dt)
|
||||
self:updateChirps(dt)
|
||||
if self.input.pause_key_was_released and self.input.start then
|
||||
self.ended = true
|
||||
self.next = Pause:new(self)
|
||||
self.input.pause_key_was_released = false
|
||||
end
|
||||
end
|
||||
|
||||
function Play:draw()
|
||||
Play.__super.draw(self)
|
||||
love.graphics.setColor(0.2, 0.2, 0.2)
|
||||
self.map:draw()
|
||||
love.graphics.setColor(0, 0.8, 0.1)
|
||||
|
Loading…
Reference in New Issue
Block a user