using BepInEx;
using System;
using UnityEngine;
using UnityEngine.XR;
using Utilla;
using System.ComponentModel;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine.InputSystem.LowLevel;
namespace GorillaPicks
{
///
/// This is your mod's main class.
///
/* This attribute tells Utilla to look for [ModdedGameJoin] and [ModdedGameLeave] */
[ModdedGamemode]
[Description("HauntedModMenu")]
[BepInDependency("org.legoandmars.gorillatag.utilla", "1.5.0")]
[BepInPlugin(PluginInfo.GUID, PluginInfo.Name, PluginInfo.Version)]
public class Plugin : BaseUnityPlugin
{
System.Random random = new System.Random();
bool inRoom;
bool RightPrimary;
bool PrevRightPrimary;
float YRotateSpeed = 1;
bool playerWon;
int timesWon = -1;
int nextDifficultyIncrease = 5;
int minPoints = 1;
int maxPoints = 2;
int pointsNextRound;
public GameObject StaticCenter = null;
public GameObject RotatingCenter = null;
public GameObject Pick = null;
GameObject rightHandPos;
InputDevice rightcontroller = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
void Start()
{
/* A lot of Gorilla Tag systems will not be set up when start is called /*
/* Put code in OnGameInitialized to avoid null references */
Utilla.Events.GameInitialized += OnGameInitialized;
}
List staticPoints = new List();
List isPicked = new List();
void HandleReset()
{
timesWon = -1;
nextDifficultyIncrease = 5;
minPoints = 1;
maxPoints = 2;
YRotateSpeed = 1;
}
void Setup()
{
GameObject cubePlaceholder = GameObject.CreatePrimitive(PrimitiveType.Cube);
cubePlaceholder.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
GameObject spherePlaceholder = GameObject.CreatePrimitive(PrimitiveType.Sphere);
spherePlaceholder.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
StaticCenter = Instantiate(cubePlaceholder);
StaticCenter.transform.SetParent(rightHandPos.transform);
StaticCenter.GetComponent().material.color = Color.red;
StaticCenter.transform.localRotation = Quaternion.identity;
StaticCenter.transform.localPosition = new Vector3(0.2f, 0, 0);
RotatingCenter = Instantiate(cubePlaceholder);
RotatingCenter.transform.SetParent(rightHandPos.transform);
RotatingCenter.transform.localRotation = quaternion.identity;
RotatingCenter.transform.localPosition = new Vector3(0.2f, 0, 0);
Pick = Instantiate(spherePlaceholder);
Pick.transform.SetParent(RotatingCenter.transform);
Pick.transform.localPosition = new Vector3(-5, 0, 0);
for (int i = 0; i < 6; i++)
{
var point = Instantiate(spherePlaceholder);
point.transform.SetParent(StaticCenter.transform);
point.transform.localPosition = new Vector3(i + 1, 0, 0);
point.SetActive(false);
staticPoints.Add(point);
isPicked.Add(true);
}
GameObject.Destroy(cubePlaceholder);
GameObject.Destroy(spherePlaceholder);
StaticCenter.SetActive(false);
RotatingCenter.SetActive(false);
Pick.SetActive(false);
}
void OnEnable()
{
/* Set up your mod here */
/* Code here runs at the start and whenever your mod is enabled*/
Setup();
HandleReset();
HarmonyPatches.ApplyHarmonyPatches();
}
void OnDisable()
{
/* Undo mod setup here */
/* This provides support for toggling mods with ComputerInterface, please implement it :) */
/* Code here runs whenever your mod is disabled (including if it disabled on startup)*/
GameObject.Destroy(Pick);
GameObject.Destroy(RotatingCenter);
GameObject.Destroy(StaticCenter);
foreach (var point in staticPoints) { GameObject.Destroy(point); }
HandleReset();
HarmonyPatches.RemoveHarmonyPatches();
}
void OnGameInitialized(object sender, EventArgs e)
{
/* Code here runs after the game initializes (i.e. GorillaLocomotion.Player.Instance != null) */
rightHandPos = GameObject.Find("Global/Local VRRig/Local Gorilla Player/rig/body/shoulder.R/upper_arm.R/forearm.R/hand.R");
Setup();
}
void Update()
{
/* Code here runs every frame when the mod is enabled */
try
{
if (inRoom)
{
if (RotatingCenter != null && Pick != null && StaticCenter != null)
{
RotatingCenter.SetActive(true);
Pick.SetActive(true);
StaticCenter.SetActive(true);
PrevRightPrimary = RightPrimary;
rightcontroller.TryGetFeatureValue(CommonUsages.primaryButton, out RightPrimary);
RotatingCenter.transform.Rotate(0, YRotateSpeed, 0, Space.Self);
if (RightPrimary && !PrevRightPrimary)
{
playerWon = true;
// for (int i = 0; i < isPicked.Count; i++)
// {
// isPicked[i] = true;
// }
}
for (int i = 0; i < isPicked.Count; i++)
{
if (isPicked[i])
staticPoints[i].GetComponent().material.color = Color.blue;
else
staticPoints[i].GetComponent().material.color = Color.red;
}
//foreach(var point in staticPoints)
//{
// if (Vector3.Distance(point.transform.position, hitMarker.transform.position) < point.transform.localScale / 2)
// {
//
// }
//}
HandleWin();
playerWon = false;
}
}
}
catch (Exception e)
{
Debug.Log("[GorillaPicks]" + e.ToString() + e.StackTrace);
}
}
void HandleWin()
{
//bool allPicked = true;
//foreach (var picked in isPicked)
//{
// if (!picked)
// allPicked = false;
//}
//playerWon = allPicked;
if (playerWon)
{
timesWon++;
Debug.Log($"[GorillaPicks] times won {timesWon}");
if (timesWon == nextDifficultyIncrease)
{
nextDifficultyIncrease += 5;
int minOrMax = random.Next(1, 2);
int picksOrSpeed = random.Next(1, 2);
if (picksOrSpeed == 1)
{
if (minOrMax == 1 && minPoints < 5 && minPoints < maxPoints)
minPoints++;
else if (minOrMax == 2 && maxPoints < 6)
maxPoints++;
}
else if (YRotateSpeed < 1.7f)
{
YRotateSpeed += 0.1f;
}
}
pointsNextRound = random.Next(minPoints, maxPoints);
for (int i = 0; i < staticPoints.Count; i++)
{ // i = 0, 1, 2, 3, 4, 5
if (i < pointsNextRound)
{
staticPoints[i].SetActive(true);
isPicked[i] = false;
}
else
{
staticPoints[i].SetActive(false);
isPicked[i] = true;
}
}
}
}
/* This attribute tells Utilla to call this method when a modded room is joined */
[ModdedGamemodeJoin]
public void OnJoin(string gamemode)
{
/* Activate your mod here */
/* This code will run regardless of if the mod is enabled*/
HandleReset();
inRoom = true;
}
/* This attribute tells Utilla to call this method when a modded room is left */
[ModdedGamemodeLeave]
public void OnLeave(string gamemode)
{
/* Deactivate your mod here */
/* This code will run regardless of if the mod is enabled*/
GameObject.Destroy(Pick);
GameObject.Destroy(RotatingCenter);
GameObject.Destroy(StaticCenter);
foreach (var point in staticPoints) { GameObject.Destroy(point); }
HandleReset();
inRoom = false;
}
}
}