summaryrefslogtreecommitdiff
path: root/template/README.md
blob: e46a3cfa798c54112f92a760d2895e8a4c25c552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# ReiLua-Enhanced Game Template

A complete game template for rapid development with ReiLua-Enhanced.

## 📁 Structure

```
template/
├── main.lua              # Entry point
├── lib/                  # Core libraries
│   ├── classic.lua       # OOP class system
│   ├── gamestate.lua     # State management
│   └── animation.lua     # Sprite animation system
├── states/               # Game states
│   ├── menu.lua          # Menu screen
│   └── game.lua          # Main gameplay
└── assets/               # Game assets (images, sounds, etc.)
```

## 🚀 Quick Start

### Development

1. **Copy template to your game folder:**
   ```bash
   xcopy /E /I template MyGame
   cd MyGame
   ```

2. **Run your game:**
   ```bash
   path\to\ReiLua.exe --log --no-logo
   ```

3. **Start coding!**
   - Edit `states/menu.lua` for your menu
   - Edit `states/game.lua` for gameplay
   - Add assets to `assets/` folder

### Release Build

1. **Copy files to build directory:**
   ```bash
   cd ReiLua-Enhanced\build
   xcopy /E /I ..\MyGame\*.lua .
   xcopy /E /I ..\MyGame\lib lib
   xcopy /E /I ..\MyGame\states states
   mkdir assets
   xcopy /E /I ..\MyGame\assets assets
   ```

2. **Build release:**
   ```bash
   cd ..
   scripts\build_release.bat
   ```

3. **Your game is ready!**
   - `build/ReiLua.exe` (or your custom name)
   - Everything embedded in one executable

## 📚 Libraries Included

### Classic (OOP)
Object-oriented class system by rxi.

```lua
local Object = require("lib.classic")
local Player = Object:extend()

function Player:new(x, y)
  self.x = x
  self.y = y
end

function Player:update(dt)
  -- Update logic
end

local player = Player(100, 100)
```

### GameState
State management system inspired by hump.gamestate.

```lua
local GameState = require("lib.gamestate")
local menu = require("states.menu")

-- Register callbacks
GameState.registerEvents()

-- Switch state
GameState.switch(menu)

-- Push state (with return)
GameState.push(pauseMenu)
GameState.pop()  -- Returns to previous state
```

**State Callbacks:**
- `state:enter(previous, ...)` - Entering state
- `state:leave()` - Leaving state
- `state:resume(previous, ...)` - Returning via pop()
- `state:update(dt)` - Frame update
- `state:draw()` - Rendering
- `state:event(event)` - Input events

### Animation
Sprite sheet animation system.

```lua
local Animation = require("lib.animation")

-- Load texture
local playerTexture = RL.LoadTexture("assets/player.png")

-- Create animation (32x32 frame size)
local anim = Animation.new(playerTexture, 32, 32, {
  idle = {frames = {1, 2, 3, 4}, fps = 8, loop = true},
  walk = {frames = {5, 6, 7, 8, 9, 10}, fps = 12, loop = true},
  jump = {frames = {11, 12, 13}, fps = 10, loop = false}
})

-- Play animation
anim:play("idle")

-- In update loop
anim:update(dt)

-- In draw loop
anim:draw(x, y)

-- Or simple draw (no rotation/scale)
anim:drawSimple(x, y)

-- Advanced features
anim:setFlip(true, false)  -- Flip horizontally
anim:setScale(2, 2)        -- Scale 2x
anim:setTint(RL.RED)       -- Color tint
anim:pause()               -- Pause animation
anim:resume()              -- Resume
anim:stop()                -- Stop and reset
```

## 🎮 Example States

### Menu State (states/menu.lua)
- Keyboard navigation (Up/Down)
- Start game / Options / Exit
- Clean UI rendering

### Game State (states/game.lua)
- Player movement (WASD/Arrows)
- Pause menu (ESC/P)
- Animation integration example
- Basic game loop

## 🎨 Adding Assets

### Sprite Sheets
Place sprite sheets in `assets/` folder:

```
assets/
├── player.png      # Player sprite sheet (grid-based)
├── enemy.png       # Enemy sprites
├── explosion.png   # Explosion animation
└── background.png  # Background image
```

