104 lines
3.3 KiB
Plaintext
104 lines
3.3 KiB
Plaintext
|
-- 玩家循环代理
|
|||
|
local PlayerFightProxy = {}
|
|||
|
|
|||
|
--> Services
|
|||
|
local Players = game:GetService("Players")
|
|||
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|||
|
local ServerStorage = game:GetService("ServerStorage")
|
|||
|
|
|||
|
--> Variables
|
|||
|
local Character = require(ServerStorage.Base.Character)
|
|||
|
local Utils = require(ReplicatedStorage.Tools.Utils)
|
|||
|
local LevelProxy = require(ServerStorage.Proxy.LevelProxy)
|
|||
|
|
|||
|
--> Json
|
|||
|
local JsonCharacter = require(ReplicatedStorage.Json.Character)
|
|||
|
|
|||
|
--> Dependencies
|
|||
|
local LevelLoop = require(script.LevelLoop)
|
|||
|
local PlayerAI = require(script.PlayerAI)
|
|||
|
|
|||
|
--------------------------------------------------------------------------------
|
|||
|
|
|||
|
local function WaitForCharacter(Player: Player)
|
|||
|
while not Player.Character or not Player.Character.Parent do
|
|||
|
Player.CharacterAdded:Wait()
|
|||
|
end
|
|||
|
return Player.Character
|
|||
|
end
|
|||
|
|
|||
|
--------------------------------------------------------------------------------
|
|||
|
|
|||
|
local PlayerRole = {}
|
|||
|
PlayerRole.__index = Character
|
|||
|
|
|||
|
function PlayerRole.new(Player: Player, CharacterId: number)
|
|||
|
local playerCharacter = WaitForCharacter(Player)
|
|||
|
|
|||
|
-- 获取怪物数据
|
|||
|
local CharacterData = Utils:GetIdDataFromJson(JsonCharacter, CharacterId)
|
|||
|
if not CharacterData then warn("CharacterId Data not found", CharacterId) return end
|
|||
|
|
|||
|
-- 调用父类Character的new方法,初始化通用属性
|
|||
|
local self = Character.new(Player, playerCharacter, CharacterData)
|
|||
|
setmetatable(self, PlayerRole)
|
|||
|
return self
|
|||
|
end
|
|||
|
|
|||
|
function PlayerRole:Died()
|
|||
|
self:ChangeState("Died", true)
|
|||
|
LevelProxy:ChallengeEnd(self.Player, false)
|
|||
|
end
|
|||
|
|
|||
|
function PlayerRole:Respawn()
|
|||
|
self:ChangeState("Died", false)
|
|||
|
self:ChangeAttribute("hp", self.Config.maxhp)
|
|||
|
-- 重置玩家位置
|
|||
|
|
|||
|
end
|
|||
|
|
|||
|
--------------------------------------------------------------------------------
|
|||
|
|
|||
|
function PlayerFightProxy:InitPlayer(Player: Player)
|
|||
|
if not PlayerFightProxy.pData then PlayerFightProxy.pData = {} end
|
|||
|
if not PlayerFightProxy.pData[Player.UserId] then
|
|||
|
PlayerFightProxy.pData[Player.UserId] = {}
|
|||
|
end
|
|||
|
|
|||
|
-- 生成玩家状态
|
|||
|
local PlayerRole = PlayerRole.new(Player, 1)
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerRole = PlayerRole
|
|||
|
|
|||
|
-- 生成关卡循环
|
|||
|
local LevelLoop = LevelLoop.new(Player, PlayerRole)
|
|||
|
PlayerFightProxy.pData[Player.UserId].LevelLoop = LevelLoop
|
|||
|
|
|||
|
-- 生成玩家AI
|
|||
|
local PlayerAI = PlayerAI.new(Player, PlayerRole)
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerAI = PlayerAI
|
|||
|
PlayerAI:AddBehaviour("Move")
|
|||
|
end
|
|||
|
|
|||
|
|
|||
|
-- 重置玩家状态,但是不删除
|
|||
|
function PlayerFightProxy:CleanPlayer(Player: Player)
|
|||
|
-- 玩家角色重生
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerRole:Respawn()
|
|||
|
-- AI重新生成
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerAI:Destroy()
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerAI = nil
|
|||
|
|
|||
|
local PlayerAI = PlayerAI.new(PlayerRole)
|
|||
|
PlayerFightProxy.pData[Player.UserId].PlayerAI = PlayerAI
|
|||
|
end
|
|||
|
|
|||
|
function PlayerFightProxy:OnPlayerRemoving(Player: Player)
|
|||
|
if not PlayerFightProxy.pData[Player.UserId] then warn("PlayerFight Remove Data not found", Player.Name) return end
|
|||
|
PlayerFightProxy.pData[Player.UserId] = nil
|
|||
|
end
|
|||
|
|
|||
|
Players.PlayerRemoving:Connect(function(Player: Player)
|
|||
|
PlayerFightProxy:OnPlayerRemoving(Player)
|
|||
|
end)
|
|||
|
|
|||
|
return PlayerFightProxy
|