flame icon indicating copy to clipboard operation
flame copied to clipboard

`IsometricTileMapComponent` doesn't set its `size` correctly

Open TheDarkUndoing opened this issue 3 years ago • 5 comments

I am currently trying to create a draggable IsometricTileComponent. So that a player will be able to drag and re position the board around the viewport. I tried to copy the drag_events example since IsometricTileComponent is a PositionComponent. However something is wrong with my implementation as it does not act like that example.

class GameBoard extends IsometricTileMapComponent with DragCallbacks {
  late List<List<Enum>> state;
  SpriteSheet spriteSheet;
  int boardsections;

  // late Selector selector;

  bool _isDragged = false;

  // late final board;
  List<List<Vector2>> centerPointMap = List.empty(growable: true);

  GameBoard(
      {required this.boardsections,
      required this.spriteSheet,
      required Vector2 destTileSize,
      Vector2? position})
      : super(spriteSheet, [], destTileSize: destTileSize, position: position) {
    state = List<List<Enum>>.filled(
        boardsections, List<Enum>.filled(boardsections, BoardPiece.empty));
    matrix = List<List<int>>.filled(
        boardsections, List<int>.filled(boardsections, 0));
  }
  @override
  Future<void> onLoad() async {
    debugMode = true;
    print("${anchor} $absoluteCenter} ");
    anchor = Anchor.center;
    // width = 800;
    // height = 80;
    position = Vector2.all(300);

    size = Vector2.all(800);
    centerPointMap = generateCenterPointMap();
    addPiece(centerPointMap[0][1], Buildings.blackWarehouse);
  }

  void addPiece(Vector2 position, Enum type) {
    print("Adding $type to board at $position");
    var piece = GamePiece(type, cartToIso(centerPointMap[3][3]), spriteSheet);
    add(piece);
  }

  @override
  void render(Canvas canvas) {
    super.render(canvas);
  }

  @override
  void onDragStart(DragStartEvent event) {
    _isDragged = true;
    print("Being Dragged");
  }

  @override
  void onDragEnd(DragEndEvent event) {
    _isDragged = false;
  }

  @override
  void onDragUpdate(DragUpdateEvent event) {
    position += event.delta;
    // position += event.delta;

    centerPointMap = generateCenterPointMap();
    print("Board Size: ${size} Board Position: ${position}");
  }

  List<List<Vector2>> generateCenterPointMap() {
    print("From GenerateCenterMap {Matrix:$matrix}");
    List<List<Vector2>> centerPointMap = List.empty(growable: true);
    var tileSize = destTileSize!.y / 2;
    print("TILESIZE: $tileSize");

    //Use tilesize to iterate all positions of Board
    for (var row = 0;
        row <= (boardsections - 1) * tileSize;
        row += tileSize.toInt()) {
      var rowList = <Vector2>[];
      for (var col = 0;
          col <= (boardsections - 1) * tileSize;
          col += tileSize.toInt()) {
        var block = Vector2(row.toDouble(), col.toDouble());
        print("Block at $row,$col = $block");

        rowList.add(block);
      }
      centerPointMap.add(rowList);
    }
    return centerPointMap;
  }
}

Here is the behavior I get with that code. I would like to be able to click anywhere on the board to drag. Screencast from 2022-11-25 00-33-32.webm

TheDarkUndoing avatar Nov 25 '22 05:11 TheDarkUndoing

The IsometricTileMapComponent doesn't seem to handle its size properly at the moment. A workaround for this is to override containsLocalPoint so that it always returns true or to handle the drags in the FlameGame instead of in the component.

spydon avatar Nov 25 '22 08:11 spydon

Thank you for that tip. I would be interested in learning why overriding the containsLocalPoint function has that effect.

TheDarkUndoing avatar Nov 25 '22 16:11 TheDarkUndoing

Thank you for that tip. I would be interested in learning why overriding the containsLocalPoint function has that effect.

Because normally it checks whether the event is within the bounds of the effect (the position+size), but since the bounds are wrong here you can say that all events should be regarded as within this component by setting that to true.

spydon avatar Nov 25 '22 19:11 spydon

I wonder how that effects the mouse clicks on the tiles. Will have to test that functionality when I implement that part of the ui

TheDarkUndoing avatar Nov 25 '22 21:11 TheDarkUndoing

I wonder how that effects the mouse clicks on the tiles. Will have to test that functionality when I implement that part of the ui

It should be fine since the drags and taps are different events in the gesture arena.

spydon avatar Nov 27 '22 17:11 spydon