5 @export var piece_name: String = ""
7 var cells: Array[Cell] = []
8 var block_size: int = 0
10 var _cell_grid: Array[Vector2i] = []
12 var _rotation_angles: Array[float] = [0.0, 90.0, 180.0, 270.0]
13 var _rotation_index: int = 0
16 func _ready() -> void:
17 for c in self.get_children():
21 # all cells must have same block size
24 block_size = c.block_size
26 assert(block_size == c.block_size, "All cells must have same block size")
28 # build cell grid coordinates
30 var pos_x: int = floori(c.position.x / block_size)
31 var pos_y: int = floori(c.position.y / block_size)
32 _cell_grid.append(Vector2i(pos_x, pos_y))
35 func _to_string() -> String:
36 return "%d: %s" % [_rotation_angles[_rotation_index % _rotation_angles.size()], str(get_cell_grid())]
39 func get_cell_grid() -> Array:
40 var rot = _rotation_angles[_rotation_index % _rotation_angles.size()]
43 return _cell_grid.duplicate()
45 return _cell_grid.map(func f(v): return Vector2i(-v.y, v.x))
47 return _cell_grid.map(func f(v): return Vector2i(-v.x, -v.y))
49 return _cell_grid.map(func f(v): return Vector2i(v.y, -v.x))
51 assert(false, "Invalid rotation: " + str(rot))
52 return _cell_grid.duplicate()
55 func rotate_cells_right():
60 func rotate_cells_left():
66 var new_cell_grid = get_cell_grid()
69 var new_pos: Vector2i = new_cell_grid.pop_front()
70 c.position.x = new_pos.x * block_size
71 c.position.y = new_pos.y * block_size