]> Untitled Git - catris.git/blob - script/piece.gd
Incomplete rotation handling
[catris.git] / script / piece.gd
1 extends Node2D
2 class_name Piece
3
4
5 @export var piece_name: String = ""
6
7 var cells: Array[Cell] = []
8 var block_size: int = 0
9
10 var _cell_grid: Array[Vector2i] = []
11
12 var _rotation_angles: Array[float] = [0.0, 90.0, 180.0, 270.0]
13 var _rotation_index: int = 0
14
15
16 func _ready() -> void:
17         for c in self.get_children():
18                 if c is Cell:
19                         cells.append(c)
20         
21         # all cells must have same block size
22         for c in cells:
23                 if block_size == 0:
24                         block_size = c.block_size
25                 else:
26                         assert(block_size == c.block_size, "All cells must have same block size")
27         
28         # build cell grid coordinates
29         for c in cells:
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))
33
34
35 func _to_string() -> String:
36         return "%d: %s" % [rotation_degrees, str(get_cell_grid())]
37
38
39 func get_cell_grid() -> Array:
40         if rotation_degrees == 0.0:
41                 return _cell_grid
42         elif rotation_degrees == 90.0:
43                 return _cell_grid.map(func f(v): return Vector2i(v.y, v.x))
44         elif rotation_degrees == 180.0:
45                 return _cell_grid.map(func f(v): return Vector2i(-v.x, -v.y))
46         elif rotation_degrees == 270.0:
47                 return _cell_grid.map(func f(v): return Vector2i(-v.y, -v.x))
48         else:
49                 assert(false, "Invalid rotation: " + str(global_rotation_degrees))
50         return []
51
52
53 func rotate_left():
54         _rotation_index += 1
55         rotation_degrees = _rotation_angles[_rotation_index % _rotation_angles.size()]