Reskin player

This commit is contained in:
2024-10-23 14:56:11 +08:00
parent 09df1be654
commit 52636a2a7e
40 changed files with 647 additions and 66 deletions
+33 -9
View File
@@ -46,6 +46,7 @@ func _on_peer_connected(new_peer_id):
await get_tree().create_timer(1).timeout
rpc("add_newly_connected_player_character", new_peer_id)
rpc_id(new_peer_id, "add_previously_connected_player_characters", connected_peer_ids)
rpc_id(new_peer_id, "sync_game_state", players, bots, game_started)
add_player_character(new_peer_id)
replace_bot_with_player(new_peer_id)
@@ -65,25 +66,35 @@ func add_player_character(peer_id):
local_player_character = player_character
func add_bot(bot_id):
if multiplayer.is_server():
rpc("create_bot", bot_id)
@rpc("call_local")
func create_bot(bot_id):
var bot_character = player_scene.instantiate()
bot_character.set_multiplayer_authority(1) # Set server as authority for bots
bot_character.name = str(bot_id)
add_child(bot_character)
bot_character.add_to_group("Players", true)
bot_character.add_to_group("Bots", true)
bots.append(bot_id)
players.append(bot_id)
if multiplayer.is_server():
bots.append(bot_id)
players.append(bot_id)
func replace_bot_with_player(player_id):
if bots.size() > 0:
if multiplayer.is_server() and bots.size() > 0:
var bot_id = bots.pop_front()
players.erase(bot_id)
players.append(player_id)
var bot_node = get_node(str(bot_id))
if bot_node:
bot_node.queue_free()
rpc("remove_bot", bot_id)
rpc("sync_players", players)
@rpc("call_local")
func remove_bot(bot_id):
var bot_node = get_node(str(bot_id))
if bot_node:
bot_node.queue_free()
func get_next_available_bot_id():
for i in range(2, max_players + 1):
if not i in players:
@@ -99,6 +110,17 @@ func add_previously_connected_player_characters(peer_ids):
for peer_id in peer_ids:
add_player_character(peer_id)
@rpc("call_local")
func sync_game_state(current_players, current_bots, is_game_started):
players = current_players
bots = current_bots
game_started = is_game_started
# Create bot characters for existing bots
for bot_id in bots:
if not has_node(str(bot_id)):
create_bot(bot_id)
func _process(_delta):
if multiplayer.is_server() and game_started:
rpc("sync_turn_index", current_turn_index)
@@ -107,13 +129,15 @@ func start_game():
if multiplayer.is_server():
game_started = true
connected_peer_ids.sort()
rpc("sync_game_start", connected_peer_ids)
rpc("sync_game_start", connected_peer_ids, players, bots)
current_turn_index = -1
next_turn()
@rpc
func sync_game_start(peer_ids):
@rpc("call_local")
func sync_game_start(peer_ids, current_players, current_bots):
connected_peer_ids = peer_ids
players = current_players
bots = current_bots
game_started = true
@rpc("reliable")
+4 -4
View File
@@ -12,19 +12,19 @@ script = ExtResource("1_xcpe3")
mesh_library = ExtResource("1_110wo")
cell_size = Vector3(1, 1, 1)
data = {
"cells": PackedInt32Array(0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 1, 0, 0, 1, 1, 0, 1, 2, 0, 1, 3, 0, 1, 4, 0, 1, 5, 0, 1, 6, 0, 1, 7, 4, 1, 8, 0, 1, 9, 4, 2, 0, 4, 2, 1, 0, 2, 2, 0, 2, 3, 0, 2, 4, 0, 2, 5, 0, 2, 6, 0, 2, 7, 0, 2, 8, 0, 2, 9, 0, 3, 0, 0, 3, 1, 0, 3, 2, 0, 3, 3, 0, 3, 4, 0, 3, 5, 0, 3, 6, 0, 3, 7, 4, 3, 8, 0, 3, 9, 0, 4, 0, 0, 4, 1, 0, 4, 2, 0, 4, 3, 0, 4, 4, 0, 4, 5, 0, 4, 6, 0, 4, 7, 0, 4, 8, 0, 4, 9, 4, 5, 0, 0, 5, 1, 0, 5, 2, 0, 5, 3, 0, 5, 4, 0, 5, 5, 0, 5, 6, 0, 5, 7, 0, 5, 8, 0, 5, 9, 0, 6, 0, 4, 6, 1, 0, 6, 2, 0, 6, 3, 0, 6, 4, 0, 6, 5, 0, 6, 6, 0, 6, 7, 0, 6, 8, 0, 6, 9, 4, 7, 0, 0, 7, 1, 0, 7, 2, 0, 7, 3, 0, 7, 4, 0, 7, 5, 0, 7, 6, 0, 7, 7, 0, 7, 8, 0, 7, 9, 0, 8, 0, 0, 8, 1, 0, 8, 2, 0, 8, 3, 0, 8, 4, 4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, 7, 0, 9, 8, 0, 9, 9, 0)
}
script = ExtResource("2_hbe1v")
non_walkable_items = Array[int]([4, 5])
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 5, 30, 5)
visible = false
transform = Transform3D(-1, 0, -8.74228e-08, -6.2963e-08, 0.693754, 0.720212, 6.06499e-08, 0.720212, -0.693754, 5, 15, -10)
environment = ExtResource("4_ky38j")
fov = 35.5
[node name="Camera3D2" type="Camera3D" parent="."]
[node name="Camera3D200" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 5, 30, 5)
visible = false
environment = ExtResource("4_ky38j")
fov = 35.5
+47 -52
View File
@@ -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)
+28 -1
View File
@@ -1,7 +1,12 @@
[gd_scene load_steps=7 format=3 uid="uid://1dbdbg3q5778"]
[gd_scene load_steps=12 format=3 uid="uid://1dbdbg3q5778"]
[ext_resource type="Script" path="res://scenes/player.gd" id="1_qecr4"]
[ext_resource type="PackedScene" uid="uid://ejeamn0pyey4" path="res://assets/characters/Bob.glb" id="2_3e0d5"]
[ext_resource type="Texture2D" uid="uid://b4y41h16q6m34" path="res://assets/textures/bub.png" id="2_5w327"]
[ext_resource type="PackedScene" uid="uid://1vk0mjnwkngi" path="res://assets/characters/Masbro.glb" id="2_mjsl8"]
[ext_resource type="PackedScene" uid="uid://d4cul3w3wem5w" path="res://assets/characters/Gatot.glb" id="4_3tlf6"]
[ext_resource type="PackedScene" uid="uid://bmln7v6v5kvxg" path="res://assets/characters/Oldpop.glb" id="5_alfd1"]
[ext_resource type="AnimationLibrary" uid="uid://c3pyopnwibckj" path="res://assets/characters/animations/animation-pack.res" id="6_5oq5w"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_xqgey"]
albedo_color = Color(0.85, 0.085, 0.238, 1)
@@ -19,7 +24,29 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.5, 0)
script = ExtResource("1_qecr4")
cell_size = Vector3(1, 1, 1)
[node name="Masbro" parent="." instance=ExtResource("2_mjsl8")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.485, 0)
[node name="Bob" parent="." instance=ExtResource("2_3e0d5")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.484627, 0)
visible = false
[node name="Gatot" parent="." instance=ExtResource("4_3tlf6")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.485, 0)
visible = false
[node name="Oldpop" parent="." instance=ExtResource("5_alfd1")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.485, 0)
visible = false
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
root_node = NodePath("../Masbro")
libraries = {
"animation-pack": ExtResource("6_5oq5w")
}
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
visible = false
mesh = SubResource("CapsuleMesh_l8ldl")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]