Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.3 kB
7
Indexable
Never
using UnityEngine;
using Photon.Pun;
using MarsFPSKit.ZombieWaveSurvival;

namespace MarsFPSKit
{
    public class ExplodeToggle : Kit_InteractableObject
    {
        public Kit_PvE_ZombieWaveSurvival zws;
        [HideInInspector]
        public Kit_IngameMain main;

        [Header("Settings")]
        public int explosionPrice; // Price to purchase the explosion action
        private bool isExplosionPurchased = false; // Flag to track if explosion is purchased

        public Kit_ExplodeableBarrelExtended barrel; // Reference to the explosive barrel

        private void Start()
        {
            main = FindObjectOfType<Kit_IngameMain>();
        }

        public override bool CanInteract(Kit_PlayerBehaviour who)
        {
            if (!isExplosionPurchased)
            {
                if (zws.localPlayerData.money >= explosionPrice)
                {
                    interactionText = "Purchase Explosion [$" + explosionPrice + "]";
                    return true;
                }
            }
            else
            {
                interactionText = "Explode";
                return true;
            }

            return false;
        }

        public override void Interact(Kit_PlayerBehaviour who)
        {
            if (!isExplosionPurchased)
            {
                if (zws.localPlayerData.money >= explosionPrice)
                {
                    zws.localPlayerData.SpendMoney(explosionPrice);
                    isExplosionPurchased = true;
                    interactionText = "Explode"; // Change interaction text to indicate explosion action
                }
            }
            else
            {
                // Trigger the explosion without causing damage when interacting
                barrel.TriggerExplosionWithoutDamage();
                
                // Simulate the damage by directly calling the DamageBarrel method
                float damage = 9999f;
                bool shotBot = false;
                int shotId = 0;

                barrel.DamageBarrel(damage, shotBot, shotId);
            }
        }
    }
}

using UnityEngine;
using Photon.Pun;
using System.Collections;

namespace MarsFPSKit
{
    public class Kit_ExplodeableBarrelExtended : MonoBehaviourPun, IKitDamageable, IPunObservable
    {
        public GameObject explosion;
        public float startHitPoints = 100f;
        private float hitPoints = 100f;
        public float decreaseHitPointsAfterDamaged = 10f;
        public ParticleSystem playWhenDamaged;
        private bool destroyedByBot;
        private int destroyedById = -1;
        private bool isExploding = false;

        void Start()
        {
            if (photonView.IsMine)
            {
                hitPoints = startHitPoints;
            }
        }

        void Update()
        {
            if (hitPoints < startHitPoints)
            {
                if (photonView.IsMine)
                {
                    if (decreaseHitPointsAfterDamaged > 0)
                    {
                        hitPoints -= Time.deltaTime * decreaseHitPointsAfterDamaged;

                        if (hitPoints <= 0)
                        {
                            StartCoroutine(RepeatExplosion());
                        }
                    }
                }

                if (playWhenDamaged)
                {
                    if (!playWhenDamaged.isPlaying)
                    {
                        playWhenDamaged.Play(true);
                    }
                }
            }
        }

        private IEnumerator RepeatExplosion()
        {
            if (!isExploding)
            {
                isExploding = true;
                int explosionCount = 15;
                for (int i = 0; i < explosionCount; i++)
                {
                    Vector3 explosionOffset = new Vector3(Random.Range(-1f, 1f), Random.Range(-1f, 1f), Random.Range(-1f, 1f));
                    GameObject go = Instantiate(explosion, transform.position + explosionOffset, transform.rotation);

                    if (go.GetComponent<Kit_Explosion>())
                    {
                        go.GetComponent<Kit_Explosion>().Explode(photonView.IsMine, destroyedByBot, destroyedById, "Barrel");
                    }

                    yield return new WaitForSeconds(1f); // Wait for 1 second before the next explosion
                }
                isExploding = false;
                PhotonNetwork.Destroy(gameObject);
            }
        }

        bool IKitDamageable.LocalDamage(float dmg, int gunID, Vector3 shotPos, Vector3 forward, float force, Vector3 hitPos, bool shotBot, int shotId)
        {
            if (photonView.Owner == null)
                photonView.RPC("DamageBarrel", RpcTarget.MasterClient, dmg, shotBot, shotId);
            else
                photonView.RPC("DamageBarrel", photonView.Owner, dmg, shotBot, shotId);

            return true;
        }

        [PunRPC]
        public void DamageBarrel(float dmg, bool shotBot, int shotId)
        {
            if (photonView.IsMine)
            {
                hitPoints -= dmg;
                destroyedByBot = shotBot;
                destroyedById = shotId;

                if (hitPoints <= 0)
                {
                    StartCoroutine(RepeatExplosion());
                }
            }
        }

        // Add a method to trigger the explosion without damage
        public void TriggerExplosionWithoutDamage()
        {
            if (!isExploding)
            {
                StartCoroutine(RepeatExplosion());
            }
        }

        void OnDestroy()
        {
            // No need for this part in the OnDestroy method
        }

        void IPunObservable.OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
        {
            if (stream.IsWriting)
            {
                stream.SendNext(hitPoints);
            }
            else
            {
                hitPoints = (float)stream.ReceiveNext();
            }
        }
    }
}

Leave a Comment