diff --git a/Content/Audio/jumpland1.flax b/Content/Audio/jumpland_var1.flax similarity index 100% rename from Content/Audio/jumpland1.flax rename to Content/Audio/jumpland_var1.flax diff --git a/Content/Audio/jumpland2.flax b/Content/Audio/jumpland_var2.flax similarity index 100% rename from Content/Audio/jumpland2.flax rename to Content/Audio/jumpland_var2.flax diff --git a/Content/Audio/jumpland3.flax b/Content/Audio/jumpland_var3.flax similarity index 100% rename from Content/Audio/jumpland3.flax rename to Content/Audio/jumpland_var3.flax diff --git a/Content/Scenes/AerowalkScene.scene b/Content/Scenes/AerowalkScene.scene index a7464a7..f21a8d6 100644 --- a/Content/Scenes/AerowalkScene.scene +++ b/Content/Scenes/AerowalkScene.scene @@ -1,7 +1,7 @@ { "ID": "194e05f445ece24ec5448d886e1334df", "TypeName": "FlaxEngine.SceneAsset", - "EngineBuild": 6330, + "EngineBuild": 6331, "Data": [ { "ID": "194e05f445ece24ec5448d886e1334df", @@ -21,9 +21,7 @@ "ID": "c95a3dab492c1b2046ce2191daa2b111", "TypeName": "Game.Q3MapImporter", "ParentID": "194e05f445ece24ec5448d886e1334df", - "V": { - "mapPath": "C:\\dev\\GoakeFlax\\Assets\\Maps\\aerowalk.map" -} + "V": {} }, { "ID": "ff6b6db54b5aa08e7286ef86246149ef", @@ -70,7 +68,7 @@ "Transform": { "Translation": { "X": 0.0, - "Y": 716.0, + "Y": 388.0, "Z": 0.0 } }, @@ -123,7 +121,7 @@ }, "Offsets": { "Left": 0.0, - "Right": 57.0, + "Right": 54.4, "Top": -97.0, "Bottom": 64.0 }, @@ -164,8 +162,8 @@ "Name": "ContainerControl 0", "Transform": { "Translation": { - "X": 45644.0, - "Y": 0.5, + "X": 45012.0, + "Y": -163.5, "Z": 0.0 } }, diff --git a/GoakeFlax.flaxproj b/GoakeFlax.flaxproj index 525d56c..5be19bb 100644 --- a/GoakeFlax.flaxproj +++ b/GoakeFlax.flaxproj @@ -16,15 +16,16 @@ "DefaultScene": "194e05f445ece24ec5448d886e1334df", "DefaultSceneSpawn": { "Position": { - "X": -902.0629, - "Y": 230.86, - "Z": 402.125732 + "X": 0.0, + "Y": 0.0, + "Z": 0.0 }, "Direction": { - "X": 0.925840437, - "Y": -0.16703406, - "Z": 0.338997364 + "X": 0.0, + "Y": 0.0, + "Z": 1.0 } }, - "MinEngineVersion": "0.0.6194" + "MinEngineVersion": "0.0.6194", + "EngineNickname": null } \ No newline at end of file diff --git a/Source/Game/AudioManager.cs b/Source/Game/AudioManager.cs new file mode 100644 index 0000000..f0ab961 --- /dev/null +++ b/Source/Game/AudioManager.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.IO; +using FlaxEngine; +using Console = Cabrito.Console; +using Object = FlaxEngine.Object; + +namespace Game +{ + public static class AudioManager + { + private class AudioInfo + { + public AudioClip[] AudioClips; + public int lastAudioPlayed; + } + + private static Random random = new Random(); + + private static Dictionary cachedAudioInfos = new Dictionary(); + + public static void PlaySound(string soundName, Actor actor, Vector3 position, float volume = 1f) + { + PlaySound(soundName, actor, position, volume, Vector2.One); + } + + public static void PlaySound(string soundName, Actor actor, Vector3 position, float volume, Vector2 pitchRange) + { + AudioInfo audio = GetSound(soundName); + if (audio.AudioClips.Length == 0) + return; + + AudioClip audioClip; + if (audio.AudioClips.Length > 1) + { + // Randomize selected clip while avoiding the last used clip + int randomIndex = 0; + for (int i = 0; i < 10; i++) + { + randomIndex = random.Next(audio.AudioClips.Length); + if (randomIndex != audio.lastAudioPlayed) + break; + } + + audioClip = audio.AudioClips[randomIndex]; + audio.lastAudioPlayed = randomIndex; + } + else + audioClip = audio.AudioClips[0]; + + float pitch; + if (pitchRange[0] < pitchRange[1]) // Randomized pitch + pitch = (float)(pitchRange[0] + (random.NextDouble() * (pitchRange[1] - pitchRange[0]))); + else + pitch = pitchRange[0]; + + var audioSource = new AudioSource(); + audioSource.Clip = audioClip; + audioSource.Position = position; + audioSource.Parent = actor.Parent; + audioSource.Pitch = pitch; + audioSource.Name = audioClip.Path; + audioSource.Volume = volume; + + audioSource.Play(); + Object.Destroy(audioSource, audioClip.Length); + } + + private static AudioInfo GetSound(string soundName) + { + if (cachedAudioInfos.TryGetValue(soundName, out AudioInfo cachedAudio)) + return cachedAudio; + + AudioInfo audio = new AudioInfo(); + + var workDir = Directory.GetCurrentDirectory(); + var audioBasePath = Path.Combine(workDir, "Content", "Audio"); + AudioClip audioClip = Content.Load(Path.Combine(audioBasePath, soundName + ".flax")); + if (audioClip != null) + audio.AudioClips = new AudioClip[] { audioClip }; + else + { + // Check if this audio has multiple variations + List audioClips = new List(); + int index = 1; + do + { + // TODO: make this more efficient, maybe get a list of assets and filter by name + audioClip = Content.Load(Path.Combine(audioBasePath, soundName + "_var" + index + ".flax")); + if (audioClip != null) + audioClips.Add(audioClip); + index++; + } while (audioClip != null); + + if (audioClips.Count > 0) + { + audio.AudioClips = audioClips.ToArray(); + } + else + Console.PrintError("AudioClip '" + soundName + "' not found"); + } + + audio.lastAudioPlayed = audio.AudioClips.Length + 1; + + cachedAudioInfos.Add(soundName, audio); + return audio; + } + } +} \ No newline at end of file diff --git a/Source/Game/Cabrito/Console/ConsolePlugin.cs b/Source/Game/Cabrito/Console/ConsolePlugin.cs index 60646ba..41c6d95 100644 --- a/Source/Game/Cabrito/Console/ConsolePlugin.cs +++ b/Source/Game/Cabrito/Console/ConsolePlugin.cs @@ -3,23 +3,30 @@ using System.Collections.Generic; using FlaxEngine; using Console = Cabrito.Console; +#if FLAX_EDITOR +using FlaxEditor; +#endif + namespace Game { public class ConsolePlugin : GamePlugin { public override void Initialize() { + Debug.Log("ConsolePlugin init"); +#if !FLAX_EDITOR Console.Init(); - base.Initialize(); +#else + Console.Clear(); +#endif } public override void Deinitialize() { - base.Deinitialize(); - FlaxEngine.Debug.Log("ConsolePlugin Deinitialize"); + Debug.Log("ConsolePlugin Deinitialize"); } - public override PluginDescription Description => new PluginDescription() + public static PluginDescription DescriptionInternal = new PluginDescription() { Author = "Ari Vuollet", Name = "Console", @@ -28,4 +35,24 @@ namespace Game IsAlpha = true, }; } + +#if FLAX_EDITOR + public class ConsoleEditorPlugin : EditorPlugin + { + public override void Initialize() + { + Debug.Log("ConsoleEditorPlugin init"); + Console.Init(); + } + + public override void Deinitialize() + { + Debug.Log("ConsoleEditorPlugin Deinitialize"); + } + + public override PluginDescription Description => ConsolePlugin.DescriptionInternal; + + public override Type GamePluginType => typeof(ConsolePlugin); + } +#endif } \ No newline at end of file diff --git a/Source/Game/PlayerActor.cs b/Source/Game/PlayerActor.cs index 9e99b2d..7267df9 100644 --- a/Source/Game/PlayerActor.cs +++ b/Source/Game/PlayerActor.cs @@ -2,11 +2,10 @@ using System.Linq; using FlaxEngine; using Cabrito; -using FlaxEditor.CustomEditors; +#if FLAX_EDITOR using FlaxEditor.CustomEditors.Dedicated; -using FlaxEditor.CustomEditors.Editors; -using FlaxEditor.CustomEditors.Elements; using FlaxEditor.Scripting; +#endif using FlaxEngine.GUI; namespace Game diff --git a/Source/Game/PlayerInput.cs b/Source/Game/PlayerInput.cs index 30fe3f0..807a94b 100644 --- a/Source/Game/PlayerInput.cs +++ b/Source/Game/PlayerInput.cs @@ -19,6 +19,7 @@ namespace Game public Vector3 verificationPosition; public Vector3 verificationVelocity; + public Vector3 verificationViewAngles; public Quaternion verificationOrientation; } @@ -28,7 +29,7 @@ namespace Game public Vector3 position; public Vector3 velocity; public Quaternion orientation; - public float viewYaw, viewPitch, viewRoll; + public Vector3 viewAngles; // yaw, pitch, roll } [StructLayout(LayoutKind.Sequential)] diff --git a/Source/Game/PlayerInputDemo.cs b/Source/Game/PlayerInputDemo.cs index 55c0854..3e8be83 100644 --- a/Source/Game/PlayerInputDemo.cs +++ b/Source/Game/PlayerInputDemo.cs @@ -60,7 +60,6 @@ namespace Game OnEndFrame(); // advances to first frame } - private int asdf = 0; public override void OnEndFrame() { // TODO: check if the current state frame matches the current frame number before advancing diff --git a/Source/Game/PlayerInputLocal.cs b/Source/Game/PlayerInputLocal.cs index e0ba953..ed5e1b0 100644 --- a/Source/Game/PlayerInputLocal.cs +++ b/Source/Game/PlayerInputLocal.cs @@ -30,8 +30,8 @@ namespace Game public override void OnUpdate() { - // Collect anything framerate independent here like camera movement - // All axis values here should be accumulated, and binary actions OR'ed + // Collect all input here + // All axis values here should be accumulated currentState.input.viewDeltaX += InputManager.GetAxisRaw("Mouse X"); currentState.input.viewDeltaY += InputManager.GetAxisRaw("Mouse Y"); @@ -43,11 +43,6 @@ namespace Game public override void OnFixedUpdate() { - // Collect all input here - /*currentState.input.moveForward = InputManager.GetAxis("Vertical"); - currentState.input.moveRight = InputManager.GetAxis("Horizontal"); - currentState.input.attacking = InputManager.GetAction("Attack"); - currentState.input.jumping = InputManager.GetAction("Jump");*/ } public override void OnEndFrame() @@ -55,8 +50,9 @@ namespace Game if (IsRecording) { currentState.input.verificationPosition = currentState.actor.position; - currentState.input.verificationOrientation = currentState.actor.orientation; currentState.input.verificationVelocity = currentState.actor.velocity; + currentState.input.verificationViewAngles = currentState.actor.viewAngles; + currentState.input.verificationOrientation = currentState.actor.orientation; currentState.input.frame = frame; buffer.Add(currentState.input); diff --git a/Source/Game/PlayerMovement.cs b/Source/Game/PlayerMovement.cs index 53280f5..1345a09 100644 --- a/Source/Game/PlayerMovement.cs +++ b/Source/Game/PlayerMovement.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using FlaxEngine; using System.Diagnostics; using System.Threading.Tasks; -using FlaxEditor.CustomEditors.Editors; -using FlaxEngine.Assertions; using Console = Cabrito.Console; using Debug = FlaxEngine.Debug; using Object = FlaxEngine.Object; @@ -33,12 +31,6 @@ namespace Game [Limit(0, 9000), Tooltip("Base Movement speed")] public float MoveSpeed { get; set; } = 320; - public AudioClip JumpLandSound; - public AudioClip JumpLandSound2; - public AudioClip JumpLandSound3; - private AudioClip lastJumpLandSound; - private Random soundRandom; - private float viewPitch; private float viewYaw; private float viewRoll; @@ -85,7 +77,6 @@ namespace Game rigidBody = Actor.As(); - soundRandom = new Random(); //rigidBody.CollisionEnter += OnCollisionEnter; //rigidBody.TriggerEnter += OnTriggerEnter; //rigidBody.TriggerExit += OnTriggerExit; @@ -185,8 +176,8 @@ namespace Game currentInputFrame2++; } - private Vector3 fixedPosition = Vector3.Zero; - private bool newframe = false; + private bool demoDeltasVerify = true; + private bool demoDeltasCorrect = true; public override void OnFixedUpdate() { @@ -197,25 +188,49 @@ namespace Game PlayerInputState inputState = input.GetCurrentInputState(); if (input is PlayerInputDemo) + { ApplyInputToCamera(inputState); + // Verify view angles first + if (demoDeltasVerify) + { + Vector3 viewAngles = new Vector3(viewYaw, viewPitch, viewRoll); + float viewAnglesDelta = (viewAngles - inputState.verificationViewAngles).Length; + if (viewAnglesDelta > 0.00001) + { + Console.PrintError("Demo verification failed, view angles delta: " + viewAnglesDelta); + if (demoDeltasCorrect) + { + SetCameraEulerAngles(inputState.verificationViewAngles); + } + } + } + } + SimulatePlayerMovement(inputState); - if (input is PlayerInputDemo) + if (input is PlayerInputDemo && demoDeltasVerify) { // verify float positionDelta = (Actor.Position - inputState.verificationPosition).Length; if (positionDelta > 0.00001) - Console.Print("pos delta: " + positionDelta + " " + (Actor.Position - inputState.verificationPosition)); + Console.Print("Demo verification failed, position delta: " + positionDelta); float velocityDelta = (currentVelocity - inputState.verificationVelocity).Length; if (velocityDelta > 0.00001) - Console.Print("pos vel: " + velocityDelta); + Console.Print("Demo verification failed, velocity delta: " + velocityDelta); float orientationDelta = (rootActor.Orientation - inputState.verificationOrientation).Length; if (orientationDelta > 0.00001) - Console.Print("pos orient: " + rootActor.Orientation); + { + Console.PrintError("Demo verification failed, orientation delta: " + orientationDelta); + if (demoDeltasCorrect) + { + + } + } + //if (currentInputFrame == 0) /*{ @@ -231,24 +246,17 @@ namespace Game position = Actor.Position, velocity = currentVelocity, orientation = rootActor.Orientation, - viewYaw = viewYaw, - viewPitch = viewPitch, - viewRoll = viewRoll + viewAngles = new Vector3(viewYaw, viewPitch, viewRoll), }); input.OnEndFrame(); - - lastInputFrame = currentInputFrame; currentInputFrame++; viewPitchLastFrame = viewPitch; viewYawLastFrame = viewYaw; viewRollLastFrame = viewRoll; - - fixedPosition = Actor.Position; - newframe = true; } private void ApplyInputToCamera(PlayerInputState inputState) @@ -256,17 +264,22 @@ namespace Game // Update camera viewf float xAxis = inputState.viewDeltaX; float yAxis = inputState.viewDeltaY; - if (xAxis != 0.0f || yAxis != 0.0f) - { - var camera = rootActor.GetChild(); + if (xAxis == 0.0f && yAxis == 0.0f) + return; - viewPitch = Mathf.Clamp(viewPitch + yAxis, -90.0f, 90.0f); - viewYaw += xAxis; + viewPitch = Mathf.Clamp(viewPitch + yAxis, -90.0f, 90.0f); + viewYaw += xAxis; - // root orientation must be set first - rootActor.Orientation = Quaternion.Euler(0, viewYaw, 0); - camera.Orientation = Quaternion.Euler(viewPitch, viewYaw, viewRoll); - } + SetCameraEulerAngles(new Vector3(viewYaw, viewPitch, viewRoll)); + } + + private void SetCameraEulerAngles(Vector3 viewAngles) + { + Camera camera = rootActor.GetChild(); + + // Root orientation must be set first + rootActor.Orientation = Quaternion.Euler(0, viewAngles.X, 0); + camera.Orientation = Quaternion.Euler(viewAngles.Y, viewAngles.X, viewAngles.Z); } private static bool SweepPlayerCollider(Actor actor, Vector3 start, Vector3 end, out RayCastHit[] hits) @@ -764,7 +777,7 @@ namespace Game private bool jumped = false; private float lastJumped = -1f; - private Vector3 safePosition; + //private Vector3 safePosition; [ReadOnly] public float CurrentVelocity @@ -870,33 +883,7 @@ namespace Game jumped = true; lastJumped = Time.GameTime; - /*var jumpLandSound = JumpLandSound; - for (int i = 0; i < 10; i++) - { - var r = soundRandom.Next(3); - if (r == 1) - jumpLandSound = JumpLandSound2; - else if (r == 2) - jumpLandSound = JumpLandSound3; - - // avoid repetition - if (jumpLandSound != lastJumpLandSound) - break; - } - - if (jumpLandSound != null && jumpLandSound.IsLoaded) - { - var audioSource = new AudioSource(); - audioSource.Clip = jumpLandSound; - audioSource.Position = rootActor.Position; //new Vector3(-350, 176, 61);//rootActor.Position; - audioSource.Parent = Actor.Parent; - audioSource.Pitch = 1f; - audioSource.Name = jumpLandSound.Path; - - audioSource.Play(); - Destroy(audioSource, jumpLandSound.Length); - lastJumpLandSound = jumpLandSound; - }*/ + AudioManager.PlaySound("jumpland", Actor, rootActor.Position); } if (onGround)