-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGridControl.gd
More file actions
104 lines (86 loc) · 2.92 KB
/
Copy pathGridControl.gd
File metadata and controls
104 lines (86 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
extends Node2D
# REFERENCES
onready var cursor = $CursorSprite
onready var gridBG = $GridBG
var sel_tile
var previousX
var previousY
# Declare member variables here. Examples:
var grid = []
var rows = 7
var columns = 7
var gridPosition = Vector2.ZERO
var incrementor = 1.0
enum {NONE , UP , DOWN , LEFT , RIGHT}
var directionToSwap = NONE
var spriteWidth = 64
var spriteOffset = 3
var lastColorId = -1
var selected = false
var temp
# Called when the node enters the scene tree for the first time.
func _ready():
var screenSize = get_viewport_rect().size
# Add a tile symbol
var scene = load("res://Symbol.tscn")
# Setup Array
for x in range(rows):
grid.append([])
for y in range(columns):
var tile = scene.instance()
#times 2 to split the screen in haft
tile.position.x = screenSize.x/2 - (spriteOffset * spriteWidth) + x * spriteWidth
tile.position.y = screenSize.y/2 - (spriteOffset * spriteWidth) + y * spriteWidth
add_child(tile)
grid[x].append(tile)
# Setup cursor sprite in top left of the grid
cursor.position.x = screenSize.x/2 - (spriteOffset * spriteWidth)
cursor.position.y = screenSize.y/2 - (spriteOffset * spriteWidth)
# Center the grid sprite
gridBG.position.x = screenSize.x/2
gridBG.position.y = screenSize.y/2
func _on_Button_pressed(extra_arg_0: String) -> void:
print(extra_arg_0)
func _physics_process(delta):
handleMove(delta)
func _move():
print("swapped")
temp = grid[gridPosition.x][gridPosition.y].tile.frame
grid[gridPosition.x][gridPosition.y].tile.frame = sel_tile
grid[previousX][previousY].tile.frame = temp
selected = !selected
# Possible optimizations:
# Couple the grid indices to the movement so that when the index changes,
# the position can update with it
# Plus all this repetitive code can probably be reorganized
func handleMove(_delta):
if Input.is_action_just_pressed("ui_up") and gridPosition.y-incrementor >= 0:
gridPosition.y -= incrementor
cursor.position.y -= incrementor*spriteWidth
if(selected):
_move()
elif Input.is_action_just_pressed("ui_down") and gridPosition.y+incrementor <= rows-1:
gridPosition.y += incrementor
cursor.position.y += incrementor*spriteWidth
if(selected):
_move()
elif Input.is_action_just_pressed("ui_left") and gridPosition.x-incrementor >= 0:
gridPosition.x -= incrementor
cursor.position.x -= incrementor*spriteWidth
if(selected):
_move()
elif Input.is_action_just_pressed("ui_right") and gridPosition.x+incrementor <= columns-1:
gridPosition.x += incrementor
cursor.position.x += incrementor*spriteWidth
if(selected):
_move()
elif Input.is_action_just_pressed("ui_accept"):
if !selected:
print("Selected element: " + str(gridPosition))
sel_tile = grid[gridPosition.x][gridPosition.y].tile.frame
previousX = gridPosition.x
previousY = gridPosition.y
selected = !selected
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass