Skip to content

Commit

Permalink
Merge pull request #37 from Kylon99/dev/master/#36_Add_One_Flail_opti…
Browse files Browse the repository at this point in the history
…ons_for_Beat_Flail

#36 Added one flail and sword options to Beat Flail
  • Loading branch information
Kylon99 authored Aug 3, 2021
2 parents 6aac3dd + c370466 commit 6699551
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 65 deletions.
177 changes: 122 additions & 55 deletions AlternativePlay/BeatFlailBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AlternativePlay.Models;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
Expand All @@ -12,9 +13,8 @@ public class BeatFlailBehavior : MonoBehaviour
private const float HandleMass = 2.0f;
private const float AngularDrag = 2.0f;
private const int LinkCount = 3;

private bool removeLeftFlail;
private bool removeRightFlail;
private readonly Pose leftHiddenPose = new Pose(new Vector3(-1.0f, -1000.0f, 0.0f), Quaternion.Euler(90.0f, 0.0f, 0.0f));
private readonly Pose rightHiddenPose = new Pose(new Vector3(1.0f, -1000.0f, 0.0f), Quaternion.Euler(90.0f, 0.0f, 0.0f));

private List<GameObject> leftPhysicsFlail;
private List<GameObject> rightPhysicsFlail;
Expand All @@ -35,18 +35,17 @@ public void BeginGameCoreScene()
TrackedDeviceManager.instance.LoadTrackedDevices();

var config = Configuration.instance.ConfigurationData;
this.removeLeftFlail = config.OneColor && !config.UseLeftFlail && config.RemoveOtherSaber;
this.removeRightFlail = config.OneColor && config.UseLeftFlail && config.RemoveOtherSaber;

if (!this.removeLeftFlail)
if (config.LeftFlailMode != BeatFlailMode.None)
{
Utilities.CheckAndDisableForTrackerTransforms(config.LeftFlailTracker);
}

if (!this.removeRightFlail)
if (config.RightFlailMode != BeatFlailMode.None)
{
Utilities.CheckAndDisableForTrackerTransforms(config.RightFlailTracker);
}

this.StartCoroutine(this.DisableSaberMeshes());
}

private void Awake()
Expand All @@ -72,35 +71,52 @@ private void FixedUpdate()
var config = Configuration.instance.ConfigurationData;
float gravity = config.FlailGravity * -9.81f;

if (!this.removeLeftFlail)
switch(config.LeftFlailMode)
{
// Apply gravity to the left handle
foreach (var link in this.leftPhysicsFlail.Skip(1))
{
var rigidBody = link.GetComponent<Rigidbody>();
rigidBody.AddForce(new Vector3(0, gravity, 0) * rigidBody.mass);
}
// Apply motion force from the left controller
var leftFirstLink = this.leftPhysicsFlail.First();
Pose leftSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetLeftSaberPose(config.LeftFlailTracker);
leftFirstLink.transform.position = leftSaberPose.position * 10.0f;
leftFirstLink.transform.rotation = leftSaberPose.rotation * Quaternion.Euler(0.0f, 90.0f, 0.0f);
default:
case BeatFlailMode.Flail:
// Apply gravity to the left handle
foreach (var link in this.leftPhysicsFlail.Skip(1))
{
var rigidBody = link.GetComponent<Rigidbody>();
rigidBody.AddForce(new Vector3(0, gravity, 0) * rigidBody.mass);
}

// Apply motion force from the left controller
var leftFirstLink = this.leftPhysicsFlail.First();
Pose leftSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetLeftSaberPose(config.LeftFlailTracker);
leftFirstLink.transform.position = leftSaberPose.position * 10.0f;
leftFirstLink.transform.rotation = leftSaberPose.rotation * Quaternion.Euler(0.0f, 90.0f, 0.0f);
break;

case BeatFlailMode.Sword:
case BeatFlailMode.None:
// Do nothing
break;
}

