|
| 1 | +using UnityEngine; |
| 2 | +using DaggerfallWorkshop.Game.Utility.ModSupport; |
| 3 | +using DaggerfallWorkshop.Game.Utility.ModSupport.ModSettings; |
| 4 | +using DaggerfallWorkshop.Game; |
| 5 | +using System.Collections; |
| 6 | +using System.IO; |
| 7 | +using DaggerfallWorkshop; |
| 8 | + |
| 9 | +namespace FlyAirplaneMod |
| 10 | +{ |
| 11 | + enum CameraViewMode |
| 12 | + { |
| 13 | + Front, |
| 14 | + Back, |
| 15 | + Right, |
| 16 | + Left, |
| 17 | + Top |
| 18 | + } |
| 19 | + public class FlyAirplaneMod : MonoBehaviour |
| 20 | + { |
| 21 | + Texture2D textureAirplane; |
| 22 | + Rect rectScreen = new Rect(0, 0, Screen.width, Screen.height); |
| 23 | + static readonly string texturePath = Path.Combine(Application.streamingAssetsPath, "Textures", "FlyAirplaneMod"); |
| 24 | + public static Mod Mod; |
| 25 | + float velocityModifier = 1f; |
| 26 | + int minSpeed = 1; |
| 27 | + Vector3 velocity; // current velocity |
| 28 | + bool isFlying = false; |
| 29 | + Vector3 direction; |
| 30 | + RaycastHit rhit; |
| 31 | + int startFlyHeight = 40; // height above player that will start flying |
| 32 | + CameraViewMode viewMode = CameraViewMode.Front; |
| 33 | + |
| 34 | + // Airplane images |
| 35 | + byte[] dataBack = File.ReadAllBytes(Directory.GetFiles(texturePath, "back.png")[0]); |
| 36 | + byte[] dataFront = File.ReadAllBytes(Directory.GetFiles(texturePath, "front.png")[0]); |
| 37 | + byte[] dataRight = File.ReadAllBytes(Directory.GetFiles(texturePath, "right.png")[0]); |
| 38 | + byte[] dataLeft = File.ReadAllBytes(Directory.GetFiles(texturePath, "left.png")[0]); |
| 39 | + byte[] dataTop = File.ReadAllBytes(Directory.GetFiles(texturePath, "top.png")[0]); |
| 40 | + |
| 41 | + // Mod options |
| 42 | + static int acceleration; |
| 43 | + static int maxSpeed; //this limit is to be safe from tests since the maxTerrainArray cant be modified and only have 256 in size , so at fast speeds the terrain need doesnt fit in 256! also if you fly too fast and terrain isnt rendered yet you will fall underground! and get stuck |
| 44 | + static bool invertedMouseY; |
| 45 | + static int fieldOfView; |
| 46 | + static KeyCode accelerateKey = KeyCode.W; |
| 47 | + static KeyCode decelerateKey = KeyCode.S; |
| 48 | + static KeyCode rudderLeftKey = KeyCode.A; |
| 49 | + static KeyCode rudderRightKey = KeyCode.D; |
| 50 | + static KeyCode rollLeftKey = KeyCode.Q; |
| 51 | + static KeyCode rollRightKey = KeyCode.E; |
| 52 | + static KeyCode cameraModeKey = KeyCode.C; |
| 53 | + static KeyCode screenshotKey = KeyCode.P; |
| 54 | + static KeyCode toggleFlyModeKey = KeyCode.F; |
| 55 | + |
| 56 | + [Invoke(StateManager.StateTypes.Start, 0)] |
| 57 | + public static void Init(InitParams initParams) |
| 58 | + { |
| 59 | + Mod = initParams.Mod; |
| 60 | + new GameObject(Mod.Title).AddComponent<FlyAirplaneMod>(); |
| 61 | + Mod.LoadSettingsCallback = LoadSettings; |
| 62 | + Mod.IsReady = true; |
| 63 | + } |
| 64 | + |
| 65 | + void Start() |
| 66 | + { |
| 67 | + textureAirplane = new Texture2D(0, 0, TextureFormat.ARGB32, false); |
| 68 | + Mod.LoadSettings(); |
| 69 | + } |
| 70 | + |
| 71 | + void OnGUI() |
| 72 | + { |
| 73 | + if (isFlying) |
| 74 | + { |
| 75 | + DaggerfallUI.DrawTexture( |
| 76 | + new Rect( |
| 77 | + rectScreen.x + rectScreen.width / 2f - (textureAirplane.width / 2f), |
| 78 | + rectScreen.y + rectScreen.height / 2f - (textureAirplane.height / 2f), |
| 79 | + textureAirplane.width, |
| 80 | + textureAirplane.height |
| 81 | + ), |
| 82 | + textureAirplane, |
| 83 | + ScaleMode.StretchToFill, |
| 84 | + true, |
| 85 | + Color.white |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void TakeScreenshot() |
| 91 | + { |
| 92 | + string screenshotName = Path.Combine(texturePath, "Screenshot_" + System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".png"); |
| 93 | + ScreenCapture.CaptureScreenshot(screenshotName); |
| 94 | + // Debug.Log("JumpScreenshotMod: Screenshot taken: " + screenshotName); |
| 95 | + } |
| 96 | + |
| 97 | + Vector3 GetAccelerationVector() |
| 98 | + { |
| 99 | + Vector3 moveInput = default; |
| 100 | + moveInput += Vector3.forward; // Keep flying forward always |
| 101 | + direction = GameManager.Instance.PlayerObject.transform.TransformVector(moveInput.normalized); |
| 102 | + return direction * acceleration * velocityModifier; |
| 103 | + } |
| 104 | + |
| 105 | + IEnumerator DisableSkyAfterSeconds(float seconds) |
| 106 | + { |
| 107 | + yield return new WaitForSeconds(seconds); |
| 108 | + GameManager.Instance.SkyRig.GetComponent<DaggerfallSky>().enabled = false; |
| 109 | + } |
| 110 | + |
| 111 | + void Update() |
| 112 | + { |
| 113 | + // Can't toggle fly mode when indoors or game paused |
| 114 | + if (GameManager.Instance.PlayerEnterExit.IsPlayerInside || InputManager.Instance.IsPaused) |
| 115 | + { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (Input.GetKeyDown(toggleFlyModeKey)) |
| 120 | + { |
| 121 | + if (!isFlying) |
| 122 | + { |
| 123 | + isFlying = true; |
| 124 | + |
| 125 | + viewMode = CameraViewMode.Front; |
| 126 | + textureAirplane.LoadImage(dataBack); |
| 127 | + |
| 128 | + GameManager.Instance.StreamingWorld.TerrainDistance = 4; // More than that will likely crash due to the "maxTerrainArray = 256" being too small |
| 129 | + GameManager.Instance.MainCamera.farClipPlane = 10000; // From tests 10k is enough to cover all when terrain distance is up to 7 |
| 130 | + GameManager.Instance.TransportManager.TransportMode = TransportModes.Foot; // Basically to remove the horse graphic in case it was on horse mode |
| 131 | + |
| 132 | + DaggerfallUI.Instance.enabled = false; |
| 133 | + InputManager.Instance.enabled = false; |
| 134 | + GameManager.Instance.MainCamera.fieldOfView = fieldOfView; |
| 135 | + |
| 136 | + DaggerfallUnity.Settings.LargeHUD = false; // this causes BUG on horizon if we dont wait to disable the sky! |
| 137 | + StartCoroutine(DisableSkyAfterSeconds(0.1f)); |
| 138 | + |
| 139 | + GameManager.Instance.RightHandWeapon.enabled = false; |
| 140 | + GameManager.Instance.PlayerController.enabled = false; // disable WASD |
| 141 | + GameManager.Instance.PlayerMouseLook.enabled = false; |
| 142 | + |
| 143 | + GameManager.Instance.PlayerObject.transform.LookAt(GameManager.Instance.PlayerObject.transform, Vector3.up); // In case player is looking the ground and fly, this resets to look the horizon |
| 144 | + GameManager.Instance.MainCamera.transform.rotation = GameManager.Instance.PlayerObject.transform.rotation; // Both camera and player rotation need to be in sync to start the flight |
| 145 | + |
| 146 | + GameManager.Instance.PlayerObject.transform.position += new Vector3(0, startFlyHeight, 0); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + isFlying = false; |
| 151 | + |
| 152 | + // Reset fly speed so next time start from default values |
| 153 | + velocityModifier = 1f; |
| 154 | + velocity = Vector3.zero; |
| 155 | + |
| 156 | + // Restore things to normal after flying |
| 157 | + DaggerfallUI.Instance.enabled = true; |
| 158 | + InputManager.Instance.enabled = true; |
| 159 | + GameManager.Instance.MainCamera.fieldOfView = DaggerfallUnity.Settings.FieldOfView; |
| 160 | + GameManager.Instance.SkyRig.GetComponent<DaggerfallSky>().enabled = true; |
| 161 | + GameManager.Instance.RightHandWeapon.enabled = true; |
| 162 | + GameManager.Instance.PlayerController.enabled = true; |
| 163 | + GameManager.Instance.PlayerMouseLook.enabled = true; |
| 164 | + |
| 165 | + // Snap player to ground |
| 166 | + var playerRotation = GameManager.Instance.PlayerObject.transform.rotation; |
| 167 | + if (Physics.Raycast(new Ray(GameManager.Instance.PlayerObject.transform.position, Vector3.down), out rhit, 200000f)) |
| 168 | + { |
| 169 | + GameManager.Instance.AcrobatMotor.ClearFallingDamage(); |
| 170 | + Vector3 groundPosition = rhit.point; |
| 171 | + groundPosition.y += 5; |
| 172 | + GameManager.Instance.PlayerObject.transform.position = groundPosition; |
| 173 | + GameManager.Instance.PlayerObject.transform.rotation = playerRotation; // This is needed to keep facing the same direction it was flying! |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + // If is not flying dont do anything from here onwards |
| 179 | + if (!isFlying) |
| 180 | + { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + if (Input.GetKeyDown(screenshotKey)) |
| 185 | + { |
| 186 | + TakeScreenshot(); |
| 187 | + } |
| 188 | + |
| 189 | + if (Input.GetKeyDown(cameraModeKey)) |
| 190 | + { |
| 191 | + // Cycle camera view modes |
| 192 | + if (viewMode < CameraViewMode.Top) |
| 193 | + { |
| 194 | + viewMode++; |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + viewMode = CameraViewMode.Front; |
| 199 | + textureAirplane.LoadImage(dataBack); |
| 200 | + } |
| 201 | + |
| 202 | + // Restore camera position |
| 203 | + GameManager.Instance.MainCamera.transform.rotation = GameManager.Instance.PlayerObject.transform.rotation; |
| 204 | + |
| 205 | + if (viewMode == CameraViewMode.Back) |
| 206 | + { |
| 207 | + GameManager.Instance.MainCamera.transform.Rotate(0, 180, 0, Space.Self); |
| 208 | + textureAirplane.LoadImage(dataFront); |
| 209 | + } |
| 210 | + else if (viewMode == CameraViewMode.Right) |
| 211 | + { |
| 212 | + GameManager.Instance.MainCamera.transform.Rotate(0, 90, 0, Space.Self); |
| 213 | + textureAirplane.LoadImage(dataRight); |
| 214 | + } |
| 215 | + else if (viewMode == CameraViewMode.Left) |
| 216 | + { |
| 217 | + GameManager.Instance.MainCamera.transform.Rotate(0, -90, 0, Space.Self); |
| 218 | + textureAirplane.LoadImage(dataLeft); |
| 219 | + } |
| 220 | + else if (viewMode == CameraViewMode.Top) |
| 221 | + { |
| 222 | + GameManager.Instance.MainCamera.transform.Rotate(90, 0, 0, Space.Self); |
| 223 | + textureAirplane.LoadImage(dataTop); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + // Avoid flying underground, certainly not the most efficient way to detect when hitting ground! |
| 228 | + if (!Physics.Raycast(new Ray(GameManager.Instance.PlayerObject.transform.position, Vector3.down), out rhit, 200000f)) |
| 229 | + { |
| 230 | + GameManager.Instance.PlayerObject.transform.position += new Vector3(0, 100, 0); |
| 231 | + } |
| 232 | + |
| 233 | + // Rotation |
| 234 | + Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), (invertedMouseY ? 1 : -1) * Input.GetAxis("Mouse Y")); |
| 235 | + Quaternion rotation = GameManager.Instance.PlayerObject.transform.rotation; |
| 236 | + Quaternion vert = Quaternion.AngleAxis(mouseDelta.y / Mathf.Clamp((10 / velocityModifier), 1, 5), Vector3.right); |
| 237 | + GameManager.Instance.PlayerObject.transform.rotation = rotation * vert; |
| 238 | + |
| 239 | + if (Input.GetKey(rollLeftKey)) |
| 240 | + { |
| 241 | + GameManager.Instance.PlayerObject.transform.Rotate(new Vector3(0, 0, 1) * Time.deltaTime * 100, Space.Self); |
| 242 | + } |
| 243 | + if (Input.GetKey(rollRightKey)) |
| 244 | + { |
| 245 | + GameManager.Instance.PlayerObject.transform.Rotate(new Vector3(0, 0, -1) * Time.deltaTime * 100, Space.Self); |
| 246 | + } |
| 247 | + if (Input.GetKey(rudderLeftKey)) |
| 248 | + { |
| 249 | + GameManager.Instance.PlayerObject.transform.Rotate(new Vector3(0, -1, 0) * Time.deltaTime * 10, Space.Self); |
| 250 | + } |
| 251 | + if (Input.GetKey(rudderRightKey)) |
| 252 | + { |
| 253 | + GameManager.Instance.PlayerObject.transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * 10, Space.Self); |
| 254 | + } |
| 255 | + |
| 256 | + if (Input.GetKey(accelerateKey) && velocityModifier < maxSpeed) |
| 257 | + { |
| 258 | + velocityModifier += 0.1f; |
| 259 | + } |
| 260 | + if (Input.GetKey(decelerateKey) && velocityModifier > minSpeed) |
| 261 | + { |
| 262 | + velocityModifier -= 0.1f; |
| 263 | + } |
| 264 | + |
| 265 | + // Position |
| 266 | + velocity += GetAccelerationVector() * Time.deltaTime; |
| 267 | + velocity = Vector3.Lerp(velocity, Vector3.zero, Time.deltaTime); |
| 268 | + GameManager.Instance.PlayerObject.transform.position += velocity * Time.deltaTime; |
| 269 | + } |
| 270 | + |
| 271 | + static KeyCode SetKeyFromText(string text) |
| 272 | + { |
| 273 | + if (System.Enum.TryParse(text.ToUpper(), out KeyCode result)) |
| 274 | + { |
| 275 | + return result; |
| 276 | + } |
| 277 | + return KeyCode.None; |
| 278 | + } |
| 279 | + |
| 280 | + static void LoadSettings(ModSettings modSettings, ModSettingsChange change) |
| 281 | + { |
| 282 | + if (modSettings != null) |
| 283 | + { |
| 284 | + // Settings |
| 285 | + acceleration = modSettings.GetValue<int>("Settings", "Acceleration"); |
| 286 | + maxSpeed = modSettings.GetValue<int>("Settings", "MaxSpeed"); |
| 287 | + fieldOfView = modSettings.GetValue<int>("Settings", "FieldOfView"); |
| 288 | + invertedMouseY = modSettings.GetValue<bool>("Settings", "InvertedMouseY"); |
| 289 | + |
| 290 | + // Controls |
| 291 | + toggleFlyModeKey = SetKeyFromText(modSettings.GetString("Controls", "ToggleFlyModeKey")); |
| 292 | + cameraModeKey = SetKeyFromText(modSettings.GetString("Controls", "CameraModeKey")); |
| 293 | + screenshotKey = SetKeyFromText(modSettings.GetString("Controls", "ScreenshotKey")); |
| 294 | + accelerateKey = SetKeyFromText(modSettings.GetString("Controls", "AccelerateKey")); |
| 295 | + decelerateKey = SetKeyFromText(modSettings.GetString("Controls", "DecelerateKey")); |
| 296 | + rudderLeftKey = SetKeyFromText(modSettings.GetString("Controls", "RudderLeftKey")); |
| 297 | + rudderRightKey = SetKeyFromText(modSettings.GetString("Controls", "RudderRightKey")); |
| 298 | + rollLeftKey = SetKeyFromText(modSettings.GetString("Controls", "RollLeftKey")); |
| 299 | + rollRightKey = SetKeyFromText(modSettings.GetString("Controls", "RollRightKey")); |
| 300 | + } |
| 301 | + } |
| 302 | + |
| 303 | + } |
| 304 | +} |
0 commit comments