protected override void Awake()
        {
            base.Awake();
            character_list.Add(this);
            rigid       = GetComponent <Rigidbody>();
            selectable  = GetComponent <Selectable>();
            destruct    = GetComponent <Destructible>();
            buildable   = GetComponent <Buildable>();
            unique_id   = GetComponent <UniqueID>();
            colliders   = GetComponentsInChildren <Collider>();
            avoid_side  = Random.value < 0.5f ? 1f : -1f;
            facing      = transform.forward;
            use_navmesh = move_enabled && use_navmesh;

            move_target       = transform.position;
            move_target_avoid = transform.position;

            destruct.onDamaged += OnDamaged;
            destruct.onDeath   += OnDeath;

            if (buildable != null)
            {
                buildable.onBuild += OnBuild;
            }

            foreach (Collider collide in colliders)
            {
                float size = collide.bounds.extents.magnitude;
                if (size > bounds_extent.magnitude)
                {
                    bounds_extent        = collide.bounds.extents;
                    bounds_center_offset = collide.bounds.center - transform.position;
                }
            }
        }
 public void StopAutoMove()
 {
     auto_move                = false;
     auto_move_select         = null;
     auto_move_attack         = null;
     auto_move_drop_inventory = null;
 }
        private void DoAttackStrike(Destructible target, bool is_ranged)
        {
            //Ranged attack
            ItemData equipped = character.EquipData.GetEquippedWeaponData();

            if (target != null && is_ranged && equipped != null)
            {
                InventoryItemData projectile_inv = character.Inventory.GetFirstItemInGroup(equipped.projectile_group);
                ItemData          projectile     = ItemData.Get(projectile_inv?.item_id);
                if (projectile != null && CanWeaponAttackRanged(target))
                {
                    character.Inventory.UseItem(projectile, 1);
                    Vector3    pos     = GetProjectileSpawnPos();
                    Vector3    dir     = target.GetCenter() - pos;
                    GameObject proj    = Instantiate(projectile.projectile_prefab, pos, Quaternion.LookRotation(dir.normalized, Vector3.up));
                    Projectile project = proj.GetComponent <Projectile>();
                    project.shooter = character;
                    project.dir     = dir.normalized;
                    project.damage  = equipped.damage;
                }
            }

            //Melee attack
            else if (IsAttackTargetInRange(target))
            {
                target.TakeDamage(character, GetAttackDamage(target));

                if (onAttackHit != null)
                {
                    onAttackHit.Invoke(target);
                }
            }
        }
