71 lines
2.1 KiB
GDScript
71 lines
2.1 KiB
GDScript
extends Node2D
|
|
|
|
@export var tile_scene: PackedScene = preload("res://scenes/tile.tscn")
|
|
@export var barrier_scene: PackedScene = preload("res://scenes/barrier.tscn")
|
|
var tiles = []
|
|
var tile_size = 128.0
|
|
var test_broken_tiles = true
|
|
const boatSize = 5
|
|
var speed = 100
|
|
var velocity = Vector2()
|
|
|
|
func _ready():
|
|
spawn_boat_tiles()
|
|
spawn_boat_barriers()
|
|
|
|
func spawn_boat_tiles():
|
|
tiles.clear()
|
|
|
|
for y in range(boatSize):
|
|
var row = []
|
|
for x in range(boatSize):
|
|
if test_broken_tiles && randf() < 0.7 || (x == floor(boatSize/2) && y == floor(boatSize/2)): # 70% de chance de spawn une tuile pour tester les bateaux incomplets
|
|
var tile = tile_scene.instantiate()
|
|
add_child(tile)
|
|
|
|
#Positionner les tuiles pour que l'on spawne toujours au centre
|
|
tile.position = Vector2(
|
|
(x - ((boatSize - 1) * 0.5)) * tile_size,
|
|
(y - ((boatSize - 1) * 0.5)) * tile_size
|
|
)
|
|
|
|
row.append(tile)
|
|
else:
|
|
row.append(null) # On garde la structure mais on marque la tuile manquante
|
|
tiles.append(row)
|
|
|
|
|
|
func spawn_boat_barriers():
|
|
for x in range(tiles.size()):
|
|
for y in range(tiles[x].size()):
|
|
if tiles[x][y]:
|
|
var x_pos = [null,null,null,null];
|
|
var y_pos = [null,null,null,null];
|
|
var rotations = [90, 180, 270, 360]
|
|
if x == 0 || !tiles[x-1][y]:
|
|
x_pos[0] = tiles[x][y].position.x
|
|
y_pos[0] = tiles[x][y].position.y - (tile_size/2)
|
|
if y == 0 || !tiles[x][y-1]:
|
|
x_pos[1] = tiles[x][y].position.x - (tile_size/2)
|
|
y_pos[1] = tiles[x][y].position.y
|
|
if x == tiles.size()-1 || !tiles[x+1][y]:
|
|
x_pos[2] = tiles[x][y].position.x
|
|
y_pos[2] = tiles[x][y].position.y + (tile_size/2)
|
|
if y == tiles[x].size()-1 || !tiles[x][y+1]:
|
|
x_pos[3] = tiles[x][y].position.x + (tile_size/2)
|
|
y_pos[3] = tiles[x][y].position.y
|
|
for z in range(4):
|
|
if x_pos[z] != null && y_pos[z] != null:
|
|
var barrier = barrier_scene.instantiate()
|
|
add_child(barrier)
|
|
barrier.position = Vector2(
|
|
x_pos[z],
|
|
y_pos[z]
|
|
)
|
|
# On applique la rotation à la barrière
|
|
barrier.rotation = deg_to_rad(rotations[z])
|
|
|
|
func _physics_process(delta):
|
|
#Just spinning
|
|
rotation += delta/2
|