//scan for player void scanForEnemy() { if (scan.getTargetFound()) //if target found { state = State.ENGAGE; //switch to engage state target = scan.getTarget(); //set target to player } else { state = State.PATROL; //switch to partol state target = null; //set target null } }
// Update is called once per frame void Update() { if (!GC.paused) //if game is not paused { if (!preSet && GC.readDifficulty() != 0) //if not set and difficutly is not 0 { preSet = true; //preset maxDelayBetweenShots = 2f * (float)GC.readDifficulty(); //set max delay minDelayBetweenShots = 1f * (float)GC.readDifficulty() / 3f; //set min delay } if (readyToFire) //if ready to fire { delayBetweenShots = Random.Range(minDelayBetweenShots, maxDelayBetweenShots); //set delay between shots readyToFire = false; //ready to fire false target = scanner.getTarget(); //get target if (target != null) //if has target { targetSpeed = target.GetComponent <PlayerControl>().getSpeed(); //read target speed leadIndicator = getLeadPoint(); //calculate lead if (Random.Range(0, 1 + (3 / GC.readDifficulty())) == 0) //random chance to miss { leadIndicator = addVariance(leadIndicator); //add random variance to lead } if (Random.Range(0, 2) == 1 && timeSinceSwitch + 15 < Time.realtimeSinceStartup) //if switch and last switch is 15 second old { timeSinceSwitch = Time.realtimeSinceStartup; //set last switch weaponToFire = decideOnWeapon(); //decide on weapon } switch (weaponToFire) //read weapon to fire { case 0: //if lead is within range and fire angle => shoot cannon if (getDistanceToTargetPoint(leadIndicator, this.transform.position) < cannonWeaponRange && isTargetInWeaponAngle(40, leadIndicator)) { fireCannon(leadIndicator, cannonPods[selectedCannonPod]); } break; case 1: //if lead is within range and fire angle => shoot beam if (getDistanceToTargetPoint(leadIndicator, this.transform.position) < beamWeaponRange && isTargetInWeaponAngle(90, leadIndicator)) { fireBeam(beamEmitters[selectedBeamEmitter], leadIndicator); } break; case 2: //if lead is within range and fire angle => shoot rocket if (getDistanceToTargetPoint(leadIndicator, this.transform.position) < rocketWeaponRange && isTargetInWeaponAngle(90, leadIndicator)) { fireRocket(rocketPods[selectedRocketPod], target); } break; } lastShotTime = Time.realtimeSinceStartup; //set last shot } } else if (Time.realtimeSinceStartup >= (lastShotTime + delayBetweenShots)) //set ready to fire on run out of firing delay { readyToFire = true; } } }