Refactor a few things and move the game battle into a proper screen.

This commit is contained in:
Feufochmar 2022-05-25 16:31:09 +02:00
parent b9385a0fe7
commit 64c6732a75
8 changed files with 82 additions and 178 deletions

15
Main.gd
View File

@ -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()

123
Main.tscn
View File

@ -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 )]

View File

@ -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):

View File

@ -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

View File

@ -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 )

20
screens/battle_screen.gd Normal file
View File

@ -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()

View File

@ -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

View File

@ -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]))