-
Notifications
You must be signed in to change notification settings - Fork 1
/
OrbitState.cs
54 lines (53 loc) · 2.57 KB
/
OrbitState.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using ThunderRoad;
namespace DaggerBending.States {
public class OrbitState : DaggerState {
const float ORBIT_RADIUS = 0.3f;
const float ORBIT_VERTICAL_RANGE = 1;
const float TARGET_DISTANCE_AHEAD = 0.8f;
public override void Enter(DaggerBehaviour dagger, DaggerController controller) {
base.Enter(dagger, controller);
dagger.transform.parent = null;
if (dagger.joint)
dagger.DeleteJoint();
dagger.item.mainCollisionHandler.RemovePhysicModifier(this);
dagger.IgnoreDaggerCollisions();
dagger.item.IgnoreRagdollCollision(Player.currentCreature.ragdoll);
dagger.item.mainCollisionHandler.SetPhysicModifier(this, 2, 0);
}
public override void Update() {
base.Update();
// TODO: move this to state handler
if (dagger.item.handlers.Any() || dagger.item.isTelekinesisGrabbed || dagger.item.isGripped) {
dagger.Deorbit();
return;
}
dagger.item.mainCollisionHandler.damagers.ForEach(damager => damager.UnPenetrateAll());
var bodyAndHeight = new Vector3(
Player.currentCreature.transform.position.x,
Mathf.Clamp(
dagger.item.transform.position.y + UnityEngine.Random.Range(-ORBIT_VERTICAL_RANGE / 4, ORBIT_VERTICAL_RANGE / 4),
Utils.GetPlayerChest().transform.position.y - ORBIT_VERTICAL_RANGE / 2,
Utils.GetPlayerChest().transform.position.y + ORBIT_VERTICAL_RANGE / 2),
Player.currentCreature.transform.position.z);
var positionOnBody = bodyAndHeight + (dagger.item.transform.position - bodyAndHeight).normalized * ORBIT_RADIUS;
var targetPosition = positionOnBody
+ Vector3.Project(dagger.rb.velocity, Vector3.Cross(Vector3.up, positionOnBody - Utils.GetPlayerChest().transform.position)).normalized
* TARGET_DISTANCE_AHEAD;
dagger.pidController.UpdateVelocity(targetPosition);
dagger.rb.AddForce((positionOnBody - dagger.item.transform.position).normalized * 3);
dagger.item.Throw();
}
public override void Exit() {
base.Exit();
dagger.ResetDaggerCollisions();
dagger.item.ResetRagdollCollision();
dagger.item.mainCollisionHandler.RemovePhysicModifier(this);
}
}
}