Texpand/scenes/Block/block.gd
2024-08-17 19:46:21 +02:00

150 lines
4.3 KiB
GDScript

extends Node2D
var secretPosition = Vector2()
@onready var blockParts = $BlockParts
@onready var stopTimer = $stopTimer
var stopped = false
var type : GLOBAL.BLOCKTYPES = GLOBAL.BLOCKTYPES.LIGHT
var TestPositions : Dictionary = {
1: $"Tests/Position 1",
2: $"Tests/Position 2",
3: $"Tests/Position 3",
4: $"Tests/Position 4",
}
var Positions : Dictionary = {
1: $"Position 1",
2: $"Position 2",
3: $"Position 3",
4: $"Position 4"
}
var currentRotation = 1
var id = 0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
secretPosition = position
generateBlockParts()
self.modulate = Color.from_hsv((randi() % 12) / 12.0, 1, 1)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
position = secretPosition.snapped(Vector2(64,64))
if not isCollidingBOTTOM() and( GLOBAL.hasSelectedSpeed or (self != GLOBAL.currentBlock)) :
secretPosition.y += GLOBAL.currentSpeed * delta * 30
if self == GLOBAL.currentBlock and not stopped:
#if not isCollidingBOTTOM():
# secretPosition.y += GLOBAL.SPEED.MAX * delta * 30
#else:
if stopTimer.is_stopped():
stopTimer.start(0.7)
#secretPosition = secretPosition.snapped(Vector2(64,64))
if Input.is_action_just_pressed("rotate_left"):
if not isNextRotationColliding(1):
currentRotation = interpolateRotation(1)
rotateBlock()
elif not isNextRotationColliding(1,1):
currentRotation = interpolateRotation(1,1)
rotateBlock()
elif Input.is_action_just_pressed("rotate_right"):
if not isNextRotationColliding(2):
currentRotation = interpolateRotation(2)
rotateBlock()
elif not isNextRotationColliding(2,1):
currentRotation = interpolateRotation(2,1)
rotateBlock()
elif Input.is_action_just_pressed("left"):
if not isCollidingLeft():
secretPosition.x -= GLOBAL.GRID
elif Input.is_action_just_pressed("right"):
if not isCollidingRIGHT():
secretPosition.x += GLOBAL.GRID
func isCollidingTOP() -> bool:
for i in blockParts.get_children():
if i.getCollisionTOP():
return true
return false
func isCollidingLeft() -> bool:
for i in blockParts.get_children():
if i.getCollisionLEFT():
return true
return false
func isCollidingRIGHT() -> bool:
for i in blockParts.get_children():
if i.getCollisionRIGHT():
return true
return false
func isCollidingBOTTOM() -> bool:
for i in blockParts.get_children():
if i.getCollisionBOTTOM():
return true
return false
func generateBlockParts():
for i in Positions[1].get_children():
var blockPart = load("res://scenes/Block/block_part.tscn").instantiate()
blockPart.position = i.position# + Vector2( GLOBAL.GRID / 2, GLOBAL.GRID / 2)
blockParts.add_child(blockPart)
func rotateBlock():
for i in blockParts.get_children().size():
blockParts.get_children()[i].position = Positions[currentRotation].get_children()[i].position
func interpolateRotation(direction, additions=0):
match direction:
1:
if currentRotation + 1 > 4:
return 1 + additions
elif currentRotation + (1 + additions) > 4:
return 1
elif currentRotation + (1 + additions) <= 4:
return currentRotation + (1 + additions)
2:
if currentRotation - 1 < 1:
return 4 - additions
elif currentRotation - (1 + additions) < 1:
return 4
elif currentRotation - (1 + additions) >= 1:
return currentRotation - (1 + additions)
func isNextRotationColliding(direction, additions=0):
var colliding = false
match direction:
1:
for i in TestPositions[interpolateRotation(1, additions)].get_overlapping_areas():
if not i.get_parent().get_parent() == TestPositions[interpolateRotation(1, additions)].get_parent().get_parent():
colliding = true
return colliding#TestPositions[interpolateRotation(1, additions)].has_overlapping_areas()
2:
for i in TestPositions[interpolateRotation(2, additions)].get_overlapping_areas():
if not i.get_parent().get_parent() == TestPositions[interpolateRotation(2, additions)].get_parent().get_parent():
colliding = true
return colliding#TestPositions[interpolateRotation(2, additions)].has_overlapping_areas()
func setId(newID):
id = newID
func setType(newType):
type = newType
func _on_stop_timer_timeout() -> void:
if isCollidingBOTTOM():
$Tests.queue_free()
for i in Positions:
Positions[i].queue_free()
stopped = true