BoatCoop/scripts/boat.gd

75 lines
2.0 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
const barrierOffset = 2
var speed = 100
var velocity = Vector2()
func _ready():
spawn_boat_tiles()
spawn_boat_barriers()
func spawn_boat_tiles():
tiles.clear()
for x in range(boatSize):
var row = []
for y 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
)
var collision_shape = barrier_scene.instantiate()
$Area2D.add_child(collision_shape)
collision_shape.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():
var collider_range = range(tiles.size())
collider_range.append_array([-1, boatSize])
for x in collider_range:
for y in collider_range:
if not has_tile(x, y):
var barrier = barrier_scene.instantiate()
$StaticBody2D.add_child(barrier)
barrier.position = Vector2(
(x - ((boatSize - 1) * 0.5)) * tile_size,
(y - ((boatSize - 1) * 0.5)) * tile_size
)
func has_tile(x : int, y : int):
if x >= 0 and y >= 0 and x < boatSize and y < boatSize:
return tiles[x][y] != null
else : return false
func _physics_process(delta):
#Just spinning
rotation += delta/6
func _on_player_is_dead():
print("Est mort")
func _on_area_2d_body_entered(body):
pass # Replace with function body.