public void FireBullet(APCharacterController launcher, AttackContext context) { if (CanFireBullet()) { if (!m_infiniteAmmo) { m_ammoBox.AddAmmo(-1); } // make sure move horizontal direction is valid bool bFaceRight = launcher.GetMotor().m_faceRight; float fAngle = Mathf.Deg2Rad * context.m_bulletDirection; Vector2 v2MoveDir = new Vector2(Mathf.Cos(fAngle), -Mathf.Sin(fAngle)); if (bFaceRight && v2MoveDir.x < 0f || !bFaceRight && v2MoveDir.x > 0f) { v2MoveDir.x = -v2MoveDir.x; } // spawn and launch bullet (add player velocity before spawn) Vector2 pointPos = launcher.transform.TransformPoint(context.m_bulletStartPosition); pointPos = pointPos + (Time.deltaTime * launcher.GetMotor().m_velocity); float bulletSign = m_bullet.m_faceRight ? 1f : -1f; Quaternion rot = Quaternion.Euler(0f, 0f, bFaceRight != m_bullet.m_faceRight ? bulletSign * context.m_bulletDirection : -bulletSign * context.m_bulletDirection); APBullet newBullet = (APBullet)UnityEngine.Object.Instantiate(m_bullet, pointPos, rot); newBullet.enabled = true; // init bullet v2MoveDir = launcher.transform.TransformDirection(v2MoveDir); newBullet.Setup(launcher, v2MoveDir); // launch listeners launcher.EventListeners.ForEach(e => e.OnAttackBulletFired(this, newBullet)); } }
public void FireBullet(APCharacterController launcher, ContextId contextId) { if ((m_ammo > 0 || m_infiniteAmmo) && (m_bullet != null)) { if (!m_infiniteAmmo) { m_ammo--; } AttackContext curContext = m_contextStand; switch (contextId) { case ContextId.eRun: curContext = m_contextRun; break; case ContextId.eInAir: curContext = m_contextInAir; break; case ContextId.eCrouched: curContext = m_contextCrouched; break; } // make sure move horizontal direction is valid float fAngle = Mathf.Deg2Rad * curContext.m_bulletDirection; Vector2 v2MoveDir = new Vector2(Mathf.Cos(fAngle), -Mathf.Sin(fAngle)); if (launcher.GetMotor().m_faceRight&& v2MoveDir.x < 0f || !launcher.GetMotor().m_faceRight&& v2MoveDir.x > 0f) { v2MoveDir.x = -v2MoveDir.x; } // spawn and launch bullet (add player velocity before spawn) Vector2 pointPos = launcher.transform.TransformPoint(curContext.m_bulletStartPosition); pointPos = pointPos + (Time.deltaTime * launcher.GetMotor().m_velocity); APBullet newBullet = (APBullet)UnityEngine.Object.Instantiate(m_bullet, pointPos, Quaternion.identity); // init bullet v2MoveDir = launcher.transform.TransformDirection(v2MoveDir); newBullet.Setup(launcher, pointPos, v2MoveDir); // launch listeners launcher.EventListeners.ForEach(e => e.OnAttackBulletFired(this, newBullet)); } }