Example #4
0
        void Awake()
        {
            selectable = GetComponent <Selectable>();
            destruct   = GetComponent <Destructible>();
            unique_id  = GetComponent <UniqueID>();
            renders.AddRange(GetComponentsInChildren <MeshRenderer>());

            foreach (MeshRenderer render in renders)
            {
                foreach (Material material in render.sharedMaterials)
                {
                    bool     valid_mat       = material && MaterialTool.HasColor(material);
                    Material material_normal = valid_mat ? new Material(material) : null;
                    Material material_trans  = valid_mat ? new Material(material) : null;
                    if (material_trans != null)
                    {
                        MaterialTool.ChangeRenderMode(material_trans, BlendMode.Fade);
                    }
                    materials.Add(material_normal);
                    materials_transparent.Add(material_trans);
                    materials_color.Add(valid_mat ? material.color : Color.white);
                }
            }

            foreach (Collider collide in GetComponentsInChildren <Collider>())
            {
                if (collide.enabled && !collide.isTrigger)
                {
                    colliders.Add(collide);
                }
            }
        }
        private void OnDeath()
        {
            rigid.velocity    = Vector3.zero;
            moving            = Vector3.zero;
            rigid.isKinematic = true;
            target            = null;
            attack_target     = null;
            attack_player     = null;
            move_target       = transform.position;
            is_moving         = false;

            foreach (Collider coll in colliders)
            {
                coll.enabled = false;
            }

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

            if (data != null)
            {
                foreach (PlayerCharacter character in PlayerCharacter.GetAll())
                {
                    character.Data.AddKillCount(data.id); //Add kill count
                }
            }

            PlayerData.Get().RemoveCharacter(GetUID());
            if (!was_spawned)
            {
                PlayerData.Get().RemoveObject(GetUID());
            }
        }
        void Update()
        {
            if (TheGame.Get().IsPaused())
            {
                return;
            }

            if (IsDead())
            {
                return;
            }

            //Attack when target is in range
            if (!character.IsDoingAction())
            {
                attack_timer += Time.deltaTime;
            }

            Destructible auto_move_attack = character.GetAutoAttackTarget();

            if (auto_move_attack != null && !character.IsDoingAction() && IsAttackTargetInRange(auto_move_attack))
            {
                character.FaceTorward(auto_move_attack.transform.position);
                character.PauseAutoMove(); //Reached target, dont keep moving

                if (attack_timer > GetAttackCooldown())
                {
                    DoAttack(auto_move_attack);
                }
            }
        }
 //Perform one attack
 public void DoAttack(Destructible resource)
 {
     if (!character.IsDoingAction())
     {
         attack_timer   = -10f;
         attack_routine = StartCoroutine(AttackRun(resource));
     }
 }
        //Make sure the current equipped weapon can hit target, and has enough bullets
        public bool CanWeaponHitTarget(Destructible target)
        {
            ItemData equipped     = character.EquipData.GetEquippedWeaponData();
            bool     valid_ranged = equipped != null && equipped.IsRangedWeapon() && CanWeaponAttackRanged(target);
            bool     valid_melee  = equipped != null && equipped.IsMeleeWeapon();

            return(valid_melee || valid_ranged);
        }
Example #9
0
 public void MoveToTarget(Vector3 pos)
 {
     force_action  = true;
     attack_target = null;
     action_target = null;
     ChangeState(PetState.MoveTo);
     character.MoveTo(pos);
 }
Example #10
0
 public void StopAction()
 {
     character.Stop();
     attack_target = null;
     action_target = null;
     force_action  = false;
     ChangeState(PetState.Idle);
 }
 public void StopAction()
 {
     character.Stop();
     is_running    = false;
     force_action  = false;
     player_target = null;
     attack_target = null;
     ChangeState(AnimalState.Wander);
 }
 public bool IsAttackTargetInRange(Destructible target)
 {
     if (target != null)
     {
         float dist = (target.transform.position - transform.position).magnitude;
         return(dist < GetTargetAttackRange(target));
     }
     return(false);
 }
        //Check if target is valid for ranged attack, and if enough bullets
        public bool CanWeaponAttackRanged(Destructible destruct)
        {
            if (destruct == null)
            {
                return(false);
            }

            return(destruct.CanAttackRanged() && HasRangedProjectile());
        }
        private void OnTriggerEnter(Collider collision)
        {
            Destructible destruct = collision.GetComponent <Destructible>();

            if (destruct != null && !destruct.attack_melee_only)
            {
                collision.GetComponent <Destructible>().TakeDamage(shooter, damage);
                Destroy(gameObject);
            }
        }
        public float GetAttackRange(Destructible target)
        {
            ItemData equipped = character.EquipData.GetEquippedWeaponData();

            if (equipped != null && CanWeaponHitTarget(target))
            {
                return(equipped.range);
            }
            return(attack_range);
        }
        public float GetAttackStikesInterval(Destructible target)
        {
            ItemData equipped = character.EquipData.GetEquippedWeaponData();

            if (equipped != null && CanWeaponHitTarget(target))
            {
                return(Mathf.Max(equipped.strike_interval, 0.01f));
            }
            return(0.01f);
        }
        public int GetAttackStrikes(Destructible target)
        {
            ItemData equipped = character.EquipData.GetEquippedWeaponData();

            if (equipped != null && CanWeaponHitTarget(target))
            {
                return(Mathf.Max(equipped.strike_per_attack, 1));
            }
            return(1);
        }
        private void OnDamagedPlayer(PlayerCharacter player)
        {
            if (IsDead() || state_timer < 2f)
            {
                return;
            }

            player_target = player;
            attack_target = null;
            ReactToThreat();
        }
        private void OnDamagedCharacter(Character character)
        {
            if (IsDead() || state_timer < 2f)
            {
                return;
            }

            player_target = null;
            attack_target = character.GetDestructible();
            ReactToThreat();
        }
 public void AttackTarget(Destructible target)
 {
     if (target != null)
     {
         ChangeState(AnimalState.Attack);
         this.attack_target = target;
         this.player_target = null;
         force_action       = true;
         character.Attack(target);
     }
 }
