rpgx 0.1.3

Lightweight, modular, and extensible RPG game engine 2D, designed for flexibility, portability, and ease of use.
Documentation
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# `Map`

A `Map` represents a game level or region composed of multiple [`Layer`](layer.md)s. Each layer applies [`Mask`](mask.md)s that define effects like collisions, textures, or actions. A map also includes a name identifier and a default spawn location.

Maps can be composed, shifted, or merged to build larger environments dynamically.

---

## Fields

### `name: String`

The name or identifier of the map. Useful for logging, editor tools, or dynamic selection.

---

### `layers: Vec<Layer>`

A list of stacked [`Layer`](layer.md)s. These define all effects applied to the map, such as collision logic, visual rendering, or event triggers.

---

### `spawn: Coordinates`

The default spawn position for players, pawns, or other entities when the map is loaded.

---

## Methods

### `Map::new(name: String, layers: Vec<Layer>, spawn: Coordinates) -> Self`

Creates a new map with a name, a list of layers, and a spawn position.

```rust
use rpgx::prelude::*;

let map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);
```

---

### `fn get_shape(&self) -> Shape`

Returns the bounding [`Shape`](shape.md) that encloses all layers.

```rust
use rpgx::prelude::*;

let map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

let bounds = map.get_shape();
```

---

### `fn contains(&self, coord: &Coordinates) -> bool`

Returns `true` if any layer in the map contains the specified coordinate.

---

### `fn is_blocking_at(&self, coord: &Coordinates) -> bool`

Returns `true` if any layer contains a blocking effect at the specified position.

```rust
use rpgx::prelude::*;

let map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

if map.is_blocking_at(&Coordinates::new(3, 4)) {
    // prevent movement
}
```

---

### `fn get_actions_at(&self, coord: &Coordinates) -> Vec<u32>`

Returns a list of action IDs applied at the specified tile across all layers.

```rust
use rpgx::prelude::*;

let map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

let actions = map.get_actions_at(&Coordinates::new(1, 1));
```

---

### `Map::compose(name, maps, layers, spawn) -> Self`

Builds a new `Map` by merging multiple maps (with positional offsets), adding new layers, and setting a spawn point.

```rust
use rpgx::prelude::*;

let map1 = Map::new(
    "example_map_1".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

let map2 = Map::new(
    "example_map_2".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

let merged = Map::compose(
    "composed_map".into(),
    vec![(map1, Coordinates::new(0, 0)), (map2, Coordinates::new(10, 0))],
    vec![],
    Coordinates::new(0, 0),
);
```

---

### `fn load_layer(&mut self, layer: Layer)`

Adds a new layer to the map, offsetting existing layers if needed to make space.

> Useful when dynamically layering content (e.g. loading a dungeon section on the fly).

---

### `fn layers_by_name(&self) -> IndexMap<String, Layer>`

Returns a map from each layer’s name to its [`Layer`](layer.md) object. Useful for quickly accessing specific layers.

```rust
use rpgx::prelude::*;

let map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

let visual_layer = map.layers_by_name().get("visuals");
```

---

### `fn merge_at(&mut self, other: &Map, top_left: Coordinates, spawn: Option<Coordinates>)`

Merges another `Map` into this one, offsetting its layers by `top_left`.

If a `spawn` is provided, updates the current map's spawn point.

---

### `fn duplicate_to_the(&mut self, direction: Direction, spawn: Option<Coordinates>)`

Clones and attaches this map in the specified `Direction` (e.g., `Right`, `Down`) to itself. Useful for building tile-based infinite maps or test grids.

```rust
use rpgx::prelude::*;

let mut map = Map::new(
    "example_map".into(),
    vec![
        Layer::new(
            "combined_grounds".into(),
            vec![
                Mask::new(
                    "ground".into(),
                    Rect::from_shape(Shape::from_square(10)).into_many(),
                    vec![Effect::Texture(1)]
                ),
                Mask::new(
                    "inner_ground".into(),
                    Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
                    vec![Effect::Texture(2)]
                )
            ],
            1
        ),
        Layer::new(
            "buildings".into(),
            vec![
                Mask::new(
                    "building".into(),
                    vec![
                        Rect::new(Coordinates::new(3, 3), Shape::new(4, 6)),
                    ],
                    vec![
                        Effect::Block(Rect::new(Coordinates::new(1, 1), Shape::new(3, 5))),
                        Effect::Texture(2),
                    ]
                )
            ],
            2
        )
    ],
    Coordinates::new(0, 0),
);

map.duplicate_to_the(Direction::Right, None);
```

---

## Usage Example

```rust
use rpgx::prelude::*;

let blocked = vec![Coordinates::new(1, 1)];
let map = Map::new(
    "sample".into(),
    vec![Layer::new(
        "blocking".into(),
        vec![
            Mask::new(
                "block_1".into(),
                vec![Rect::from_xywh(1, 1, 1, 1)],
                vec![Effect::Block(Rect::from_xywh(1, 1, 1, 1))],
            )
        ],
        1,
    )],
    Coordinates::new(0, 0),
);

assert!(map.is_blocking_at(&Coordinates::new(1, 1)));
```

---

## Design Notes

- `Map` is a high-level composition of effects, built from layers and masks.
- It supports composability via `compose`, `merge_at`, and `duplicate_to_the`, enabling procedural and modular map generation.
- Layer order (Z-index) determines render or processing priority.
- Spawn points allow maps to define default entry locations for game entities.

---

## See Also

- [`Layer`]layer.md: A stackable collection of masks with z-order.
- [`Mask`]mask.md: A group of [`Rect`]rect.mds with attached [`Effect`]effect.mds.
- [`Effect`]effect.md: Modifiers like blocking, texture, and action.
- [`Rect`]rect.md, [`Shape`]shape.md, [`Coordinates`]coordinates.md, [`Delta`]delta.md