public void getNextState() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (vertical != 0) { state = ShipDefinitions.SState.Searching; ship.move(vertical); } if (horizontal != 0) { state = ShipDefinitions.SState.Searching; ship.rotate(horizontal); } if (Input.GetButton("Fire1")) { state = ShipDefinitions.SState.Firing; ship.fire(); if (GetComponent <FiringModule>().canFire() == false) { state = ShipDefinitions.SState.Cooling; } } if (Input.GetButton("Fire2")) { state = ShipDefinitions.SState.Searching; ship.brake(); } }
/* * */ private void handleState() { if (state == ShipDefinitions.SState.Searching) { // purpose of state is to find a target // if target is found, switch to state aiming Vector3 target = badVector; GameObject obj = GetComponent <TargetFinder>().getTarget(faction); /* * if ((GetComponent<FiringModule>().GetType(). * Equals(typeof(HealMod)))) * { * obj = GetComponent<TargetFinder>().getFriendly(faction); * } */ this.target = obj; if (obj) { target = obj.transform.position; } else { obj = GetComponent <TargetFinder>().getFriendly(faction); if (obj) { if (!obj.Equals(gameObject)) { target = obj.transform.position; } } } if (target != badVector) { this.target = obj; state = ShipDefinitions.SState.Aiming; } else { ship.brake(); } } else if (state == ShipDefinitions.SState.Aiming) { bool move = true; if (target == null) { state = ShipDefinitions.SState.Searching; return; } Vector3 diff = target.transform.position - transform.position; //print(diff); float targetAngle = Mathf.Atan(diff.y / diff.x) * 180 / Mathf.PI + 90; if (diff.x > 0) { targetAngle = 180 + targetAngle; } targetAngle = (int)targetAngle; float shipAngle = transform.rotation.eulerAngles.z; if (ShipDefinitions.quickestRotation(shipAngle, targetAngle)) { ship.rotate(0.5f); } else { ship.rotate(-0.5f); } ShipIntf shipObject = GetComponent <ShipIntf>(); if ((shipAngle + shipObject.getEffectiveAngle() > targetAngle && shipAngle - shipObject.getEffectiveAngle() < targetAngle)) { if (Vector3.Distance(transform.position, target.transform.position) < shipObject.getEffectiveDistance()) { if (target.GetComponent <ShipController>() != null) { if (target.GetComponent <ShipController>() .getFaction() != faction) { state = ShipDefinitions.SState.Firing; } else { ship.brake(); move = false; state = ShipDefinitions.SState.Searching; } } if ((GetComponent <FiringModule>().GetType(). Equals(typeof(HealMod)))) { if (target.GetComponent <ShipIntf>().getHealthPercent() < 0.95) { state = ShipDefinitions.SState.Firing; } } } } shipObject = null; if (move) { ship.move(1); } else { ship.brake(); } } else if (state == ShipDefinitions.SState.Firing) { ship.fire(); if (GetComponent <FiringModule>().canFire() == false) { state = ShipDefinitions.SState.Cooling; } } else if (state == ShipDefinitions.SState.Cooling) { if (GetComponent <FiringModule>().canFire()) { state = ShipDefinitions.SState.Searching; } } }