X-Git-Url: http://git.purplebirdman.com/catris.git/blobdiff_plain/35c13ea7b3cdfe2da392fbd9c5b0c7aac97f5d6f..a626f4d0c215bd5f18ad3073ef9bda8e48ab8138:/script/piece.gd diff --git a/script/piece.gd b/script/piece.gd index f2aa81c..33a9c7d 100644 --- a/script/piece.gd +++ b/script/piece.gd @@ -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,8 +29,27 @@ 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 str(cell_grid) + return "%d: %s" % [rotation_degrees, str(get_cell_grid())] + + +func get_cell_grid() -> Array: + if rotation_degrees == 0.0: + return _cell_grid + elif rotation_degrees == 90.0: + return _cell_grid.map(func f(v): return Vector2i(v.y, v.x)) + elif rotation_degrees == 180.0: + return _cell_grid.map(func f(v): return Vector2i(-v.x, -v.y)) + elif rotation_degrees == 270.0: + return _cell_grid.map(func f(v): return Vector2i(-v.y, -v.x)) + else: + assert(false, "Invalid rotation: " + str(global_rotation_degrees)) + return [] + + +func rotate_left(): + _rotation_index += 1 + rotation_degrees = _rotation_angles[_rotation_index % _rotation_angles.size()]