mirror of
https://github.com/Rundll86/Dog-Lynx-And-HCN.git
synced 2026-05-30 16:01:53 +08:00
feat(多人游戏): 实现多人游戏状态同步和单机/多人模式切换
添加多人游戏状态管理,包括连接状态和玩家名称同步 修改WorldManager和EntityBase以支持多人游戏逻辑 在Starter面板中实现单机和多人游戏启动功能
This commit is contained in:
@@ -19,7 +19,6 @@ config/icon="res://icon.svg"
|
||||
|
||||
window/size/viewport_width=1280
|
||||
window/size/viewport_height=720
|
||||
window/size/mode=2
|
||||
|
||||
[dotnet]
|
||||
|
||||
|
||||
@@ -53,9 +53,16 @@ func rebuildAllPlayers(playerNames: Array[String]):
|
||||
addPlayerName(i)
|
||||
@rpc("any_peer")
|
||||
func startMultiplayerGame():
|
||||
MultiplayerState.isMultiplayer = true
|
||||
WorldManager.rootNode.multiplayer.multiplayer_peer = MultiplayerState.connection
|
||||
MultiplayerState.playerName = playerNameInput.text
|
||||
MultiplayerState.connection = multiplayer.multiplayer_peer
|
||||
for i in getPlayerNames():
|
||||
EntityBase.generatePlayer(i)
|
||||
UIState.closeCurrentPanel()
|
||||
func startSingleplayerGame():
|
||||
MultiplayerState.isMultiplayer = false
|
||||
EntityBase.generatePlayer(playerNameInput.text)
|
||||
Wave.next()
|
||||
UIState.closeCurrentPanel()
|
||||
|
||||
@@ -74,9 +81,7 @@ func _ready():
|
||||
)
|
||||
startSingleplayerBtn.pressed.connect(
|
||||
func():
|
||||
EntityBase.generatePlayer(playerNameInput.text)
|
||||
Wave.next()
|
||||
UIState.closeCurrentPanel()
|
||||
startSingleplayerGame()
|
||||
)
|
||||
startMultiplayerBtn.pressed.connect(
|
||||
func():
|
||||
|
||||
@@ -105,6 +105,7 @@ var currentStage: int = 0
|
||||
var spawnTime: float = 0
|
||||
|
||||
func _ready():
|
||||
multiplayer.multiplayer_peer = MultiplayerState.connection
|
||||
if useStatic:
|
||||
texture = texture.get_node("staticAnimation")
|
||||
spawnTime = WorldManager.getTime()
|
||||
@@ -170,7 +171,7 @@ func _physics_process(_delta: float) -> void:
|
||||
velocity = Vector2.ZERO
|
||||
if (isPlayer() or is_instance_valid(currentFocusedBoss)) and not charginup and canRunAi:
|
||||
if isPlayer():
|
||||
if MultiplayerState.playerName == displayName:
|
||||
if MultiplayerState.playerName == displayName or not MultiplayerState.isMultiplayer:
|
||||
ai()
|
||||
else:
|
||||
ai()
|
||||
@@ -180,6 +181,13 @@ func _physics_process(_delta: float) -> void:
|
||||
storeEnergy(randf_range(0.01, 0.05 + fields.get(FieldStore.Entity.ENERGY_REGENERATION) - 1), true)
|
||||
trailParticle.emitting = trailing
|
||||
|
||||
# 同步状态
|
||||
@rpc("any_peer")
|
||||
func syncPosition(player: String, newPosition: Vector2, newVelocity: Vector2):
|
||||
if player != displayName: return
|
||||
position = newPosition
|
||||
velocity = newVelocity
|
||||
|
||||
# 通用方法
|
||||
func rebuildWeaponIcons():
|
||||
if isPlayer():
|
||||
@@ -216,6 +224,9 @@ func move(direction: Vector2, isSprinting: bool = false):
|
||||
var currentDirection = sign(direction.x)
|
||||
if currentDirection != 0:
|
||||
lastDirection = currentDirection
|
||||
if MultiplayerState.isMultiplayer:
|
||||
print("test")
|
||||
syncPosition.rpc(displayName, position, velocity)
|
||||
func getSprintInitialDisplace():
|
||||
return displace(velocity) * sprintMultiplier
|
||||
func getSprintProgress():
|
||||
@@ -437,6 +448,7 @@ func kill():
|
||||
static func generatePlayer(playerName: String):
|
||||
var player = generate(ComponentManager.getCharacter("Rooster"), Vector2.ZERO, false, false, true)
|
||||
player.displayName = playerName
|
||||
player.name = "Player_%s" % playerName
|
||||
return player
|
||||
static func generate(
|
||||
entity: PackedScene,
|
||||
|
||||
@@ -20,9 +20,12 @@ static var stateColorMap = {
|
||||
ConnectionState.CONNECTED_CLIENT: Color.GREEN,
|
||||
}
|
||||
|
||||
static var isMultiplayer: bool = false
|
||||
static var state: ConnectionState = ConnectionState.DISCONNECTED
|
||||
static var playerName: String
|
||||
|
||||
static var connection: ENetMultiplayerPeer
|
||||
|
||||
static var maxPlayer: int = 10
|
||||
|
||||
static func isConnected():
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
extends Node2D
|
||||
class_name WorldManager
|
||||
|
||||
static var rootNode: Node2D
|
||||
static var rootNode: WorldManager
|
||||
static var tree: SceneTree
|
||||
static var runningTime: int = 0
|
||||
static var peer: ENetMultiplayerPeer
|
||||
@@ -10,11 +10,12 @@ func _ready():
|
||||
tree = get_tree()
|
||||
rootNode = self
|
||||
ComponentManager.init()
|
||||
peer = ENetMultiplayerPeer.new()
|
||||
func _physics_process(delta):
|
||||
runningTime += delta * 1000
|
||||
if EntityBase.mobCount() == 0 and runningTime > 3000:
|
||||
UIState.setPanel("MakeFeed")
|
||||
if multiplayer.is_server() or not MultiplayerState.isMultiplayer:
|
||||
if EntityBase.mobCount() == 0 and runningTime > 3000:
|
||||
Wave.next()
|
||||
UIState.setPanel("MakeFeed")
|
||||
|
||||
static func getTime():
|
||||
return runningTime
|
||||
|
||||
Reference in New Issue
Block a user