using com.jbrettob.data.vo.items.mechanics; using com.jbrettob.enemies; using com.jbrettob.traps; using UnityEngine; public class RotateMechanic:AbstractGameMechanic { public float rotationSpeed = 0f; // private Trap _trap; private TrapModelRotate _trapModelRotate; private TrapModelCog _trapModelCog; // private RotateMechanicData _data; private ShootMechanic _shootMechanic; private bool _isReady = false; private Vector3 _targetPos = Vector3.zero; private Vector3 _ourPos = Vector3.zero; public void setData(Trap trap, RotateMechanicData data) { _data = data; _trap = trap; rotationSpeed = _data.rotationSpeed; _trapModelRotate = _trap.model.GetComponentInChildren(); _trapModelCog = _trap.model.GetComponentInChildren(); _shootMechanic = _trapModelRotate.transform.gameObject.GetSafeComponent(); _shootMechanic.setData(_trap, _data.shootMechanicData, this); } void Update() { if (!_trap.isPlaced()) return; if (rotationSpeed != 0f && getTarget() != null) { _targetPos = getTarget().position; _targetPos.y = 0; _ourPos = transform.position; _ourPos.y = 0; Quaternion newRotation = Quaternion.LookRotation(_targetPos - _ourPos, Vector3.forward); newRotation.x = 0f; newRotation.z = 0f; float rotDiff = _trapModelRotate.transform.rotation.eulerAngles.y - newRotation.eulerAngles.y; if (rotDiff > rotationSpeed) { rotDiff = (rotationSpeed * 1.2f); } else if (rotDiff < -rotationSpeed) { rotDiff = -(rotationSpeed * 1.2f); } _trapModelRotate.transform.Rotate((Vector3.up * -rotDiff) * Time.deltaTime * TimeScale.TIME, Space.World); if (Mathf.Abs(rotDiff) < 10f) _isReady = true; rotateCog(rotDiff); } } private void rotateCog(float rotDiff) { if (_trapModelCog != null) _trapModelCog.transform.Rotate((Vector3.left * -rotDiff) * Time.deltaTime * TimeScale.TIME, Space.Self); } private Transform getTarget() { Transform target = getNearestTarget(); // in the future, so we can add different kinds of getTarget based on behaviour. // IE: get weakest first, get strongest first, get nearest first, get farest first, or even based on enemy type. return target; } private Transform getNearestTarget() { Transform target = null; float distance = float.MaxValue; float tempDistance; for (int i = 0; i < SpawnManager.getInstance().enemies.Count; i++) { tempDistance = Vector3.Distance(gameObject.transform.position, SpawnManager.getInstance().enemies[i].transform.position); if (tempDistance < distance) { distance = tempDistance; target = SpawnManager.getInstance().enemies[i].transform; } } return target; } public TrapModelRotate trapModelRotate { get { return _trapModelRotate; } } public ShootMechanic shootMechanic { get { return _shootMechanic; } } public bool isReady() { return _isReady; } }