Boss Room 是一款使用 Unity MLAPI 制作的全功能合作多人 RPG。 它旨在作为学习样本,展示类似游戏中经常出现的某些典型游戏模式。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

118 行
3.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BossRoom.Server
{
[RequireComponent(typeof(NetworkCharacterState))]
public class ServerCharacter : MLAPI.NetworkedBehaviour
{
public NetworkCharacterState NetState { get; private set; }
[SerializeField]
[Tooltip("If enabled, this character has an AIBrain and behaves as an enemy")]
public bool IsNPC;
[SerializeField]
[Tooltip("If IsNPC, this is how far the npc can detect others (in meters)")]
public float DetectRange = 10;
private ActionPlayer m_actionPlayer;
private AIBrain m_aiBrain;
/// <summary>
/// Temp place to store all the active characters (to avoid having to
/// perform insanely-expensive GameObject.Find operations during Update)
/// </summary>
private static List<ServerCharacter> g_activeServerCharacters = new List<ServerCharacter>();
private void OnEnable()
{
g_activeServerCharacters.Add(this);
}
private void OnDisable()
{
g_activeServerCharacters.Remove(this);
}
public static List<ServerCharacter> GetAllActiveServerCharacters()
{
return g_activeServerCharacters;
}
// Start is called before the first frame update
void Start()
{
NetState = GetComponent<NetworkCharacterState>();
m_actionPlayer = new ActionPlayer(this);
if (IsNPC)
{
m_aiBrain = new AIBrain(this, m_actionPlayer);
}
}
public override void NetworkStart()
{
if (!IsServer) { this.enabled = false; }
else
{
this.NetState = GetComponent<NetworkCharacterState>();
this.NetState.DoActionEventServer += this.OnActionPlayRequest;
}
}
/// <summary>
/// Play an action!
/// </summary>
/// <param name="data">Contains all data necessary to create the action</param>
public void PlayAction(ref ActionRequestData data )
{
this.m_actionPlayer.PlayAction(ref data);
}
/// <summary>
/// Clear all active Actions.
/// </summary>
public void ClearActions()
{
this.m_actionPlayer.ClearActions();
}
private void OnActionPlayRequest( ActionRequestData data )
{
this.PlayAction(ref data);
}
/// <summary>
/// Receive an HP change from somewhere. Could be healing or damage.
/// </summary>
/// <param name="Inflicter">Person dishing out this damage/healing. Can be null. </param>
/// <param name="HP">The HP to receive. Positive value is healing. Negative is damage. </param>
public void RecieveHP( ServerCharacter inflicter, int HP)
{
//in a more complicated implementation, we might look up all sorts of effects from the inflicter, and compare them
//to our own effects, and modify the damage or healing as appropriate. But in this game, we just take it straight.
NetState.HitPoints.Value += HP;
if( NetState.HitPoints.Value <= 0 )
{
//TODO: handle death state.
GameObject.Destroy(this.gameObject);
}
}
// Update is called once per frame
void Update()
{
m_actionPlayer.Update();
if (m_aiBrain != null)
{
m_aiBrain.Update();
}
}
}
}