protected override void OnUpdate() { EntityManager manager = World.Active.GetOrCreateManager <EntityManager>(); for (int turretIdx = 0; turretIdx < m_turretData.Length; ++turretIdx) { if (m_turretData.State[turretIdx].TimeSinceLastFire >= 1.0f && m_turretData.State[turretIdx].CanFire == 1) { ComponentTypes.TurretHeadState stateCopy = m_turretData.State[turretIdx]; Entity body = m_turretData.Parent[turretIdx].Value; Position bodyPosition = manager.GetComponentData <Position>(body); PostUpdateCommands.CreateEntity(Bootstrap.MissileArchetype); PostUpdateCommands.SetComponent(new Position { Value = new Vector3(bodyPosition.Value.x, bodyPosition.Value.y, bodyPosition.Value.z) + new Vector3(m_turretData.Positions[turretIdx].Value.x, m_turretData.Positions[turretIdx].Value.y, m_turretData.Positions[turretIdx].Value.z) }); PostUpdateCommands.SetComponent(new MoveSpeed { speed = 10.0f }); PostUpdateCommands.SetComponent(new Rotation { Value = Quaternion.Euler(0.0f, stateCopy.Angle, 0.0f) }); PostUpdateCommands.SetComponent(new ComponentTypes.MissileState { BirthTime = Time.time }); PostUpdateCommands.AddSharedComponent(Bootstrap.MissileLook); stateCopy.TimeSinceLastFire = 0.0f; m_turretData.State[turretIdx] = stateCopy; } } }
protected override void OnUpdate() { EntityManager manager = World.Active.GetOrCreateManager <EntityManager>(); for (int idx = 0; idx < m_data.Length; ++idx) { float nearestDistance = float.MaxValue; Vector3 nearestToEnemy = new Vector3(0.0f, 0.0f, 1.0f); Vector3 nearestPosition = new Vector3(0.0f, 0.0f, 0.0f); bool nearestFound = false; Entity turretBody = m_data.Parent[idx].Value; Position position = manager.GetComponentData <Position>(turretBody); for (int enemyIdx = 0; enemyIdx < m_enemies.Length; ++enemyIdx) { Vector3 toEnemy = m_enemies.EnemyPositions[enemyIdx].Value - position.Value; if (toEnemy.magnitude < nearestDistance && toEnemy.magnitude < 5.0f) { nearestDistance = toEnemy.magnitude; nearestPosition = m_enemies.EnemyPositions[enemyIdx].Value; nearestToEnemy = toEnemy; nearestFound = true; } } ComponentTypes.TurretHeadState tempState = m_data.State[idx]; tempState.TimeSinceLastFire += Time.deltaTime; tempState.CanFire = 0; float delta = tempState.TargetAngle - tempState.Angle; float rotSpeed = 45.0f; if (nearestFound) { tempState.CanFire = 1; nearestToEnemy.Normalize(); float dot = Vector3.Dot(new Vector3(0.0f, 0.0f, 1.0f), nearestToEnemy); Vector3 cross = Vector3.Cross(new Vector3(0.0f, 0.0f, 1.0f), nearestToEnemy); tempState.TargetAngle = Mathf.Rad2Deg * Mathf.Acos(dot); if (cross.y < 0.0f) { tempState.TargetAngle = 360.0f - tempState.TargetAngle; } rotSpeed = 180.0f; Vector3 basis = new Vector3(0.0f, 0.0f, 1.0f); Vector3 angleVector = Quaternion.Euler(0.0f, tempState.Angle, 0.0f) * basis; Vector3 targetVector = Quaternion.Euler(0.0f, tempState.TargetAngle, 0.0f) * basis; Vector3 angleTargetCross = Vector3.Cross(angleVector, targetVector); angleTargetCross.Normalize(); float adjustment = Time.deltaTime * rotSpeed; if (adjustment > Mathf.Abs(tempState.TargetAngle - tempState.Angle)) { adjustment = Mathf.Abs(tempState.TargetAngle - tempState.Angle); } if (angleTargetCross.y < 0.0f) { tempState.Angle -= adjustment; } else { tempState.Angle += adjustment; } } if (tempState.Angle < 0.0f) { tempState.Angle += 360.0f; } if (tempState.Angle > 0.0f) { tempState.Angle -= 360.0f; } LocalRotation tempRotation = m_data.LocalRotation[idx]; tempRotation.Value = Quaternion.Euler(0.0f, tempState.Angle, 0.0f); m_data.LocalRotation[idx] = tempRotation; m_data.State[idx] = tempState; } }