if (!this.removeRightFlail)
switch (config.RightFlailMode)
{
// Apply gravity to the right handle
foreach (var link in this.rightPhysicsFlail.Skip(1))
{
var rigidBody = link.GetComponent<Rigidbody>();
rigidBody.AddForce(new Vector3(0, gravity, 0) * rigidBody.mass);
}

// Apply motion force from the right controller
var rightFirstLink = this.rightPhysicsFlail.First();
Pose rightSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetRightSaberPose(config.RightFlailTracker);
rightFirstLink.transform.position = rightSaberPose.position * 10.0f;
rightFirstLink.transform.rotation = rightSaberPose.rotation * Quaternion.Euler(0.0f, 90.0f, 0.0f);
default:
case BeatFlailMode.Flail:
// Apply gravity to the right handle
foreach (var link in this.rightPhysicsFlail.Skip(1))
{
var rigidBody = link.GetComponent<Rigidbody>();
rigidBody.AddForce(new Vector3(0, gravity, 0) * rigidBody.mass);
}

// Apply motion force from the right controller
var rightFirstLink = this.rightPhysicsFlail.First();
Pose rightSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetRightSaberPose(config.RightFlailTracker);
rightFirstLink.transform.position = rightSaberPose.position * 10.0f;
rightFirstLink.transform.rotation = rightSaberPose.rotation * Quaternion.Euler(0.0f, 90.0f, 0.0f);
break;

case BeatFlailMode.Sword:
case BeatFlailMode.None:
// Do nothing
break;
}
}

Expand All @@ -110,32 +126,64 @@ private void Update()
if (Configuration.instance.ConfigurationData.PlayMode != PlayMode.BeatFlail) { return; }

var config = Configuration.instance.ConfigurationData;
if (!this.removeLeftFlail)
switch (config.LeftFlailMode)
{
var lastLeftLink = this.leftPhysicsFlail.Last();
Pose leftLastLinkPose = new Pose(lastLeftLink.transform.position / 10.0f, lastLeftLink.transform.rotation * Quaternion.Euler(0.0f, -90.0f, 180.0f));
BehaviorCatalog.instance.SaberDeviceManager.SetLeftSaberPose(leftLastLinkPose);
Utilities.MoveLinkMeshes(this.leftLinkMeshes, this.leftPhysicsFlail, (float)config.LeftFlailLength / 100f);

Pose leftSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetLeftSaberPose(config.LeftFlailTracker);
float oneChainDistance = config.LeftFlailLength / 100.0f / (leftPhysicsFlail.Count - 1);
Vector3 moveHandleUp = leftSaberPose.rotation * new Vector3(0.0f, 0.0f, oneChainDistance); // Move handle forward one chain length
this.leftHandleMesh.transform.position = leftSaberPose.position + moveHandleUp;
this.leftHandleMesh.transform.rotation = leftSaberPose.rotation;
default:
case BeatFlailMode.Flail:
// Move saber to the last link
var lastLeftLink = this.leftPhysicsFlail.Last();
Pose leftLastLinkPose = new Pose(lastLeftLink.transform.position / 10.0f, lastLeftLink.transform.rotation * Quaternion.Euler(0.0f, -90.0f, 180.0f));
BehaviorCatalog.instance.SaberDeviceManager.SetLeftSaberPose(leftLastLinkPose);

// Move all links into place
Utilities.MoveLinkMeshes(this.leftLinkMeshes, this.leftPhysicsFlail, (float)config.LeftFlailLength / 100f);

// Move handle based on the original saber position
Pose leftSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetLeftSaberPose(config.LeftFlailTracker);
float oneChainDistance = config.LeftFlailLength / 100.0f / (leftPhysicsFlail.Count - 1);
Vector3 moveHandleUp = leftSaberPose.rotation * new Vector3(0.0f, 0.0f, oneChainDistance); // Move handle forward one chain length
this.leftHandleMesh.transform.position = leftSaberPose.position + moveHandleUp;
this.leftHandleMesh.transform.rotation = leftSaberPose.rotation;
break;

case BeatFlailMode.Sword:
// Do nothing
break;

case BeatFlailMode.None:
// Remove the sword
BehaviorCatalog.instance.SaberDeviceManager.SetLeftSaberPose(leftHiddenPose);
break;
}

if (!this.removeRightFlail)
switch (config.RightFlailMode)
{
var lastRightLink = this.rightPhysicsFlail.Last();
Pose rightLastLinkPose = new Pose(lastRightLink.transform.position / 10.0f, lastRightLink.transform.rotation * Quaternion.Euler(0.0f, -90.0f, 180.0f));
BehaviorCatalog.instance.SaberDeviceManager.SetRightSaberPose(rightLastLinkPose);
Utilities.MoveLinkMeshes(this.rightLinkMeshes, this.rightPhysicsFlail, config.RightFlailLength / 100.0f);

Pose rightSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetRightSaberPose(config.RightFlailTracker);
float oneChainDistance = config.RightFlailLength / 100.0f / (rightPhysicsFlail.Count - 1);
Vector3 moveHandleUp = rightSaberPose.rotation * new Vector3(0.0f, 0.0f, oneChainDistance); // Move handle forward one chain length
this.rightHandleMesh.transform.position = rightSaberPose.position + moveHandleUp;
this.rightHandleMesh.transform.rotation = rightSaberPose.rotation;
default:
case BeatFlailMode.Flail:
// Move saber to the last link
var lastRightLink = this.rightPhysicsFlail.Last();
Pose rightLastLinkPose = new Pose(lastRightLink.transform.position / 10.0f, lastRightLink.transform.rotation * Quaternion.Euler(0.0f, -90.0f, 180.0f));
BehaviorCatalog.instance.SaberDeviceManager.SetRightSaberPose(rightLastLinkPose);

// Move all links into place
Utilities.MoveLinkMeshes(this.rightLinkMeshes, this.rightPhysicsFlail, config.RightFlailLength / 100.0f);

// Move handle based on the original saber position
Pose rightSaberPose = BehaviorCatalog.instance.SaberDeviceManager.GetRightSaberPose(config.RightFlailTracker);
float oneChainDistance = config.RightFlailLength / 100.0f / (rightPhysicsFlail.Count - 1);
Vector3 moveHandleUp = rightSaberPose.rotation * new Vector3(0.0f, 0.0f, oneChainDistance); // Move handle forward one chain length
this.rightHandleMesh.transform.position = rightSaberPose.position + moveHandleUp;
this.rightHandleMesh.transform.rotation = rightSaberPose.rotation;
break;

case BeatFlailMode.Sword:
// Do nothing
break;

case BeatFlailMode.None:
// Remove the sword
BehaviorCatalog.instance.SaberDeviceManager.SetRightSaberPose(rightHiddenPose);
break;
}
}

Expand Down Expand Up @@ -182,5 +230,24 @@ private List<GameObject> CreatePhysicsChain(string prefix, float length)
Utilities.ConnectChain(chain, length);
return chain;
}

