Advertisement
MikeWhitfordAuthor

Untitled

Jan 20th, 2024 (edited)
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. func rook_move(object, start_square, new_square):
  2.     var start_xy = Global.label_to_xy(start_square.label)
  3.     var new_xy = Global.label_to_xy(new_square.label)
  4.     if new_xy.x != start_xy.x and new_xy.y != start_xy.y:
  5.         return false
  6.     if new_xy.y == start_xy.y:
  7.         if new_xy.x > start_xy.x:
  8.             for i in range(start_xy.x + 1, new_xy.x, 1):
  9.                 var square = Global.xy_to_square(Vector2i(i, start_xy.y))
  10.                 if square.occupied:
  11.                     if square.guest.colour != colour:
  12.                         return true
  13.                     else:
  14.                         return false
  15.         else:
  16.             for i in range(new_xy.x - 1, start_xy.x, -1):
  17.                 var square = Global.xy_to_square(Vector2i(i, start_xy.y))
  18.                 if square.occupied:
  19.                     if square.guest.colour == colour:
  20.                         return false
  21.                     else:
  22.                         return true
  23.             return true
  24.     elif new_xy.x == start_xy.x:
  25.         if new_xy.y > start_xy.y:
  26.             for i in range(start_xy.y + 1, new_xy.y, 1):
  27.                 var square = Global.xy_to_square(Vector2i(start_xy.x, i))
  28.                 if square.occupied:
  29.                     if square.guest.colour != colour:
  30.                         return true
  31.                     else:
  32.                         return false
  33.         else:
  34.             for i in range(new_xy.y - 1, start_xy.y, -1):
  35.                 var square = Global.xy_to_square(Vector2i(start_xy.x, i))
  36.                 if square.occupied:
  37.                     if square.guest.colour == colour:
  38.                         return false
  39.                     else:
  40.                         return true
  41.             return true
  42.     else:
  43.         return false
  44.  
  45. ----
  46. # Global functions used above:
  47.  
  48. func label_to_xy(label: String):
  49.     var out: Vector2i
  50.     var files = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "H": 8}
  51.     var file = files[label[0]]
  52.     var rank = int(label[1])
  53.     out = Vector2i(file, rank)
  54.     return out
  55.  
  56. func xy_to_square(xy: Vector2i):
  57.     var label = xy_to_label(xy)
  58.     var square = label_squareobj[label]
  59.     return square
  60.  
  61. # The Squares class
  62. extends Node2D
  63. class_name squares
  64. signal broadcast(ID: Object, label: String, occupied: bool)
  65.  
  66. @export var light: bool
  67. var label: String
  68. var occupied: bool
  69. var guest: Object
  70. var xy: Vector2i
  71.  
  72. func _ready():
  73.     $Sprite2D.scale = Vector2i(70,70)
  74.     if light: $Sprite2D.self_modulate = Color(0.98, 0.647, 0.455)
  75.     else: $Sprite2D.self_modulate = Color(0.321, 0.175, 0.029)
  76.     var Label = Label.new()
  77.     Label.text = label
  78.     if light: Label.self_modulate.a = 0.9
  79.     else: Label.self_modulate.a = 0.5
  80.     Label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
  81.     Label.vertical_alignment = VERTICAL_ALIGNMENT_BOTTOM
  82.     Label.position.x += 10
  83.     Label.position.y += 10
  84.     add_child(Label)
  85.     emit_signal("broadcast", self, label, null)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement