// Check what state the cannon is in to perform certain actions public void UpdateState(CannonStates cannonState) { switch (cannonState) { // If the player is holding nothing and the cannon is holding nothing case CannonStates.cEmpty: break; // If the player is loading in the cannonball case CannonStates.cCannonBall: if (currentState == CannonStates.cGunpowder) { currentState = CannonStates.cFullyLoaded; break; } currentState = CannonStates.cCannonBall; break; // If the player is loading in the gunpowder case CannonStates.cGunpowder: if (currentState == CannonStates.cCannonBall) { currentState = CannonStates.cFullyLoaded; break; } currentState = CannonStates.cGunpowder; break; // If the cannon is ready to be fired, run code when player has the torch case CannonStates.cFullyLoaded: currentState = CannonStates.cFullyLoaded; break; // What to do when loading cannon case CannonStates.cPreLoaded: break; // Fallback case incase of an error default: break; } }
private void beginShooting() { this.state = CannonStates.shooting; countdownUntilAction = (int)(XNACS1Base.World.TicksInASecond * 0.5f); }
private void beginIdle() { this.Width = 20.0f; this.Height = 20.0f; this.state = CannonStates.idle; }
private void beginCooldown() { this.state = CannonStates.coolingDown; countdownUntilAction = (int)(XNACS1Base.World.TicksInASecond * 0.25f); }
// Update is called once per frame void Update() { if (state == CannonStates.Charging) { if (!chargingParticleSystem.isPlaying) { chargingEmission.enabled = true; chargingParticleSystem.Play(); chargeStarted = Time.time; } if (chargeStarted + chargingTime < Time.time) { chargingEmission.enabled = false; chargingParticleSystem.Stop(); state = CannonStates.Aiming; } } else { if (chargingParticleSystem.isPlaying) { chargingEmission.enabled = false; chargingParticleSystem.Stop(); } } if (state == CannonStates.Aiming || state == CannonStates.Shooting) { Vector3 shipPos = motherShip.transform.position; Vector3 curPos = barrel.transform.position; var shipDir = shipPos - curPos; //TODO: limit angle to 180 degrees if (state == CannonStates.Aiming) { float angleDiff = Vector3.SignedAngle(barrel.transform.up, shipDir, Vector3.forward); float rotateDir = angleDiff < 0 ? -1 : 1; float rotateAmount = Mathf.Min(Mathf.Abs(angleDiff), Time.deltaTime * 90); barrel.transform.Rotate(Vector3.forward, rotateAmount * rotateDir); if (rotateAmount < 0.1f) { state = CannonStates.Shooting; } } if (state == CannonStates.Shooting) { float targetDistance = Vector3.Distance(transform.position, motherShip.transform.position); Vector3 scale = laser.transform.localScale; Vector3 target = barrel.transform.position; Vector3 objectPos = laserHitParticleSystem.transform.position; target.x = target.x - objectPos.x; target.y = target.y - objectPos.y; float angle = Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg + 90; laserHitParticleSystem.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle)); laserHitParticleSystem.transform.Rotate(0, 180, 0); if (targetDistance > (scale.x / laserScaleMultiplier)) { laser.transform.localScale = new Vector3(scale.x + laserShootSpeed * Time.deltaTime, 1, 1); } else { barrel.transform.Rotate(Vector3.forward, 5 * Time.deltaTime); laserHitParticleSystem.transform.position = laser.transform.position + laser.transform.right * scale.x / laserScaleMultiplier * 0.89f; if (laserHitStarted < 0) { laserHitStarted = Time.time; laserHitParticleSystem.Play(); laserHitParticleSystem.transform.position = laser.transform.position + laser.transform.right * scale.x / laserScaleMultiplier; motherShip.Wobble(); } if (laserHitStarted + laserHitTime < Time.time) { state = CannonStates.Cooling; laser.transform.localScale = new Vector3(1, 1, 1); laserHitParticleSystem.Stop(); laserHitStarted = -1f; HurtShip(); } } } } if (state == CannonStates.Cooling) { if (cooldownStarted < 0) { cooldownStarted = Time.time; } float angleDiff = Vector3.SignedAngle(barrel.transform.up, startPos, Vector3.forward); float rotateDir = angleDiff < 0 ? -1 : 1; float rotateAmount = Mathf.Min(Mathf.Abs(angleDiff), Time.deltaTime * 90); barrel.transform.Rotate(Vector3.forward, rotateAmount * rotateDir); if (cooldownStarted + cooldownTime < Time.time) { state = CannonStates.Charging; cooldownStarted = -1f; } } }
// Use this for initialization void Start() { currentState = CannonStates.cEmpty; }