Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Minimize Vector2 creation in IsometricTileMapComponent #3018

Merged
merged 5 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions packages/flame/lib/src/components/isometric_tile_map_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ class IsometricTileMapComponent extends PositionComponent {
final element = matrix[i][j];
if (element != -1) {
_renderSprite = tileset.getSpriteById(element);
final p = getBlockRenderPositionInts(j, i);
final blockPosition = getBlockRenderPositionInts(j, i);
_renderSprite.render(
canvas,
position: p,
position: blockPosition,
size: size,
);
}
Expand All @@ -110,15 +110,21 @@ class IsometricTileMapComponent extends PositionComponent {
return getBlockRenderPositionInts(block.x, block.y);
}

final Vector2 _blockRenderPositionCache = Vector2.zero();
final Vector2 _cartesianPositionCache = Vector2.zero();

/// Same as getBlockRenderPosition but the arguments are exploded as integers.
Vector2 getBlockRenderPositionInts(int i, int j) {
final halfTile = Vector2(
effectiveTileSize.x / 2,
(effectiveTileSize.y / 2) / scalingFactor,
)..multiply(scale);
final cartesianPosition = Vector2(i.toDouble(), j.toDouble())
final halfTile = _blockRenderPositionCache
..setValues(
effectiveTileSize.x / 2,
(effectiveTileSize.y / 2) / scalingFactor,
)
..multiply(scale);
final cartesianPosition = _cartesianPositionCache
..setValues(i.toDouble(), j.toDouble())
..multiply(halfTile);
return cartToIso(cartesianPosition) - halfTile;
return cartToIso(cartesianPosition)..sub(halfTile);
}

/// Get the position of the center of the surface of the isometric tile in
Expand All @@ -127,10 +133,11 @@ class IsometricTileMapComponent extends PositionComponent {
/// This is the opposite of [getBlock].
Vector2 getBlockCenterPosition(Block block) {
final tile = effectiveTileSize;
final result = getBlockRenderPosition(block) +
(Vector2(tile.x / 2, tile.y - effectiveTileHeight - tile.y / 4)
..multiply(scale));
return result;
return getBlockRenderPosition(block)
..addValues(
spydon marked this conversation as resolved.
Show resolved Hide resolved
(tile.x / 2) * scale.x,
(tile.y - effectiveTileHeight - tile.y / 4) * scale.y,
);
}

/// Converts a coordinate from the isometric space to the cartesian space.
Expand All @@ -147,29 +154,42 @@ class IsometricTileMapComponent extends PositionComponent {
return Vector2(x, y);
}

final Vector2 _getBlockCache = Vector2.zero();
final Vector2 _getBlockIsoCache = Vector2.zero();

/// Get which block's surface is at isometric position [p].
///
/// This can be used to handle clicks or hovers.
/// This is the opposite of [getBlockCenterPosition].
Block getBlock(Vector2 p) {
final halfTile = (effectiveTileSize / 2)..multiply(scale);
final halfTile = _getBlockCache
..setFrom(effectiveTileSize)
..multiply(scale / 2);
final multiplier = 1 - halfTile.y / (2 * effectiveTileHeight * scale.x);
final delta = halfTile.clone()..multiply(Vector2(1, multiplier));
final cart = isoToCart(p - position + delta);
final iso = _getBlockIsoCache
..setFrom(p)
..sub(position)
..addValues(halfTile.x, halfTile.y * multiplier);
spydon marked this conversation as resolved.
Show resolved Hide resolved
final cart = isoToCart(iso);
final px = (cart.x / halfTile.x - 1).ceil();
final py = (cart.y / halfTile.y).ceil();
return Block(px, py);
}

final Vector2 _blockPositionCache = Vector2.zero();

/// Get which block should be rendered on position [p].
///
/// This is the opposite of [getBlockRenderPosition].
Block getBlockRenderedAt(Vector2 p) {
final tile = effectiveTileSize;
return getBlock(
p +
(Vector2(tile.x / 2, tile.y - effectiveTileHeight - tile.y / 4)
..multiply(scale)),
_blockPositionCache
..setFrom(p)
..addValues(
spydon marked this conversation as resolved.
Show resolved Hide resolved
(tile.x / 2) * scale.x,
(tile.y - effectiveTileHeight - tile.y / 4) * scale.y,
),
);
}

Expand Down
6 changes: 6 additions & 0 deletions packages/flame/lib/src/extensions/vector2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ extension Vector2Extension on Vector2 {
}
}

/// Adds [x] and [y] to the current [Vector2].
void addValues(double x, double y) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that same as translate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aaah, of course, good catch :)

this.x += x;
this.y += y;
}

spydon marked this conversation as resolved.
Show resolved Hide resolved
/// Signed angle in a coordinate system where the Y-axis is flipped.
///
/// Since on a canvas/screen y is smaller the further up you go, instead of
Expand Down
6 changes: 6 additions & 0 deletions packages/flame/test/extensions/vector2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ void main() {
expect(p1.hashCode == p2.hashCode, false);
});

test('addValues', () {
final p = Vector2(1.0, 0.0);
p.addValues(2.0, 3.0);
expect(p, Vector2(3.0, 3.0));
});

spydon marked this conversation as resolved.
Show resolved Hide resolved
test('limit', () {
final p1 = Vector2(1.0, 0.0);
p1.clampScalar(0, 0.75);
Expand Down
Loading