Ejemplo n.º 1
0
        static void TeleportToTarget()
        {
            var targetTransform = SelectedObject.transform;

            Vector3    targetPos = targetTransform.position + (targetTransform.forward.GetFlatVector().GetSpaceVector() * 1f);
            Quaternion targetRot = targetTransform.rotation;

            var playerTransform = WorldUtils.GetPlayerObject().transform;

            playerTransform.position = targetPos;
            playerTransform.rotation = targetRot;
        }
Ejemplo n.º 2
0
        static void GetActiveCamera()
        {
            var camera = WorldUtils.GetActiveCamera();

            if (camera != null)
            {
                Debug.Log($"Found active camera on {camera.gameObject.name}");
            }
            else
            {
                Debug.Log($"Couldn't find active camera!");
            }
        }
Ejemplo n.º 3
0
        private void Update()
        {
            //maybe die
            if (StayTime > 0)
            {
                Elapsed += Time.deltaTime;

                if (Elapsed >= StayTime)
                {
                    //Debug.Log($"Destroying {name} at {Elapsed:F2}s/{StayTime:F2}");
                    Destroy(this.gameObject);
                }
            }

            if (transform.position.magnitude > MaxDist)
            {
                //Debug.Log($"Destroying {name} because it's really far away");
                Destroy(this.gameObject);
            }

            //raycast
            bool    hasRigidbody = Rigidbody != null;
            Vector3 forward      = hasRigidbody ? Rigidbody.velocity.normalized : transform.forward;
            float   maxDistance  = hasRigidbody ? Rigidbody.velocity.magnitude / 30f : DefaultProbeDist;
            //var hits = Physics.RaycastAll(transform.position, forward, maxDistance, RaycastLayerMask, QueryTriggerInteraction.Collide);

            //find closest hit
            //var (otherController, hitPoint, hitLocation, hitMaterial) = GetClosestHit(hits);
            var hit = WorldUtils.RaycastAttackHit(transform.position, forward, maxDistance, true, true, null);

            if (hit.Controller != null && hit.Controller != HitInfo.Originator)
            {
                //Debug.Log("Bullet hit " + otherController.name + " via raycast!");

                float damageMultiplier;
                bool  allDamageIsPierce;
                if (hit.Hitbox != null)
                {
                    //Debug.Log((hit.Hitbox as MonoBehaviour)?.name);
                    damageMultiplier  = hit.Hitbox.DamageMultiplier;
                    allDamageIsPierce = hit.Hitbox.AllDamageIsPierce;
                }
                else
                {
                    damageMultiplier  = 1;
                    allDamageIsPierce = false;
                }

                HandleCollision(hit.Controller, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Finds the player and returns their controller (does not guarantee an actual PlayerController!)
        /// </summary>
        public static BaseController GetPlayerController()                          //TODO split into Get() and TryGet()
        {
            var pc = WorldUtils.GetPlayerObject()?.GetComponent <BaseController>(); //should be safe because GetPlayerObject returns true null

            if (pc != null)
            {
                return(pc);
            }
            else
            {
                Debug.LogWarning("Couldn't find PlayerController!");
                return(null);
            }
        }
Ejemplo n.º 5
0
        private void RestorePlayerToIntent(MetaState mgs, GameObject player)
        {
            if (mgs.PlayerIntent != null)
            {
                if (!string.IsNullOrEmpty(mgs.PlayerIntent.SpawnPoint))
                {
                    GameObject spawnPoint = WorldUtils.FindPlayerSpawn(mgs.PlayerIntent.SpawnPoint);
                    if (spawnPoint != null)
                    {
                        player.transform.position = spawnPoint.transform.position;
                        player.transform.rotation = spawnPoint.transform.rotation;
                        return;
                    }
                }
                else if (mgs.PlayerIntent.SpawnPoint != null) //not null, but is empty
                {
                    GameObject spawnPoint = WorldUtils.FindPlayerSpawn();
                    if (spawnPoint != null)
                    {
                        player.transform.position = spawnPoint.transform.position;
                        player.transform.rotation = spawnPoint.transform.rotation;
                        return;
                    }
                }
                else
                {
                    player.transform.position = mgs.PlayerIntent.Position;
                    player.transform.rotation = mgs.PlayerIntent.Rotation;
                    return;
                }

                Debug.LogWarning($"Failed to restore player to spawn point from player intent because spawn point \"{mgs.PlayerIntent.SpawnPoint}\" could not be found!");
            }
            //else

            {
                GameObject spawnPoint = WorldUtils.FindPlayerSpawn();
                if (spawnPoint != null)
                {
                    player.transform.position = spawnPoint.transform.position;
                    player.transform.rotation = spawnPoint.transform.rotation;
                }

                //Debug.LogWarning("No player spawn intent exists!");
            }
        }
Ejemplo n.º 6
0
        public override void OnActivate(GameObject activator)
        {
            if (CheckEligibility(activator))
            {
                string spawnPointHack = SpawnPoint;
                if (UsePosition)
                {
                    spawnPointHack = null;
                }
                else if (string.IsNullOrEmpty(SpawnPoint))
                {
                    spawnPointHack = string.Empty;
                }

                WorldUtils.ChangeScene(Scene, spawnPointHack, Position, Rotation);
            }
        }
Ejemplo n.º 7
0
        public void SpawnEntity()
        {
            if (SpawnedEntity)
            {
                return;
            }

            //check for warning conditions
            if (string.IsNullOrEmpty(FormID))
            {
                Debug.LogError($"EntityPlaceholder on {gameObject.name}: No entity defined!");
                return;
            }

            if (string.IsNullOrEmpty(ThingID) && UsePlaceholderTID && !DestroyPlaceholder)
            {
                Debug.LogWarning($"EntityPlaceholder on {gameObject.name}: No TID given, and set to use placeholder TID without destroying placeholder (will result in objects with same TID!)");
            }

            string tid = string.IsNullOrEmpty(ThingID) ? (UsePlaceholderTID ? gameObject.name : null) : ThingID;

            try
            {
                //this looks like archaic C style error checking
                if (WorldUtils.SpawnEntity(FormID, tid, transform.position, transform.eulerAngles, transform.parent) == null)
                {
                    Debug.LogError($"EntityPlaceholder on {gameObject.name}: Failed to spawn entity (unknown)");
                }
            }
            catch (Exception e)
            {
                Debug.LogError($"EntityPlaceholder on {gameObject.name}: Failed to spawn entity ({e.GetType().Name})");
                if (ConfigState.Instance.UseVerboseLogging)
                {
                    Debug.LogException(e);
                }
            }

            SpawnedEntity = true;

            if (DestroyPlaceholder)
            {
                Destroy(this.gameObject);
            }
        }
Ejemplo n.º 8
0
        static void Pick(string tid)
        {
            var obj = WorldUtils.FindObjectByTID(tid);

            if (obj == null)
            {
                ConsoleModule.WriteLine("Couldn't find TID!");
                return;
            }
            if (obj.GetComponent <BaseController>() == null)
            {
                ConsoleModule.WriteLine("Ref has no controller!");
            }

            SelectedTID    = tid;
            SelectedObject = obj;

            ConsoleModule.WriteLine("Found TID: " + tid);
        }
Ejemplo n.º 9
0
        private void RestorePlayerToIntent(MetaState mgs, GameObject player)
        {
            if (mgs.PlayerIntent != null)
            {
                if (!string.IsNullOrEmpty(mgs.PlayerIntent.SpawnPoint))
                {
                    GameObject spawnPoint = WorldUtils.FindObjectByTID(mgs.PlayerIntent.SpawnPoint);
                    player.transform.position = spawnPoint.transform.position;
                    player.transform.rotation = spawnPoint.transform.rotation;
                }
                else if (mgs.PlayerIntent.SpawnPoint != null) //not null, but is empty
                {
                    GameObject spawnPoint = WorldUtils.FindObjectByTID("DefaultPlayerSpawn");
                    if (spawnPoint != null)
                    {
                        player.transform.position = spawnPoint.transform.position;
                        player.transform.rotation = spawnPoint.transform.rotation;
                    }
                }
                else
                {
                    player.transform.position = mgs.PlayerIntent.Position;
                    player.transform.rotation = mgs.PlayerIntent.Rotation;
                }
            }
            else
            {
                GameObject spawnPoint = WorldUtils.FindObjectByTID("DefaultPlayerSpawn");
                if (spawnPoint != null)
                {
                    player.transform.position = spawnPoint.transform.position;
                    player.transform.rotation = spawnPoint.transform.rotation;
                }

                Debug.LogWarning("No player spawn intent exists!");
            }
        }
Ejemplo n.º 10
0
        private void EnterPainState()
        {
            Transform effectSpawnPoint = EffectSpawnPoint == null ? transform : EffectSpawnPoint;

            if (Animator != null)
            {
                Animator.Play(HurtingState);
            }
            if (FacingSpriteComponent != null)
            {
                FacingSpriteComponent.SetState(DestroyableThingState.Hurting);
            }

            if (!string.IsNullOrEmpty(PainSoundName))
            {
                AudioPlayer.Instance.PlaySoundPositional(PainSoundName, SoundType.Sound, false, effectSpawnPoint.position);
            }
            PainSoundSource.Ref()?.Play();

            if (!string.IsNullOrEmpty(PainEffect))
            {
                WorldUtils.SpawnEffect(PainEffect, effectSpawnPoint.position, effectSpawnPoint.eulerAngles, null);
            }
        }
Ejemplo n.º 11
0
        private void UpdateDeathState()
        {
            if (ForceDeadState)
            {
                //force dead state after a restore
                Debug.LogWarning($"{name}: force dead state after restore");

                //force objects if they exist
                if (AliveObject != null)
                {
                    AliveObject.SetActive(false);
                }
                if (DeadObject != null)
                {
                    DeadObject.SetActive(true);
                }

                //force animator
                if (Animator != null)
                {
                    Animator.Play(DeadState);
                }

                //force FacingSprite
                if (FacingSpriteComponent != null)
                {
                    FacingSpriteComponent.SetState(DestroyableThingState.Dead);
                }

                //force colliders
                if (DisableCollidersOnDeath)
                {
                    foreach (var collider in GetComponentsInChildren <Collider>())
                    {
                        collider.enabled = false;
                    }
                }

                if (RepeatDeathSpecial)
                {
                    DeathSpecial.Ref()?.Execute(new ActionInvokerData()
                    {
                        Activator = LastDamageDealer
                    });                                                                                    //note that LastDamageDealer will almost certainly be null
                }
                //destroy/deactivate
                if (DeactivateOnDeath)
                {
                    gameObject.SetActive(false);
                }
                if (DestroyOnDeath)
                {
                    Destroy(gameObject);
                }

                ForceDeadState = false;
                IsDead         = true;
            }

            /*
             * if(IsDead && Health > 0 && Reversible)
             * {
             *  IsDead = false; //TODO full resurrections
             *  return;
             * }
             */

            if (IsDead)
            {
                return;
            }

            if (Health <= 0)
            {
                Transform effectSpawnPoint = EffectSpawnPoint == null ? transform : EffectSpawnPoint;

                //die

                //animation
                if (AliveObject != null)
                {
                    AliveObject.SetActive(false);
                }
                if (DeadObject != null)
                {
                    DeadObject.SetActive(true);
                }

                //Animator animation options
                if (Animator != null)
                {
                    Animator.Play(DyingState);
                }
                //FacingSprite animation options
                if (FacingSpriteComponent != null)
                {
                    FacingSpriteComponent.SetState(DestroyableThingState.Dying);
                }

                //colliders
                if (DisableCollidersOnDeath)
                {
                    foreach (var collider in GetComponentsInChildren <Collider>())
                    {
                        collider.enabled = false;
                    }
                }

                //sound
                if (!string.IsNullOrEmpty(DeathSoundName))
                {
                    AudioPlayer.Instance.PlaySoundPositional(DeathSoundName, SoundType.Sound, false, effectSpawnPoint.position);
                }
                DeathSoundSource.Ref()?.Play();

                //effect
                if (!string.IsNullOrEmpty(DeathEffect))
                {
                    WorldUtils.SpawnEffect(DeathEffect, effectSpawnPoint.position, effectSpawnPoint.eulerAngles, null);
                }

                DeathSpecial.Ref()?.Execute(new ActionInvokerData()
                {
                    Activator = LastDamageDealer
                });

                //destroy/deactivate
                if (DeactivateOnDeath)
                {
                    gameObject.SetActive(false);
                }
                if (DestroyOnDeath)
                {
                    Destroy(gameObject);
                }

                IsDead = true;
            }
        }
Ejemplo n.º 12
0
 static void Warp(string scene, Vector3 position, Vector3 rotation)
 {
     WorldUtils.ChangeScene(scene, null, position, rotation);
 }
Ejemplo n.º 13
0
 static void Warp(string scene, string spawnPoint)
 {
     WorldUtils.ChangeScene(scene, spawnPoint, Vector3.zero, Vector3.zero);
 }
Ejemplo n.º 14
0
 static void Spawn(string fid)
 {
     WorldUtils.SpawnEntity(fid, null, (WorldUtils.GetPlayerObject().transform.position + (WorldUtils.GetPlayerObject().transform.forward * 1.0f)), Vector3.zero, null);
 }
Ejemplo n.º 15
0
        static void ListEntitiesInScene()
        {
            StringBuilder sb       = new StringBuilder();
            var           entities = CoreUtils.GetWorldRoot().GetComponentsInChildren <BaseController>(true);

            foreach (var entity in entities)
            {
                sb.AppendLine($"{entity.gameObject.name} ({entity.FormID ?? entity.EditorFormID} : {entity.GetType().Name}) [{entity.transform.position.ToString("F2")}] {(entity.isActiveAndEnabled ? "" : "(disabled)")} {(entity is ITakeDamage && WorldUtils.IsAlive(entity) ? "" : "(dead)")}");
            }

            ConsoleModule.WriteLine(sb.ToString());
        }
Ejemplo n.º 16
0
        private void Update()
        {
            //TODO pause handling?

            //maybe die
            if (StayTime > 0)
            {
                Elapsed += Time.deltaTime;

                if (Elapsed >= StayTime && !DeferredHitBegan)
                {
                    //Debug.Log($"Destroying {name} at {Elapsed:F2}s/{StayTime:F2}");
                    Destroy(this.gameObject);
                }
            }

            //distance travel check
            if (MaxDistToTravel > 0 && !DeferredHitBegan)
            {
                float distance = (Origin - transform.position).magnitude;
                if (distance > MaxDistToTravel)
                {
                    Destroy(this.gameObject);
                }
            }

            if (MaxDistFromOrigin >= 0 && transform.position.magnitude > MaxDistFromOrigin && !DeferredHitBegan)
            {
                //Debug.Log($"Destroying {name} because it's really far away");
                Destroy(this.gameObject);
            }

            if (DeferredHitBegan && TimeToDeferredHit > 0)
            {
                TimeToDeferredHit -= Time.deltaTime;
                if (TimeToDeferredHit <= 0)
                {
                    //handle deferred hit
                    DeferredHitAction();
                }
            }

            if (EnableRaycasting && !DeferredHitBegan)
            {
                //raycast
                bool    hasRigidbody = Rigidbody != null;
                Vector3 forward      = hasRigidbody ? Rigidbody.velocity.normalized : transform.forward;
                float   maxDistance  = OverrideProbeDist > 0 ? OverrideProbeDist : (hasRigidbody ? Rigidbody.velocity.magnitude / 30f : DefaultProbeDist);
                //var hits = Physics.RaycastAll(transform.position, forward, maxDistance, RaycastLayerMask, QueryTriggerInteraction.Collide);

                //find closest hit
                //var (otherController, hitPoint, hitLocation, hitMaterial) = GetClosestHit(hits);
                var hit = WorldUtils.RaycastAttackHit(transform.position, forward, maxDistance, true, true, null);

                if (hit.Controller != null && hit.Controller != HitInfo.Originator)
                {
                    //Debug.Log("Bullet hit " + hit.Controller.name + " via raycast!");

                    float damageMultiplier;
                    bool  allDamageIsPierce;
                    if (hit.Hitbox != null)
                    {
                        //Debug.Log((hit.Hitbox as MonoBehaviour)?.name);
                        damageMultiplier  = hit.Hitbox.DamageMultiplier;
                        allDamageIsPierce = hit.Hitbox.AllDamageIsPierce;
                    }
                    else
                    {
                        damageMultiplier  = 1;
                        allDamageIsPierce = false;
                    }

                    if (EnableDeferredHits)
                    {
                        DeferredHitBegan  = true;
                        DeferredHitAction = () => HandleHit(hit.Controller, hit.Hitbox, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce);
                        TimeToDeferredHit = (transform.position - hit.HitPoint).magnitude / Rigidbody.velocity.magnitude;
                        //Debug.Log($"bullet: {transform.position} | target: {hit.HitPoint} | distance: {(transform.position - hit.HitPoint).magnitude} | velocity: {Rigidbody.velocity.magnitude}");
                    }
                    else
                    {
                        HandleHit(hit.Controller, hit.Hitbox, hit.HitLocation, hit.HitMaterial, hit.HitPoint, damageMultiplier, allDamageIsPierce);
                    }
                }
                else if (hit.Controller == null && hit.HitCollider != null)
                {
                    var hitMaterial         = hit.HitMaterial;
                    var colliderHitMaterial = hit.HitCollider.gameObject.GetComponent <ColliderHitMaterial>();
                    if (colliderHitMaterial != null)
                    {
                        hitMaterial = colliderHitMaterial.Material;
                    }

                    if (EnableDeferredHits)
                    {
                        DeferredHitBegan  = true;
                        DeferredHitAction = () => HandleHit(null, null, hit.HitLocation, hitMaterial, hit.HitPoint, 1, false);
                        TimeToDeferredHit = (transform.position - hit.HitPoint).magnitude / Rigidbody.velocity.magnitude;
                        //Debug.Log($"bullet: {transform.position} | target: {hit.HitPoint} | distance: {(transform.position - hit.HitPoint).magnitude} | velocity: {Rigidbody.velocity.magnitude}");
                    }
                    else
                    {
                        HandleHit(null, null, hit.HitLocation, hitMaterial, hit.HitPoint, 1, false);
                    }
                }
            }
        }