This commit is contained in:
2024-10-23 18:00:25 +08:00
parent 52636a2a7e
commit df5ecfa7bc
7 changed files with 175 additions and 34 deletions
+65 -10
View File
@@ -12,7 +12,8 @@ var is_player_moving: bool = false
@export var center_y: bool = false
@export var center_z: bool = false
@export var use_diagonal_movement: bool = false:
@export var movement_range: int = 1 # How many blocks can be moved at once
@export var use_diagonal_movement: bool = false: # Allow diagonal movement
set(value):
use_diagonal_movement = value
if enhanced_gridmap:
@@ -67,7 +68,7 @@ func find_valid_starting_position() -> Vector2i:
return Vector2i(0, 0)
func find_random_valid_position() -> Vector2i:
func find_random_valid_position_in_range() -> Vector2i:
var rng = RandomNumberGenerator.new()
rng.randomize()
@@ -75,8 +76,23 @@ func find_random_valid_position() -> Vector2i:
var attempts = 0
while attempts < max_attempts:
var random_position = Vector2i(rng.randi_range(0, enhanced_gridmap.columns - 1),
rng.randi_range(0, enhanced_gridmap.rows - 1))
var range_x = min(enhanced_gridmap.columns - 1, movement_range)
var range_y = min(enhanced_gridmap.rows - 1, movement_range)
# Generate position within movement range of current position
var offset_x = rng.randi_range(-range_x, range_x)
var offset_y = rng.randi_range(-range_y, range_y)
var random_position = Vector2i(
clamp(current_position.x + offset_x, 0, enhanced_gridmap.columns - 1),
clamp(current_position.y + offset_y, 0, enhanced_gridmap.rows - 1)
)
# Check if the position is within movement range
if not is_within_movement_range(random_position):
attempts += 1
continue
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:
@@ -91,7 +107,8 @@ func _physics_process(_delta):
rpc("remote_set_position", global_position)
func _unhandled_input(event):
if not is_multiplayer_authority() or not is_my_turn or is_player_moving:
var main = get_node("/root/Main")
if not is_multiplayer_authority() or (main.turn_based_mode and (not is_my_turn or is_player_moving)):
return
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
@@ -120,7 +137,30 @@ func raycast_to_grid(from: Vector3, to: Vector3) -> Vector2i:
return Vector2i(-1, -1)
func is_within_movement_range(target_position: Vector2i) -> bool:
var distance = 0
if use_diagonal_movement:
# For diagonal movement, use max of x and y differences
distance = max(abs(target_position.x - current_position.x),
abs(target_position.y - current_position.y))
else:
# For orthogonal movement, use Manhattan distance
distance = abs(target_position.x - current_position.x) + \
abs(target_position.y - current_position.y)
return distance <= movement_range
func move_player_to_clicked_position(grid_position: Vector2i):
if not is_multiplayer_authority():
return
if is_player_moving:
return
# Check if the movement is within range
if not is_within_movement_range(grid_position):
print("Movement out of range")
return
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:
@@ -131,11 +171,12 @@ func move_player_to_clicked_position(grid_position: Vector2i):
if path.size() > 1:
path.pop_front()
move_player_along_path(path)
rpc("start_movement_along_path", path)
else:
print("No valid path found")
func move_player_along_path(path: Array):
@rpc("any_peer", "call_local")
func start_movement_along_path(path: Array):
is_player_moving = true
var tween = create_tween()
tween.set_trans(Tween.TRANS_CUBIC)
@@ -150,10 +191,19 @@ func move_player_along_path(path: Array):
is_player_moving = false
enhanced_gridmap.clear_path_visualization()
has_moved_this_turn = true
end_turn()
var main = get_node("/root/Main")
if main.turn_based_mode:
end_turn()
)
func move_bot_along_path(path: Array):
func move_bot_along_path(path: Array, bot_id: int):
if not is_multiplayer_authority():
return
rpc("start_bot_movement_along_path", path, bot_id)
@rpc("any_peer", "call_local")
func start_bot_movement_along_path(path: Array, bot_id: int):
is_player_moving = true
var tween = create_tween()
tween.set_trans(Tween.TRANS_CUBIC)
@@ -168,7 +218,12 @@ func move_bot_along_path(path: Array):
is_player_moving = false
enhanced_gridmap.clear_path_visualization()
has_moved_this_turn = true
end_turn()
var main = get_node("/root/Main")
if main.turn_based_mode:
end_turn()
# Notify main that bot movement is complete
if multiplayer.is_server():
main.bot_movement_completed(bot_id)
)
func update_player_position(grid_position: Vector2i):