Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/GwyrddGlas/GameStateManager
https://github.com/GwyrddGlas/GameStateManager
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/GwyrddGlas/GameStateManager
- Owner: GwyrddGlas
- License: mit
- Created: 2024-03-13T01:22:05.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-06-29T14:53:21.000Z (7 months ago)
- Last Synced: 2024-08-02T06:17:41.455Z (6 months ago)
- Language: Lua
- Size: 14.6 KB
- Stars: 8
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-love2d - GameStateManager - A lightweight, optimized, and easy-to-implement solution for efficient game state management. (Helpers)
README
# GameState Library for LÖVE
A simple and flexible state management library for [LÖVE](https://love2d.org/). It allows easy switching between game states, such as menus, levels, or screens. Handling input and rendering delegation based on the current state.
## Features
- Simple API for managing game states
- Automatic delegation of input and rendering to the current state
- Support for entering and exiting actions when switching states## Installation
1. Download `GameStateManager.lua` and place it in your project directory.
2. Require the GameStateManager in your main game file:```lua
local GameStateManager = require("GameStateManager")
```## Usage
### Defining States
Each state is a Lua table with functions that correspond to LÖVE's callback functions. Here's an example of a simple state:
```lua
local menuState = {
enter = function()
print("Entering menu state")
end,
update = function(dt)
-- Update menu items
end,
draw = function()
-- Draw menu UI
end,
keypressed = function(key, scancode, isrepeat)
if key == "return" then
-- Start the game
end
end
}
```### Switching States
To switch the current state, use the `setState` method:
```lua
GameStateManager:setState(menuState)
```### Integrating with LÖVE
Delegate LÖVE's callback functions to the GameStateManager in your `main.lua`:
```lua
function love.update(dt)
GameStateManager:update(dt)
endfunction love.draw()
GameStateManager:draw()
end-- Delegate other callback functions as needed...
```## API
- `GameStateManager:setState(newState)`: Set the current state. The new state's `enter` method will be called if it exists.
- `GameStateManager:getState()`: Get the current state.
- `GameStateManager:getPreviousState()`: Get the previous state before the last state switch.The GameStateManager automatically delegates the following LÖVE callbacks to the current state if they are defined:
- `update(dt)`
- `draw()`
- `mousemoved(x, y)`
- `mousepressed(x, y, button)`
- `mousereleased(x, y, button)`
- `keypressed(key, scancode, isrepeat)`
- `keyreleased(key, scancode)`
- `wheelmoved(x, y)`
- `textinput(text)`
- `quit()`## Contributing
Contributions to the GameState library are welcome! Please feel free to submit pull requests or report issues on the GitHub repository.
## License
This library is open-sourced and licensed under the MIT license. See the LICENSE file for details.