diff --git a/Main.gd b/Main.gd index 329e501..a39023d 100644 --- a/Main.gd +++ b/Main.gd @@ -3,18 +3,3 @@ extends Node # Called when the node enters the scene tree for the first time. func _ready(): pass - -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(_delta): - if Input.is_action_just_pressed("ui_up"): - $GemGrid.up() - elif Input.is_action_just_pressed("ui_down"): - $GemGrid.down() - elif Input.is_action_just_pressed("ui_left"): - $GemGrid.left() - elif Input.is_action_just_pressed("ui_right"): - $GemGrid.right() - elif Input.is_action_just_pressed("ui_accept"): - $GemGrid.select() - elif Input.is_action_just_pressed("ui_cancel"): - $GemGrid.cancel() diff --git a/Main.tscn b/Main.tscn index 62944d8..e31d31c 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,126 +1,9 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=3 format=2] -[ext_resource path="res://entities/gem_grid.tscn" type="PackedScene" id=1] -[ext_resource path="res://entities/mana_counter.tscn" type="PackedScene" id=2] +[ext_resource path="res://screens/battle_screen.tscn" type="PackedScene" id=1] [ext_resource path="res://Main.gd" type="Script" id=3] [node name="Main" type="Node"] script = ExtResource( 3 ) -[node name="GemGrid" parent="." instance=ExtResource( 1 )] -anchor_left = 0.5 -anchor_top = 0.5 -anchor_right = 0.5 -anchor_bottom = 0.5 -margin_left = -256.0 -margin_top = -256.0 -margin_right = 256.0 -margin_bottom = 256.0 - -[node name="Counters" type="Control" parent="."] -margin_right = 256.0 -margin_bottom = 600.0 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="ManaCounter" parent="Counters" instance=ExtResource( 2 )] - -[node name="ManaCounter2" parent="Counters" instance=ExtResource( 2 )] -margin_top = 32.0 -margin_bottom = 64.0 -type = 1 - -[node name="ManaCounter3" parent="Counters" instance=ExtResource( 2 )] -margin_top = 64.0 -margin_bottom = 96.0 -type = 2 - -[node name="ManaCounter4" parent="Counters" instance=ExtResource( 2 )] -margin_top = 96.0 -margin_bottom = 128.0 -type = 3 - -[node name="ManaCounter5" parent="Counters" instance=ExtResource( 2 )] -margin_top = 128.0 -margin_bottom = 160.0 -type = 4 - -[node name="ManaCounter6" parent="Counters" instance=ExtResource( 2 )] -margin_top = 160.0 -margin_bottom = 192.0 -type = 5 - -[node name="ManaCounter7" parent="Counters" instance=ExtResource( 2 )] -margin_top = 192.0 -margin_bottom = 224.0 -type = 6 - -[node name="ManaCounter8" parent="Counters" instance=ExtResource( 2 )] -margin_top = 224.0 -margin_bottom = 256.0 -type = 7 - -[node name="ManaCounter9" parent="Counters" instance=ExtResource( 2 )] -margin_top = 256.0 -margin_bottom = 288.0 -effect = 1 - -[node name="ManaCounter10" parent="Counters" instance=ExtResource( 2 )] -margin_top = 288.0 -margin_bottom = 320.0 -type = 1 -effect = 1 - -[node name="ManaCounter11" parent="Counters" instance=ExtResource( 2 )] -margin_top = 320.0 -margin_bottom = 352.0 -type = 2 -effect = 1 - -[node name="ManaCounter12" parent="Counters" instance=ExtResource( 2 )] -margin_top = 352.0 -margin_bottom = 384.0 -type = 3 -effect = 1 - -[node name="ManaCounter13" parent="Counters" instance=ExtResource( 2 )] -margin_top = 384.0 -margin_bottom = 416.0 -type = 4 -effect = 1 - -[node name="ManaCounter14" parent="Counters" instance=ExtResource( 2 )] -margin_top = 416.0 -margin_bottom = 448.0 -type = 5 -effect = 1 - -[node name="ManaCounter15" parent="Counters" instance=ExtResource( 2 )] -margin_top = 448.0 -margin_bottom = 480.0 -type = 6 -effect = 1 - -[node name="ManaCounter16" parent="Counters" instance=ExtResource( 2 )] -margin_top = 480.0 -margin_bottom = 512.0 -type = 7 -effect = 1 - -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter2" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter3" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter4" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter5" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter6" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter7" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter8" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter9" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter10" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter11" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter12" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter13" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter14" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter15" method="update_count"] -[connection signal="gem_matched" from="GemGrid" to="Counters/ManaCounter16" method="update_count"] +[node name="BattleScreen" parent="." instance=ExtResource( 1 )] diff --git a/entities/gem_grid.gd b/entities/gem_grid.gd index b29f84c..045bd83 100644 --- a/entities/gem_grid.gd +++ b/entities/gem_grid.gd @@ -15,7 +15,6 @@ const grid_height = 8 # The grid var grid = LogicalGrid.new(grid_width, grid_height) -#var grid: Array = [Array(), Array(), Array(), Array(), Array(), Array(), Array(), Array()] # Cursor position var cursor_i: int = 0 @@ -44,7 +43,6 @@ enum State { NONE, INIT, READY, SWAPPING, MATCHING } var current_state = State.INIT var next_state = State.NONE -# # Called when the node enters the scene tree for the first time. func _ready(): @@ -55,7 +53,7 @@ func _ready(): # match gems (without emitting events) match_gems() # change the direction - grid.set_flow_direction(LogicalGrid.FlowDirection.Down) + grid.set_flow_direction(LogicalGrid.FlowDirection.DOWN) # State is ready current_state = State.READY # Tell all gems the grid is ready @@ -148,46 +146,36 @@ func match_gems(): to_remove.push_front([i, j]) # Remove the gems grid.remove_values(to_remove) - # Add new gems. The initial positions depends on the flow direction. + # Add new gems to fill the empty spaces. + # The initial positions depends on the flow direction. var counts = [0] var dir = grid.get_flow_direction() - match dir: - LogicalGrid.FlowDirection.Up: - counts.resize(grid_width) - LogicalGrid.FlowDirection.Down: - counts.resize(grid_width) - LogicalGrid.FlowDirection.Left: - counts.resize(grid_height) - LogicalGrid.FlowDirection.Right: - counts.resize(grid_height) + if LogicalGrid.isFlowDirectionVertical(dir): + counts.resize(grid_width) + else: + counts.resize(grid_height) for i in range(counts.size()): counts[i] = 0 for idx in to_remove: var screen_idx = [] var i = 0 + # Gems are not added on the position they are on the grid, + # but outside so that the animation looks correct match dir: - LogicalGrid.FlowDirection.Up: + LogicalGrid.FlowDirection.UP: i = idx[0] screen_idx = [i, grid_height + counts[i]] - LogicalGrid.FlowDirection.Down: + LogicalGrid.FlowDirection.DOWN: i = idx[0] screen_idx = [i, -1 - counts[i]] - LogicalGrid.FlowDirection.Left: + LogicalGrid.FlowDirection.LEFT: i = idx[1] screen_idx = [grid_width + counts[i], i] - LogicalGrid.FlowDirection.Right: + LogicalGrid.FlowDirection.RIGHT: i = idx[1] screen_idx = [-1 - counts[i], i] counts[i] = counts[i] + 1 add_gem(idx[0], idx[1], screen_idx[0], screen_idx[1]) - ## Add new gems to fill the empty spaces - #for i in range(grid_width): - # var count = 0 - # for _j in range(grid[i].size(), grid_height): - # # gems are not added on the position they are on the grid, - # # but outside so that animation plays correctly - # add_gem(i, count + grid_height) - # count += 1 # Update gems next position for i in range(grid_width): for j in range(grid_height): diff --git a/entities/gem_grid.tscn b/entities/gem_grid.tscn index 94d5fe9..757912c 100644 --- a/entities/gem_grid.tscn +++ b/entities/gem_grid.tscn @@ -13,9 +13,6 @@ margin_right = 512.0 margin_bottom = 512.0 rect_clip_content = true script = ExtResource( 1 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="ColorRect" type="ColorRect" parent="."] anchor_right = 1.0 diff --git a/entities/mana_counter.tscn b/entities/mana_counter.tscn index f23e505..7bc31af 100644 --- a/entities/mana_counter.tscn +++ b/entities/mana_counter.tscn @@ -7,9 +7,6 @@ margin_right = 192.0 margin_bottom = 32.0 script = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} [node name="Gem" parent="." instance=ExtResource( 1 )] rect_scale = Vector2( 0.5, 0.5 ) diff --git a/screens/battle_screen.gd b/screens/battle_screen.gd new file mode 100644 index 0000000..3f94aa0 --- /dev/null +++ b/screens/battle_screen.gd @@ -0,0 +1,20 @@ +extends Control + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(_delta): + if Input.is_action_just_pressed("ui_up"): + $GemGrid.up() + elif Input.is_action_just_pressed("ui_down"): + $GemGrid.down() + elif Input.is_action_just_pressed("ui_left"): + $GemGrid.left() + elif Input.is_action_just_pressed("ui_right"): + $GemGrid.right() + elif Input.is_action_just_pressed("ui_accept"): + $GemGrid.select() + elif Input.is_action_just_pressed("ui_cancel"): + $GemGrid.cancel() diff --git a/screens/battle_screen.tscn b/screens/battle_screen.tscn new file mode 100644 index 0000000..a444d7c --- /dev/null +++ b/screens/battle_screen.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://entities/gem_grid.tscn" type="PackedScene" id=1] +[ext_resource path="res://screens/battle_screen.gd" type="Script" id=2] + +[node name="BattleScreen" type="Control"] +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +script = ExtResource( 2 ) + +[node name="GemGrid" parent="." instance=ExtResource( 1 )] +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +margin_left = -256.0 +margin_top = -256.0 +margin_right = 256.0 +margin_bottom = 256.0 diff --git a/tools/logical_grid.gd b/tools/logical_grid.gd index d9c3233..1c0a1d9 100644 --- a/tools/logical_grid.gd +++ b/tools/logical_grid.gd @@ -4,13 +4,30 @@ class_name LogicalGrid # Grid with a flow direction for the item added / removed # The position on grid must be translated as the position in the array is different -enum FlowDirection { Up, Down, Left, Right } +enum FlowDirection { UP, DOWN, LEFT, RIGHT } +# Predicate on FlowDirection : vertical direction +static func isFlowDirectionVertical(dir): + match dir: + FlowDirection.UP: + return true + FlowDirection.DOWN: + return true + return false +# Predicate on FlowDirection : horizontal direction +static func isFlowDirectionHorizontal(dir): + match dir: + FlowDirection.LEFT: + return true + FlowDirection.RIGHT: + return true + return false + # Fields var width: int # width var height: int # height var table: Array = [] # table = array of columns or rows depending on the direction -var flow_direction = FlowDirection.Up +var flow_direction = FlowDirection.UP # Constructor func _init(width: int, height: int): @@ -33,7 +50,7 @@ func set_flow_direction(dir): self.flow_direction = dir # Update the array to reflect the change in flow direction self.table = [] - if (dir == FlowDirection.Up) or (dir == FlowDirection.Down): + if isFlowDirectionVertical(dir): for nx in range(width): self.table.push_back(Array()) for ny in range(height): @@ -53,13 +70,13 @@ func set_flow_direction(dir): # Get the index in the table depending on the flow direction func get_idx(x, y, dir = self.flow_direction): match dir: - FlowDirection.Up: + FlowDirection.UP: return [x, y] - FlowDirection.Down: + FlowDirection.DOWN: return [x, height - 1 - y] - FlowDirection.Left: + FlowDirection.LEFT: return [y, x] - FlowDirection.Right: + FlowDirection.RIGHT: return [y, width - 1 - x] # Get value at position @@ -72,12 +89,8 @@ func set_at(x: int, y: int, val): var idx = get_idx(x, y) self.table[idx[0]][idx[1]] = val -# Remove a value from the table -#func remove_at(x: int, y:int): -# var idx = get_idx(x, y) -# self.table[idx[0]].remove(idx[1]) - # Remove the values from a list of indexes +# To avoid problems, the items are removed from a descending lexicographical order of indexes class IdxSorter: static func higher_idx_first(a, b): return (a[0] > b[0]) or ((a[0] == b[0]) and (a[1] > b[1]))