//spawn the an unit instance of given the prefab private UnitAI SpawnUnit(GameObject prefab, Vector3 spawnPos, Quaternion rot, string name = "") { //instantiate the unit, assign the layer and name //~ GameObject unitObj=(GameObject)Instantiate(prefab, spawnPos, rot); GameObject unitObj = ObjectPoolManager.Spawn(prefab, spawnPos, rot); unitObj.layer = TDS.GetLayerAIUnit(); unitObj.name = name; //get the UnitAI instance and assign target UnitAI unitInstance = unitObj.GetComponent <UnitAI>(); unitInstance.target = GameControl.GetPlayer(); //if(anchorToPoint) unitInstance.SetAnchorPoint(transform, anchorRadius); //not in use atm //override the unit default hitpoint if overrideHitPoint is enabled if (overrideHitPoint) { unitInstance.OverrideHitPoint(spawnHP, overrideHPMode); } AddUnit(unitInstance); //track unit return(unitInstance); }
void Start() { if (trackSpeed > 0 && GameControl.GetPlayer() != null) { thisT.position = GameControl.GetPlayer().thisT.position + posOffset; } }
// Update is called once per frame void Update() { Shake(); float wantedZoom = defaultZoom; float wantedZoomOrtho = cam.orthographicSize; UnitPlayer player = GameControl.GetPlayer(); if (player != null) { Vector3 targetPos = player.thisT.position + posOffset; //if enabled limit is enabled, clamp the position so it wont go out of bound if (enableLimit) { targetPos.x = Mathf.Clamp(targetPos.x, minPosX, maxPosX); targetPos.z = Mathf.Clamp(targetPos.z, minPosZ, maxPosZ); } //lerp to target's position thisT.position = Vector3.Lerp(thisT.position, targetPos, Time.deltaTime * trackSpeed); //adjust wanted zoom level based on player speed wantedZoom = defaultZoom * (1 + (player.GetVelocity() / zoomNormalizeFactor)); wantedZoomOrtho = defaultZoomOrtho * (1 + (player.GetVelocity() / zoomNormalizeFactor)); } //if dynamicZoom is enabled, adjust the zoom acording to wanted zoom level if (enableDynamicZoom) { camT.localPosition = new Vector3(0, 0, Mathf.Lerp(camT.localPosition.z, wantedZoom, Time.deltaTime * zoomSpeed)); cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, wantedZoomOrtho, Time.deltaTime * zoomSpeed); } }
//function call to fire the weapon public void Fire() { currentCD = GetCoolDown(); recoil += GetRecoilMagnitude() * 2; AudioManager.PlaySound(shootSFX); //for weapon with finite clip if (currentClip > 0) { currentClip -= 1; if (currentClip <= 0) //if out of ammo //if this is a temporary weapon, remove the weapon { if (temporary) { GameControl.GetPlayer().RemoveWeapon(); return; } //if auto reload is enabled, reload if (GameControl.EnableAutoReload()) { Reload(); } } } }
void Update() { player = GameControl.GetPlayer(); if (player != null && playMoveIdleSound) { if (player.GetVelocity() > 0.15f) { if (playerMoveClip != null) { if (moveIdleAudioSource.clip != playerMoveClip) { moveIdleAudioSource.clip = playerMoveClip; moveIdleAudioSource.Play(); } else { if (!moveIdleAudioSource.isPlaying) { moveIdleAudioSource.Play(); } } } else if (moveIdleAudioSource.isPlaying) { moveIdleAudioSource.Stop(); } } else { if (playerIdleClip != null) { if (moveIdleAudioSource.clip != playerIdleClip) { moveIdleAudioSource.clip = playerIdleClip; moveIdleAudioSource.Play(); } else { if (!moveIdleAudioSource.isPlaying) { moveIdleAudioSource.Play(); } } } else { if (moveIdleAudioSource.isPlaying) { moveIdleAudioSource.Stop(); } } } } }
//check if the ability is ready to launch public string IsReady() { if (GameControl.GetPlayer().energy < GetCost()) { return("Insufficient Energy"); } if (currentCD > 0) { return("Ability on Cooldown"); } return(""); }
public override void OnTriggerEnter(Collider collider) { UnitAI unit = collider.gameObject.GetComponent <UnitAI>(); if (unit == null) { Debug.Log("no unit, return"); return; } if (destroyUnit) { unit.ClearUnit(); } GameControl.GetPlayer().GainHitPoint(-hitPointLost); //GameControl.GainCredits(-creditLost); GameControl.GainScore(-scoreLost); Triggered(); }
//function call when the unit collide with a player unit, for contact(melee) attack void OnPlayerContact() { if (!enableContactAttack) { return; } if (contactCurrentCD > 0) { return; //if attack is still on cd } if (uAnimation != null) { uAnimation.AttackMelee(); //play attack animation } contactCurrentCD = contactCooldown; //set the cooldown //create an attack instance and attack the player unit AttackInstance attInstance = new AttackInstance(this, ModifyAttackStatsToExistingEffect(contactAttackStats.Clone())); GameControl.GetPlayer().ApplyAttack(attInstance); }
//function call to launch the passed ability //for launching weapon alt-fire public static void LaunchAbility(Ability ability, bool useCostNCD = true) { bool teleport = ability.type == _AbilityType.Movement & ability.moveType == _MoveType.Teleport; if (ability.type == _AbilityType.AOE || ability.type == _AbilityType.Shoot || teleport) { //get the hit point and activate the ability on that particular spot Ray ray = CameraControl.GetMainCamera().ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { ability.Activate(hit.point); } else { ability.Activate(GameControl.GetPlayer().thisT.position); //use player position if there's no valid position } } else { //activate the ability on the player position ability.Activate(GameControl.GetPlayer().thisT.position); } }
//fire main weapon public void FireWeapon() { if (destroyed || IsStunned()) { return; } if (!thisObj.activeInHierarchy) { return; } int fireState = CanFire(); if (fireState == 0) { if (weaponList[weaponID].RequireAiming()) { Vector2 cursorPos = Input.mousePosition; if (weaponList[weaponID].RandCursorForRecoil()) { float recoil = GameControl.GetPlayer().GetRecoil() * 4; cursorPos += new Vector2(Random.value - 0.5f, Random.value - 0.5f) * recoil; } Ray ray = Camera.main.ScreenPointToRay(cursorPos); RaycastHit hit; //LayerMask mask=1<<TDS.GetLayerTerrain(); //Physics.Raycast(ray, out hit, Mathf.Infinity, mask); Physics.Raycast(ray, out hit, Mathf.Infinity); ShootObject.AimInfo aimInfo = new ShootObject.AimInfo(hit); StartCoroutine(ShootRoutine(aimInfo)); } else { StartCoroutine(ShootRoutine()); } weaponList[weaponID].Fire(); } else { string text = ""; if (fireState == 1) { text = "attack disabled"; } if (fireState == 2) { text = "weapon's on cooldown"; } if (fireState == 3) { text = "weapon's out of ammo"; } if (fireState == 4) { text = "weapon's reloading"; } TDS.FireFail(text); if (GetCurrentClip() == 0 && GameControl.EnableAutoReload()) { weaponList[weaponID].Reload(); } } }
public override void Update() { if (GameControl.IsGameOver() || destroyed || IsStunned()) { return; } //get player as target target = GameControl.GetPlayer(); if (target != null && !target.thisObj.activeInHierarchy) { target = null; } //reduce the attack cooldown currentCD -= Time.deltaTime; contactCurrentCD -= Time.deltaTime; //base.Update(); //if shootPeriodically, then shoot if (shootPeriodically) { ShootTarget(); } //if there's not target, reset the turret to origin if (target == null) { ResetTurret(); return; } //calculate the target dist, this is for later use float targetDist = Vector3.Distance(thisT.position, target.thisT.position); //now depend on the unit behaviour, act accordingly if (behaviour == _Behaviour.Aggressive) { EngageHostile(targetDist); //engage the target } else if (behaviour == _Behaviour.Aggressive_Trigger) { //if target is close enough to trigger a respond, set behaviour to aggressive if (Vector3.Distance(thisT.position, target.thisT.position) <= aggroRange) { behaviour = _Behaviour.Aggressive; } } else if (behaviour == _Behaviour.StandGuard) { if (guardTriggered) //if the unit is aggro'ed { EngageHostile(targetDist); //engage the target //this line is irrelevant atm since anchor to point is not in used //if(anchorToPoint) dist=Vector3.Distance(anchorPoint, target.thisT.position); //if target is out of range already, set aggro status to false if (targetDist > aggroRange * 2) { guardTriggered = false; //if(anchorToPoint) GenerateNewAnchor(); } } else { //if(anchorToPoint) MoveToPoint(currentAnchor, 0.5f); //if target is close enough to aggor the unit, set guardTriggered to true so the target can be engaged in next frame if (targetDist <= aggroRange) { guardTriggered = true; } } } }
//launch the ability, at the position given public void Activate(Vector3 pos = default(Vector3), bool useCostNCD = true) { if (useCostNCD) { currentCD = GetCooldown(); //set the cooldown GameControl.GetPlayer().energy -= GetCost(); //deduct player energy by the ability cost } AudioManager.PlaySound(launchSFX); //instantiate the launch object, if there's any if (launchObj != null) { GameObject obj = (GameObject)MonoBehaviour.Instantiate(launchObj, pos, Quaternion.identity); if (autoDestroyLaunchObj) { MonoBehaviour.Destroy(obj, launchObjActiveDuration); } } //for aoe ability if (type == _AbilityType.AOE || type == _AbilityType.AOESelf) { //get all the collider in range Collider[] cols = Physics.OverlapSphere(pos, GetAOERadius()); for (int i = 0; i < cols.Length; i++) { Unit unitInstance = cols[i].gameObject.GetComponent <Unit>(); //only continue if the collider is a unit and is not player if (unitInstance != null && unitInstance != GameControl.GetPlayer()) { //create an AttackInstance and mark it as AOE attack AttackInstance aInstance = new AttackInstance(GameControl.GetPlayer(), GetRuntimeAttackStats()); aInstance.isAOE = true; aInstance.aoeDistance = Vector3.Distance(pos, cols[i].transform.position); //apply the AttackInstance unitInstance.ApplyAttack(aInstance); } } //apply explosion force TDSPhysics.ApplyExplosionForce(pos, aStats); } //for ability that affects all hostile unit in game else if (type == _AbilityType.All) { //get all hostile unit for unit tracker List <Unit> unitList = new List <Unit>(UnitTracker.GetAllUnitList()); //loop through all unit, create an AttackInstance and apply the attack for (int i = 0; i < unitList.Count; i++) { AttackInstance aInstance = new AttackInstance(GameControl.GetPlayer(), GetRuntimeAttackStats()); unitList[i].ApplyAttack(aInstance); } } //for ability that meant to be cast on player unit else if (type == _AbilityType.Self) { //apply the attack on player AttackInstance aInstance = new AttackInstance(GameControl.GetPlayer(), GetRuntimeAttackStats()); GameControl.GetPlayer().ApplyAttack(aInstance); } //for ability that fires a shoot object else if (type == _AbilityType.Shoot) { //get the position and rotation to fire the shoot object from Transform srcT = GetShootObjectSrcTransform(); Vector3 shootPos = srcT.TransformPoint(shootPosOffset); pos.y = shootPos.y; Quaternion shootRot = Quaternion.LookRotation(pos - shootPos); //create the AttackInstance AttackInstance aInstance = new AttackInstance(GameControl.GetPlayer(), GetRuntimeAttackStats()); //Instantiate the shoot-object and fires it GameObject soObj = (GameObject)MonoBehaviour.Instantiate(shootObject, shootPos, shootRot); ShootObject soInstance = soObj.GetComponent <ShootObject>(); soInstance.Shoot(GameControl.GetPlayer().thisObj.layer, GetRange(), srcT, aInstance); } else if (type == _AbilityType.Movement) { if (moveType == _MoveType.Blink) { GameControl.GetPlayer().thisT.position += GameControl.GetPlayer().thisT.TransformVector(Vector3.forward * range); } else if (moveType == _MoveType.Dash) { //GameControl.GetPlayer().thisT.position+=GameControl.GetPlayer().thisT.TransformPoint(Vector3.z)*range; GameControl.GetPlayer().Dash(range, duration); } else if (moveType == _MoveType.Teleport) { Transform playerT = GameControl.GetPlayer().thisT; Vector3 tgtPos = new Vector3(pos.x, playerT.position.y, pos.z); if (Vector3.Distance(playerT.position, tgtPos) > range) { tgtPos = playerT.position + (tgtPos - playerT.position).normalized * range; } playerT.position = tgtPos; } } }
//to get the player turret object (for shooting ability) public Transform GetShootObjectSrcTransform() { return(GameControl.GetPlayer().turretObj != null?GameControl.GetPlayer().turretObj : GameControl.GetPlayer().thisT); }