From 49c852a963c887fd1d9482ab3399680b304b4c4f Mon Sep 17 00:00:00 2001 From: Strong Recomme Date: Tue, 3 May 2022 23:30:51 +0700 Subject: [PATCH] 1.2 Component and data. New controller! prerelease! --- HMpatch.cs | 3 +- MenuControll.cs | 13 +-- NETHost.cs | 23 ++-- PACKETS.cs | 49 ++++----- Properties/AssemblyInfo.cs | 2 +- VehComponent.cs | 209 +++++++++++++++++++++++++++++++++++++ VehicleController.cs | 163 ++++------------------------- data.cs | 109 +++---------------- main.cs | 161 +++++++--------------------- vehiclemod.csproj | 1 + 10 files changed, 323 insertions(+), 410 deletions(-) create mode 100644 VehComponent.cs diff --git a/HMpatch.cs b/HMpatch.cs index 092d37e..fe008e6 100644 --- a/HMpatch.cs +++ b/HMpatch.cs @@ -7,8 +7,7 @@ namespace vehiclemod [HarmonyPatch(typeof(WheelCollider), "LoadWheelCollider")] /* https://github.com/werasik2aa/BringBackComponents */ public class WheelCollider : Collider { - public extern WheelCollider(IntPtr Value); - public static Il2CppSystem.Type Il2CppType { get; } + public WheelCollider(IntPtr Value) : base (Value) { } public float radius; public bool isGrounded { get; } public float steerAngle; diff --git a/MenuControll.cs b/MenuControll.cs index e481a6b..2da1303 100644 --- a/MenuControll.cs +++ b/MenuControll.cs @@ -29,7 +29,6 @@ public class MenuControll public static void menuc() { GameObject key1 = GameObject.Instantiate(main.lb[0].LoadAsset("menucars"), Vector3.zero, Quaternion.identity); - //GameManager.m_ActiveSceneIsRegion; key1.transform.Find("Stat").gameObject.SetActive(false); key1.transform.Find("Menu").gameObject.SetActive(false); key1.transform.Find("MenuCar").gameObject.SetActive(false); @@ -76,20 +75,22 @@ private static void ADDCARS() } public static void Update(int i) { + if (!GetObj(main.targetcar)) return; + InfoMain gg = GetObj(main.targetcar).GetComponent().vehicleData; if (i == 0 && CountCars) { CountCars.text = main.vehicles.Count.ToString(); } if (i == 1 && Speed) { - Speed.maxValue = VehicleController.maxspeed; - Speed.value = VehicleController.curspeed; - CountSpeed.text = Mathf.RoundToInt(VehicleController.curspeed).ToString(); + Speed.maxValue = gg.m_MaxSpeed; + Speed.value = gg.m_CurSpeed; + CountSpeed.text = Mathf.RoundToInt(gg.m_CurSpeed).ToString(); } if (i == 2 && Fuel) { - Fuel.maxValue = VehicleController.maxfuel; - Fuel.value = VehicleController.curfuel; + Fuel.maxValue = gg.m_MaxFuel; + Fuel.value = gg.m_CurFuel; } } public static void CarStatScreen(string PlayerName, bool allowsit, bool allowdrive, float fuel) diff --git a/NETHost.cs b/NETHost.cs index 56106c7..aaaa80b 100644 --- a/NETHost.cs +++ b/NETHost.cs @@ -5,14 +5,18 @@ namespace vehiclemod { public static class NETHost { - public static void NetPacketStat(bool allowdrive, bool allowsit, float fuel, string name) + public static void NetPacketStat(int carId, bool allowdrive, bool allowsit, bool isdrive, bool sound, bool light, float fuel, string name) { - if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.NONE) return; + if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.NONE || SkyCoop.API.m_ClientState != SkyCoop.API.SkyCoopClientState.HOST) return; Packet packet = packet = new Packet((int)ClientPackets.CUSTOM); packet.Write(0011); + packet.Write(carId); packet.Write(allowdrive); packet.Write(allowsit); + packet.Write(isdrive); + packet.Write(sound); + packet.Write(light); packet.Write(Mathf.Round(fuel)); packet.Write(name); Send(packet); @@ -73,27 +77,18 @@ public static void NetSpawnCar(string name, Vector3 Position, Quaternion Rotatio Send(packet); } - public static void NetSoundOn(int ID) + public static void NetSound(int ID, bool state) { if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.NONE) return; Packet packet = packet = new Packet((int)ClientPackets.CUSTOM); packet.Write(1100); packet.Write(ID); + packet.Write(state); Send(packet); } - public static void NetSoundOff(int ID) - { - if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.NONE) return; - Packet packet = packet = new Packet((int)ClientPackets.CUSTOM); - - packet.Write(1110); - packet.Write(ID); - - Send(packet); - } - public static void NetLightOn(int ID, bool state) + public static void NetLight(int ID, bool state) { if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.NONE) return; Packet packet = packet = new Packet((int)ClientPackets.CUSTOM); diff --git a/PACKETS.cs b/PACKETS.cs index 26afe1e..ea36ac1 100644 --- a/PACKETS.cs +++ b/PACKETS.cs @@ -1,6 +1,7 @@ using UnityEngine; using GameServer; using SkyCoop; +using static vehiclemod.data; namespace vehiclemod { @@ -19,7 +20,6 @@ static void Postfix(SkyCoop.API __instance, Packet _pak, int from) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); if (CheckEnv(from)) VehicleController.PlayerCarMove(ID, Position, Rotation); } @@ -32,34 +32,24 @@ static void Postfix(SkyCoop.API __instance, Packet _pak, int from) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); if (CheckEnv(from)) main.SpawnCar(from, SkyCoop.MyMod.playersData[from].m_Levelid, name, Position, Rotation); } if (packetid == 0011) // UPDATE CAR DATA { + int carid = _pak.ReadInt(); bool allowdrive = _pak.ReadBool(); bool allowsit = _pak.ReadBool(); + bool isdrive = _pak.ReadBool(); + bool sound = _pak.ReadBool(); + bool light = _pak.ReadBool(); float fuel = _pak.ReadFloat(); - string name = _pak.ReadString(); - - if (from == -1 && SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.CLIENT) - { - from = _pak.ReadInt(); - } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from)) data.UpdateCarData(from, allowdrive, allowsit, fuel, name); - } - if (packetid == 1010) // SEND Driver - { - int ID = _pak.ReadInt(); - bool drived = _pak.ReadBool(); if (from == -1 && SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.CLIENT) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from)) data.UpdateDriver(ID, drived); + string plname = SkyCoop.MyMod.playersData[from].m_Name; + if (CheckEnv(from)) UpdateCarData(carid, allowdrive, allowsit, isdrive, sound, light, fuel, plname); } if (packetid == 1100) // SEND Sound ON { @@ -68,41 +58,38 @@ static void Postfix(SkyCoop.API __instance, Packet _pak, int from) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) VehicleController.EngineSound(ID, 1); + if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) GetObj(ID).GetComponent().UpdateSound(); } - if (packetid == 1110) // SEND Sound OFF + if (packetid == 1111) // PASSANGER { int ID = _pak.ReadInt(); - + int SitID = _pak.ReadInt(); if (from == -1 && SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.CLIENT) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) VehicleController.EngineSound(ID, 0); + if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) UpdatePassanger(ID, SitID, from); } - if (packetid == 1111) // SIT SIT POS + if (packetid == 1101) // Turn LIGHT { int ID = _pak.ReadInt(); - int SitID = _pak.ReadInt(); + bool state = _pak.ReadBool(); if (from == -1 && SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.CLIENT) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) data.UpdatePassanger(ID, SitID, from); + if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) GetObj(ID).GetComponent().UpdateLight(); } - if (packetid == 1101) // Turn LIGHT + if (packetid == 1010) // SEND Driver { int ID = _pak.ReadInt(); - bool state = _pak.ReadBool(); + bool drived = _pak.ReadBool(); + if (from == -1 && SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.CLIENT) { from = _pak.ReadInt(); } - if (API.m_ClientState == API.SkyCoopClientState.HOST) API.SendDataToEveryone(_pak, from, true); - if (CheckEnv(from) && main.vehicles.ContainsKey(ID)) VehicleController.CarLight(ID, state); + if (CheckEnv(from)) UpdateDriver(ID, drived); } } private static bool CheckEnv(int from) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 401012f..2ab779d 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -10,7 +10,7 @@ [assembly: AssemblyCopyright("Copyright © 2022")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: MelonInfo(typeof(vehiclemod.main), "VehicleMod", "1.1.1", "NopirateOnlySteam")] +[assembly: MelonInfo(typeof(vehiclemod.main), "VehicleMod", "1.2", "NopirateOnlySteam")] [assembly: MelonGame("Hinterland", "TheLongDark")] [assembly: ComVisible(false)] [assembly: Guid("b1e85fa5-23d9-48b6-9d96-e9afe7c41d28")] diff --git a/VehComponent.cs b/VehComponent.cs new file mode 100644 index 0000000..924302a --- /dev/null +++ b/VehComponent.cs @@ -0,0 +1,209 @@ +using UnityEngine; +using System; +using BringBackComponents; +using System.Collections.Generic; +using MelonLoader; +using static vehiclemod.data; +namespace vehiclemod +{ + public class InfoMain + { + public Transform m_VehicleMain = null; + public List m_Wheels = new List(); + public List m_Lights = new List(); + public List m_Sits = new List(); + public Dictionary Passangers = new Dictionary(); + public int m_Type = -1; + public int m_OwnerId = -1; + public int m_MaxSpeed = 0; + public int m_MaxFuel = 0; + public string m_OwnerName = "NaN"; + public string m_VehicleName = "NaN"; + public float m_MotorTorque = 0; + public float m_CurFuel = 0; + public float m_CurSpeed = 0; + public bool m_AllowSit = true; + public bool m_AllowDrive = true; + public bool m_isDrive = false; + public bool m_SoundPlay = false; + public bool m_Light = false; + public AudioSource m_audio; + } + public class VehComponent : MonoBehaviour + { + public VehComponent(IntPtr ptr) : base(ptr) { } + private float move = 0, turn = 0; + public InfoMain vehicleData = null; + public Rigidbody m_Rigidbody; + public bool enableds = false; + public void NEWDATA(int PlayerId, string name, string PlayerName) + { + vehicleData = new InfoMain(); + vehicleData.m_VehicleMain = transform; + vehicleData.m_audio = transform.GetComponent(); + m_Rigidbody = transform.GetComponent(); + m_Rigidbody.mass = float.Parse(GetInfo(name, "Weight")); + vehicleData.m_Type = int.Parse(GetInfo(name, "Type")); + vehicleData.m_MotorTorque = float.Parse(GetInfo(name, "MotorTorque")); + vehicleData.m_MaxFuel = int.Parse(GetInfo(name, "MaxFuel")); + vehicleData.m_MaxSpeed = int.Parse(GetInfo(name, "MaxSpeed")); + vehicleData.m_VehicleName = name; + vehicleData.m_OwnerId = PlayerId; + vehicleData.m_OwnerName = PlayerName; + + foreach (Transform g in transform.GetComponentsInChildren()) + { + if (g.name.StartsWith("Wheel")) + { + vehicleData.m_Wheels.Add(g); + g.gameObject.layer = LayerMask.NameToLayer("Player"); + } + if (g.name.StartsWith("Light")) + { + if (!g.GetComponent()) + MelonLogger.Msg("[" + g.name + "] This GameObject doesn't have Light component!"); + else + vehicleData.m_Lights.Add(g.GetComponent()); + } + if (g.name.StartsWith("Sit")) vehicleData.m_Sits.Add(g); + } + if (vehicleData.m_Type == 0) + { + JointSpring aaa = new JointSpring(); + aaa.spring = float.Parse(GetInfo(name, "SpringForce")); + aaa.damper = float.Parse(GetInfo(name, "DamperForce")); + + foreach (Transform g in vehicleData.m_Wheels) + { + WheelComponent.AddComponent(g); + WheelComponent.Set_JointSpring(g, aaa); + WheelComponent.Set_Mass(g, m_Rigidbody.mass/2); + } + } + enableds = true; + } + public void LateUpdate() + { + if (vehicleData == null || !enableds || !m_Rigidbody) return; + vehicleData.m_CurSpeed = m_Rigidbody.velocity.magnitude; + if (CountPassanger() > 0) foreach (int i in vehicleData.Passangers.Keys) SkyCoop.MyMod.players[i].SetActive(false); + EngineSoundAndLight(); + } + public void FixedUpdate() + { + if (vehicleData.m_Type == 0) + { + foreach (var wheelpost in vehicleData.m_Wheels) + { + if (!wheelpost || !wheelpost.GetChild(0)) return; + Transform Wheel_BodyTarget = wheelpost.GetChild(0); + WheelComponent.Get_WorldPose(wheelpost, out Vector3 pos, out Quaternion rot); + Wheel_BodyTarget.position = pos; + Wheel_BodyTarget.rotation = rot; + } + } + } + public void Update() + { + move = Input.GetAxis("Vertical"); + turn = Input.GetAxis("Horizontal"); + + if (vehicleData == null || !enableds || !m_Rigidbody) return; + if (vehicleData.m_Type == 0) + { + float MotorTorque = 1000 * Time.fixedDeltaTime; + if (m_Rigidbody.velocity.magnitude <= 0.05) MotorTorque = 1000 * Time.fixedDeltaTime * vehicleData.m_CurSpeed; + + for (int i = 0; i != vehicleData.m_Wheels.Count; i++) + { + if (move != 0 && !Input.GetKey(KeyCode.Space)) + { + WheelComponent.Set_BrakeTorque(vehicleData.m_Wheels[i], 0); + WheelComponent.Set_MotorTorque(vehicleData.m_Wheels[i], MotorTorque * move); + } + else + { + WheelComponent.Set_MotorTorque(vehicleData.m_Wheels[i], 0); + WheelComponent.Set_BrakeTorque(vehicleData.m_Wheels[i], MotorTorque * 3); + } + if (vehicleData.m_Wheels[i].name.StartsWith("WheelMain")) WheelComponent.Set_SteerAngle(vehicleData.m_Wheels[i], Mathf.Clamp(vehicleData.m_CurSpeed * turn + turn * 10, -45, 45)); + } + } + } + private void EngineSoundAndLight() // SOUNDS FOR VEHICLE + { + if (!vehicleData.m_audio) return; + + if (!vehicleData.m_SoundPlay && vehicleData.m_audio.isPlaying) vehicleData.m_audio.Stop(); + if (!vehicleData.m_audio.isPlaying && vehicleData.m_SoundPlay) vehicleData.m_audio.Play(); + foreach (var i in vehicleData.m_Lights) i.enabled = vehicleData.m_Light; + + + if (vehicleData.m_CurSpeed == 0) vehicleData.m_CurSpeed = 1; + vehicleData.m_audio.volume = calkrange(); + vehicleData.m_audio.pitch = (0.30f + vehicleData.m_CurSpeed * 0.025f); + if (vehicleData.m_CurSpeed > 30) vehicleData.m_audio.pitch = (0.25f + vehicleData.m_CurSpeed * 0.015f); + if (vehicleData.m_CurSpeed > 40) vehicleData.m_audio.pitch = (0.20f + vehicleData.m_CurSpeed * 0.013f); + if (vehicleData.m_CurSpeed > 49) vehicleData.m_audio.pitch = (0.15f + vehicleData.m_CurSpeed * 0.011f); + if (vehicleData.m_audio.pitch > 2.0f) vehicleData.m_audio.pitch = 2.0f; + } + private float calkrange() //CALCULATE VOLUME DEPENDS ON RANGE -> P-C + { + float i = Vector3.Distance(transform.position, main.MyPosition.position); + i = i / 100; + i = 1 - i; + Mathf.Clamp(i, 0, 1); + return i; + } + public void AddPassanger(int from, int where) + { + GameObject newob = GameObject.Instantiate(SkyCoop.MyMod.players[from]); + Transform sit = vehicleData.m_Sits[where]; + + newob.transform.SetParent(sit); + newob.transform.position = sit.position; + newob.transform.rotation.SetLookRotation(sit.forward, sit.up); + newob.SetActive(true); + newob.name = from.ToString(); + + vehicleData.Passangers.Add(from, where); + } + public void DeletePassanger(int from) + { + vehicleData.Passangers.TryGetValue(from, out int sit); + GameObject.Destroy(vehicleData.m_Sits[sit].GetChild(0).gameObject); + vehicleData.Passangers.Remove(from); + } + public void UpdateDriver(bool state) + { + vehicleData.m_isDrive = state; + } + public bool isDrive() + { + return vehicleData.m_isDrive & vehicleData.m_AllowDrive; + } + public int CountPassanger() + { + return vehicleData.Passangers.Count; + } + public void UpdateMainCarData(int PlayerId, bool allowdrive, bool allowsit, bool isDrive, bool sound, bool light, float fuel, string playername) + { + vehicleData.m_OwnerId = PlayerId; + vehicleData.m_OwnerName = playername; + vehicleData.m_AllowDrive = allowdrive; + vehicleData.m_AllowSit = allowsit; + vehicleData.m_CurFuel = fuel; + vehicleData.m_isDrive = isDrive; + vehicleData.m_SoundPlay = sound; + vehicleData.m_Light = light; + } + public void UpdateLight() + { + vehicleData.m_Light = !vehicleData.m_Light; + } + public void UpdateSound() + { + vehicleData.m_SoundPlay = !vehicleData.m_SoundPlay; + } + } +} diff --git a/VehicleController.cs b/VehicleController.cs index 15af5b3..57d7a9f 100644 --- a/VehicleController.cs +++ b/VehicleController.cs @@ -8,44 +8,32 @@ namespace vehiclemod public class VehicleController { //MISC INTERNAL - private static Transform[] wheels = new Transform[4]; - private static AudioSource audio; public static Transform myparent; //CAMERA PARTS public static Transform cameracenter = null; public static Transform cameracar = null; public static bool fps; public static int siter; - - //CAR PARAMS - public static float maxspeed = 30; - public static float curspeed = 0; - public static float prevspeed = 0; - public static float curfuel = 50; - public static float maxfuel = 100; //INPUT MOVE private static float curX = 0f; private static float curY = 0f; public static float move = 0f; public static float turn = 0f; - public static float MotorTorque = 20f; public static void MoveDrive(int number) { GameObject car = GetObj(number); if (!car) return; - NETHost.NETSIT(number, siter); CameraFollow(number);//FOLLOW CAMERA curX += Input.GetAxis("Mouse X") * 4f; curY += Input.GetAxis("Mouse Y") * 4f; - curspeed = car.GetComponent().velocity.magnitude; if (main.allowdrive && !main.isChat) { float dist = Vector3.Distance(GetObj(number).transform.position, GameManager.GetVpFPSPlayer().transform.position); if (GetObj(number) && !InterfaceManager.m_Panel_Loading.IsLoading() && dist > 2) { - Transform sit = GetObj(number).transform.Find("SIT" + siter); + Transform sit = GetObj(number).transform.Find("SITS/").GetChild(siter - 1); GetObj(number).transform.position = GameManager.GetVpFPSPlayer().transform.position + GameManager.GetVpFPSPlayer().transform.up * 5f; foreach (Collider col in GameManager.GetPlayerTransform().GetComponents()) { @@ -58,69 +46,21 @@ public static void MoveDrive(int number) cameracar.gameObject.AddComponent().CopyFrom(GameManager.GetVpFPSCamera().m_Camera.GetComponent()); cameracar.gameObject.SetActive(true); } - - MotorTorque = 1000 * Time.fixedDeltaTime; - if (curspeed <= 0.05) MotorTorque = 1000 * Time.fixedDeltaTime * curspeed; - - for (int i = 0; i != wheels.Length; i++) - { - if (move != 0 && !Input.GetKey(KeyCode.Space)) - { - WheelComponent.Set_BrakeTorque(wheels[i], 0); - WheelComponent.Set_MotorTorque(wheels[i], MotorTorque * move); - } else { - WheelComponent.Set_MotorTorque(wheels[i], 0); - WheelComponent.Set_BrakeTorque(wheels[i], MotorTorque * 3); - } - } - - WheelComponent.Set_SteerAngle(wheels[0], Mathf.Clamp(curspeed * turn + turn * 10, -45, 45)); - WheelComponent.Set_SteerAngle(wheels[1], Mathf.Clamp(curspeed * turn + turn * 10, -45, 45)); - - UpdateDriver(number, true); - NETHost.NetSendDriver(number, true); - NETHost.NetSoundOn(number); NETHost.NetCar(number, car.transform.position, car.transform.rotation); } if (!main.allowdrive) { - Transform sit = GetObj(number).transform.Find("SIT" + siter); + Transform sit = GetObj(number).transform.Find("SITS").GetChild(siter-1); GameManager.GetVpFPSPlayer().transform.parent = sit; GameManager.GetVpFPSPlayer().transform.position = sit.position; } - NETHost.NETSIT(number, siter); - EngineSound(number, 1); - } - public static void wheel(GameObject car) - { - Transform[] wheels = new Transform[4]; - wheels[0] = car.transform.Find("FR"); - wheels[1] = car.transform.Find("FL"); - wheels[2] = car.transform.Find("BL"); - wheels[3] = car.transform.Find("BR"); - - foreach (var wheelpost in wheels) - { - Transform Wheel_BodyTarget = wheelpost.GetChild(0); - if (!Wheel_BodyTarget) return; - WheelComponent.Get_WorldPose(wheelpost, out Vector3 pos, out Quaternion rot); - Wheel_BodyTarget.position = pos; - Wheel_BodyTarget.rotation = rot; - } } public static void PlayerCarMove(int CarID, Vector3 Position, Quaternion Rotation) { - Transform car = GetObj(CarID).transform; - if (!car || !main.vehicles.ContainsKey(CarID)) - main.SpawnCar(CarID, main.levelid, CarData(CarID)[3], Position, Rotation); - else - { - car.transform.rotation = Quaternion.Lerp(car.transform.rotation, Rotation, 2f); - if (Vector3.Distance(car.transform.position, Position) > 10) - car.transform.position = Position; - else - car.transform.position = Vector3.Lerp(car.transform.position, Position, 15); - } + GameObject car = GetObj(CarID); + car.transform.rotation = Quaternion.Lerp(car.transform.rotation, Rotation, 2f); + car.transform.position = Vector3.Lerp(car.transform.position, Position, 15); + if (Vector3.Distance(car.transform.position, Position) > 10) car.transform.position = Position; } public static void SitCar(int number) { @@ -128,23 +68,14 @@ public static void SitCar(int number) siter = CountPassangers(number) + 1; if (main.allowdrive) siter = 1; if (main.isSit) siter = 0; - - UpdatePassanger(number, siter, main.MyId); - NETHost.NETSIT(number, siter); // PARENT THE LOCAL 3D MODEL TO VEHICLE OR unparent it if (main.isSit) { if (main.allowdrive) { - data.UpdateDriver(number, false); + UpdateDriver(number, false); NETHost.NetSendDriver(number, false); - foreach(var i in wheels) - { - WheelComponent.Set_MotorTorque(i, 0); - WheelComponent.Set_BrakeTorque(i, 2000); - } } - GameManager.GetPlayerManagerComponent().TeleportPlayer(main.MyPosition.position - car.transform.right * 2.5f + car.transform.up, Quaternion.identity); GameManager.GetPlayerManagerComponent().StickPlayerToGround(); foreach (Collider col in GameManager.GetPlayerTransform().GetComponents()) col.enabled = true; @@ -153,31 +84,20 @@ public static void SitCar(int number) cameracar.SetParent(null); main.targetcar = -1; MenuControll.Open(1); - if (CountPassangers(number) <= 1) - { - EngineSound(number, 0); - NETHost.NetSoundOff(number); - } + if (CountPassangers(number) == 0 || !isDrive(number)) NETHost.NetSound(number, false); main.isSit = false; GameManager.GetVpFPSPlayer().transform.SetParent(myparent); - NETHost.NETSIT(number, 0); + GetObj(number).GetComponent().UpdateSound(); } else { - Transform sit = car.transform.Find("SIT" + siter); - if (!sit) return; - if (main.allowdrive) - { - data.UpdateDriver(number, true); + if (main.allowdrive) { + UpdateDriver(number, true); NETHost.NetSendDriver(number, true); } + Transform sit = car.transform.Find("SITS").GetChild(siter); + if (!sit) return; foreach (Collider col in GameManager.GetPlayerTransform().GetComponents()) col.enabled = false; - - wheels[0] = car.transform.Find("FR"); - wheels[1] = car.transform.Find("FL"); - wheels[2] = car.transform.Find("BL"); - wheels[3] = car.transform.Find("BR"); - cameracar.gameObject.SetActive(true); cameracenter = car.transform.Find("CAMERACENTER"); cameracar.transform.position = cameracenter.position; @@ -185,47 +105,27 @@ public static void SitCar(int number) MenuControll.Open(11); GameManager.GetVpFPSPlayer().transform.SetParent(sit); GameManager.GetVpFPSPlayer().transform.position = sit.position; + NETHost.NetSound(number, true); + if (!GetObj(number).GetComponent().vehicleData.m_SoundPlay) GetObj(number).GetComponent().UpdateSound(); } + UpdatePassanger(number, siter, main.MyId); + NETHost.NETSIT(number, siter); + MelonLogger.Msg("================================="); MelonLogger.Msg("[Sit Manager]: ID | LOCALL | SERVER | COUNTPASSANGER"); MelonLogger.Msg("[Sit Manager]: " + number + " | " + main.allowdrive + " | " + isDrive(number) + " | " + CountPassangers(number) + " | " + siter); MelonLogger.Msg("================================="); } - public static void EngineSound(int ID, int state) // SOUNDS FOR VEHICLE - { - if (!GetObj(ID)) return; - if (curspeed == 0) curspeed = 1; - - audio = GetObj(ID).GetComponent(); - - if (state == 0) - { - audio.Stop(); - return; - } - - if (!audio.isPlaying) audio.Play(); - if (audio) - { - var dist = Vector3.Distance(GetObj(ID).transform.position, main.MyPosition.position); - audio.volume = calkrange(dist); - audio.pitch = (0.30f + curspeed * 0.025f); - if (curspeed > 30) audio.pitch = (0.25f + curspeed * 0.015f); - if (curspeed > 40) audio.pitch = (0.20f + curspeed * 0.013f); - if (curspeed > 49) audio.pitch = (0.15f + curspeed * 0.011f); - if (audio.pitch > 2.0f) audio.pitch = 2.0f; - - } - } private static void CameraFollow(int CarId) { // CAMERA CONTROLLER FOR VEHICLE - main.vehicles.TryGetValue(CarId, out GameObject car); + GameObject car = GetObj(CarId); + Transform sit = car.transform.Find("SITS/" + siter); if (!fps) { - GameObject player = GetObj(CarId).transform.Find("SIT" + siter + "/" + main.MyId).gameObject; + GameObject player = GetObj(CarId).transform.Find("SITS").GetChild(siter).GetChild(0).gameObject; if (player && !player.active) player.SetActive(true); if (main.allowdrive) @@ -243,31 +143,14 @@ private static void CameraFollow(int CarId) } else { - GameObject player = GetObj(CarId).transform.Find("SIT" + siter + "/" + main.MyId).gameObject; + GameObject player = sit.GetChild(0).gameObject; if (player && player.active) player.SetActive(false); cameracar.transform.SetParent(car.transform); curY = Mathf.Clamp(curY, -60, 90); curX = Mathf.Clamp(curX, -180, 180); cameracar.transform.localRotation = Quaternion.Euler(-curY, curX, 0); - if (car.transform.Find("SIT" + siter)) cameracar.transform.position = car.transform.Find("SIT" + siter).position + car.transform.up * 1.7f; - } - } - public static void CarLight(int CarId, bool state) // Spot light on car - { - GameObject obj = GetObj(CarId); - if (!obj) return; - - GameObject LeftTorch = obj.transform.Find("LF").gameObject; - GameObject RightTorch = obj.transform.Find("RF").gameObject; - if (LeftTorch) - { - LeftTorch.GetComponent().enabled = state; - } - if (RightTorch) - { - RightTorch.GetComponent().enabled = state; + cameracar.transform.position = sit.position + car.transform.up * 1.7f; } - main.light = state; } } } \ No newline at end of file diff --git a/data.cs b/data.cs index f2dacab..15b8f76 100644 --- a/data.cs +++ b/data.cs @@ -1,5 +1,4 @@ using Il2CppSystem.Reflection; -using MelonLoader; using System; using System.Collections.Generic; using UnityEngine; @@ -17,105 +16,34 @@ public static GameObject LoadObject(string name) } return i; } - public static String[] CarData(int ID) + public static void UpdateCarData(int carid, bool allowdrive, bool allowsit, bool isDrive, bool sound, bool light, float fuel, string playername) { - if (main.vehicledata.TryGetValue(ID, out String[] ss)) - return ss; - else - return null; + GetObj(carid).GetComponent().UpdateMainCarData(carid, allowdrive, allowsit, isDrive, sound, light, fuel, playername); } - public static void UpdateCarData(int PlayerId, bool allowdrive, bool allowsit, float fuel, string name) + public static void UpdateDriver(int CarID, bool state) { - string[] newvals = new string[4]; - newvals[0] = allowdrive.ToString(); - newvals[1] = allowsit.ToString(); - newvals[2] = fuel.ToString(); - newvals[3] = name; - - if (main.vehicledata.TryGetValue(PlayerId, out string[] ss)) - { - if (bool.Parse(ss[0]) != allowdrive || bool.Parse(ss[1]) != allowsit || int.Parse(ss[2]) != fuel || ss[3] != name) - main.vehicledata[PlayerId] = newvals; - } - else - main.vehicledata.Add(PlayerId, newvals); - + GetObj(CarID).GetComponent().UpdateDriver(state); } - public static void UpdateDriver(int ID, bool state) + public static bool isDrive(int ID) { - if (main.drivers.TryGetValue(ID, out bool b)) - main.drivers[ID] = state; - else - main.drivers.Add(ID, state); - } - public static Boolean isDrive(int ID) - { - if (main.drivers.TryGetValue(ID, out bool i)) - return i; - else - return false; + return GetObj(ID).GetComponent().isDrive(); } public static GameObject GetObj(int ID) { - if (main.vehicles.Count == 0) return null; - if (main.vehicles.TryGetValue(ID, out GameObject g)) - return g; - else - return null; - } - public static void Hide() - { - foreach (var i in main.passanger) - { - if (SkyCoop.MyMod.players[i.Key]) SkyCoop.MyMod.players[i.Key].SetActive(false); - } + if (main.vehicles.Count <= 0) return null; + main.vehicles.TryGetValue(ID, out GameObject g); + return g; } - public static int CountPassangers(int ID) + public static int CountPassangers(int CarID) { - int i = 0; - foreach (var j in main.passanger) - { - if (j.Value[0] == ID) - { - i = i + 1; - } - } - return i; + return GetObj(CarID).GetComponent().CountPassanger(); } public static void UpdatePassanger(int CarID, int Number, int from) { - int[] i = new int[2]; - i[0] = CarID; - i[1] = Number; - - if (!GetObj(CarID)) return; - if (Number > 0) - { - if (!main.passanger.ContainsKey(from)) - { - main.passanger.Add(from, i); - GameObject newob = GameObject.Instantiate(SkyCoop.MyMod.players[from]); - Transform sit = GetObj(CarID).transform.Find("SIT" + Number); - if (!sit || !newob) return; - newob.transform.SetParent(sit); - newob.transform.position = sit.position; - newob.transform.LookAt(sit.transform.forward * 2f); - newob.SetActive(true); - newob.name = from.ToString(); - } - } + if (Number == 0) + GetObj(CarID).GetComponent().DeletePassanger(from); else - { - if (main.passanger.ContainsKey(from)) - { - main.passanger.TryGetValue(from, out int[] data); - GameObject player = GetObj(data[0]).transform.Find("SIT" + data[1] + "/" + from).gameObject; - if (!player) return; - GameObject.Destroy(player); - SkyCoop.MyMod.players[from].SetActive(true); - main.passanger.Remove(from); - } - } + GetObj(CarID).GetComponent().AddPassanger(from, Number); } public static Component CopyComponent(Component which, GameObject to) { @@ -129,19 +57,12 @@ public static Component CopyComponent(Component which, GameObject to) } return added; } - public static float calkrange(float i) //CALCULATE VOLUME DEPENDS ON RANGE -> P-C - { - i = i / 100; - i = 1 - i; - Mathf.Clamp(i, 0, 1); - return i; - } public static String GetInfo(string addon, string what) { MenuControll.addonData.TryGetValue(addon, out KeyValuePair[] keyvals); foreach (var ff in keyvals) { - if (what == ff.Key) return ff.Value; + if (what == ff.Key) { return ff.Value; }; } return "NaN"; } diff --git a/main.cs b/main.cs index 1e0321d..3baea34 100644 --- a/main.cs +++ b/main.cs @@ -6,7 +6,6 @@ using System.IO; using static vehiclemod.data; using UnityEngine.Rendering.PostProcessing; -using BringBackComponents; using UnhollowerRuntimeLib; namespace vehiclemod @@ -17,15 +16,9 @@ public class main : MelonMod public static List lb = new List(); private Ray ray; private RaycastHit hit; - public static bool light; //VEHICLE PART public static Dictionary vehicles = new Dictionary(); - public static Dictionary vehicledata = new Dictionary(); - public static Dictionary drivers = new Dictionary(); - public static Dictionary passanger = new Dictionary(); - public static Dictionary PlayerCar = new Dictionary(); - public static Transform MyPosition = null; public static int targetcar = 0; @@ -33,24 +26,23 @@ public class main : MelonMod public static int levelid = 0; public static string levelname = ""; public static int MyId = 0; - private static string MyNick; //CHECK PART FOR VEHICLE CONTROLLER public static bool isSit = false; public static bool allowdrive = false; public static bool changedDrivePlace = false; public static bool isChat = false; + private static string MyNick; public override void OnApplicationStart() { DirectoryInfo dir = new DirectoryInfo("Mods/vehiclemod"); DirectoryInfo[] vehicles = dir.GetDirectories("*"); AssetBundle load = AssetBundle.LoadFromFile("Mods/vehiclemod\\menu.addon"); ClassInjector.RegisterTypeInIl2Cpp(); + ClassInjector.RegisterTypeInIl2Cpp(); if (load) lb.Add(load); - load = null; foreach (DirectoryInfo i in vehicles) { - int number = 0; string PATH = i.FullName + "\\"; using (StreamReader sr = new StreamReader(PATH + "info.ini")) @@ -80,23 +72,19 @@ public override void OnApplicationStart() else MelonLogger.Msg("[PREINIT] [" + GetInfo(i.Name, "FileName") + "] File corrupted or not supported. Or maybe not found"); } - MelonLogger.Msg("[PREINIT] Vehicle mod loaded and vehicle files too"); } public override void OnSceneWasLoaded(int level, string name) { levelid = level; levelname = name; + MyNick = SkyCoop.MyMod.MyChatName; if (levelname != "Empty" && levelname != "MainMenu" && levelname != "Boot") { MelonLogger.Msg("[InitCanvas] Spawning Menu on Level:> " + levelname); MenuControll.menuc(); - Transform newcam = GameObject.Instantiate(new GameObject("00100"), Vector3.zero, Quaternion.identity).transform; - newcam.gameObject.SetActive(false); - VehicleController.cameracar = newcam; } if (GameManager.GetVpFPSPlayer()) VehicleController.myparent = GameManager.GetVpFPSPlayer().transform.parent; - passanger.Clear(); } public override void OnUpdate() { @@ -105,10 +93,17 @@ public override void OnUpdate() GameManager.GetVpFPSPlayer().transform.SetParent(VehicleController.myparent); } if (levelname == "Empty" || levelname == "MainMenu" || levelname == "Boot" || levelname == "" || !GameManager.GetPlayerTransform()) return;//check - VehicleController.turn = Input.GetAxis("Horizontal"); - VehicleController.move = Input.GetAxis("Vertical"); + if (!VehicleController.cameracar) + { + VehicleController.cameracar = GameObject.Instantiate(new GameObject("00100"), Vector3.zero, Quaternion.identity).transform; + VehicleController.cameracar.gameObject.SetActive(false); + VehicleController.cameracar.gameObject.AddComponent().CopyFrom(GameManager.GetVpFPSCamera().m_Camera.GetComponent()); + CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); + CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); + CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); + VehicleController.cameracar.gameObject.GetComponent().volumeTrigger = VehicleController.cameracar; + } MyId = API.m_MyClientID; - MyNick = SkyCoop.MyMod.MyChatName; ray = GameManager.GetVpFPSCamera().m_Camera.ScreenPointToRay(Input.mousePosition); MyPosition = GameManager.GetVpFPSPlayer().transform; isChat = SkyCoop.MyMod.chatInput.IsActive(); @@ -117,81 +112,44 @@ public override void OnUpdate() { if (isSit && allowdrive) { - bool cur = true; - if (light) - cur = false; - else - cur = true; - - VehicleController.CarLight(targetcar, cur); - NETHost.NetLightOn(targetcar, cur); + GetObj(targetcar).GetComponent().vehicleData.m_Light = !GetObj(targetcar).GetComponent().vehicleData.m_Light; } } - Hide(); - // fuck + // SIT EXECUTE if (Input.GetKeyDown(KeyCode.E) && !isChat && !InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled) { - int number = -1; if (!isSit && Physics.Raycast(ray, out hit, 3f)) { - if (!VehicleController.cameracar.GetComponent()) - { - VehicleController.cameracar.gameObject.AddComponent().CopyFrom(GameManager.GetVpFPSCamera().m_Camera.GetComponent()); - CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); - CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); - CopyComponent(GameManager.GetVpFPSCamera().m_Camera.GetComponent(), VehicleController.cameracar.gameObject); - VehicleController.cameracar.gameObject.GetComponent().volumeTrigger = VehicleController.cameracar; - } - GameObject car = hit.collider.gameObject; - if (car.name == "BAGAGE") - { - loot(car); - MelonLogger.Msg("[Interact] Bagage> " + car.name); - return; - } - try { number = int.Parse(car.name); } catch { return; }; - if (!vehicles.ContainsKey(number)) return; - + if (!car.GetComponent()) return; + int number = car.GetComponent().vehicleData.m_OwnerId; allowdrive = !isDrive(number); - targetcar = number; } if (targetcar > -1) VehicleController.SitCar(targetcar); } - - if (Input.GetKeyDown(KeyCode.Mouse2)) - if (VehicleController.cameracar && VehicleController.fps == false) - VehicleController.fps = true; - else - VehicleController.fps = false; - - if (InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled) - { - MenuControll.openmenu.gameObject.SetActive(true); - } - else - { - MenuControll.openmenu.gameObject.SetActive(false); - if (MenuControll.MenuMainCars) MenuControll.MenuMainCars.gameObject.SetActive(false); - } + if (Input.GetKeyDown(KeyCode.Mouse2)) VehicleController.fps = !VehicleController.fps; + MenuControll.openmenu.gameObject.active = InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled; + if (!InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled) MenuControll.MenuMainCars.gameObject.SetActive(false); } public override void OnFixedUpdate() { if (levelname == "Empty" || levelname == "MainMenu" || levelname == "Boot" || levelname == "" || !GameManager.GetPlayerTransform()) return; - if (vehicles.Count != 0) foreach (var i in vehicles) if (i.Value) VehicleController.wheel(i.Value); if (isSit) VehicleController.MoveDrive(targetcar); if (vehicles.Count > 0) foreach (var i in vehicles) if (!i.Value) vehicles.Remove(i.Key); } public override void OnLateUpdate() { if (levelname == "Empty" || levelname == "MainMenu" || levelname == "Boot" || levelname == "" || !GameManager.GetPlayerTransform()) return; - if (GetObj(MyId) && !isDrive(MyId)) - { - NETHost.NetCar(MyId, GetObj(MyId).transform.position, GetObj(MyId).transform.rotation); - NETHost.NetPacketStat(true, true, VehicleController.curfuel, CarData(MyId)[3]); - } + if (vehicles.Count > 0) foreach (var i in vehicles) + { + InfoMain ii = i.Value.GetComponent().vehicleData; + + if (SkyCoop.API.m_ClientState == SkyCoop.API.SkyCoopClientState.HOST) NETHost.NetPacketStat(ii.m_OwnerId, ii.m_AllowDrive, ii.m_AllowSit, ii.m_isDrive, ii.m_SoundPlay, ii.m_Light, ii.m_CurFuel, ii.m_VehicleName); + if (!isDrive(ii.m_OwnerId)) NETHost.NetCar(ii.m_OwnerId, i.Value.transform.position, i.Value.transform.rotation); + } + MenuControll.Update(0); // COUNT CARS MenuControll.Update(1); // COOUNT SPEED MenuControll.Update(2); // COUNT FUEL @@ -199,33 +157,16 @@ public override void OnLateUpdate() { MenuControll.Open(1); } - if (vehicles.Count > 0 && Physics.Raycast(ray, out hit, 3f) && !isSit) + if (vehicles.Count > 0 && Physics.Raycast(ray, out hit, 3f) && !isSit && !isChat && !InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled) { GameObject ho = hit.transform.gameObject; - if (!isChat && !InterfaceManager.m_Panel_PauseMenu.isActiveAndEnabled) - { - int number; - string othname = "Unknown"; - try { number = int.Parse(ho.name); } catch { return; } - if (!vehicledata.ContainsKey(number)) return; - - othname = SkyCoop.MyMod.playersData[number].m_Name; - if (number == MyId) othname = MyNick; - - MenuControll.Open(2); - MenuControll.CarStatScreen(othname, bool.Parse(CarData(number)[0]), !isDrive(number), Mathf.Round(VehicleController.curfuel)); - } - else - { - MenuControll.Open(22); - } + if (!ho.GetComponent()) return; + InfoMain values = ho.GetComponent().vehicleData; + MenuControll.Open(2); + MenuControll.CarStatScreen(values.m_OwnerName, values.m_AllowSit, !isDrive(values.m_OwnerId), values.m_CurFuel); } else MenuControll.Open(22); - if (isSit) - { - SkyCoop.MyMod.MyAnimState = "Sit"; - } } public static bool deletecar(int PlayerId, int who) { @@ -249,15 +190,12 @@ public static void SpawnCar(int PlayerId, int SceneId, string name, Vector3 Posi { if (Position == Vector3.zero && isSit) return; bool a = vehicles.ContainsKey(PlayerId); - if (PlayerId != MyId) if (a) return; - if (PlayerId == MyId && a) if (deletecar(MyId, 0)) NETHost.NetDeleteCar(); - - GameObject key1 = LoadObject(name); if (!key1) { MelonLogger.Msg("[Car spawner] This Car Doesn't exist: " + name); return; } + if (Position == Vector3.zero) key1 = GameObject.Instantiate(key1, MyPosition.position + MyPosition.up * 2 + MyPosition.forward * 6f, Rotation); else @@ -265,38 +203,17 @@ public static void SpawnCar(int PlayerId, int SceneId, string name, Vector3 Posi MelonLogger.Msg("[Car spawner] CarId |> " + PlayerId + " :Scene: " + SceneId + " :AT: " + Position.ToString() + " <|"); - key1.name = PlayerId.ToString(); - key1.tag = "CarVehicleMod"; - key1.GetComponent().centerOfMass = Vector3.down; + string playername = SkyCoop.MyMod.playersData[PlayerId].m_Name; + if (MyId == PlayerId) playername = MyNick; - foreach (Transform g in key1.GetComponentsInChildren()) g.gameObject.layer = LayerMask.NameToLayer("NPC"); - key1.GetComponent().mass = int.Parse(GetInfo(name, "Weight")); + key1.AddComponent().NEWDATA(PlayerId, name, playername); - if (int.Parse(GetInfo(name, "Type")) == 0) - { - JointSpring aaa = new JointSpring(); - aaa.spring = float.Parse(GetInfo(name, "SpringForce")); - aaa.damper = float.Parse(GetInfo(name, "DamperForce")); - - foreach (Transform g in key1.GetComponentsInChildren()) if (g.name.Contains("FL") || g.name.Contains("FR") || g.name.Contains("BL") || g.name.Contains("BR")) - { - WheelComponent.AddComponent(g); - WheelComponent.Set_JointSpring(g, aaa); - } - } - - key1.layer = LayerMask.NameToLayer("Player"); - if (MyId == PlayerId) - { - NETHost.NetSpawnCar(name, key1.transform.position, key1.transform.rotation); - } - UpdateDriver(PlayerId, false); - UpdateCarData(PlayerId, true, true, 50, name); + if (MyId == PlayerId) NETHost.NetSpawnCar(name, key1.transform.position, key1.transform.rotation); + key1.gameObject.layer = LayerMask.NameToLayer("Player"); vehicles.Add(PlayerId, key1); } private void loot(GameObject bagage) { - GameObject conta = GameObject.Instantiate(Resources.Load("CONTAINER_MetalBox"), Vector3.one, Quaternion.identity); conta.transform.root.position = bagage.transform.position; conta.transform.SetParent(bagage.transform); diff --git a/vehiclemod.csproj b/vehiclemod.csproj index bb3be55..5f09e51 100644 --- a/vehiclemod.csproj +++ b/vehiclemod.csproj @@ -103,6 +103,7 @@ +