forked from tmokmss/com.unity.multiplayer.samples.coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaunchProjectileAction.cs
107 lines (90 loc) · 3.93 KB
/
LaunchProjectileAction.cs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using Unity.BossRoom.Gameplay.GameplayObjects;
using Unity.BossRoom.Gameplay.GameplayObjects.Character;
using Unity.BossRoom.Infrastructure;
using Unity.Netcode;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.Actions
{
/// <summary>
/// Action responsible for creating a projectile object.
/// </summary>
[CreateAssetMenu(menuName = "BossRoom/Actions/Launch Projectile Action")]
public class LaunchProjectileAction : Action
{
private bool m_Launched = false;
public override bool OnStart(ServerCharacter serverCharacter)
{
//snap to face the direction we're firing, and then broadcast the animation, which we do immediately.
serverCharacter.physicsWrapper.Transform.forward = Data.Direction;
serverCharacter.serverAnimationHandler.NetworkAnimator.SetTrigger(Config.Anim);
serverCharacter.clientCharacter.RecvDoActionClientRPC(Data);
return true;
}
public override void Reset()
{
m_Launched = false;
base.Reset();
}
public override bool OnUpdate(ServerCharacter clientCharacter)
{
if (TimeRunning >= Config.ExecTimeSeconds && !m_Launched)
{
LaunchProjectile(clientCharacter);
}
return true;
}
/// <summary>
/// Looks through the ProjectileInfo list and finds the appropriate one to instantiate.
/// For the base class, this is always just the first entry with a valid prefab in it!
/// </summary>
/// <exception cref="System.Exception">thrown if no Projectiles are valid</exception>
protected virtual ProjectileInfo GetProjectileInfo()
{
foreach (var projectileInfo in Config.Projectiles)
{
if (projectileInfo.ProjectilePrefab && projectileInfo.ProjectilePrefab.GetComponent<PhysicsProjectile>())
return projectileInfo;
}
throw new System.Exception($"Action {name} has no usable Projectiles!");
}
/// <summary>
/// Instantiates and configures the arrow. Repeatedly calling this does nothing.
/// </summary>
/// <remarks>
/// This calls GetProjectilePrefab() to find the prefab it should instantiate.
/// </remarks>
protected void LaunchProjectile(ServerCharacter parent)
{
if (!m_Launched)
{
m_Launched = true;
var projectileInfo = GetProjectileInfo();
NetworkObject no = NetworkObjectPool.Singleton.GetNetworkObject(projectileInfo.ProjectilePrefab, projectileInfo.ProjectilePrefab.transform.position, projectileInfo.ProjectilePrefab.transform.rotation);
// point the projectile the same way we're facing
no.transform.forward = parent.physicsWrapper.Transform.forward;
//this way, you just need to "place" the arrow by moving it in the prefab, and that will control
//where it appears next to the player.
no.transform.position = parent.physicsWrapper.Transform.localToWorldMatrix.MultiplyPoint(no.transform.position);
no.GetComponent<PhysicsProjectile>().Initialize(parent.NetworkObjectId, projectileInfo);
no.Spawn(true);
}
}
public override void End(ServerCharacter serverCharacter)
{
//make sure this happens.
LaunchProjectile(serverCharacter);
}
public override void Cancel(ServerCharacter serverCharacter)
{
if (!string.IsNullOrEmpty(Config.Anim2))
{
serverCharacter.serverAnimationHandler.NetworkAnimator.SetTrigger(Config.Anim2);
}
}
public override bool OnUpdateClient(ClientCharacter clientCharacter)
{
return ActionConclusion.Continue;
}
}
}