Documentation for the Unity C# Library
Loading...
Searching...
No Matches
BreakableInteractable.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using UnityEngine;
6
7namespace PixoVR.Event
8{
13 public class BreakableInteractable<TBreakType> : InteractableClass where TBreakType : Enum
14 {
15 [SerializeField] private List<Break<TBreakType>> _breaks;
16
17 protected TBreakType _currentBreakdownType;
19 private void Start()
20 {
22 }
23
24 protected List<Break<TBreakType>> Breaks
25 {
26 get => _breaks;
27 set => _breaks = value;
28 }
29
30 public override void GenerateBreakdown(int percentage)
31 {
32 base.GenerateBreakdown(percentage);
34 if (IsBroken)
35 {
36 var maxValue = Breaks.Sum(t => t.percentChance);
37 var random = UnityEngine.Random.Range(0, maxValue+1);
38 int lowerLimit = -1;
39 int upperBorder = 0;
40
41 for (var i = 0; i < Breaks.Count; i++)
42 {
43 var breakItem = Breaks[i];
44 upperBorder += breakItem.percentChance;
45
46 breakItem.IsActive = lowerLimit < random && upperBorder >= random;
47
48 if (breakItem.IsActive)
49 {
50 BrokenId = i;
51 _currentBreakdownType = (TBreakType)Enum.ToObject(typeof(TBreakType), BrokenId);
52 return;
53 }
54
55 lowerLimit = upperBorder;
56 }
57 }
58 }
59 }
60}
Base type for objects with different types of breakdowns.
override void GenerateBreakdown(int percentage)
Generate a breakdown on the object with a certain probability.
List< Break< TBreakType > > _breaks
[SerializeField]
s Base class for interactable objects with which the step manager interacts. It also stores a set of ...