]> Untitled Git - catris.git/blobdiff - script/piece.gd
Broken: doesn't rotate correctly on the right side
[catris.git] / script / piece.gd
index aa50f0a231615b17828873e912d600f74718ee28..51582416c87a1cd8f8b6445425dee077a2196de4 100644 (file)
@@ -7,7 +7,11 @@ class_name Piece
 var cells: Array[Cell] = []
 var block_size: int = 0
 
-var cell_grid: Array[Vector2i] = []
+var _cell_grid: Array[Vector2i] = []
+
+var _rotation_angles: Array[float] = [0.0, 90.0, 180.0, 270.0]
+var _rotation_index: int = 0
+
 
 func _ready() -> void:
        for c in self.get_children():
@@ -25,4 +29,43 @@ func _ready() -> void:
        for c in cells:
                var pos_x: int = floori(c.position.x / block_size)
                var pos_y: int = floori(c.position.y / block_size)
-               cell_grid.append(Vector2i(pos_x, pos_y))
+               _cell_grid.append(Vector2i(pos_x, pos_y))
+
+
+func _to_string() -> String:
+       return "%d: %s" % [_rotation_angles[_rotation_index % _rotation_angles.size()], str(get_cell_grid())]
+
+
+func get_cell_grid() -> Array:
+       var rot = _rotation_angles[_rotation_index % _rotation_angles.size()]
+       
+       if rot == 0.0:
+               return _cell_grid.duplicate()
+       elif rot == 90.0:
+               return _cell_grid.map(func f(v): return Vector2i(-v.y, v.x))
+       elif rot == 180.0:
+               return _cell_grid.map(func f(v): return Vector2i(-v.x, -v.y))
+       elif rot == 270.0:
+               return _cell_grid.map(func f(v): return Vector2i(v.y, -v.x))
+       else:
+               assert(false, "Invalid rotation: " + str(rot))
+       return _cell_grid.duplicate()
+
+
+func rotate_cells_right():
+       _rotation_index -= 1
+       rotate_cells()
+
+
+func rotate_cells_left():
+       _rotation_index += 1
+       rotate_cells()
+
+
+func rotate_cells():
+       var new_cell_grid = get_cell_grid()
+       
+       for c in cells:
+               var new_pos: Vector2i = new_cell_grid.pop_front()
+               c.position.x = new_pos.x * block_size
+               c.position.y = new_pos.y * block_size