Exemple #1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        var bs = collision.gameObject.GetComponent <BulletScript>();

        if (bs != null)
        {
            float dt = 0, dr = 0;
            if (GameState.Instance.Player.Armor > 0)
            {
                dt = HasArmorDT;
                dr = HasArmorDR;
                GameState.Instance.Player.Armor -= bs.Damage + bs.DamagePierce;
            }
            float bulletDamage = DamageUtil.CalculateDamage(bs.Damage, bs.DamagePierce, dt, dr);
            GameState.Instance.Player.Health -= bulletDamage;
            //gameUI.GetComponent<HealthUI>().LoseHealth();
            if (GameState.Instance.Player.Health <= 0)
            {
                Debug.Log("Game Over");
                MessageInterface.PushToBus(new PlayerDeathMessage());
                //gameOverScene.SetActive(true); //TODO make this signal a gamecontroller, preferably using messaging, because I mean DAMN
            }
            Destroy(collision.gameObject);
        }
    }
Exemple #2
0
        private void HandleInteraction()
        {
            //get thing, probe and display tooltip, check use

            bool haveTarget = false;

            int layerMask = LayerMask.GetMask("Default", "ActorHitbox", "Actor");

            Debug.DrawRay(CameraRoot.position, CameraRoot.transform.forward * MaxProbeDist);

            //raycast all, go through the hits ignoring hits to self
            RaycastHit[] hits = Physics.RaycastAll(CameraRoot.transform.position, CameraRoot.transform.forward, MaxProbeDist * 2, layerMask, QueryTriggerInteraction.Collide);
            if (hits != null && hits.Length > 0)
            {
                //GameObject nearestObject = null;
                InteractableComponent nearestInteractable = null;
                float nearestDist = float.MaxValue;
                foreach (RaycastHit hit in hits)
                {
                    //skip if it's further than nearestDist (occluded) or flatdist is further than MaxProbeDist (too far away)
                    if (hit.distance > nearestDist)
                    {
                        continue;
                    }

                    float fDist = VectorUtils.GetFlatVectorToTarget(transform.position, hit.point).magnitude;
                    if (fDist > MaxProbeDist)
                    {
                        continue;
                    }

                    //nearestObject = hit.collider.gameObject;

                    //if there's a PlayerController attached, we've hit ourselves
                    if (hit.collider.GetComponent <PlayerController>() != null)
                    {
                        continue;
                    }

                    //TODO pull a similar trick to see if we're pointing at an Actor?

                    //get the interactable component and hitbox component; if it doesn't have either then it's an obstacle
                    InteractableComponent ic  = hit.collider.GetComponent <InteractableComponent>();
                    IHitboxComponent      ahc = hit.collider.GetComponent <IHitboxComponent>();
                    if (ic == null && ahc == null)
                    {
                        //we null out our hit first since it's occluded by this one
                        nearestInteractable = null;
                        nearestDist         = hit.distance;
                        continue;
                    }

                    //it's just us lol
                    if (ahc != null && ahc.ParentController is PlayerController)
                    {
                        continue;
                    }

                    //we have an interactablecomponent and we're not occluded
                    if (ic != null)
                    {
                        nearestInteractable = ic;
                        nearestDist         = hit.distance;
                        continue;
                    }

                    //if it doesn't meet any of those criteria then it's an occluder
                    nearestInteractable = null;
                    nearestDist         = hit.distance;
                }

                //if(nearestObject != null)
                //    Debug.Log("Nearest: " + nearestObject.name);

                if (nearestInteractable != null && nearestInteractable.enabled)
                {
                    //Debug.Log("Detected: " + nearestInteractable.Tooltip);

                    //HUDScript.SetTargetMessage(nearestInteractable.Tooltip);
                    nearestInteractable.OnLook(this.gameObject);
                    if (!string.IsNullOrEmpty(nearestInteractable.Tooltip))
                    {
                        MessageInterface.PushToBus(new QdmsKeyValueMessage("PlayerHasTarget", "Target", nearestInteractable.Tooltip));
                        HadTargetLastFrame = true;
                        haveTarget         = true;
                    }

                    //actual use
                    if (MappedInput.GetButtonDown(DefaultControls.Use) && !GameState.Instance.PlayerFlags.Contains(PlayerFlags.NoInteract))
                    {
                        nearestInteractable.OnActivate(this.gameObject);
                    }
                }
            }

            if (!haveTarget && HadTargetLastFrame)
            {
                MessageInterface.PushToBus(new QdmsFlagMessage("PlayerClearTarget")); //should probably not do this constantly
            }

            HadTargetLastFrame = haveTarget;
        }