Example #21
0
 public void AttackTarget(Destructible target)
 {
     if (target != null)
     {
         attack_target = target;
         action_target = null;
         force_action  = true;
         character.Attack(target);
         ChangeState(PetState.Attack);
     }
 }
 public void EscapeTarget(Destructible target)
 {
     if (target != null)
     {
         ChangeState(AnimalState.Escape);
         this.attack_target = target;
         this.player_target = null;
         force_action       = true;
         character.Escape(target.gameObject);
     }
 }
        //---- Special actions

        public void Sleep(ActionSleep sleep_target)
        {
            if (!is_sleep && !is_riding && !IsSwimming())
            {
                this.sleep_target = sleep_target;
                is_sleep          = true;
                auto_move         = false;
                auto_move_attack  = null;
                TheGame.Get().SetGameSpeedMultiplier(sleep_target.sleep_speed_mult);
            }
        }
 //Meant to be called every frame, for this reason don't do navmesh
 public void DirectMoveTo(Vector3 pos)
 {
     move_target       = pos;
     move_target_avoid = pos;
     target            = null;
     attack_target     = null;
     attack_player     = null;
     is_escaping       = false;
     is_moving         = true;
     direct_move       = true;
     move_timer        = 0f;
 }
 public void Stop()
 {
     target         = null;
     attack_target  = null;
     attack_player  = null;
     rigid.velocity = Vector3.zero;
     move_target    = transform.position;
     is_moving      = false;
     is_attacking   = false;
     move_timer     = 0f;
     direct_move    = false;
 }
Example #26
0
 public void PetPet(PlayerCharacter character)
 {
     StopAction();
     player_target = character;
     attack_target = null;
     action_target = null;
     ChangeState(PetState.Pet);
     if (animator != null)
     {
         animator.SetTrigger("Pet");
     }
 }
        public void AttackNearest()
        {
            Destructible destruct = Destructible.GetNearestAutoAttack(this, transform.position, 4f);

            if (Combat.HasRangedProjectile())
            {
                AttackRanged();
            }
            else if (destruct != null)
            {
                Attack(destruct);
            }
        }
 public void UpdateMoveTo(Vector3 pos)
 {
     //Meant to be called every frame, for this reason don't do navmesh
     auto_move                = true;
     auto_move_target         = pos;
     auto_move_target_next    = pos;
     path_found               = false;
     calculating_path         = false;
     auto_move_select         = null;
     auto_move_attack         = null;
     auto_move_drop           = -1;
     auto_move_drop_inventory = null;
 }
        public void Escape(GameObject target)
        {
            this.target        = target;
            this.attack_target = null;
            this.attack_player = null;
            Vector3 dir = target.transform.position - transform.position;

            move_target = transform.position - dir;
            is_escaping = true;
            is_moving   = true;
            move_timer  = 0f;
            direct_move = false;
        }
Example #30
0
        void Awake()
        {
            character = GetComponent <Character>();
            destruct  = GetComponent <Destructible>();
            animator  = GetComponentInChildren <Animator>();
            start_pos = transform.position;

            character.onAttack += OnAttack;
            destruct.onDamaged += OnTakeDamage;
            destruct.onDeath   += OnKill;

            transform.rotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f);
        }