Exemple #1
0
    public void Attack(Vector3 target)
    {
        if (eq != null)
        {
            Item weapon = eq.GetWeapon();

            if (weapon != null)
            {
                float buffSpeed = 1f;
                for (int i = this.activeBuffs.Count - 1; i >= 0; i--)
                {
                    GameObject buffItem = this.activeBuffs[i] as GameObject;
                    if (buffItem != null)
                    {
                        Item buffItemTmp = buffItem.GetComponent <Item>();
                        buffSpeed *= buffItemTmp.speedChange;
                    }
                }

                if (Time.time - lastAttackTime > weapon.cooldown / buffSpeed)
                {
                    GameObject leftSlot = eq.leftSlot;
                    Sounds     sounds   = leftSlot.GetComponent <Sounds>();
                    if (leftSlot != null && sounds != null)
                    {
                        sounds.play(gameObject, sounds.spawn);
                    }

                    Vector3 attackDirection = target - sc.position;
                    if (weapon.missilePrefab != null)
                    {
                        GameObject missileObj = Instantiate(weapon.missilePrefab) as GameObject;
                        missileObj.GetComponent <Missile>().startingSpeed = Mathf.Max(0, Vector3.Dot(GetComponent <SC>().velocity, attackDirection.normalized)) / Time.deltaTime;
                        missileObj.GetComponent <Missile>().team          = team;
                        missileObj.GetComponent <Missile>().SetParamsFromWeapon(eq.leftSlot);
                        missileObj.GetComponent <SC>().SetPosition(GetComponent <SC>().position);
                        missileObj.GetComponent <SC>().SetDirectionTo(target);
                        World.AddMissile(missileObj);
                    }
                    else
                    {
                        Quaternion postRotation = Quaternion.FromToRotation(sc.direction, attackDirection.normalized);
                        GameObject slashFx      = Instantiate(Resources.Load("Slash"), transform.localPosition, postRotation) as GameObject;
                        slashFx.transform.parent = transform;

                        List <GameObject> enemies = World.GetOppositeCollection(team);
                        for (int i = enemies.Count - 1; i >= 0; --i)
                        {
                            SC enemySc = enemies[i].GetComponent <SC>();
                            if (sc.GetAngleFromTo(attackDirection, enemySc.position) < weapon.angle && Vector3.Distance(sc.position, enemySc.position) < weapon.maxDist)
                            {
                                float buffDamage = 0f;
                                foreach (GameObject gameBuff in this.activeBuffs)
                                {
                                    Item buffItem = gameBuff.GetComponent <Item>();
                                    buffDamage += buffItem.damage;
                                }
                                enemies[i].GetComponent <Critter>().TakeDamage(weapon.damage + buffDamage);
                            }
                        }
                    }
                    lastAttackTime = Time.time;
                }
            }
        }
    }