-
-
Notifications
You must be signed in to change notification settings - Fork 913
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Split layers into files (#1916)
renderable_tile_map.dart was getting bigger with each new PR and was containing multiple classes. This PR breaks that file into multiple parts. More specifically, it moves out all the layer classes into their own file under renderable_layers directory.
- Loading branch information
1 parent
999caca
commit dac2ee1
Showing
8 changed files
with
782 additions
and
710 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
include: package:flame_lint/analysis_options.yaml | ||
|
||
linter: | ||
rules: | ||
- public_member_api_docs |
42 changes: 42 additions & 0 deletions
42
packages/flame_tiled/lib/src/renderable_layers/group_layer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flame/extensions.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_tiled/src/renderable_layers/renderable_layer.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:tiled/tiled.dart'; | ||
|
||
@internal | ||
class GroupLayer extends RenderableLayer<Group> { | ||
/// The child layers of this [Group] to be rendered recursively. | ||
/// | ||
/// NOTE: This is set externally instead of via constructor params because | ||
/// there are cyclic dependencies when loading the renderable layers. | ||
late final List<RenderableLayer> children; | ||
|
||
GroupLayer( | ||
super.layer, | ||
super.parent, | ||
super.map, | ||
super.destTileSize, | ||
); | ||
|
||
@override | ||
void refreshCache() { | ||
for (final child in children) { | ||
child.refreshCache(); | ||
} | ||
} | ||
|
||
@override | ||
void handleResize(Vector2 canvasSize) { | ||
for (final child in children) { | ||
child.handleResize(canvasSize); | ||
} | ||
} | ||
|
||
@override | ||
void render(Canvas canvas, Camera? camera) { | ||
for (final child in children) { | ||
child.render(canvas, camera); | ||
} | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
packages/flame_tiled/lib/src/renderable_layers/image_layer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import 'package:flame/extensions.dart'; | ||
import 'package:flame/flame.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_tiled/src/renderable_layers/group_layer.dart'; | ||
import 'package:flame_tiled/src/renderable_layers/renderable_layer.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:tiled/tiled.dart' as tiled; | ||
|
||
@internal | ||
class ImageLayer extends RenderableLayer<tiled.ImageLayer> { | ||
final Image _image; | ||
late final ImageRepeat _repeat; | ||
Rect _paintArea = Rect.zero; | ||
|
||
ImageLayer( | ||
super.layer, | ||
super.parent, | ||
this._image, | ||
super.map, | ||
super.destTileSize, | ||
) { | ||
_initImageRepeat(); | ||
} | ||
|
||
@override | ||
void handleResize(Vector2 canvasSize) { | ||
_paintArea = Rect.fromLTWH(0, 0, canvasSize.x, canvasSize.y); | ||
} | ||
|
||
@override | ||
void render(Canvas canvas, Camera? camera) { | ||
canvas.save(); | ||
|
||
canvas.translate(offsetX, offsetY); | ||
|
||
if (camera != null) { | ||
applyParallaxOffset(canvas, camera); | ||
} | ||
|
||
paintImage( | ||
canvas: canvas, | ||
rect: _paintArea, | ||
image: _image, | ||
opacity: opacity, | ||
alignment: Alignment.topLeft, | ||
repeat: _repeat, | ||
); | ||
|
||
canvas.restore(); | ||
} | ||
|
||
void _initImageRepeat() { | ||
if (layer.repeatX && layer.repeatY) { | ||
_repeat = ImageRepeat.repeat; | ||
} else if (layer.repeatX) { | ||
_repeat = ImageRepeat.repeatX; | ||
} else if (layer.repeatY) { | ||
_repeat = ImageRepeat.repeatY; | ||
} else { | ||
_repeat = ImageRepeat.noRepeat; | ||
} | ||
} | ||
|
||
static Future<ImageLayer> load( | ||
tiled.ImageLayer layer, | ||
GroupLayer? parent, | ||
Camera? camera, | ||
tiled.TiledMap map, | ||
Vector2 destTileSize, | ||
) async { | ||
return ImageLayer( | ||
layer, | ||
parent, | ||
await Flame.images.load(layer.image.source!), | ||
map, | ||
destTileSize, | ||
); | ||
} | ||
|
||
@override | ||
void refreshCache() {} | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/flame_tiled/lib/src/renderable_layers/object_layer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import 'package:flame/extensions.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_tiled/src/renderable_layers/renderable_layer.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:tiled/tiled.dart'; | ||
|
||
@internal | ||
class ObjectLayer extends RenderableLayer<ObjectGroup> { | ||
ObjectLayer( | ||
super.layer, | ||
super.parent, | ||
super.map, | ||
super.destTileSize, | ||
); | ||
|
||
@override | ||
void render(Canvas canvas, Camera? camera) { | ||
// nothing to do | ||
} | ||
|
||
// ignore unrenderable layers when looping over the layers to render | ||
@override | ||
bool get visible => false; | ||
|
||
static Future<ObjectLayer> load( | ||
ObjectGroup layer, | ||
TiledMap map, | ||
Vector2 destTileSize, | ||
) async { | ||
return ObjectLayer(layer, null, map, destTileSize); | ||
} | ||
|
||
@override | ||
void handleResize(Vector2 canvasSize) {} | ||
|
||
@override | ||
void refreshCache() {} | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/flame_tiled/lib/src/renderable_layers/renderable_layer.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import 'package:flame/extensions.dart'; | ||
import 'package:flame/game.dart'; | ||
import 'package:flame_tiled/src/renderable_layers/group_layer.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:tiled/tiled.dart'; | ||
|
||
@internal | ||
abstract class RenderableLayer<T extends Layer> { | ||
final T layer; | ||
final Vector2 destTileSize; | ||
final TiledMap map; | ||
|
||
/// The parent [Group] layer (if it exists) | ||
final GroupLayer? parent; | ||
|
||
RenderableLayer( | ||
this.layer, | ||
this.parent, | ||
this.map, | ||
this.destTileSize, | ||
); | ||
|
||
bool get visible => layer.visible; | ||
|
||
void render(Canvas canvas, Camera? camera); | ||
|
||
void handleResize(Vector2 canvasSize); | ||
|
||
void refreshCache(); | ||
|
||
double get scaleX => destTileSize.x / map.tileWidth; | ||
double get scaleY => destTileSize.y / map.tileHeight; | ||
|
||
late double offsetX = layer.offsetX * scaleX + (parent?.offsetX ?? 0); | ||
|
||
late double offsetY = layer.offsetY * scaleY + (parent?.offsetY ?? 0); | ||
|
||
late double opacity = layer.opacity * (parent?.opacity ?? 1); | ||
|
||
late double parallaxX = layer.parallaxX * (parent?.parallaxX ?? 1); | ||
|
||
late double parallaxY = layer.parallaxY * (parent?.parallaxY ?? 1); | ||
|
||
/// Calculates the offset we need to apply to the canvas to compensate for | ||
/// parallax positioning and scroll for the layer and the current camera | ||
/// position | ||
/// https://doc.mapeditor.org/en/latest/manual/layers/#parallax-scrolling-factor | ||
void applyParallaxOffset(Canvas canvas, Camera camera) { | ||
final cameraX = camera.position.x; | ||
final cameraY = camera.position.y; | ||
final vpCenterX = camera.viewport.effectiveSize.x / 2; | ||
final vpCenterY = camera.viewport.effectiveSize.y / 2; | ||
|
||
// Due to how Tiled treats the center of the view as the reference | ||
// point for parallax positioning (see Tiled docs), we need to offset the | ||
// entire layer | ||
var x = (1 - parallaxX) * vpCenterX; | ||
var y = (1 - parallaxY) * vpCenterY; | ||
// compensate the offset for zoom | ||
x /= camera.zoom; | ||
y /= camera.zoom; | ||
|
||
// Now add the scroll for the current camera position | ||
x += cameraX - (cameraX * parallaxX); | ||
y += cameraY - (cameraY * parallaxY); | ||
|
||
canvas.translate(x, y); | ||
} | ||
} | ||
|
||
@internal | ||
class UnsupportedLayer extends RenderableLayer { | ||
UnsupportedLayer( | ||
super.layer, | ||
super.parent, | ||
super.map, | ||
super.destTileSize, | ||
); | ||
|
||
@override | ||
void render(Canvas canvas, Camera? camera) {} | ||
|
||
@override | ||
void handleResize(Vector2 canvasSize) {} | ||
|
||
@override | ||
void refreshCache() {} | ||
} |
Oops, something went wrong.