예제 #1
0
        //Deal damages to the destructible, if it reaches 0 HP it will be killed
        private void ApplyDamage(int damage)
        {
            if (!dead)
            {
                int adamage = Mathf.Max(damage - armor, 1);
                hp -= adamage;

                PlayerData.Get().SetCustomValue(GetHpUID(), hp);

                if (shake_on_hit)
                {
                    ShakeFX();
                }

                if (select.IsActive() && select.IsNearCamera(20f))
                {
                    if (hit_fx != null)
                    {
                        Instantiate(hit_fx, transform.position, Quaternion.identity);
                    }

                    TheAudio.Get().PlaySFX("destruct", hit_sound);
                }

                onDamaged?.Invoke();
            }
        }
예제 #2
0
        public int PutItem(ItemData item, ItemData create, float duration, int quantity)
        {
            if (current_item == null || create == current_item)
            {
                if (current_quantity < quantity_max && quantity > 0)
                {
                    int max   = quantity_max - current_quantity;             //Maximum space remaining
                    int quant = Mathf.Min(max, quantity + current_quantity); //Cant put more than maximum

                    prev_item         = item;
                    current_item      = create;
                    current_quantity += quant;
                    timer             = 0f;
                    this.duration     = duration;

                    if (progress_prefab != null && duration > 0.1f)
                    {
                        GameObject obj = Instantiate(progress_prefab, transform);
                        progress        = obj.GetComponent <ActionProgress>();
                        progress.manual = true;
                    }

                    if (select.IsNearCamera(10f))
                    {
                        TheAudio.Get().PlaySFX("furnace", put_audio);
                    }

                    return(quant);  //Return actual quantity that was used
                }
            }
            return(0);
        }
        void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (IsDead())
            {
                return;
            }

            if (buildable && buildable.IsBuilding())
            {
                return;
            }

            attack_timer        += Time.deltaTime;
            move_timer          += Time.deltaTime;
            navmesh_timer       += Time.deltaTime;
            ground_refesh_timer += Time.deltaTime;

            //Detect obstacles and ground
            if (ground_refesh_timer > ground_refresh_rate)
            {
                ground_refesh_timer = Random.Range(-0.02f, 0.02f);
                DetectGrounded();
                DetectFronted();
            }

            //Save position
            PlayerData.Get().SetCharacterPosition(GetUID(), SceneNav.GetCurrentScene(), transform.position, transform.rotation);

            //Stop moving
            if (is_moving && !HasTarget() && HasReachedMoveTarget(moving_threshold * 2f))
            {
                Stop();
            }

            //Stop attacking
            if (is_attacking && !HasAttackTarget())
            {
                Stop();
            }

            //Following
            if (target != null)
            {
                Vector3 targ_dir = (target.transform.position - transform.position);
                targ_dir.y = 0f;

                if (is_moving && !is_attacking)
                {
                    if (is_escaping)
                    {
                        Vector3 targ_pos = transform.position - targ_dir.normalized * 4f;
                        move_target = targ_pos;
                    }
                    else
                    {
                        move_target = target.transform.position;

                        //Stop following
                        if ((attack_target != null || attack_player != null) && targ_dir.magnitude < GetAttackTargetHitRange() * 0.8f)
                        {
                            move_target = transform.position;
                            is_moving   = false;
                        }

                        //Stop following
                        if (attack_target == null && attack_player == null && HasReachedMoveTarget(follow_distance))
                        {
                            move_target = transform.position;
                            is_moving   = false;
                        }
                    }
                }

                //Start following again
                if (!is_moving && !is_attacking)
                {
                    if (targ_dir.magnitude > GetAttackTargetHitRange())
                    {
                        is_moving = true;
                    }
                }
            }

            //Attacking
            if (HasAttackTarget() && attack_enabled)
            {
                if (!is_attacking)
                {
                    if (attack_timer > attack_cooldown)
                    {
                        Vector3 targ_dir = (target.transform.position - transform.position);
                        targ_dir.y = 0f;

                        if (targ_dir.magnitude < GetAttackTargetHitRange())
                        {
                            is_attacking = true;
                            is_moving    = false;
                            attack_hit   = false;
                            attack_timer = 0f;

                            if (onAttack != null)
                            {
                                onAttack.Invoke();
                            }
                        }
                    }
                }

                if (is_attacking)
                {
                    move_target       = transform.position;
                    move_target_avoid = transform.position;
                    FaceTorward(target.transform.position);

                    if (!attack_hit && attack_timer > attack_windup)
                    {
                        float range = (target.transform.position - transform.position).magnitude;
                        if (range < GetAttackTargetHitRange())
                        {
                            if (attack_target != null)
                            {
                                attack_target.TakeDamage(this, attack_damage);
                            }
                            if (attack_player != null)
                            {
                                attack_player.Combat.TakeDamage(attack_damage);
                            }
                        }
                        attack_hit = true;

                        if (selectable.IsNearCamera(20f))
                        {
                            TheAudio.Get().PlaySFX("character", attack_audio);
                        }
                    }

                    if (attack_timer > attack_duration)
                    {
                        is_attacking = false;
                        attack_timer = 0f;
                        is_moving    = true;

                        if (attack_target != null)
                        {
                            Attack(attack_target);
                        }
                        if (attack_player != null)
                        {
                            Attack(attack_player);
                        }
                    }
                }

                if (attack_target != null && attack_target.IsDead())
                {
                    Stop();
                }

                if (attack_player != null && attack_player.IsDead())
                {
                    Stop();
                }
            }

            //Add an offset to escape path when fronted
            if (avoid_obstacles && !direct_move)
            {
                if (is_fronted_left && !is_fronted_right)
                {
                    avoid_side = 1f;
                }
                if (is_fronted_right && !is_fronted_left)
                {
                    avoid_side = -1f;
                }

                //When fronted on all sides, use target to influence which side to go
                if (is_fronted_center && is_fronted_left && is_fronted_right && target)
                {
                    Vector3 dir = target.transform.position - transform.position;
                    dir = dir * (is_escaping ? -1f : 1f);
                    float dot = Vector3.Dot(dir.normalized, transform.right);
                    if (Mathf.Abs(dot) > 0.5f)
                    {
                        avoid_side = Mathf.Sign(dot);
                    }
                }

                float angle       = avoid_side * 90f;
                float far_val     = is_fronted ? 1f - (front_dist / destruct.hit_range) : Mathf.Abs(angle) / 90f; //1f = close, 0f = far
                float angle_speed = far_val * 150f + 50f;
                avoid_angle = Mathf.MoveTowards(avoid_angle, is_fronted ? angle : 0f, angle_speed * Time.deltaTime);
            }
        }