Skip to content

Commit

Permalink
Merge branch 'main' into flame-engine#69-dart-sdk
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/tiled/pubspec.yaml
  • Loading branch information
benni-tec committed Dec 20, 2024
2 parents 6a8e362 + e297f55 commit ab5611f
Show file tree
Hide file tree
Showing 13 changed files with 202 additions and 41 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2023-12-10

### Changes

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`tiled` - `v0.10.2`](#tiled---v0102)

---

#### `tiled` - `v0.10.2`

- **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35))
- **FEAT**: Adding a method to get any object in a map by its unique ID ([#75](https://github.com/flame-engine/tiled.dart/issues/75)). ([4faf43b4](https://github.com/flame-engine/tiled.dart/commit/4faf43b45002e19c8fdbf2af8dd09969bcf4781c))
- **FEAT**: Omit TiledImage without source from TiledMap.tiledImages ([#68](https://github.com/flame-engine/tiled.dart/issues/68)). ([41c9439f](https://github.com/flame-engine/tiled.dart/commit/41c9439f9c0f1345b8b803b9b33d3a507e45bf1a))
- **FEAT**: Add convenience method for getting images in each layer ([#66](https://github.com/flame-engine/tiled.dart/issues/66)). ([1d3043f7](https://github.com/flame-engine/tiled.dart/commit/1d3043f75dc59449e98c9f2f637141b8ac127508))


## 2022-11-26

### Changes
Expand Down
7 changes: 7 additions & 0 deletions packages/tiled/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.10.2

- **FIX**: `ObjectAlignment` enum names ([#74](https://github.com/flame-engine/tiled.dart/issues/74)). ([628f1f6c](https://github.com/flame-engine/tiled.dart/commit/628f1f6cc89f6dd9b0a9cadcdd619549cf180e35))
- **FEAT**: Adding a method to get any object in a map by its unique ID ([#75](https://github.com/flame-engine/tiled.dart/issues/75)). ([4faf43b4](https://github.com/flame-engine/tiled.dart/commit/4faf43b45002e19c8fdbf2af8dd09969bcf4781c))
- **FEAT**: Omit TiledImage without source from TiledMap.tiledImages ([#68](https://github.com/flame-engine/tiled.dart/issues/68)). ([41c9439f](https://github.com/flame-engine/tiled.dart/commit/41c9439f9c0f1345b8b803b9b33d3a507e45bf1a))
- **FEAT**: Add convenience method for getting images in each layer ([#66](https://github.com/flame-engine/tiled.dart/issues/66)). ([1d3043f7](https://github.com/flame-engine/tiled.dart/commit/1d3043f75dc59449e98c9f2f637141b8ac127508))

## 0.10.1

- **FEAT**: Add `imageRect` for `Tile` ([#64](https://github.com/flame-engine/tiled.dart/issues/64)). ([33d99b70](https://github.com/flame-engine/tiled.dart/commit/33d99b70e9c0c9b11483d9a25abfc1375869c87f))
Expand Down
22 changes: 17 additions & 5 deletions packages/tiled/lib/src/common/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,19 @@ enum ObjectAlignment {
right,
bottomLeft,
bottom,
bottomRight,
bottomRight;

/// Returns the [ObjectAlignment] based on given [name].
///
/// Throws an [ArgumentError] if no match is found.
static ObjectAlignment fromName(String name) {
for (final value in ObjectAlignment.values) {
if (value.name == name) {
return value;
}
}
throw ArgumentError.value(name, 'name', 'No enum value with that name');
}
}

extension ObjectAlignmentExtension on ObjectAlignment {
Expand All @@ -390,23 +402,23 @@ extension ObjectAlignmentExtension on ObjectAlignment {
case ObjectAlignment.unspecified:
return 'unspecified';
case ObjectAlignment.topLeft:
return 'topLeft';
return 'topleft';
case ObjectAlignment.top:
return 'top';
case ObjectAlignment.topRight:
return 'topRight';
return 'topright';
case ObjectAlignment.left:
return 'left';
case ObjectAlignment.center:
return 'center';
case ObjectAlignment.right:
return 'right';
case ObjectAlignment.bottomLeft:
return 'bottomLeft';
return 'bottomleft';
case ObjectAlignment.bottom:
return 'bottom';
case ObjectAlignment.bottomRight:
return 'bottomRight';
return 'bottomright';
}
}
}
Expand Down
28 changes: 17 additions & 11 deletions packages/tiled/lib/src/common/flips.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ class Flips {
final bool diagonally;
final bool antiDiagonally;

const Flips(
this.horizontally,
this.vertically,
this.diagonally,
this.antiDiagonally,
);
const Flips({
required this.horizontally,
required this.vertically,
required this.diagonally,
required this.antiDiagonally,
});

const Flips.defaults() : this(false, false, false, false);
const Flips.defaults()
: this(
horizontally: false,
vertically: false,
diagonally: false,
antiDiagonally: false,
);

Flips copyWith({
bool? horizontally,
Expand All @@ -22,10 +28,10 @@ class Flips {
bool? antiDiagonally,
}) {
return Flips(
horizontally ?? this.horizontally,
vertically ?? this.vertically,
diagonally ?? this.diagonally,
antiDiagonally ?? this.antiDiagonally,
horizontally: horizontally ?? this.horizontally,
vertically: vertically ?? this.vertically,
diagonally: diagonally ?? this.diagonally,
antiDiagonally: antiDiagonally ?? this.antiDiagonally,
);
}
}
8 changes: 4 additions & 4 deletions packages/tiled/lib/src/common/gid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ class Gid {
flippedDiagonallyFlag |
flippedAntiDiagonallyFlag);
final flip = Flips(
flippedHorizontally,
flippedVertically,
flippedDiagonally,
flippedAntiDiagonally,
horizontally: flippedHorizontally,
vertically: flippedVertically,
diagonally: flippedDiagonally,
antiDiagonally: flippedAntiDiagonally,
);
return Gid(tileId, flip);
}
Expand Down
26 changes: 13 additions & 13 deletions packages/tiled/lib/src/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ abstract class Layer {
CustomProperties properties;

Layer({
this.id,
required this.name,
required this.type,
this.id,
this.class_,
this.x = 0,
this.y = 0,
Expand Down Expand Up @@ -298,7 +298,7 @@ abstract class Layer {
}
final text = xml.element.children.first;
if (text is XmlText) {
return text.text;
return text.value;
}
return null;
},
Expand Down Expand Up @@ -380,8 +380,10 @@ class TileLayer extends Layer {
List<List<Gid>>? tileData;

TileLayer({
super.id,
required super.name,
required this.width,
required this.height,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -396,8 +398,6 @@ class TileLayer extends Layer {
super.opacity,
super.visible,
super.properties,
required this.width,
required this.height,
this.compression,
this.encoding = FileEncoding.csv,
this.chunks,
Expand Down Expand Up @@ -441,8 +441,9 @@ class ObjectGroup extends Layer {
ColorData color;

ObjectGroup({
super.id,
required super.name,
required this.objects,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -458,7 +459,6 @@ class ObjectGroup extends Layer {
super.visible,
super.properties,
this.drawOrder = DrawOrder.topDown,
required this.objects,
this.colorHex = defaultColorHex,
this.color = defaultColor,
}) : super(
Expand Down Expand Up @@ -487,8 +487,11 @@ class ImageLayer extends Layer {
bool repeatY;

ImageLayer({
super.id,
required super.name,
required this.image,
required this.repeatX,
required this.repeatY,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -503,9 +506,6 @@ class ImageLayer extends Layer {
super.opacity,
super.visible,
super.properties,
required this.image,
required this.repeatX,
required this.repeatY,
this.transparentColorHex,
this.transparentColor,
}) : super(
Expand All @@ -518,8 +518,9 @@ class Group extends Layer {
List<Layer> layers;

Group({
super.id,
required super.name,
required this.layers,
super.id,
super.class_,
super.x,
super.y,
Expand All @@ -534,7 +535,6 @@ class Group extends Layer {
super.opacity,
super.visible,
super.properties,
required this.layers,
}) : super(
type: LayerType.imageLayer,
);
Expand Down
32 changes: 28 additions & 4 deletions packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,23 @@ class TiledMap {
List<EditorSetting> editorSettings;
CustomProperties properties;

// Cache the object by ID when accessed.
Map<int, TiledObject>? _cachedObjects;

// only for hexagonal maps:
int? hexSideLength;
StaggerAxis? staggerAxis;
StaggerIndex? staggerIndex;

TiledMap({
this.type = TileMapType.map,
this.version = '1.0',
this.tiledVersion,
required this.width,
required this.height,
this.infinite = false,
required this.tileWidth,
required this.tileHeight,
this.type = TileMapType.map,
this.version = '1.0',
this.tiledVersion,
this.infinite = false,
this.tilesets = const [],
this.layers = const [],
this.backgroundColorHex,
Expand Down Expand Up @@ -279,6 +282,27 @@ class TiledMap {
throw ArgumentError('Layer $name not found');
}

/// Finds the [TiledObject] in this map with the unique [id].
/// Objects have map wide unique IDs which are never reused.
/// https://doc.mapeditor.org/en/stable/reference/tmx-map-format/#object
///
/// This reads through a cached map of all the objects so it does not
/// need to loop through all the object layers each time.
///
/// Returns null if not found.
TiledObject? objectById(int id) {
if (_cachedObjects == null) {
_cachedObjects = {};
layers.whereType<ObjectGroup>().forEach((objectGroup) {
for (final object in objectGroup.objects) {
_cachedObjects![object.id] = object;
}
});
}

return _cachedObjects?[id];
}

Tileset tilesetByName(String name) {
return tilesets.firstWhere(
(element) => element.name == name,
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/tileset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class Tileset {
final firstGid = parser.getIntOrNull('firstgid');
final margin = parser.getInt('margin', defaults: 0);
final name = parser.getStringOrNull('name');
final objectAlignment = ObjectAlignment.values.byName(
final objectAlignment = ObjectAlignment.fromName(
parser.getString('objectalignment', defaults: 'unspecified'),
);
final source = parser.getStringOrNull('source');
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tiled
version: 0.10.1
version: 0.10.2
description: A Dart Tiled library. Parse your TMX files into useful representations. Compatible with Flame.
homepage: https://github.com/flame-engine/tiled.dart

Expand Down
53 changes: 52 additions & 1 deletion packages/tiled/test/map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void main() {
image: const TiledImage(),
),
],
)
),
],
);
});
Expand Down Expand Up @@ -380,4 +380,55 @@ void main() {
expect(map.tilesetByName('Humans'), equals(tileset));
});
});

group('Map.objectById', () {
late TiledMap map;
setUp(() {
map = TiledMap(
width: 2,
height: 2,
tileWidth: 8,
tileHeight: 8,
layers: [
TileLayer(
name: 'tile layer 1',
width: 2,
height: 2,
data: [1, 0, 2, 0],
),
ObjectGroup(
name: 'object layer 1',
objects: [
TiledObject(id: 1, name: 'object one'),
TiledObject(id: 5, name: 'object five'),
],
),
],
tilesets: [
Tileset(
name: 'TileSet_1',
image: const TiledImage(source: 'tileset_1.png'),
firstGid: 1,
columns: 1,
tileCount: 2,
tiles: [
Tile(localId: 0),
Tile(localId: 1),
],
),
],
);
});

test('gets images only in use on each TileLayer', () {
final object1 = map.objectById(1);
expect(object1?.name, equals('object one'));

final object5 = map.objectById(5);
expect(object5?.name, equals('object five'));

final object3 = map.objectById(3);
expect(object3, equals(null));
});
});
}
Loading

0 comments on commit ab5611f

Please sign in to comment.