Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Main.gd
- extends Control
- enum {
- RED,GREEN,BLUE,COUNT
- }
- var type_to_color = {
- RED: Color.red,
- GREEN: Color.green,
- BLUE: Color.blue
- }
- var matrix := [
- [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
- [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
- [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
- [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
- [randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT,randi() % COUNT],
- ]
- var element_size = 64
- var pieces := []
- var piece_scene = preload("res://Piece.tscn")
- onready var ray = $RayCast2D
- func _ready():
- randomize()
- generate_matrix()
- func _physics_process(delta):
- ray.position.x += 50 * delta
- if(ray.position.x > get_columns() * element_size):
- ray.position.x = 0
- for y in get_rows():
- for x in get_columns():
- var piece = pieces[y][x]
- piece.checked = false
- var collider = ray.get_collider()
- if collider:
- check_piece(collider)
- func generate_matrix():
- var rows = get_rows()
- var columns = get_columns()
- if pieces.size() > 0:
- for y in rows:
- for x in columns:
- pieces[y][x].queue_free()
- pieces.clear()
- for y in rows:
- pieces.append([])
- for x in columns:
- var piece = piece_scene.instance()
- add_child(piece)
- piece.position = Vector2(x*element_size+32, y*element_size+32)
- piece.x = x
- piece.y = y
- piece.type = matrix[y][x]
- piece.modulate = type_to_color[piece.type]
- pieces[y].append(piece)
- func get_rows():
- return matrix.size()
- func get_columns():
- return matrix[0].size()
- func get_left(x, y):
- if x > 0:
- return pieces[y][x-1]
- func get_right(x, y):
- if x < get_columns() - 1:
- return pieces[y][x+1]
- func get_top(x, y):
- if y > 0:
- return pieces[y-1][x]
- func get_bottom(x, y):
- if y < get_rows() - 1:
- return pieces[y+1][x]
- func check_piece(piece):
- piece.checked = true
- var left = get_left(piece.x, piece.y)
- if left and !left.checked and left.type == piece.type:
- check_piece(left)
- var right = get_right(piece.x, piece.y)
- if right and !right.checked and right.type == piece.type:
- check_piece(right)
- var top = get_top(piece.x, piece.y)
- if top and !top.checked and top.type == piece.type:
- check_piece(top)
- var bottom = get_bottom(piece.x, piece.y)
- if bottom and !bottom.checked and bottom.type == piece.type:
- check_piece(bottom)
- # Piece.gd
- extends Area2D
- var x = 0
- var y = 0
- var type = 0
- var checked := false
- var growing := false
- onready var sprite = $Sprite
- func _physics_process(delta):
- if checked:
- if growing:
- sprite.scale *= 1.1
- if(sprite.scale.x >= 1): growing = false
- else:
- sprite.scale *= 0.9
- if(sprite.scale.x <= 0.1): growing = true
- else:
- sprite.scale = Vector2(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement