Skip to content

Commit

Permalink
Merge pull request #66 from Kylon99/dev/master/#65_Add_variable_lengt…
Browse files Browse the repository at this point in the history
…h_Flail_Handle_for_Staff_Flail

Dev/master/#65 add variable length flail handle for staff flail
  • Loading branch information
Kylon99 authored Aug 14, 2023
2 parents 3c11ffa + bb470de commit 0e1a284
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 29 deletions.
8 changes: 6 additions & 2 deletions AlternativePlay/AssetLoaderBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ public class AssetLoaderBehavior : MonoBehaviour
{
public GameObject TrackerPrefab { get; private set; }
public GameObject SaberPrefab { get; private set; }
public GameObject FlailHandlePrefab { get; private set; }
public GameObject FlailHandleSegmentPrefab { get; private set; }
public GameObject FlailTopCapPrefab { get; private set; }
public GameObject FlailBottomCapPrefab { get; private set; }
public GameObject LinkPrefab { get; private set; }

private void Awake()
{
AssetBundle assetBundle = AssetBundle.LoadFromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("AlternativePlay.Resources.alternativeplaymodels"));
this.TrackerPrefab = assetBundle.LoadAsset<GameObject>("APTracker");
this.SaberPrefab = assetBundle.LoadAsset<GameObject>("APSaber");
this.FlailHandlePrefab = assetBundle.LoadAsset<GameObject>("APFlailHandle");
this.FlailHandleSegmentPrefab = assetBundle.LoadAsset<GameObject>("APFlailHandleSegment");
this.FlailTopCapPrefab = assetBundle.LoadAsset<GameObject>("APFlailTopCap");
this.FlailBottomCapPrefab = assetBundle.LoadAsset<GameObject>("APFlailBottomCap");
this.LinkPrefab = assetBundle.LoadAsset<GameObject>("APLink");
assetBundle.Unload(false);
}
Expand Down
85 changes: 76 additions & 9 deletions AlternativePlay/BeatFlailBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AlternativePlay.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -52,13 +53,20 @@ private void Awake()
// Do nothing if we aren't playing Flail
if (Configuration.Current.PlayMode != PlayMode.BeatFlail) { return; }

// Create the GameObjects used for physics calculations
this.leftPhysicsFlail = this.CreatePhysicsChain("Left", Configuration.Current.LeftFlailLength / 100.0f);
this.rightPhysicsFlail = this.CreatePhysicsChain("Right", Configuration.Current.RightFlailLength / 100.0f);
this.leftLinkMeshes = Utilities.CreateLinkMeshes(this.leftPhysicsFlail.Count, Configuration.Current.LeftFlailLength / 100.0f);
this.leftHandleMesh = GameObject.Instantiate(BehaviorCatalog.instance.AssetLoaderBehavior.FlailHandlePrefab);
this.rightLinkMeshes = Utilities.CreateLinkMeshes(this.rightPhysicsFlail.Count, Configuration.Current.RightFlailLength / 100.0f);
this.rightHandleMesh = GameObject.Instantiate(BehaviorCatalog.instance.AssetLoaderBehavior.FlailHandlePrefab);
// Create the GameObjects for the flails
if (Configuration.Current.LeftFlailMode == BeatFlailMode.Flail)
{
this.leftPhysicsFlail = this.CreatePhysicsChain("Left", Configuration.Current.LeftFlailLength / 100.0f);
this.leftLinkMeshes = Utilities.CreateLinkMeshes(this.leftPhysicsFlail.Count, Configuration.Current.LeftFlailLength / 100.0f);
this.leftHandleMesh = this.CreateFlailHandle("LeftHandle", Configuration.Current.LeftHandleLength / 100.0f);
}

if (Configuration.Current.RightFlailMode == BeatFlailMode.Flail)
{
this.rightPhysicsFlail = this.CreatePhysicsChain("Right", Configuration.Current.RightFlailLength / 100.0f);
this.rightLinkMeshes = Utilities.CreateLinkMeshes(this.rightPhysicsFlail.Count, Configuration.Current.RightFlailLength / 100.0f);
this.rightHandleMesh = this.CreateFlailHandle("RightHandle", Configuration.Current.RightHandleLength / 100.0f);
}
}

private void FixedUpdate()
Expand All @@ -68,7 +76,7 @@ private void FixedUpdate()

float gravity = Configuration.Current.Gravity * -9.81f;

switch(Configuration.Current.LeftFlailMode)
switch (Configuration.Current.LeftFlailMode)
{
default:
case BeatFlailMode.Flail:
Expand Down Expand Up @@ -140,7 +148,8 @@ private void Update()
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;

break;

case BeatFlailMode.Sword:
// Do nothing
Expand Down Expand Up @@ -244,5 +253,63 @@ private IEnumerator DisableSaberMeshes()
BehaviorCatalog.instance.SaberDeviceManager.DisableRightSaberMesh();
}
}

/// <summary>
/// Create a segmented variable length flail handle
/// </summary>
/// <param name="name">The name for the parent handle GameObject</param>
/// <param name="flailTotalLength">The length of the handle in meters</param>
/// <returns>The parent GameObject for the flail handle</returns>
private GameObject CreateFlailHandle(string name, float flailTotalLength)
{
const float flailSegmentLength = 0.10f; // 10 cm or 0.1m
const float topCapLength = 0.063f; // 6.3cm
const float bottomCapLength = 0.01f; // 1 cm

int segmentCount = (int)(flailTotalLength / flailSegmentLength);

var assetLoader = BehaviorCatalog.instance.AssetLoaderBehavior;

// Instantiate One-Off Game Objects
var handle = new GameObject(name);
var topCap = GameObject.Instantiate(assetLoader.FlailTopCapPrefab, Vector3.zero, Quaternion.identity, handle.transform);
var bottomCapPosition = new Vector3(0.0f, 0.0f, (flailSegmentLength * segmentCount * -1.0f) - topCapLength - bottomCapLength);
var bottomCap = GameObject.Instantiate(assetLoader.FlailBottomCapPrefab, bottomCapPosition, Quaternion.identity, handle.transform);

// Instantiate Handle Segments
var segmentList = Enumerable.Range(0, segmentCount).Select(i =>
{
float zPosition = (flailSegmentLength * i * -1.0f) - topCapLength;
var positionRelative = new Vector3(0.0f, 0.0f, zPosition);
var gameObject = GameObject.Instantiate(assetLoader.FlailHandleSegmentPrefab, positionRelative, Quaternion.identity, handle.transform);

return new { GameObject = gameObject, Index = i };

}).ToList();
segmentList.ForEach(segment => this.remapSegmentUV(segment.GameObject, segment.Index));

return handle;
}

/// <summary>
/// Remaps the UV coordinates in the U direction based on the MOD 4 of the index.
/// </summary>
private void remapSegmentUV(GameObject segment, int index)
{
const int segmentsPerTexture = 4;

Mesh mesh = segment.GetComponent<MeshFilter>().mesh;
var uvList = new List<Vector2>();
mesh.GetUVs(0, uvList);

var modifiedUV = uvList.Select(uv =>
{
float correctedX = (uv.x < 0.01f && uv.x > -0.01f) ? 0.0f : 0.25f;
float multipleX = 0.25f * (index % segmentsPerTexture);
return new Vector2(correctedX + multipleX, uv.y);
}).ToList();

mesh.SetUVs(0, modifiedUV);
}
}
}
2 changes: 2 additions & 0 deletions AlternativePlay/Models/ConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public class PlayModeSettings
public BeatFlailMode RightFlailMode { get; set; } = BeatFlailMode.Flail;
public int LeftFlailLength { get; set; } = 80; // in centimetres
public int RightFlailLength { get; set; } = 80; // in centimetres
public int LeftHandleLength { get; set; } = 40; // in centimetres
public int RightHandleLength { get; set; } = 40; // in centimetres
public int MoveNotesBack { get; set; } = 0; // in centimetres

// Gameplay Changes Options
Expand Down
4 changes: 2 additions & 2 deletions AlternativePlay/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.7.7.0")]
[assembly: AssemblyFileVersion("0.7.7.0")]
[assembly: AssemblyVersion("0.8.0.0")]
[assembly: AssemblyFileVersion("0.8.0.0")]
Binary file modified AlternativePlay/Resources/alternativeplaymodels
Binary file not shown.
32 changes: 18 additions & 14 deletions AlternativePlay/UI/BeatFlailView.bsml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@
</horizontal>

<!-- Main View -->
<vertical pref-width="75" pref-height="45" vertical-fit="PreferredSize">
<vertical pref-width="75" pref-height="48" vertical-fit="PreferredSize">
<horizontal>
<horizontal pad-right="1">
<img preserve-aspect="true" pref-width="8" pref-height="8" source="~LeftFlailModeIcon" />
</horizontal>
<dropdown-list-setting text="Left Hand" value="LeftFlailMode" choices="LeftFlailModeList" bind-value="true" apply-on-change="true"
hover-hint="Choose the weapon for the left hand"/>
<dropdown-list-setting text="Left Hand" value="LeftFlailMode" choices="LeftFlailModeList" bind-value="true" apply-on-change="true" hover-hint="Choose the weapon for the left hand"/>
</horizontal>

<horizontal>
<increment-setting text="Left Handle Length" value="LeftHandleLength" bind-value="true" apply-on-change="true" integer-only="true" min="10" max="200" increment="10"
formatter="LengthFormatter" hover-hint="The length of the left flail handle" />
<increment-setting text="Left Chain Length" value="LeftFlailLength" bind-value="true" apply-on-change="true" integer-only="true" min="50" max="150" increment="10"
formatter="LengthFormatter" hover-hint="The distance between the handle and the ball on the left" />

<horizontal pad-top="2">
<horizontal pad-right="1">
<img preserve-aspect="true" pref-width="8" pref-height="8" source="~RightFlailModeIcon" />
</horizontal>
<dropdown-list-setting text="Right Hand" value="RightFlailMode" choices="RightFlailModeList" bind-value="true" apply-on-change="true"
hover-hint="Choose the weapon for the right hand"/>
<dropdown-list-setting text="Right Hand" value="RightFlailMode" choices="RightFlailModeList" bind-value="true" apply-on-change="true" hover-hint="Choose the weapon for the right hand"/>
</horizontal>

<increment-setting text="Left Flail Length" value="LeftFlailLength" bind-value="true" apply-on-change="true" integer-only="true" min="50" max="150" increment="10"
formatter="LengthFormatter" hover-hint="The distance between the handle and the ball on the left" />
<increment-setting text="Right Flail Length" value="RightFlailLength" bind-value="true" apply-on-change="true" integer-only="true" min="50" max="150" increment="10"
formatter="LengthFormatter" hover-hint="The distance between the handle and the ball on the right" />
<increment-setting text="Move Notes Back Distance" value="MoveNotesBack" bind-value="true" apply-on-change="true" integer-only="true" min="0" max="150" increment="10"
formatter="LengthFormatter" hover-hint="Move notes back to account for chain length." />
<increment-setting text="Right Handle Length" value="RightHandleLength" bind-value="true" apply-on-change="true" integer-only="true" min="10" max="200" increment="10"
formatter="LengthFormatter" hover-hint="The length of the right flail handle" />
<increment-setting text="Right Chain Length" value="RightFlailLength" bind-value="true" apply-on-change="true" integer-only="true" min="50" max="150" increment="10"
formatter="LengthFormatter" hover-hint="The distance between the handle and the ball on the right" />


<horizontal pad-top="2">
<increment-setting text="Move Notes Back Distance" value="MoveNotesBack" bind-value="true" apply-on-change="true" integer-only="true" min="0" max="150" increment="10"
formatter="LengthFormatter" hover-hint="Move notes back to account for chain length." />
</horizontal>
<horizontal preferred-width="75" horizontal-fit="PreferredSize" pad-bottom="3">
<slider-setting preferred-width="55" text="Gravity" value="Gravity" bind-value="true" apply-on-change="true" integer-only="false" min="0" max="5" increment="0.1" get-event="RefreshGravity" hover-hint="Amount of G force applied to the Flail (multiple of -9.81 m/s^2)" />
<button pref-width="18" preferred-height="8" text="Reset" pad-left="2" hover-hint='Reset gravity to 3.5' on-click="OnResetGravity"/>
Expand Down
22 changes: 22 additions & 0 deletions AlternativePlay/UI/BeatFlailView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ private int RightFlailLength
}
}

[UIValue(nameof(LeftHandleLength))]
private int LeftHandleLength
{
get => this.settings.LeftHandleLength;
set
{
this.settings.LeftHandleLength = value;
Configuration.instance.SaveConfiguration();
}
}

[UIValue(nameof(RightHandleLength))]
private int RightHandleLength
{
get => this.settings.RightHandleLength;
set
{
this.settings.RightHandleLength = value;
Configuration.instance.SaveConfiguration();
}
}

[UIValue(nameof(Gravity))]
private float Gravity
{
Expand Down
1 change: 1 addition & 0 deletions AlternativePlay/UI/PlayModeSelectTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void UpdatePlayModeSelectList()
this.SelectModeList.data.Clear();
this.SelectModeList.data = list.Cast<object>().ToList();
this.SelectModeList.tableView.ReloadData();
this.SelectModeList.tableView.SelectCellWithIdx(Configuration.instance.ConfigurationData.Selected);
this.SelectModeList.tableView.ScrollToCellWithIdx(Configuration.SelectedIndex, TableView.ScrollPositionType.Center, false);
}

Expand Down
2 changes: 1 addition & 1 deletion AlternativePlay/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"icon": "AlternativePlay.Public.DarthMaulColor.png",
"id": "AlternativePlay",
"name": "AlternativePlay",
"version": "0.7.7",
"version": "0.8.0",
"dependsOn": {
"BeatSaberMarkupLanguage": "^1.6.3",
"BS Utils": "^1.12.0",
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Drop the AlternativePlay.dll file into your Plugins folder under your BeatSaber

## Changelog

### 0.7.7
### 0.8.0
- Added options to change flail handle length
- United support for Beat Saber 1.21 - 1.26 and 1.27 - 1.29.1

### 0.7.6
Expand Down

0 comments on commit 0e1a284

Please sign in to comment.