Reskin player
This commit is contained in:
+47
-52
@@ -1,60 +1,55 @@
|
||||
extends Node3D # This script is attached to a Node3D
|
||||
extends Node3D
|
||||
|
||||
# Export variables for inspector configuration
|
||||
@export var enhanced_gridmap_path: NodePath = "/root/Main/EnhancedGridMap" # Path to the EnhancedGridMap node
|
||||
var enhanced_gridmap: EnhancedGridMap # References to the EnhancedGridMap node
|
||||
@export var current_position: Vector2i # Current grid position of the player
|
||||
var is_player_moving: bool = false # Flag to prevent movement while already moving
|
||||
@export var enhanced_gridmap_path: NodePath = "/root/Main/EnhancedGridMap"
|
||||
var enhanced_gridmap: EnhancedGridMap
|
||||
@export var current_position: Vector2i
|
||||
var is_player_moving: bool = false
|
||||
|
||||
# Customizable cell size and offset
|
||||
@export var cell_size: Vector3 = Vector3(2, 2, 2) # Size of each grid cell
|
||||
@export var cell_offset: Vector3 = Vector3(0, 0, 0) # Offset for the grid
|
||||
@export var cell_size: Vector3 = Vector3(2, 2, 2)
|
||||
@export var cell_offset: Vector3 = Vector3(0, 0, 0)
|
||||
|
||||
# Center offset flags
|
||||
@export var center_x: bool = false # Center the player on X axis
|
||||
@export var center_y: bool = false # Center the player on Y axis
|
||||
@export var center_z: bool = false # Center the player on Z axis
|
||||
@export var center_x: bool = false
|
||||
@export var center_y: bool = false
|
||||
@export var center_z: bool = false
|
||||
|
||||
# Diagonal movement flag
|
||||
@export var use_diagonal_movement: bool = false: # Allow diagonal movement
|
||||
@export var use_diagonal_movement: bool = false:
|
||||
set(value):
|
||||
use_diagonal_movement = value
|
||||
if enhanced_gridmap:
|
||||
enhanced_gridmap.set_diagonal_movement(value)
|
||||
|
||||
# Turn management variables
|
||||
@export var is_my_turn: bool = false: # Flag to indicate if it's this player's turn
|
||||
@export var is_my_turn: bool = false:
|
||||
set(value):
|
||||
is_my_turn = value
|
||||
if is_my_turn and is_multiplayer_authority():
|
||||
rpc("display_message", "It's your turn!") # RPC call to display turn message
|
||||
@export var has_moved_this_turn = false # Flag to track if player has moved this turn
|
||||
@onready var main_scene = get_tree().current_scene # Reference to the main scene
|
||||
rpc("display_message", "It's your turn!")
|
||||
@export var has_moved_this_turn = false
|
||||
@onready var main_scene = get_tree().current_scene
|
||||
|
||||
func _ready(): # Called when the node enters the scene tree
|
||||
name = str(get_multiplayer_authority()) # Set the node name to the multiplayer authority ID
|
||||
$Name.text = str(name) # Set the displayed name
|
||||
func _ready():
|
||||
name = str(get_multiplayer_authority())
|
||||
$Name.text = str(name)
|
||||
|
||||
enhanced_gridmap = get_node(enhanced_gridmap_path) # Get the EnhancedGridMap node
|
||||
enhanced_gridmap = get_node(enhanced_gridmap_path)
|
||||
|
||||
if main_scene:
|
||||
enhanced_gridmap = main_scene.get_node("EnhancedGridMap") # Get EnhancedGridMap from main scene
|
||||
enhanced_gridmap = main_scene.get_node("EnhancedGridMap")
|
||||
else:
|
||||
push_error("Main scene not found") # Error if main scene not found
|
||||
push_error("Main scene not found")
|
||||
|
||||
if not enhanced_gridmap:
|
||||
push_error("EnhancedGridMap node not found. Please set the correct path in the inspector.")
|
||||
return
|
||||
|
||||
enhanced_gridmap.initialize_astar() # Initialize A* pathfinding
|
||||
enhanced_gridmap.set_diagonal_movement(use_diagonal_movement) # Set diagonal movement option
|
||||
enhanced_gridmap.initialize_astar()
|
||||
enhanced_gridmap.set_diagonal_movement(use_diagonal_movement)
|
||||
|
||||
current_position = find_valid_starting_position() # Find a valid starting position
|
||||
update_player_position(current_position) # Update player's position
|
||||
current_position = find_valid_starting_position()
|
||||
update_player_position(current_position)
|
||||
|
||||
set_process_unhandled_input(is_multiplayer_authority()) # Only process input for the authority
|
||||
set_process_unhandled_input(is_multiplayer_authority())
|
||||
|
||||
func find_valid_starting_position() -> Vector2i: # Find a valid starting position
|
||||
func find_valid_starting_position() -> Vector2i:
|
||||
var rng = RandomNumberGenerator.new()
|
||||
rng.randomize()
|
||||
|
||||
@@ -62,15 +57,15 @@ func find_valid_starting_position() -> Vector2i: # Find a valid starting positi
|
||||
var attempts = 0
|
||||
|
||||
while attempts < max_attempts:
|
||||
current_position = Vector2i(0, rng.randi_range(0, 9)) # Generate random position
|
||||
current_position = Vector2i(0, rng.randi_range(0, 9))
|
||||
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(current_position.x, 0, current_position.y))
|
||||
|
||||
if cell_item not in enhanced_gridmap.non_walkable_items:
|
||||
return current_position # Return valid position
|
||||
return current_position
|
||||
|
||||
attempts += 1
|
||||
|
||||
return Vector2i(0, 0) # Default position if no valid position found
|
||||
return Vector2i(0, 0)
|
||||
|
||||
func find_random_valid_position() -> Vector2i:
|
||||
var rng = RandomNumberGenerator.new()
|
||||
@@ -85,17 +80,17 @@ func find_random_valid_position() -> Vector2i:
|
||||
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(random_position.x, 0, random_position.y))
|
||||
|
||||
if cell_item not in enhanced_gridmap.non_walkable_items and random_position != current_position:
|
||||
return random_position # Return valid position
|
||||
return random_position
|
||||
|
||||
attempts += 1
|
||||
|
||||
return current_position # Return current position if no new valid position found
|
||||
return current_position
|
||||
|
||||
func _physics_process(_delta): # Called every physics frame
|
||||
func _physics_process(_delta):
|
||||
if is_multiplayer_authority():
|
||||
rpc("remote_set_position", global_position) # RPC call to sync position
|
||||
rpc("remote_set_position", global_position)
|
||||
|
||||
func _unhandled_input(event): # Handle input events
|
||||
func _unhandled_input(event):
|
||||
if not is_multiplayer_authority() or not is_my_turn or is_player_moving:
|
||||
return
|
||||
|
||||
@@ -108,7 +103,7 @@ func _unhandled_input(event): # Handle input events
|
||||
if click_position != Vector2i(-1, -1):
|
||||
move_player_to_clicked_position(click_position)
|
||||
|
||||
func raycast_to_grid(from: Vector3, to: Vector3) -> Vector2i: # Convert 3D raycast to grid position
|
||||
func raycast_to_grid(from: Vector3, to: Vector3) -> Vector2i:
|
||||
var plane = Plane(Vector3.UP, cell_offset.y)
|
||||
var intersection = plane.intersects_ray(from, to - from)
|
||||
|
||||
@@ -123,9 +118,9 @@ func raycast_to_grid(from: Vector3, to: Vector3) -> Vector2i: # Convert 3D rayc
|
||||
grid_position.y >= 0 and grid_position.y < enhanced_gridmap.rows:
|
||||
return grid_position
|
||||
|
||||
return Vector2i(-1, -1) # Invalid position
|
||||
return Vector2i(-1, -1)
|
||||
|
||||
func move_player_to_clicked_position(grid_position: Vector2i): # Move player to clicked position
|
||||
func move_player_to_clicked_position(grid_position: Vector2i):
|
||||
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(grid_position.x, 0, grid_position.y))
|
||||
|
||||
if cell_item in enhanced_gridmap.non_walkable_items:
|
||||
@@ -140,7 +135,7 @@ func move_player_to_clicked_position(grid_position: Vector2i): # Move player to
|
||||
else:
|
||||
print("No valid path found")
|
||||
|
||||
func move_player_along_path(path: Array): # Move player along calculated path
|
||||
func move_player_along_path(path: Array):
|
||||
is_player_moving = true
|
||||
var tween = create_tween()
|
||||
tween.set_trans(Tween.TRANS_CUBIC)
|
||||
@@ -176,10 +171,10 @@ func move_bot_along_path(path: Array):
|
||||
end_turn()
|
||||
)
|
||||
|
||||
func update_player_position(grid_position: Vector2i): # Update player's position
|
||||
func update_player_position(grid_position: Vector2i):
|
||||
position = grid_to_world(grid_position)
|
||||
|
||||
func grid_to_world(grid_position: Vector2i) -> Vector3: # Convert grid position to world position
|
||||
func grid_to_world(grid_position: Vector2i) -> Vector3:
|
||||
var world_position = Vector3(
|
||||
grid_position.x * cell_size.x,
|
||||
cell_size.y,
|
||||
@@ -198,24 +193,24 @@ func grid_to_world(grid_position: Vector2i) -> Vector3: # Convert grid position
|
||||
|
||||
return world_position + cell_offset
|
||||
|
||||
func start_turn(): # Start player's turn
|
||||
func start_turn():
|
||||
has_moved_this_turn = false
|
||||
is_my_turn = true
|
||||
if is_multiplayer_authority():
|
||||
rpc("display_message", "It's your turn!") # RPC call to display turn message
|
||||
rpc("display_message", "It's your turn!")
|
||||
|
||||
func end_turn(): # End player's turn
|
||||
func end_turn():
|
||||
is_my_turn = false
|
||||
has_moved_this_turn = false
|
||||
if is_multiplayer_authority():
|
||||
get_node("/root/Main").request_next_turn() # Request next turn from main scene
|
||||
get_node("/root/Main").request_next_turn()
|
||||
|
||||
@rpc("any_peer", "call_local", "unreliable")
|
||||
func remote_set_position(authority_position): # RPC function to sync position
|
||||
func remote_set_position(authority_position):
|
||||
global_position = authority_position
|
||||
|
||||
@rpc("any_peer", "call_local")
|
||||
func display_message(message): # RPC function to display messages
|
||||
func display_message(message):
|
||||
$Bubble.show()
|
||||
$Bubble/Message.show()
|
||||
$Bubble/Message.text = str(message)
|
||||
|
||||
Reference in New Issue
Block a user