Advertisement
Tkap1

Untitled

Nov 15th, 2020 (edited)
1,785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. # Main.gd
  5.  
  6. extends Control
  7.  
  8. enum {
  9.     RED,GREEN,BLUE,COUNT
  10. }
  11.  
  12. var type_to_color = {
  13.     RED: Color.red,
  14.     GREEN: Color.green,
  15.     BLUE: Color.blue
  16. }
  17.  
  18. var matrix := [
  19.     [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
  20.     [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
  21.     [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
  22.     [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
  23.     [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
  24. ]
  25.  
  26. var element_size = 64
  27. var pieces := []
  28. var piece_scene = preload("res://Piece.tscn")
  29.  
  30. onready var ray = $RayCast2D
  31.  
  32.  
  33. func _ready():
  34.     randomize()
  35.     generate_matrix()
  36.    
  37.    
  38. func _physics_process(delta):
  39.  
  40.     ray.position.x += 50 * delta
  41.     if(ray.position.x > get_columns() * element_size):
  42.         ray.position.x = 0
  43.        
  44.    
  45.     for y in get_rows():
  46.         for x in get_columns():
  47.             var piece = pieces[y][x]
  48.             piece.checked = false
  49.        
  50.     var collider = ray.get_collider()
  51.     if collider:
  52.         check_piece(collider)
  53.            
  54.  
  55. func generate_matrix():
  56.     var rows = get_rows()
  57.     var columns = get_columns()
  58.     if pieces.size() > 0:
  59.         for y in rows:
  60.             for x in columns:
  61.                 pieces[y][x].queue_free()
  62.         pieces.clear()
  63.     for y in rows:
  64.         pieces.append([])
  65.         for x in columns:
  66.             var piece = piece_scene.instance()
  67.             add_child(piece)
  68.             piece.position = Vector2(x*element_size+32, y*element_size+32)
  69.             piece.x = x
  70.             piece.y = y
  71.             piece.type = matrix[y][x]
  72.             piece.modulate = type_to_color[piece.type]
  73.             pieces[y].append(piece)
  74.  
  75.  
  76. func get_rows():
  77.     return matrix.size()
  78.  
  79. func get_columns():
  80.     return matrix[0].size()
  81.  
  82. func get_left(x, y):
  83.     if x > 0:
  84.         return pieces[y][x-1]
  85.        
  86. func get_right(x, y):
  87.     if x < get_columns() - 1:
  88.         return pieces[y][x+1]
  89.        
  90. func get_top(x, y):
  91.     if y > 0:
  92.         return pieces[y-1][x]
  93.        
  94. func get_bottom(x, y):
  95.     if y < get_rows() - 1:
  96.         return pieces[y+1][x]
  97.  
  98.  
  99. func check_piece(piece):
  100.  
  101.     piece.checked = true
  102.    
  103.     var left = get_left(piece.x, piece.y)
  104.     if left and !left.checked and left.type == piece.type:
  105.         check_piece(left)
  106.        
  107.     var right = get_right(piece.x, piece.y)
  108.     if right and !right.checked and right.type == piece.type:
  109.         check_piece(right)
  110.        
  111.     var top = get_top(piece.x, piece.y)
  112.     if top and !top.checked and top.type == piece.type:
  113.         check_piece(top)
  114.        
  115.     var bottom = get_bottom(piece.x, piece.y)
  116.     if bottom and !bottom.checked and bottom.type == piece.type:
  117.         check_piece(bottom)
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. # Piece.gd
  138.  
  139. extends Area2D
  140.  
  141. var x = 0
  142. var y = 0
  143. var type = 0
  144. var checked := false
  145. var growing := false
  146.  
  147. onready var sprite = $Sprite
  148.  
  149. func _physics_process(delta):
  150.  
  151.     if checked:
  152.         if growing:
  153.             sprite.scale *= 1.1
  154.             if(sprite.scale.x >= 1): growing = false
  155.         else:
  156.             sprite.scale *= 0.9
  157.             if(sprite.scale.x <= 0.1): growing = true
  158.            
  159.     else:
  160.         sprite.scale = Vector2(1, 1)
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement