private BulletBase NewBullet(BulletType bulletID) { BulletBase bullet = null; Stack <BulletBase> _stack; // 判断对象池中是否有该类型的对象,如果有,则取出 if (_bulletsPool.TryGetValue(bulletID, out _stack)) { if (_stack.Count > 0) { bullet = _stack.Pop(); } } else { _stack = new Stack <BulletBase>(); _bulletsPool.Add(bulletID, _stack); } // 如果对象池内没有该对象,则创建 if (bullet == null) { switch (bulletID) { case BulletType.ReimuA_Sub1: bullet = new BulletReimuASub1(); break; case BulletType.Player_Laser: bullet = new PlayerLaser(); break; case BulletType.Player_Simple: bullet = new PlayerBulletSimple(); break; case BulletType.Enemy_Laser: bullet = new EnemyLaser(); break; case BulletType.Enemy_CurveLaser: bullet = new EnemyCurveLaser(); break; case BulletType.Enemy_Simple: bullet = new EnemySimpleBullet(); break; case BulletType.Enemy_LinearLaser: bullet = new EnemyLinearLaser(); break; default: Logger.Log("Create Bullet Fail! Invalid BulledId!"); break; } } return(bullet); }
protected override void CreateMainBullets() { // 左侧子弹 PlayerBulletSimple bullet = ObjectsPool.GetInstance().CreateBullet(BulletType.Player_Simple) as PlayerBulletSimple; bullet.ChangeStyleById(_mainBulletId); bullet.DoStraightMove(18f, 90f); bullet.SetPosition(_curPos.x + _leftBulletOffset.x, _curPos.y + _leftBulletOffset.y); bullet.SetDamage(2); // 右侧子弹 bullet = ObjectsPool.GetInstance().CreateBullet(BulletType.Player_Simple) as PlayerBulletSimple; bullet.ChangeStyleById(_mainBulletId); bullet.DoStraightMove(18f, 90f); bullet.SetPosition(_curPos.x + _rightBulletOffset.x, _curPos.y + _rightBulletOffset.y); bullet.SetDamage(2); }