-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortalToInstance.cs
More file actions
76 lines (73 loc) · 3.64 KB
/
PortalToInstance.cs
File metadata and controls
76 lines (73 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// a portal that teleports a player into a dungeon instance for his party
using UnityEngine;
namespace uMMORPG
{
[RequireComponent(typeof(Collider))]
public class PortalToInstance : MonoBehaviour
{
[Tooltip("Instance template in the Scene. Don't use a prefab, Mirror can't handle prefabs that contain NetworkIdentity children.")]
public Instance instanceTemplate;
void OnPortal(Player player)
{
// check party again, just to be sure.
if (player.party.InParty())
{
// is there an instance for the player's party yet?
if (instanceTemplate.instances.TryGetValue(player.party.party.partyId, out Instance existingInstance))
{
// teleport player to instance entry
if (player.isServer) player.movement.Warp(existingInstance.entry.position);
Debug.Log("Teleporting " + player.name + " to existing instance=" + existingInstance.name + " with partyId=" + player.party.party.partyId);
}
// otherwise create a new one
else
{
Instance instance = Instance.CreateInstance(instanceTemplate, player.party.party.partyId);
if (instance != null)
{
// teleport player to instance entry
if (player.isServer) player.movement.Warp(instance.entry.position);
Debug.Log("Teleporting " + player.name + " to new instance=" + instance.name + " with partyId=" + player.party.party.partyId);
}
else if (player.isServer) player.chat.TargetMsgInfo("There are already too many " + instanceTemplate.name + " instances. Please try again later.");
}
}
}
void OnTriggerEnter(Collider co)
{
if (instanceTemplate != null)
{
// a player might have a root collider and a hip collider.
// only fire Portal OnTriggerEnter code here ONCE.
// => only for the collider ON the player
// => so we check GetComponent. DO NOT check GetComponentInParent.
Player player = co.GetComponent<Player>();
if (player != null)
{
// only call this for server and for local player. not for other
// players on the client. no need in locally creating their
// instances too.
if (player.isServer || player.isLocalPlayer)
{
// required level?
if (player.level.current >= instanceTemplate.requiredLevel)
{
// can only enter with a party
if (player.party.InParty())
{
// call OnPortal on server and on local client
OnPortal(player);
}
// show info message on client directly. no need to do it via Rpc.
else if (player.isClient)
player.chat.AddMsgInfo("Can't enter instance without a party.");
}
// show info message on client directly. no need to do it via Rpc.
else if (player.isClient)
player.chat.AddMsgInfo("Portal requires level " + instanceTemplate.requiredLevel);
}
}
}
}
}
}