feat (?) First tries
This commit is contained in:
70
scripts/boat.gd
Normal file
70
scripts/boat.gd
Normal file
@@ -0,0 +1,70 @@
|
||||
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
|
||||
27
scripts/game.gd
Normal file
27
scripts/game.gd
Normal file
@@ -0,0 +1,27 @@
|
||||
extends Node2D
|
||||
|
||||
@export var player_scene: PackedScene = preload("res://scenes/player.tscn")
|
||||
@export var boat_scene: PackedScene = preload("res://scenes/boat.tscn")
|
||||
|
||||
var player_instance: CharacterBody2D
|
||||
var boat_instance: Node2D
|
||||
|
||||
func _ready():
|
||||
# On enlève les scènes par défau
|
||||
var default_boat = $Boat
|
||||
var default_player = $Player
|
||||
if default_boat:
|
||||
default_boat.queue_free()
|
||||
if default_player:
|
||||
default_player.queue_free()
|
||||
|
||||
# On instancie les scènes que l'on veut garder
|
||||
boat_instance = boat_scene.instantiate()
|
||||
player_instance = player_scene.instantiate()
|
||||
|
||||
# On ajoue les scènes essentielles à la scène principale
|
||||
add_child(boat_instance)
|
||||
add_child(player_instance)
|
||||
|
||||
# Et on assigne le bateau au joueur
|
||||
player_instance.boat = boat_instance
|
||||
@@ -1,10 +1,48 @@
|
||||
extends CharacterBody2D
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -400.0
|
||||
@export var boat: Node2D
|
||||
|
||||
func _physics_process(delta):
|
||||
velocity.x = Input.get_axis("ui_left", "ui_right") * SPEED
|
||||
velocity.y = Input.get_axis("ui_up", "ui_down") * SPEED
|
||||
const TILE_SIZE = 128
|
||||
var relative_position_on_boat = Vector2(0, 0)
|
||||
var is_on_boat = true
|
||||
var last_boat_rotation = 0.0
|
||||
|
||||
move_and_slide()
|
||||
func _ready():
|
||||
if boat:
|
||||
relative_position_on_boat = position - boat.position
|
||||
last_boat_rotation = boat.rotation
|
||||
|
||||
func _physics_process(_delta):
|
||||
# On tourne le joueur vers la souris
|
||||
var mouse_position = get_global_mouse_position()
|
||||
look_at(mouse_position)
|
||||
if is_on_boat:
|
||||
if boat:
|
||||
# On calcule la difference de rotation du bateau depuis le dernier mouvement
|
||||
var delta_rotation = boat.rotation - last_boat_rotation
|
||||
|
||||
# On adape la position du joueur en fonction de la nouvelle rota
|
||||
relative_position_on_boat = relative_position_on_boat.rotated(delta_rotation)
|
||||
position = boat.position + relative_position_on_boat
|
||||
|
||||
# On autorise le joueur à se déplacer dans le bateau
|
||||
velocity.x = Input.get_axis("ui_left", "ui_right") * SPEED
|
||||
velocity.y = Input.get_axis("ui_up", "ui_down") * SPEED
|
||||
|
||||
move_and_slide()
|
||||
|
||||
# Mise à jour des paramètres pour le déplacement du joueur en fonction du bateau
|
||||
relative_position_on_boat = position - boat.position
|
||||
last_boat_rotation = boat.rotation
|
||||
|
||||
else:
|
||||
# Quand on est pas sur le bateau, on se déplace librement (le lapin surtout)
|
||||
velocity.x = Input.get_axis("ui_left", "ui_right") * SPEED/2
|
||||
velocity.y = Input.get_axis("ui_up", "ui_down") * SPEED/2
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func set_on_boat(on_boat: bool):
|
||||
#TODO
|
||||
is_on_boat = on_boat
|
||||
|
||||
Reference in New Issue
Block a user