Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- The following license supersedes all notices in the source code.
- Copyright (c) 2020 Kurt Dekker/PLBM Games All rights reserved.
- http://www.twitter.com/kurtdekker
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are
- met:
- Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- Neither the name of the Kurt Dekker/PLBM Games nor the names of its
- contributors may be used to endorse or promote products derived from
- this software without specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- // This is part of Jetpack Kurt Space Flight, by Kurt Dekker, available here:
- //
- // Appstore: https://itunes.apple.com/us/app/jetpack-kurt/id1033348911
- // GooglePlay: https://play.google.com/store/apps/details?id=com.plbm.jetpack
- //
- // External TestFlight link: https://testflight.apple.com/join/wt0HTUba
- //
- // Youtube playlist: https://www.youtube.com/playlist?list=PLTzufax_A179M51bQgMFDJ7y9Gjp9kOVR
- //
- // Itch.io: https://kurtdekker.itch.io/jetpack
- //
- // Simmer.io: https://simmer.io/@kurtdekker/jetpack-kurt
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class WeaponShot1Controller : MonoBehaviour
- {
- const float LaserBoltSpeed = 150.0f;
- const float Gravity = -10.0f;
- Vector3 velocity;
- bool reactive = true;
- public static WeaponShot1Controller Create(
- Vector3 startPosition,
- Vector3 startForward,
- Vector3 playerVelocity)
- {
- var sc = new GameObject( "WeaponShot1Controller.Create();").AddComponent<WeaponShot1Controller>();
- TTL.Attach( sc.gameObject, Random.Range( 3.5f, 4.5f));
- sc.transform.position = startPosition;
- Instantiate<GameObject>( Resources.Load<GameObject>(
- "Prefabs/WeaponShot1Controller_prefab"), sc.transform).GetComponent<ParticleSystem>();
- sc.velocity = playerVelocity + startForward.normalized * LaserBoltSpeed;
- sc.UpdateMovement();
- return sc;
- }
- void UpdateMovement()
- {
- velocity += Vector3.up * Gravity * Time.deltaTime;
- Vector3 movement = velocity * Time.deltaTime;
- Vector3 newPosition = transform.position + movement;
- // possible outcomes from impact/contact with a collider:
- //
- // - don't ricochet off a damage-taker (this might not be necessary)
- //
- // - case of ricochet:
- // - choose random speed attenuation
- // - deflect according to normal
- // - deflect a bit more randomly
- // - if below a certain amount, become unreactive
- //
- // - case of impact and finish:
- // - destroy
- //
- // If reactive, leave a little splash cloud of particles
- //
- Ray ray = new Ray( transform.position, velocity);
- RaycastHit rch;
- if (Physics.Raycast( ray, out rch, movement.magnitude, ~(1 << MyLayers.i_Player)))
- {
- bool CanRicochet = reactive;
- newPosition = rch.point;
- var da = rch.collider.GetComponent<ICombatDamageable>();
- if (da != null)
- {
- da.TakeDamage( 1.0f);
- CanRicochet = false;
- STATE.MissionShotsHit++;
- }
- if (reactive)
- {
- SpaceFlightShotImpact.Create( newPosition, rch.normal);
- }
- // chance of carom/ricochet?
- if (CanRicochet)
- {
- if (Random.Range( 0, 6) == 0)
- {
- float attenuate1 = Random.Range( 0.05f, 0.6f);
- float attenuate2 = Mathf.Min( attenuate1, Random.Range( 0.05f, 0.6f));
- float attenuate3 = Mathf.Min( attenuate2, Random.Range( 0.05f, 0.6f));
- velocity.x *= attenuate3;
- velocity.y *= attenuate1;
- velocity.z *= attenuate3;
- var impactSpeed = Vector3.Dot( velocity, rch.normal);
- var reflect = rch.normal * impactSpeed * Random.Range( 1.8f, 3.0f);
- velocity -= reflect;
- // chance of becoming unreactive: we no longer make ricochet splash visuals
- if (velocity.magnitude < 30)
- {
- // and a sub-chance of just going away when we're this slow
- if (Random.Range( 0, 3) > 0)
- {
- reactive = false;
- }
- }
- }
- }
- else
- {
- Destroy( gameObject);
- }
- }
- Vector3 oldPosition = transform.position;
- transform.position = newPosition;
- }
- void Update ()
- {
- UpdateMovement();
- }
- }
Add Comment
Please, Sign In to add comment