refactor structure
This commit is contained in:
8
scenes/match3/Tile.tscn
Normal file
8
scenes/match3/Tile.tscn
Normal file
@@ -0,0 +1,8 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://bnk1gqom3oi6q"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://3bu5wgfarlgp" path="res://scenes/match3/tile.gd" id="1_tile_script"]
|
||||
|
||||
[node name="Tile" type="Node2D"]
|
||||
script = ExtResource("1_tile_script")
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
102
scenes/match3/match3.gd
Normal file
102
scenes/match3/match3.gd
Normal file
@@ -0,0 +1,102 @@
|
||||
extends Node2D
|
||||
|
||||
const GRID_SIZE := Vector2i(8, 8)
|
||||
const TILE_TYPES := 5
|
||||
const TILE_SCENE := preload("res://scenes/match3/tile.tscn")
|
||||
|
||||
var grid := []
|
||||
|
||||
func _ready():
|
||||
_initialize_grid()
|
||||
|
||||
func _initialize_grid():
|
||||
for y in range(GRID_SIZE.y):
|
||||
grid.append([])
|
||||
for x in range(GRID_SIZE.x):
|
||||
var tile = TILE_SCENE.instantiate()
|
||||
tile.position = Vector2(x, y) * 64 # Adjust to your tile size
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
add_child(tile)
|
||||
grid[y].append(tile)
|
||||
|
||||
func _has_match_at(pos: Vector2i) -> bool:
|
||||
var matches_horizontal = _get_match_line(pos, Vector2i(1, 0))
|
||||
if matches_horizontal.size() >= 3:
|
||||
return true
|
||||
|
||||
var matches_vertical = _get_match_line(pos, Vector2i(0, 1))
|
||||
return matches_vertical.size() >= 3
|
||||
|
||||
func _get_match_line(start: Vector2i, dir: Vector2i) -> Array:
|
||||
var line = [grid[start.y][start.x]]
|
||||
var type = grid[start.y][start.x].tile_type
|
||||
|
||||
for offset in [1, -1]:
|
||||
var current = start + dir * offset
|
||||
while current.x >= 0 and current.y >= 0 and current.x < GRID_SIZE.x and current.y < GRID_SIZE.y:
|
||||
var neighbor = grid[current.y][current.x]
|
||||
if neighbor.tile_type != type:
|
||||
break
|
||||
line.append(neighbor)
|
||||
current += dir * offset
|
||||
|
||||
return line
|
||||
|
||||
func _clear_matches():
|
||||
var to_clear := []
|
||||
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
var pos = Vector2i(x, y)
|
||||
var horiz = _get_match_line(pos, Vector2i(1, 0))
|
||||
if horiz.size() >= 3:
|
||||
to_clear.append_array(horiz)
|
||||
var vert = _get_match_line(pos, Vector2i(0, 1))
|
||||
if vert.size() >= 3:
|
||||
to_clear.append_array(vert)
|
||||
|
||||
# Remove duplicates using dictionary keys trick
|
||||
var unique_dict := {}
|
||||
for tile in to_clear:
|
||||
unique_dict[tile] = true
|
||||
to_clear = unique_dict.keys()
|
||||
|
||||
for tile in to_clear:
|
||||
grid[tile.grid_position.y][tile.grid_position.x] = null
|
||||
tile.queue_free()
|
||||
|
||||
_drop_tiles()
|
||||
await get_tree().create_timer(0.2).timeout
|
||||
_fill_empty_cells()
|
||||
|
||||
func _drop_tiles():
|
||||
var moved = true
|
||||
while moved:
|
||||
moved = false
|
||||
for x in range(GRID_SIZE.x):
|
||||
for y in range(GRID_SIZE.y - 2, -1, -1):
|
||||
var tile = grid[y][x]
|
||||
if tile and not grid[y + 1][x]:
|
||||
grid[y + 1][x] = tile
|
||||
grid[y][x] = null
|
||||
tile.grid_position = Vector2i(x, y + 1)
|
||||
# You can animate position here using Tween for smooth drop:
|
||||
# tween.interpolate_property(tile, "position", tile.position, Vector2(x, y + 1) * 64, 0.2)
|
||||
tile.position = Vector2(x, y + 1) * 64
|
||||
moved = true
|
||||
|
||||
func _fill_empty_cells():
|
||||
for y in range(GRID_SIZE.y):
|
||||
for x in range(GRID_SIZE.x):
|
||||
if not grid[y][x]:
|
||||
var tile = TILE_SCENE.instantiate()
|
||||
tile.grid_position = Vector2i(x, y)
|
||||
tile.tile_type = randi() % TILE_TYPES
|
||||
tile.position = Vector2(x, y) * 64
|
||||
grid[y][x] = tile
|
||||
add_child(tile)
|
||||
|
||||
# Recheck matches
|
||||
await get_tree().create_timer(0.1).timeout
|
||||
_clear_matches()
|
||||
13
scenes/match3/match3.tscn
Normal file
13
scenes/match3/match3.tscn
Normal file
@@ -0,0 +1,13 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://b4kv7g7kllwgb"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bvyiwvpepqfdu" path="res://scenes/match3/match3.gd" id="1_mvfdp"]
|
||||
[ext_resource type="Texture2D" uid="uid://ccprr0qrj3lgm" path="res://assets/sprites/gems/rg_19.png" id="2_0fhn8"]
|
||||
|
||||
[node name="Match3" type="Node2D"]
|
||||
script = ExtResource("1_mvfdp")
|
||||
|
||||
[node name="GridContainer" type="Node2D" parent="."]
|
||||
|
||||
[node name="Sprite2D" type="Sprite2D" parent="."]
|
||||
position = Vector2(584, 317)
|
||||
texture = ExtResource("2_0fhn8")
|
||||
33
scenes/match3/tile.gd
Normal file
33
scenes/match3/tile.gd
Normal file
@@ -0,0 +1,33 @@
|
||||
extends Node2D
|
||||
|
||||
@export var tile_type: int = 0
|
||||
var grid_position: Vector2i
|
||||
|
||||
@onready var sprite: Sprite2D = $Sprite2D
|
||||
|
||||
# Target size for each tile to fit in the 64x64 grid cells
|
||||
const TILE_SIZE = 48 # Slightly smaller than 64 to leave some padding
|
||||
|
||||
var tile_textures = [
|
||||
preload("res://assets/sprites/gems/bg_19.png"),
|
||||
preload("res://assets/sprites/gems/dg_19.png"),
|
||||
preload("res://assets/sprites/gems/gg_19.png"),
|
||||
preload("res://assets/sprites/gems/mg_19.png"),
|
||||
]
|
||||
|
||||
func _set_tile_type(value: int) -> void:
|
||||
tile_type = value
|
||||
if value >= 0 and value < tile_textures.size():
|
||||
sprite.texture = tile_textures[value]
|
||||
_scale_sprite_to_fit()
|
||||
|
||||
func _scale_sprite_to_fit() -> void:
|
||||
if sprite.texture:
|
||||
var texture_size = sprite.texture.get_size()
|
||||
var max_dimension = max(texture_size.x, texture_size.y)
|
||||
var scale_factor = TILE_SIZE / max_dimension
|
||||
sprite.scale = Vector2(scale_factor, scale_factor)
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
_set_tile_type(tile_type)
|
||||
Reference in New Issue
Block a user