private float _fieldRadiusSquared;
大 中 小 三种石头的数量
、、重要的。。重要的
//打碎石头之后的处理
、、、、、、、、、、、、、、、、、、、、、、下面是Asteroid
public class Asteroid : SwarmItem
{
private AsteroidManager _asteroidManager;
private ASTEROID_TYPE _asteroidType;
private Vector3 _rotation;
private Vector3 _velocity;
private Vector3 _originalScale;
public enum ASTEROID_TYPE
{
Small = 0,
Medium = 1,
Large = 2
}
/// 重要的初始化函数
public override void Initialize(SwarmItemManager swarmItemManager, int prefabIndex, bool debugEvents)
{
base.Initialize(swarmItemManager, prefabIndex, debugEvents);
_originalScale = _thisTransform.localScale;
}
/// Sets the asteroids parameters
public void Set(AsteroidManager asteroidManager, ASTEROID_TYPE asteroidType, Vector3 rotation, Vector3 velocity, Vector3 position)
{
_asteroidManager = asteroidManager;
_asteroidType = asteroidType;
_rotation = rotation;
_velocity = velocity;
// We use the Accessor for this member to update the transform position
Position = position;
}
//更新每一帧
public override void FrameUpdate()
{
// move asteroid
Position += (_velocity * Time.deltaTime);
// reflect the asteroid's velocity within the field so that asteroid's aren't flying off into infinity
if (Position.sqrMagnitude >= _asteroidManager.FieldRadiusSquared)
{
// save the normalized position since we'll be using it twice below.
// this cuts down on the normalization calculations which can be expensive,
// especially when done every frame.
Vector3 normalizedPosition = Position.normalized;
// clip the asteroid's position to the field radius
Position = normalizedPosition * _asteroidManager.fieldRadius;
// reflect the velocity across the inverted normal (makes the asteroid bounce back)
_velocity = Vector3.Reflect(_velocity, -normalizedPosition);
}
_thisTransform.Rotate(_rotation * Time.deltaTime);
base.FrameUpdate();
}
/// Called after being activated or deactivated
protected override void OnStateChange ()
{
switch (_state)
{
case STATE.Inactive:
_asteroidManager.BreakUpAsteroid(_asteroidType, Position);
Explosion explosion = (Explosion)Main.Instance.explosionManager.ActivateItem();
if (explosion != null)
explosion.Position = Position;
break;
}
}
public override void OnSetParentTransform()
{
_thisTransform.localScale = _originalScale;
}
}