**Sprite Sheet Format:**
- Grid-based (equal-sized frames)
- Frames read left-to-right, top-to-bottom
- Example: 32x32 frames in 256x256 texture = 8x8 grid (64 frames)

### Sounds & Music
```lua
-- In state:enter()
self.music = RL.LoadMusicStream("assets/music.ogg")
self.jumpSound = RL.LoadSound("assets/jump.wav")

-- In state:update()
RL.UpdateMusicStream(self.music)

-- Play sound
RL.PlaySound(self.jumpSound)
```

## 🔧 Configuration

### Window Settings (main.lua)
```lua
local GAME_TITLE = "My Awesome Game"
local WINDOW_WIDTH = 1280
local WINDOW_HEIGHT = 720
local TARGET_FPS = 60
local START_FULLSCREEN = false  -- Start in fullscreen?
```

For complete window configuration (fullscreen, resizable, etc.), see [WINDOW_CONFIG.md](WINDOW_CONFIG.md).

### Exit Key Behavior
By default, the template disables ESC as an exit key so you can use it for pause menus:
```lua
RL.SetExitKey(0)  -- 0 = No exit key
```

To change this behavior, see [COMMON_ISSUES.md](COMMON_ISSUES.md#esc-key-closes-the-game).

### Global Hotkeys
- **F1 / F11** - Toggle fullscreen
- **ESC** - Pause game (in game state)

## 📖 State Management Patterns

### Simple State Switch
```lua
-- From menu to game
GameState.switch(gameState)
```

### State Stack (Pause Menu)
```lua
-- Push pause menu (game continues in background)
GameState.push(pauseMenu)

-- Return to game
GameState.pop()
```

### Passing Data Between States
```lua
-- Pass score to game over screen
GameState.switch(gameOverState, score, highScore)

-- In game over state:
function gameOverState:enter(previous, score, highScore)
  self.score = score
  self.highScore = highScore
end
```

## 🎯 Best Practices

### Performance
- Load assets in `state:enter()`, not `update()`
- Unload assets in `state:leave()`
- Use object pooling for bullets/particles
- Profile with `--log` flag

### Organization
- One file per state
- Keep states small and focused
- Use OOP for game entities (Player, Enemy, etc.)
- Separate rendering from logic

### Asset Loading
```lua
function state:enter()
  -- Show loading if needed
  local assets = {"player.png", "enemy.png", "music.ogg"}
  RL.BeginAssetLoading(#assets)
  
  for _, asset in ipairs(assets) do
    RL.UpdateAssetLoading(asset)
    -- Load asset...
  end
  
  RL.EndAssetLoading()
end
```

## 🐛 Debugging

### Development Mode
```bash
ReiLua.exe --log --no-logo
```
- Shows console output
- Skips splash screens
- Fast iteration

### Common Issues

**Animation not showing:**
- Check texture loaded: `if not texture then print("Failed!") end`
- Verify frame size matches sprite sheet
- Check animation is playing: `anim:play("idle")`

**State not switching:**
- Verify `GameState.registerEvents()` called in `RL.init()`
- Check state has required functions (`:update()`, `:draw()`)
- Use `print()` in `:enter()` to verify state switch

**Assets not loading:**
- Use relative paths: `"assets/player.png"` not `"C:/..."`
- Check file exists with `RL.FileExists()`
- View console with `--log` flag

## 📦 Building for Release

See main ReiLua-Enhanced documentation:
- [EMBEDDING.md](../docs/EMBEDDING.md) - Embedding guide
- [BUILD_SCRIPTS.md](../docs/BUILD_SCRIPTS.md) - Build automation
- [CUSTOMIZATION.md](../docs/CUSTOMIZATION.md) - Branding/icons

## 📄 License

- **classic.lua** - MIT License (rxi)
- **gamestate.lua** - Inspired by hump (MIT)
- **animation.lua** - MIT License
- **Template** - Free to use for any project

## 🎮 Ready to Make Games!

This template gives you everything needed to start building games immediately. Focus on gameplay, not boilerplate!

For more examples and documentation, see the main ReiLua-Enhanced repository.

Happy game jamming! 🚀