/// <summary>
/// Disables the rendering of the saber
/// </summary>
private IEnumerator DisableSaberMeshes()
{
yield return new WaitForSecondsRealtime(0.1f);

var config = Configuration.instance.ConfigurationData;
if (config.LeftFlailMode == BeatFlailMode.None)
{
BehaviorCatalog.instance.SaberDeviceManager.DisableLeftSaberMesh();
}

if (config.RightFlailMode == BeatFlailMode.None)
{
BehaviorCatalog.instance.SaberDeviceManager.DisableRightSaberMesh();
}
}
}
}
5 changes: 4 additions & 1 deletion AlternativePlay/HarmonyPatches/OneColorSaberPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ internal class OneColorSaberPatch
{
private static void Postfix(ref bool saberTypeOK)
{
if (Configuration.instance.ConfigurationData.PlayMode == PlayMode.BeatSpear ||
var config = Configuration.instance.ConfigurationData;
if (config.PlayMode == PlayMode.BeatSpear ||
// Allow multicutting if playing a one handed style
(config.PlayMode == PlayMode.BeatFlail && (config.LeftFlailMode == BeatFlailMode.None || config.RightFlailMode == BeatFlailMode.None)) ||
(Configuration.instance.ConfigurationData.OneColor && BS_Utils.Plugin.LevelData.Mode != BS_Utils.Gameplay.Mode.Multiplayer))
{
// Always allow saber hits from any type for OneColor or Spear
Expand Down
10 changes: 9 additions & 1 deletion AlternativePlay/Models/ConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public enum ControllerCountEnum
Two
};

public enum BeatFlailMode
{
Flail,
Sword,
None
};

[Serializable]
public class ConfigurationData
{
Expand Down Expand Up @@ -70,7 +77,8 @@ public class ConfigurationData
public TrackerConfigData RightNunchakuTracker { get; set; } = new TrackerConfigData();

// Flail Options
public bool UseLeftFlail { get; set; }
public BeatFlailMode LeftFlailMode { get; set; } = BeatFlailMode.Flail;
public BeatFlailMode RightFlailMode { get; set; } = BeatFlailMode.Flail;
public int LeftFlailLength { get; set; } = 80; // in centimetres
public int RightFlailLength { get; set; } = 80; // in centimetres
public float FlailGravity { get; set; } = 3.5f;
Expand Down
8 changes: 5 additions & 3 deletions AlternativePlay/UI/BeatFlailView.bsml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
</horizontal>

<!-- Main View -->
<vertical pref-width="75" vertical-fit="PreferredSize">
<checkbox-setting text="Use Left Flail For One Color" value="UseLeftFlail" apply-on-change="true"
on-change="OnUseLeftFlailChanged" hover-hint="Use the left flail when playing One Color" />
<vertical pref-width="75" pref-height="45" vertical-fit="PreferredSize">
<dropdown-list-setting text="Left Hand" value="LeftFlailMode" choices="LeftFlailModeList" apply-on-change="true"
on-change="OnLeftFlailModeListChanged" hover-hint="Choose the weapon for the left hand"/>
<dropdown-list-setting text="Right Hand" value="RightFlailMode" choices="RightFlailModeList" apply-on-change="true"
on-change="OnRightFlailModeListChanged" hover-hint="Choose the weapon for the right hand"/>
<increment-setting text="Left Flail Length" value="LeftFlailLength" integer-only="true" min="50" max="150" increment="10" formatter="LengthFormatter"
on-change="OnLeftFlailLengthChanged" hover-hint="The distance between the handle and the ball on the left" />
<increment-setting text="Right Flail Length" value="RightFlailLength" integer-only="true" min="50" max="150" increment="10" formatter="LengthFormatter"
Expand Down
24 changes: 19 additions & 5 deletions AlternativePlay/UI/BeatFlailView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BeatSaberMarkupLanguage.Parser;
using BeatSaberMarkupLanguage.ViewControllers;
using System;
using System.Collections.Generic;

namespace AlternativePlay.UI
{
Expand All @@ -27,12 +28,25 @@ protected override void DidActivate(bool firstActivation, bool addedToHierarchy,
SetTrackerText();
}

[UIValue("UseLeftFlail")]
private bool useLeftFlail = Configuration.instance.ConfigurationData.UseLeftFlail;
[UIAction("OnUseLeftFlailChanged")]
private void OnUseLeftFlailChanged(bool value)
[UIValue("LeftFlailMode")]
private string LeftFlailMode = Configuration.instance.ConfigurationData.LeftFlailMode.ToString();
[UIValue("LeftFlailModeList")]
private List<object> LeftFlailModeList = new List<object> { BeatFlailMode.Flail.ToString(), BeatFlailMode.Sword.ToString(), BeatFlailMode.None.ToString() };
[UIAction("OnLeftFlailModeListChanged")]
private void OnLeftFlailModeListChanged(string value)
{
Configuration.instance.ConfigurationData.UseLeftFlail = value;
Configuration.instance.ConfigurationData.LeftFlailMode = (BeatFlailMode)Enum.Parse(typeof(BeatFlailMode), value);
Configuration.instance.SaveConfiguration();
}

[UIValue("RightFlailMode")]
private string RightFlailMode = Configuration.instance.ConfigurationData.RightFlailMode.ToString();
[UIValue("RightFlailModeList")]
private List<object> RightFlailModeList = new List<object> { BeatFlailMode.Flail.ToString(), BeatFlailMode.Sword.ToString(), BeatFlailMode.None.ToString() };
[UIAction("OnRightFlailModeListChanged")]
private void OnRightFlailModeListChanged(string value)
{
Configuration.instance.ConfigurationData.RightFlailMode = (BeatFlailMode)Enum.Parse(typeof(BeatFlailMode), value);
Configuration.instance.SaveConfiguration();
}

Expand Down

0 comments on commit 6699551

Please sign in to comment.