refactoring
This commit is contained in:
parent
c8e8890e30
commit
719aeff907
@ -1,143 +1,22 @@
|
|||||||
extends GridMap
|
extends GridMap
|
||||||
|
|
||||||
const ExplodingLine = preload("res://ExplodingLine/ExplodingLine.tscn")
|
|
||||||
const Tetromino = preload("res://Tetrominos/Tetromino.gd")
|
const Tetromino = preload("res://Tetrominos/Tetromino.gd")
|
||||||
const TetroI = preload("res://Tetrominos/TetroI.tscn")
|
const ExplodingLine = preload("res://ExplodingLine/ExplodingLine.tscn")
|
||||||
const TetroJ = preload("res://Tetrominos/TetroJ.tscn")
|
|
||||||
const TetroL = preload("res://Tetrominos/TetroL.tscn")
|
|
||||||
const TetroO = preload("res://Tetrominos/TetroO.tscn")
|
|
||||||
const TetroS = preload("res://Tetrominos/TetroS.tscn")
|
|
||||||
const TetroT = preload("res://Tetrominos/TetroT.tscn")
|
|
||||||
const TetroZ = preload("res://Tetrominos/TetroZ.tscn")
|
|
||||||
|
|
||||||
const NB_LINES = 20
|
|
||||||
const NB_COLLUMNS = 10
|
|
||||||
|
|
||||||
const EMPTY_CELL = -1
|
const EMPTY_CELL = -1
|
||||||
|
const MINO = 0
|
||||||
|
|
||||||
const NEXT_POSITION = Vector3(13, 16, 0)
|
export (int) var NB_LINES = 20
|
||||||
const START_POSITION = Vector3(5, 20, 0)
|
export (int) var NB_COLLUMNS = 10
|
||||||
const HOLD_POSITION = Vector3(-5, 16, 0)
|
|
||||||
|
|
||||||
const movements = {
|
|
||||||
"move_right": Vector3(1, 0, 0),
|
|
||||||
"move_left": Vector3(-1, 0, 0),
|
|
||||||
"soft_drop": Vector3(0, -1, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
const SCORES = [
|
|
||||||
[0, 4, 1],
|
|
||||||
[1, 8, 2],
|
|
||||||
[3, 12],
|
|
||||||
[5, 16],
|
|
||||||
[8]
|
|
||||||
]
|
|
||||||
const LINES_CLEARED_NAMES = ["", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS"]
|
|
||||||
const T_SPIN_NAMES = ["", "T-SPIN", "MINI T-SPIN"]
|
|
||||||
|
|
||||||
const LINE_CLEAR_MIDI_CHANNELS = [2, 6]
|
|
||||||
|
|
||||||
var next_piece = random_piece()
|
|
||||||
var current_piece
|
|
||||||
var held_piece
|
|
||||||
var current_piece_held = false
|
|
||||||
|
|
||||||
var autoshift_action = ""
|
|
||||||
|
|
||||||
var exploding_lines = []
|
var exploding_lines = []
|
||||||
var random_bag = []
|
|
||||||
var playing = true
|
|
||||||
|
|
||||||
var level = 0
|
|
||||||
var goal = 0
|
|
||||||
var score = 0
|
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
randomize()
|
|
||||||
for y in range(NB_LINES):
|
for y in range(NB_LINES):
|
||||||
exploding_lines.append(ExplodingLine.instance())
|
exploding_lines.append(ExplodingLine.instance())
|
||||||
add_child(exploding_lines[y])
|
add_child(exploding_lines[y])
|
||||||
exploding_lines[y].translation = Vector3(NB_COLLUMNS/2, y, 1)
|
exploding_lines[y].translation = Vector3(NB_COLLUMNS/2, y, 1)
|
||||||
resume()
|
|
||||||
new_level()
|
|
||||||
|
|
||||||
func new_level():
|
|
||||||
level += 1
|
|
||||||
goal += 5 * level
|
|
||||||
$DropTimer.wait_time = pow(0.8 - ((level - 1) * 0.007), level - 1)
|
|
||||||
if level > 15:
|
|
||||||
$LockDelay.wait_time = 0.5 * pow(0.9, level-15)
|
|
||||||
print("LEVEL ", level, " Goal ", goal)
|
|
||||||
new_piece()
|
|
||||||
|
|
||||||
func random_piece():
|
|
||||||
if not random_bag:
|
|
||||||
random_bag = [
|
|
||||||
TetroI, TetroJ, TetroL, TetroO,
|
|
||||||
TetroS, TetroT, TetroZ
|
|
||||||
]
|
|
||||||
var choice = randi() % random_bag.size()
|
|
||||||
var piece = random_bag[choice].instance()
|
|
||||||
random_bag.remove(choice)
|
|
||||||
add_child(piece)
|
|
||||||
return piece
|
|
||||||
|
|
||||||
func new_piece():
|
|
||||||
current_piece = next_piece
|
|
||||||
current_piece.translation = START_POSITION
|
|
||||||
current_piece.emit_trail(true)
|
|
||||||
autoshift_action = ""
|
|
||||||
next_piece = random_piece()
|
|
||||||
next_piece.translation = NEXT_POSITION
|
|
||||||
if move(movements["soft_drop"]):
|
|
||||||
$DropTimer.start()
|
|
||||||
$LockDelay.start()
|
|
||||||
current_piece_held = false
|
|
||||||
else:
|
|
||||||
game_over()
|
|
||||||
|
|
||||||
func _process(delta):
|
|
||||||
if autoshift_action:
|
|
||||||
if not Input.is_action_pressed(autoshift_action):
|
|
||||||
$AutoShiftDelay.stop()
|
|
||||||
$AutoShiftTimer.stop()
|
|
||||||
autoshift_action = ""
|
|
||||||
if Input.is_action_just_pressed("pause"):
|
|
||||||
if playing:
|
|
||||||
pause()
|
|
||||||
else:
|
|
||||||
resume()
|
|
||||||
if playing:
|
|
||||||
process_actions()
|
|
||||||
|
|
||||||
func process_actions():
|
|
||||||
for action in movements:
|
|
||||||
if action != autoshift_action:
|
|
||||||
if Input.is_action_pressed(action):
|
|
||||||
move(movements[action])
|
|
||||||
autoshift_action = action
|
|
||||||
$AutoShiftTimer.stop()
|
|
||||||
$AutoShiftDelay.start()
|
|
||||||
if Input.is_action_just_pressed("hard_drop"):
|
|
||||||
while move(movements["soft_drop"]):
|
|
||||||
pass
|
|
||||||
lock_piece()
|
|
||||||
if Input.is_action_just_pressed("rotate_clockwise"):
|
|
||||||
rotate(Tetromino.CLOCKWISE)
|
|
||||||
if Input.is_action_just_pressed("rotate_counterclockwise"):
|
|
||||||
rotate(Tetromino.COUNTERCLOCKWISE)
|
|
||||||
if Input.is_action_just_pressed("hold"):
|
|
||||||
hold()
|
|
||||||
|
|
||||||
func _on_AutoShiftDelay_timeout():
|
|
||||||
if playing and autoshift_action:
|
|
||||||
move(movements[autoshift_action])
|
|
||||||
$AutoShiftTimer.start()
|
|
||||||
|
|
||||||
func _on_AutoShiftTimer_timeout():
|
|
||||||
if playing and autoshift_action:
|
|
||||||
move(movements[autoshift_action])
|
|
||||||
|
|
||||||
func is_free_cell(position):
|
func is_free_cell(position):
|
||||||
return (
|
return (
|
||||||
0 <= position.x and position.x < NB_COLLUMNS
|
0 <= position.x and position.x < NB_COLLUMNS
|
||||||
@ -157,107 +36,22 @@ func possible_positions(initial_positions, movement):
|
|||||||
else:
|
else:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
func move(movement):
|
func lock(piece):
|
||||||
if current_piece.move(movement):
|
for position in piece.positions():
|
||||||
$LockDelay.start()
|
set_cell_item(position.x, position.y, 0, MINO)
|
||||||
return true
|
|
||||||
else:
|
|
||||||
return false
|
|
||||||
|
|
||||||
func rotate(direction):
|
|
||||||
if current_piece.rotate(direction):
|
|
||||||
$LockDelay.start()
|
|
||||||
return true
|
|
||||||
else:
|
|
||||||
return false
|
|
||||||
|
|
||||||
func _on_DropTimer_timeout():
|
func clear_lines():
|
||||||
move(movements["soft_drop"])
|
var nb_minoes
|
||||||
|
|
||||||
func _on_LockDelay_timeout():
|
|
||||||
if not move(movements["soft_drop"]):
|
|
||||||
lock_piece()
|
|
||||||
|
|
||||||
func lock_piece():
|
|
||||||
for mino in current_piece.minoes:
|
|
||||||
set_cell_item(current_piece.to_global(mino.translation).x, current_piece.to_global(mino.translation).y, 0, 0)
|
|
||||||
remove_child(current_piece)
|
|
||||||
line_clear()
|
|
||||||
|
|
||||||
func line_clear():
|
|
||||||
var NB_MINOES
|
|
||||||
var lines_cleared = 0
|
var lines_cleared = 0
|
||||||
for y in range(NB_LINES-1, -1, -1):
|
for y in range(NB_LINES-1, -1, -1):
|
||||||
NB_MINOES = 0
|
nb_minoes = 0
|
||||||
for x in range(NB_COLLUMNS):
|
for x in range(NB_COLLUMNS):
|
||||||
if get_cell_item(x, y, 0) == 0:
|
if get_cell_item(x, y, 0) == MINO:
|
||||||
NB_MINOES += 1
|
nb_minoes += 1
|
||||||
if NB_MINOES == NB_COLLUMNS:
|
if nb_minoes == NB_COLLUMNS:
|
||||||
for y2 in range(y, NB_LINES+2):
|
for y2 in range(y, NB_LINES+2):
|
||||||
for x in range(NB_COLLUMNS):
|
for x in range(NB_COLLUMNS):
|
||||||
set_cell_item(x, y2, 0, get_cell_item(x, y2+1, 0))
|
set_cell_item(x, y2, 0, get_cell_item(x, y2+1, 0))
|
||||||
lines_cleared += 1
|
lines_cleared += 1
|
||||||
exploding_lines[y].restart()
|
exploding_lines[y].restart()
|
||||||
if lines_cleared or current_piece.t_spin:
|
return lines_cleared
|
||||||
var s = SCORES[lines_cleared][current_piece.t_spin]
|
|
||||||
score += 100 * s
|
|
||||||
goal -= s
|
|
||||||
print(T_SPIN_NAMES[current_piece.t_spin], ' ', LINES_CLEARED_NAMES[lines_cleared], " Score ", score)
|
|
||||||
|
|
||||||
if lines_cleared == Tetromino.NB_MINOES:
|
|
||||||
for channel in LINE_CLEAR_MIDI_CHANNELS:
|
|
||||||
$MidiPlayer.channel_status[channel].vomume = 127
|
|
||||||
$MidiPlayer/LineCLearTimer.wait_time = 0.86
|
|
||||||
else:
|
|
||||||
for channel in LINE_CLEAR_MIDI_CHANNELS:
|
|
||||||
$MidiPlayer.channel_status[channel].vomume = 100
|
|
||||||
$MidiPlayer/LineCLearTimer.wait_time = 0.43
|
|
||||||
$MidiPlayer.unmute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
|
||||||
$MidiPlayer/LineCLearTimer.start()
|
|
||||||
if goal <= 0:
|
|
||||||
new_level()
|
|
||||||
else:
|
|
||||||
new_piece()
|
|
||||||
|
|
||||||
func hold():
|
|
||||||
if not current_piece_held:
|
|
||||||
current_piece.emit_trail(false)
|
|
||||||
if held_piece:
|
|
||||||
var tmp = held_piece
|
|
||||||
held_piece = current_piece
|
|
||||||
current_piece = tmp
|
|
||||||
current_piece.translation = START_POSITION
|
|
||||||
current_piece.emit_trail(true)
|
|
||||||
else:
|
|
||||||
held_piece = current_piece
|
|
||||||
new_piece()
|
|
||||||
held_piece.translation = HOLD_POSITION
|
|
||||||
current_piece_held = true
|
|
||||||
|
|
||||||
func resume():
|
|
||||||
playing = true
|
|
||||||
$DropTimer.start()
|
|
||||||
$LockDelay.start()
|
|
||||||
$MidiPlayer.resume()
|
|
||||||
$MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
|
||||||
print("RESUME")
|
|
||||||
|
|
||||||
func pause():
|
|
||||||
playing = false
|
|
||||||
$DropTimer.stop()
|
|
||||||
$LockDelay.stop()
|
|
||||||
$MidiPlayer.stop()
|
|
||||||
print("PAUSE")
|
|
||||||
|
|
||||||
func game_over():
|
|
||||||
pause()
|
|
||||||
print("GAME OVER")
|
|
||||||
|
|
||||||
func _notification(what):
|
|
||||||
if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT:
|
|
||||||
pause()
|
|
||||||
if what == MainLoop.NOTIFICATION_WM_FOCUS_IN:
|
|
||||||
resume()
|
|
||||||
|
|
||||||
func _on_LineCLearTimer_timeout():
|
|
||||||
$MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
|
@ -6,7 +6,7 @@
|
|||||||
[ext_resource path="res://GridMap/BackMaterial.tres" type="Material" id=4]
|
[ext_resource path="res://GridMap/BackMaterial.tres" type="Material" id=4]
|
||||||
[ext_resource path="res://midi/MidiPlayer.tscn" type="PackedScene" id=5]
|
[ext_resource path="res://midi/MidiPlayer.tscn" type="PackedScene" id=5]
|
||||||
|
|
||||||
[sub_resource type="CubeMesh" id=1]
|
[sub_resource type="CubeMesh" id=3]
|
||||||
|
|
||||||
material = ExtResource( 4 )
|
material = ExtResource( 4 )
|
||||||
custom_aabb = AABB( 0, 0, 0, 0, 0, 0 )
|
custom_aabb = AABB( 0, 0, 0, 0, 0, 0 )
|
||||||
@ -17,7 +17,6 @@ subdivide_depth = 0
|
|||||||
|
|
||||||
[node name="GridMap" type="GridMap"]
|
[node name="GridMap" type="GridMap"]
|
||||||
|
|
||||||
editor/display_folded = true
|
|
||||||
theme = ExtResource( 1 )
|
theme = ExtResource( 1 )
|
||||||
cell_size = Vector3( 1, 1, 1 )
|
cell_size = Vector3( 1, 1, 1 )
|
||||||
cell_octant_size = 8
|
cell_octant_size = 8
|
||||||
@ -36,6 +35,8 @@ __meta__ = {
|
|||||||
"_editor_clip_": 1,
|
"_editor_clip_": 1,
|
||||||
"_editor_floor_": Vector3( 0, -1, 0 )
|
"_editor_floor_": Vector3( 0, -1, 0 )
|
||||||
}
|
}
|
||||||
|
NB_LINES = 20
|
||||||
|
NB_COLLUMNS = 10
|
||||||
|
|
||||||
[node name="DropTimer" type="Timer" parent="." index="0"]
|
[node name="DropTimer" type="Timer" parent="." index="0"]
|
||||||
|
|
||||||
@ -68,17 +69,17 @@ autostart = true
|
|||||||
[node name="GridBack" parent="." index="4" instance=ExtResource( 3 )]
|
[node name="GridBack" parent="." index="4" instance=ExtResource( 3 )]
|
||||||
|
|
||||||
transform = Transform( 10, 0, 0, 0, 20, 0, 0, 0, 0.1, 4.5, 9.5, -1 )
|
transform = Transform( 10, 0, 0, 0, 20, 0, 0, 0, 0.1, 4.5, 9.5, -1 )
|
||||||
mesh = SubResource( 1 )
|
mesh = SubResource( 3 )
|
||||||
|
|
||||||
[node name="HoldBack" parent="." index="5" instance=ExtResource( 3 )]
|
[node name="HoldBack" parent="." index="5" instance=ExtResource( 3 )]
|
||||||
|
|
||||||
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, -5, 16, -1 )
|
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, -5, 16, -1 )
|
||||||
mesh = SubResource( 1 )
|
mesh = SubResource( 3 )
|
||||||
|
|
||||||
[node name="NextBack" parent="." index="6" instance=ExtResource( 3 )]
|
[node name="NextBack" parent="." index="6" instance=ExtResource( 3 )]
|
||||||
|
|
||||||
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, 14, 16, -1 )
|
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, 14, 16, -1 )
|
||||||
mesh = SubResource( 1 )
|
mesh = SubResource( 3 )
|
||||||
|
|
||||||
[node name="MidiPlayer" parent="." index="7" instance=ExtResource( 5 )]
|
[node name="MidiPlayer" parent="." index="7" instance=ExtResource( 5 )]
|
||||||
|
|
||||||
|
216
Main.gd
Normal file
216
Main.gd
Normal file
@ -0,0 +1,216 @@
|
|||||||
|
extends WorldEnvironment
|
||||||
|
|
||||||
|
const Tetromino = preload("res://Tetrominos/Tetromino.gd")
|
||||||
|
const TetroI = preload("res://Tetrominos/TetroI.tscn")
|
||||||
|
const TetroJ = preload("res://Tetrominos/TetroJ.tscn")
|
||||||
|
const TetroL = preload("res://Tetrominos/TetroL.tscn")
|
||||||
|
const TetroO = preload("res://Tetrominos/TetroO.tscn")
|
||||||
|
const TetroS = preload("res://Tetrominos/TetroS.tscn")
|
||||||
|
const TetroT = preload("res://Tetrominos/TetroT.tscn")
|
||||||
|
const TetroZ = preload("res://Tetrominos/TetroZ.tscn")
|
||||||
|
|
||||||
|
const NEXT_POSITION = Vector3(13, 16, 0)
|
||||||
|
const START_POSITION = Vector3(5, 20, 0)
|
||||||
|
const HOLD_POSITION = Vector3(-5, 16, 0)
|
||||||
|
|
||||||
|
const movements = {
|
||||||
|
"move_right": Vector3(1, 0, 0),
|
||||||
|
"move_left": Vector3(-1, 0, 0),
|
||||||
|
"soft_drop": Vector3(0, -1, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
const SCORES = [
|
||||||
|
[0, 4, 1],
|
||||||
|
[1, 8, 2],
|
||||||
|
[3, 12],
|
||||||
|
[5, 16],
|
||||||
|
[8]
|
||||||
|
]
|
||||||
|
const LINES_CLEARED_NAMES = ["", "SINGLE", "DOUBLE", "TRIPLE", "TETRIS"]
|
||||||
|
const T_SPIN_NAMES = ["", "T-SPIN", "MINI T-SPIN"]
|
||||||
|
|
||||||
|
const LINE_CLEAR_MIDI_CHANNELS = [2, 6]
|
||||||
|
|
||||||
|
var random_bag = []
|
||||||
|
|
||||||
|
var next_piece = random_piece()
|
||||||
|
var current_piece
|
||||||
|
var held_piece
|
||||||
|
var current_piece_held = false
|
||||||
|
|
||||||
|
var autoshift_action = ""
|
||||||
|
|
||||||
|
var playing = true
|
||||||
|
|
||||||
|
var level = 0
|
||||||
|
var goal = 0
|
||||||
|
var score = 0
|
||||||
|
|
||||||
|
func random_piece():
|
||||||
|
if not random_bag:
|
||||||
|
random_bag = [
|
||||||
|
TetroI, TetroJ, TetroL, TetroO,
|
||||||
|
TetroS, TetroT, TetroZ
|
||||||
|
]
|
||||||
|
var choice = randi() % random_bag.size()
|
||||||
|
var piece = random_bag[choice].instance()
|
||||||
|
random_bag.remove(choice)
|
||||||
|
add_child(piece)
|
||||||
|
return piece
|
||||||
|
|
||||||
|
func _ready():
|
||||||
|
resume()
|
||||||
|
new_level()
|
||||||
|
new_piece()
|
||||||
|
|
||||||
|
func new_level():
|
||||||
|
level += 1
|
||||||
|
goal += 5 * level
|
||||||
|
$DropTimer.wait_time = pow(0.8 - ((level - 1) * 0.007), level - 1)
|
||||||
|
if level > 15:
|
||||||
|
$LockDelay.wait_time = 0.5 * pow(0.9, level-15)
|
||||||
|
print("LEVEL ", level, " Goal ", goal)
|
||||||
|
|
||||||
|
func new_piece():
|
||||||
|
current_piece = next_piece
|
||||||
|
current_piece.translation = START_POSITION
|
||||||
|
current_piece.emit_trail(true)
|
||||||
|
autoshift_action = ""
|
||||||
|
next_piece = random_piece()
|
||||||
|
next_piece.translation = NEXT_POSITION
|
||||||
|
if move(movements["soft_drop"]):
|
||||||
|
$DropTimer.start()
|
||||||
|
$LockDelay.start()
|
||||||
|
current_piece_held = false
|
||||||
|
else:
|
||||||
|
game_over()
|
||||||
|
|
||||||
|
func _process(delta):
|
||||||
|
if Input.is_action_just_pressed("pause"):
|
||||||
|
if playing:
|
||||||
|
pause()
|
||||||
|
else:
|
||||||
|
resume()
|
||||||
|
if playing:
|
||||||
|
for action in movements:
|
||||||
|
if action == autoshift_action:
|
||||||
|
if not Input.is_action_pressed(action):
|
||||||
|
$AutoShiftDelay.stop()
|
||||||
|
$AutoShiftTimer.stop()
|
||||||
|
autoshift_action = ""
|
||||||
|
else:
|
||||||
|
if Input.is_action_pressed(action):
|
||||||
|
move(movements[action])
|
||||||
|
autoshift_action = action
|
||||||
|
$AutoShiftTimer.stop()
|
||||||
|
$AutoShiftDelay.start()
|
||||||
|
if Input.is_action_just_pressed("hard_drop"):
|
||||||
|
hard_drop()
|
||||||
|
if Input.is_action_just_pressed("rotate_clockwise"):
|
||||||
|
rotate(Tetromino.CLOCKWISE)
|
||||||
|
if Input.is_action_just_pressed("rotate_counterclockwise"):
|
||||||
|
rotate(Tetromino.COUNTERCLOCKWISE)
|
||||||
|
if Input.is_action_just_pressed("hold"):
|
||||||
|
hold()
|
||||||
|
|
||||||
|
func hard_drop():
|
||||||
|
while move(movements["soft_drop"]):
|
||||||
|
pass
|
||||||
|
lock()
|
||||||
|
|
||||||
|
func _on_AutoShiftDelay_timeout():
|
||||||
|
if playing and autoshift_action:
|
||||||
|
move(movements[autoshift_action])
|
||||||
|
$AutoShiftTimer.start()
|
||||||
|
|
||||||
|
func _on_AutoShiftTimer_timeout():
|
||||||
|
if playing and autoshift_action:
|
||||||
|
move(movements[autoshift_action])
|
||||||
|
|
||||||
|
func move(movement):
|
||||||
|
if current_piece.move(movement):
|
||||||
|
$LockDelay.start()
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
|
func rotate(direction):
|
||||||
|
if current_piece.rotate(direction):
|
||||||
|
$LockDelay.start()
|
||||||
|
return true
|
||||||
|
else:
|
||||||
|
return false
|
||||||
|
|
||||||
|
func _on_DropTimer_timeout():
|
||||||
|
move(movements["soft_drop"])
|
||||||
|
|
||||||
|
func _on_LockDelay_timeout():
|
||||||
|
if not move(movements["soft_drop"]):
|
||||||
|
lock()
|
||||||
|
|
||||||
|
func lock():
|
||||||
|
$GridMap.lock(current_piece)
|
||||||
|
remove_child(current_piece)
|
||||||
|
var lines_cleared = $GridMap.clear_lines()
|
||||||
|
if lines_cleared or current_piece.t_spin:
|
||||||
|
var s = SCORES[lines_cleared][current_piece.t_spin]
|
||||||
|
score += 100 * s
|
||||||
|
goal -= s
|
||||||
|
print(T_SPIN_NAMES[current_piece.t_spin], ' ', LINES_CLEARED_NAMES[lines_cleared], " Score ", score)
|
||||||
|
|
||||||
|
if lines_cleared == Tetromino.NB_MINOES:
|
||||||
|
for channel in LINE_CLEAR_MIDI_CHANNELS:
|
||||||
|
$MidiPlayer.channel_status[channel].vomume = 127
|
||||||
|
$MidiPlayer/LineCLearTimer.wait_time = 0.86
|
||||||
|
else:
|
||||||
|
for channel in LINE_CLEAR_MIDI_CHANNELS:
|
||||||
|
$MidiPlayer.channel_status[channel].vomume = 100
|
||||||
|
$MidiPlayer/LineCLearTimer.wait_time = 0.43
|
||||||
|
$MidiPlayer.unmute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
||||||
|
$MidiPlayer/LineCLearTimer.start()
|
||||||
|
if goal <= 0:
|
||||||
|
new_level()
|
||||||
|
new_piece()
|
||||||
|
|
||||||
|
func hold():
|
||||||
|
if not current_piece_held:
|
||||||
|
current_piece.emit_trail(false)
|
||||||
|
if held_piece:
|
||||||
|
var tmp = held_piece
|
||||||
|
held_piece = current_piece
|
||||||
|
current_piece = tmp
|
||||||
|
current_piece.translation = START_POSITION
|
||||||
|
current_piece.emit_trail(true)
|
||||||
|
else:
|
||||||
|
held_piece = current_piece
|
||||||
|
new_piece()
|
||||||
|
held_piece.translation = HOLD_POSITION
|
||||||
|
current_piece_held = true
|
||||||
|
|
||||||
|
func resume():
|
||||||
|
playing = true
|
||||||
|
$DropTimer.start()
|
||||||
|
$LockDelay.start()
|
||||||
|
$MidiPlayer.resume()
|
||||||
|
$MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
||||||
|
print("RESUME")
|
||||||
|
|
||||||
|
func pause():
|
||||||
|
playing = false
|
||||||
|
$DropTimer.stop()
|
||||||
|
$LockDelay.stop()
|
||||||
|
$MidiPlayer.stop()
|
||||||
|
print("PAUSE")
|
||||||
|
|
||||||
|
func game_over():
|
||||||
|
pause()
|
||||||
|
print("GAME OVER")
|
||||||
|
|
||||||
|
func _notification(what):
|
||||||
|
if what == MainLoop.NOTIFICATION_WM_FOCUS_OUT:
|
||||||
|
pause()
|
||||||
|
if what == MainLoop.NOTIFICATION_WM_FOCUS_IN:
|
||||||
|
resume()
|
||||||
|
|
||||||
|
func _on_LineCLearTimer_timeout():
|
||||||
|
$MidiPlayer.mute_channels(LINE_CLEAR_MIDI_CHANNELS)
|
@ -14,13 +14,13 @@ func rotate(direction):
|
|||||||
return false
|
return false
|
||||||
|
|
||||||
func detect_t_spin():
|
func detect_t_spin():
|
||||||
var center = to_global(minoes[0].translation)
|
if t_spin != T_SPIN:
|
||||||
var a = not get_parent().is_free_cell(center + T_SLOT[orientation])
|
var center = to_global(minoes[0].translation)
|
||||||
var b = not get_parent().is_free_cell(center + T_SLOT[(1+orientation)%4])
|
var a = not grid_map.is_free_cell(center + T_SLOT[orientation])
|
||||||
var c = not get_parent().is_free_cell(center + T_SLOT[(2+orientation)%4])
|
var b = not grid_map.is_free_cell(center + T_SLOT[(1+orientation)%4])
|
||||||
var d = not get_parent().is_free_cell(center + T_SLOT[(3+orientation)%4])
|
var c = not grid_map.is_free_cell(center + T_SLOT[(2+orientation)%4])
|
||||||
if a and b and (c or d):
|
var d = not grid_map.is_free_cell(center + T_SLOT[(3+orientation)%4])
|
||||||
t_spin = T_SPIN
|
if a and b and (c or d):
|
||||||
elif c and d and (a or b):
|
t_spin = T_SPIN
|
||||||
if t_spin != T_SPIN:
|
elif c and d and (a or b):
|
||||||
t_spin = MINI_T_SPIN
|
t_spin = MINI_T_SPIN
|
@ -74,11 +74,14 @@ const SUPER_ROTATION_SYSTEM = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
var minoes
|
var minoes
|
||||||
|
var grid_map
|
||||||
var orientation = 0
|
var orientation = 0
|
||||||
var t_spin = NO_T_SPIN
|
var t_spin = NO_T_SPIN
|
||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
|
randomize()
|
||||||
minoes = [$Mino0, $Mino1, $Mino2, $Mino3]
|
minoes = [$Mino0, $Mino1, $Mino2, $Mino3]
|
||||||
|
grid_map = get_parent().get_node("GridMap")
|
||||||
|
|
||||||
func positions():
|
func positions():
|
||||||
var p = []
|
var p = []
|
||||||
@ -101,8 +104,7 @@ func apply_positions(positions):
|
|||||||
minoes[i].translation = to_local(positions[i])
|
minoes[i].translation = to_local(positions[i])
|
||||||
|
|
||||||
func move(movement):
|
func move(movement):
|
||||||
var new_positions = get_parent().possible_positions(positions(), movement)
|
if grid_map.possible_positions(positions(), movement):
|
||||||
if new_positions:
|
|
||||||
translate(movement)
|
translate(movement)
|
||||||
return true
|
return true
|
||||||
else:
|
else:
|
||||||
@ -111,10 +113,8 @@ func move(movement):
|
|||||||
func rotate(direction):
|
func rotate(direction):
|
||||||
var rotated_positions = rotated_positions(direction)
|
var rotated_positions = rotated_positions(direction)
|
||||||
var movements = SUPER_ROTATION_SYSTEM[orientation][direction]
|
var movements = SUPER_ROTATION_SYSTEM[orientation][direction]
|
||||||
var test_position
|
|
||||||
for movement in movements:
|
for movement in movements:
|
||||||
test_position = get_parent().possible_positions(rotated_positions, movement)
|
if grid_map.possible_positions(rotated_positions, movement):
|
||||||
if test_position:
|
|
||||||
orientation -= direction
|
orientation -= direction
|
||||||
orientation %= NB_MINOES
|
orientation %= NB_MINOES
|
||||||
apply_positions(rotated_positions)
|
apply_positions(rotated_positions)
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
[gd_scene load_steps=5 format=2]
|
[gd_scene load_steps=12 format=2]
|
||||||
|
|
||||||
[ext_resource path="res://starmap_g8k.jpg" type="Texture" id=1]
|
[ext_resource path="res://starmap_g8k.jpg" type="Texture" id=1]
|
||||||
[ext_resource path="res://GridMap/GridMap.tscn" type="PackedScene" id=2]
|
[ext_resource path="res://Main.gd" type="Script" id=2]
|
||||||
|
[ext_resource path="res://Tetrominos/Mino/MinoLibrary.tres" type="MeshLibrary" id=3]
|
||||||
|
[ext_resource path="res://GridMap/GridMap.gd" type="Script" id=4]
|
||||||
|
[ext_resource path="res://GridMap/GridBack.tscn" type="PackedScene" id=5]
|
||||||
|
[ext_resource path="res://GridMap/BackMaterial.tres" type="Material" id=6]
|
||||||
|
[ext_resource path="res://midi/MidiPlayer.tscn" type="PackedScene" id=7]
|
||||||
|
[ext_resource path="res://midi/MidiPlayer.gd" type="Script" id=8]
|
||||||
|
|
||||||
[sub_resource type="PanoramaSky" id=1]
|
[sub_resource type="PanoramaSky" id=1]
|
||||||
|
|
||||||
@ -89,11 +95,36 @@ adjustment_contrast = 1.0
|
|||||||
adjustment_saturation = 0.34
|
adjustment_saturation = 0.34
|
||||||
_sections_unfolded = [ "Adjustments", "Ambient Light", "Background", "Fog", "Resource" ]
|
_sections_unfolded = [ "Adjustments", "Ambient Light", "Background", "Fog", "Resource" ]
|
||||||
|
|
||||||
[node name="WorldEnvironment" type="WorldEnvironment"]
|
[sub_resource type="CubeMesh" id=3]
|
||||||
|
|
||||||
|
material = ExtResource( 6 )
|
||||||
|
custom_aabb = AABB( 0, 0, 0, 0, 0, 0 )
|
||||||
|
size = Vector3( 1, 1, 1 )
|
||||||
|
subdivide_width = 0
|
||||||
|
subdivide_height = 0
|
||||||
|
subdivide_depth = 0
|
||||||
|
|
||||||
|
[node name="Main" type="WorldEnvironment"]
|
||||||
|
|
||||||
environment = SubResource( 2 )
|
environment = SubResource( 2 )
|
||||||
|
script = ExtResource( 2 )
|
||||||
|
|
||||||
[node name="Camera" type="Camera" parent="." index="0"]
|
[node name="HUD" type="Control" parent="." index="0"]
|
||||||
|
|
||||||
|
anchor_left = 0.0
|
||||||
|
anchor_top = 0.0
|
||||||
|
anchor_right = 0.0
|
||||||
|
anchor_bottom = 0.0
|
||||||
|
margin_right = 40.0
|
||||||
|
margin_bottom = 40.0
|
||||||
|
rect_pivot_offset = Vector2( 0, 0 )
|
||||||
|
rect_clip_content = false
|
||||||
|
mouse_filter = 0
|
||||||
|
mouse_default_cursor_shape = 0
|
||||||
|
size_flags_horizontal = 1
|
||||||
|
size_flags_vertical = 1
|
||||||
|
|
||||||
|
[node name="Camera" type="Camera" parent="." index="1"]
|
||||||
|
|
||||||
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4.5, 10, 12 )
|
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4.5, 10, 12 )
|
||||||
keep_aspect = 1
|
keep_aspect = 1
|
||||||
@ -110,7 +141,7 @@ near = 0.05
|
|||||||
far = -1.22275e+007
|
far = -1.22275e+007
|
||||||
_sections_unfolded = [ "Transform" ]
|
_sections_unfolded = [ "Transform" ]
|
||||||
|
|
||||||
[node name="DirectionalLight" type="DirectionalLight" parent="." index="1"]
|
[node name="DirectionalLight" type="DirectionalLight" parent="." index="2"]
|
||||||
|
|
||||||
transform = Transform( 1, 0, 0, 0, 0.999391, -0.0348995, 0, 0.0348995, 0.999391, 0, 0, 50 )
|
transform = Transform( 1, 0, 0, 0, 0.999391, -0.0348995, 0, 0.0348995, 0.999391, 0, 0, 50 )
|
||||||
layers = 1
|
layers = 1
|
||||||
@ -138,25 +169,139 @@ directional_shadow_depth_range = 0
|
|||||||
directional_shadow_max_distance = 200.0
|
directional_shadow_max_distance = 200.0
|
||||||
_sections_unfolded = [ "Light", "Transform" ]
|
_sections_unfolded = [ "Light", "Transform" ]
|
||||||
|
|
||||||
[node name="GridMap" parent="." index="2" instance=ExtResource( 2 )]
|
[node name="GridMap" type="GridMap" parent="." index="3"]
|
||||||
|
|
||||||
|
theme = ExtResource( 3 )
|
||||||
|
cell_size = Vector3( 1, 1, 1 )
|
||||||
|
cell_octant_size = 8
|
||||||
|
cell_center_x = false
|
||||||
|
cell_center_y = false
|
||||||
|
cell_center_z = false
|
||||||
|
cell_scale = 1.0
|
||||||
|
collision_layer = 1
|
||||||
|
collision_mask = 1
|
||||||
data = {
|
data = {
|
||||||
"cells": PoolIntArray( )
|
"cells": PoolIntArray( )
|
||||||
}
|
}
|
||||||
|
script = ExtResource( 4 )
|
||||||
|
_sections_unfolded = [ "Cell", "Transform" ]
|
||||||
|
__meta__ = {
|
||||||
|
"_editor_clip_": 1,
|
||||||
|
"_editor_floor_": Vector3( 0, -1, 0 )
|
||||||
|
}
|
||||||
|
NB_LINES = 20
|
||||||
|
NB_COLLUMNS = 10
|
||||||
|
|
||||||
[node name="HUD" type="Control" parent="." index="3"]
|
[node name="GridBack" type="MeshInstance" parent="." index="4" instance=ExtResource( 5 )]
|
||||||
|
|
||||||
anchor_left = 0.0
|
transform = Transform( 10, 0, 0, 0, 20, 0, 0, 0, 0.1, 4.5, 9.5, -1 )
|
||||||
anchor_top = 0.0
|
layers = 1
|
||||||
anchor_right = 0.0
|
material_override = null
|
||||||
anchor_bottom = 0.0
|
cast_shadow = 1
|
||||||
margin_right = 40.0
|
extra_cull_margin = 0.0
|
||||||
margin_bottom = 40.0
|
use_in_baked_light = false
|
||||||
rect_pivot_offset = Vector2( 0, 0 )
|
lod_min_distance = 0.0
|
||||||
rect_clip_content = false
|
lod_min_hysteresis = 0.0
|
||||||
mouse_filter = 0
|
lod_max_distance = 0.0
|
||||||
mouse_default_cursor_shape = 0
|
lod_max_hysteresis = 0.0
|
||||||
size_flags_horizontal = 1
|
mesh = SubResource( 3 )
|
||||||
size_flags_vertical = 1
|
skeleton = NodePath("..")
|
||||||
|
material/0 = null
|
||||||
|
_sections_unfolded = [ "Transform" ]
|
||||||
|
|
||||||
|
[node name="HoldBack" type="MeshInstance" parent="." index="5" instance=ExtResource( 5 )]
|
||||||
|
|
||||||
|
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, -5, 16, -1 )
|
||||||
|
layers = 1
|
||||||
|
material_override = null
|
||||||
|
cast_shadow = 1
|
||||||
|
extra_cull_margin = 0.0
|
||||||
|
use_in_baked_light = false
|
||||||
|
lod_min_distance = 0.0
|
||||||
|
lod_min_hysteresis = 0.0
|
||||||
|
lod_max_distance = 0.0
|
||||||
|
lod_max_hysteresis = 0.0
|
||||||
|
mesh = SubResource( 3 )
|
||||||
|
skeleton = NodePath("..")
|
||||||
|
material/0 = null
|
||||||
|
_sections_unfolded = [ "Transform" ]
|
||||||
|
|
||||||
|
[node name="NextBack" type="MeshInstance" parent="." index="6" instance=ExtResource( 5 )]
|
||||||
|
|
||||||
|
transform = Transform( 7, 0, 0, 0, 7, 0, 0, 0, 0.1, 14, 16, -1 )
|
||||||
|
layers = 1
|
||||||
|
material_override = null
|
||||||
|
cast_shadow = 1
|
||||||
|
extra_cull_margin = 0.0
|
||||||
|
use_in_baked_light = false
|
||||||
|
lod_min_distance = 0.0
|
||||||
|
lod_min_hysteresis = 0.0
|
||||||
|
lod_max_distance = 0.0
|
||||||
|
lod_max_hysteresis = 0.0
|
||||||
|
mesh = SubResource( 3 )
|
||||||
|
skeleton = NodePath("..")
|
||||||
|
material/0 = null
|
||||||
|
_sections_unfolded = [ "Transform" ]
|
||||||
|
|
||||||
|
[node name="DropTimer" type="Timer" parent="." index="7"]
|
||||||
|
|
||||||
|
process_mode = 1
|
||||||
|
wait_time = 1.0
|
||||||
|
one_shot = false
|
||||||
|
autostart = false
|
||||||
|
|
||||||
|
[node name="LockDelay" type="Timer" parent="." index="8"]
|
||||||
|
|
||||||
|
process_mode = 1
|
||||||
|
wait_time = 0.5
|
||||||
|
one_shot = true
|
||||||
|
autostart = false
|
||||||
|
|
||||||
|
[node name="AutoShiftDelay" type="Timer" parent="." index="9"]
|
||||||
|
|
||||||
|
process_mode = 1
|
||||||
|
wait_time = 0.17
|
||||||
|
one_shot = true
|
||||||
|
autostart = false
|
||||||
|
|
||||||
|
[node name="AutoShiftTimer" type="Timer" parent="." index="10"]
|
||||||
|
|
||||||
|
process_mode = 1
|
||||||
|
wait_time = 0.03
|
||||||
|
one_shot = false
|
||||||
|
autostart = false
|
||||||
|
|
||||||
|
[node name="MidiPlayer" type="Node" parent="." index="11" instance=ExtResource( 7 )]
|
||||||
|
|
||||||
|
script = ExtResource( 8 )
|
||||||
|
max_polyphony = 64
|
||||||
|
file = "res://midi/Tetris - Song A.mid"
|
||||||
|
playing = false
|
||||||
|
channel_mute = [ false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false ]
|
||||||
|
play_speed = 1.0
|
||||||
|
volume_db = -24
|
||||||
|
key_shift = 0
|
||||||
|
loop = true
|
||||||
|
loop_start = 1.81
|
||||||
|
soundfont = "res://midi/FluidR3 GM.sf2"
|
||||||
|
mix_target = 0
|
||||||
|
bus = "Master"
|
||||||
|
|
||||||
|
[node name="LineCLearTimer" type="Timer" parent="MidiPlayer" index="1"]
|
||||||
|
|
||||||
|
process_mode = 1
|
||||||
|
wait_time = 1.41
|
||||||
|
one_shot = true
|
||||||
|
autostart = false
|
||||||
|
|
||||||
|
[connection signal="timeout" from="DropTimer" to="." method="_on_DropTimer_timeout"]
|
||||||
|
|
||||||
|
[connection signal="timeout" from="LockDelay" to="." method="_on_LockDelay_timeout"]
|
||||||
|
|
||||||
|
[connection signal="timeout" from="AutoShiftDelay" to="." method="_on_AutoShiftDelay_timeout"]
|
||||||
|
|
||||||
|
[connection signal="timeout" from="AutoShiftTimer" to="." method="_on_AutoShiftTimer_timeout"]
|
||||||
|
|
||||||
|
[connection signal="timeout" from="MidiPlayer/LineCLearTimer" to="." method="_on_LineCLearTimer_timeout"]
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user