/// <summary> /// Should be called to initialize the projectile. /// </summary> public virtual void Initialize(BehaviourTrigger OnTriggerCallback, int playerID, BehaviourWeight settings, WeaponBehaviour.WeaponStats stats) { OnTriggerBehaviour = OnTriggerCallback; PlayerID = playerID; BehaviourSettings = settings; Stats = stats; }
/// <summary> /// Initializes the behaviour. /// Call in recursive function to initialize a behaviour chain. /// </summary> /// <param name="playerID">ID of the player holding the weapon this behaviour will be attached to.</param> /// <param name="behaviourSettings">The settings for this.</param> /// <param name="nextBehaviour">The next behaviour in the behaviour chain.</param> public WeaponBehaviour(int playerID, BehaviourWeight behaviourSettings, WeaponBehaviour nextBehaviour) { _playerID = playerID; _behaviourSettings = behaviourSettings; _nextBehaviour = nextBehaviour; // Finish initialization by passing this behaviour to the next one in the chain. if (_nextBehaviour != null) { _nextBehaviour._previousBehaviour = this; } }
public GrenadeBehaviour(int playerID, BehaviourWeight behaviourSettings, WeaponBehaviour nextBehaviour) : base(playerID, behaviourSettings, nextBehaviour) { // Calculate damage of bullets spawned by grenade. childProjectileDamage = Stats._projectileDamage / (Settings.LerpWeightInt() / 2); // Reduce damage of projectiles spawned from explosion. if (NextBehaviour != null) { NextBehaviour.Stats.SetProjectileSpeed(childProjectileDamage); } // Get default projectile type to spawn from explosion from settings. if (behaviourSettings.GetType() == typeof(GrenadeWeight)) { _spawnedProjectileType = ((GrenadeWeight)behaviourSettings).spawnedProjectileType; } }
/// <summary> /// Generates a new behaviour chain from a queue of selected behaviours. /// </summary> private WeaponBehaviour GenerateBehaviourChain(int playerID, Queue <BehaviourWeight> selectedBehaviours) { // Grab the behaviour to generate in this loop. BehaviourWeight behaviourSettings = selectedBehaviours.Dequeue(); // Check which type of behaviour we are generating. // This is used to instantiate a new behaviour of that type. Type behaviourType = behaviourSettings.behaviourScript.GetType(); // Check if there are any behaviours left to generate after the current one. if (selectedBehaviours.Count > 0) { // Generate behaviour and move to the next behaviour in the chain. return((WeaponBehaviour)Activator.CreateInstance(behaviourType, playerID, behaviourSettings, GenerateBehaviourChain(playerID, selectedBehaviours))); } else { // Generate the final behaviour in the chain. return((WeaponBehaviour)Activator.CreateInstance(behaviourType, playerID, behaviourSettings, null)); } }
// Update UI elements. private void OnValidate() { // Adjust some UI elements if variable type is set. if (weightedVariableType != null) { // Set names to weighted variable name. GetComponent <Text>().text = weightedVariableType.variableName + ":"; name = weightedVariableType.variableName; // Make sure threshold marker is not null before checking if we want to // do anything with it. if (thresholdIndicator != null) { System.Type type = weightedVariableType.GetType(); // If this is a behaviour, set the threshold marker. if (type == typeof(BehaviourWeight) || type == typeof(GrenadeWeight)) { // Show threshold indicator. thresholdIndicator.gameObject.SetActive(true); // Cast to BehaviourWeight so we can grab threshold value. BehaviourWeight behaviourType = ((BehaviourWeight)weightedVariableType); // Calculate threshold indicator position (from 0 to 1). float indicatorXCoord = behaviourType.thresholdWeight / behaviourType.maxedWeight; // Place slider at correct position. thresholdIndicator.rectTransform.anchoredPosition = new Vector2 ( indicatorXCoord, thresholdIndicator.rectTransform.anchoredPosition.y ); } else { // Not a behaviour, so hide threshold indicator. thresholdIndicator.gameObject.SetActive(false); } } } }
public LaserBehaviour(int playerID, BehaviourWeight behaviourSettings, WeaponBehaviour nextBehaviour) : base(playerID, behaviourSettings, nextBehaviour) { }
public override void Initialize(BehaviourTrigger OnTriggerCallback, int playerID, BehaviourWeight settings, WeaponBehaviour.WeaponStats stats) { base.Initialize(OnTriggerCallback, playerID, settings, stats); width = BehaviourSettings.LerpWeight(); Activate(); }
private Queue <BehaviourWeight> CalculateBehaviours(LocalPlayerController player) { // Queue of selected behaviour names. Queue <string> selectedBehaviourNames = new Queue <string>(); Queue <BehaviourWeight> selectedBehaviours = new Queue <BehaviourWeight>(); // Total weight to distribute at the start of generating behaviours. float totalWeight = currentBaseWeaponWeight * player.weaponWeightScalar; Debug.Log("Player #" + player.playerIndex + " - Behaviours Weight: " + totalWeight); int increments = 0; // Get a list of all behaviours within the player's weight range. List <BehaviourWeight> remainingBehaviours = GetListOfPossibleBehaviours(totalWeight); // Loop until all behaviours are maxed out or until totalWeight is depleted. while (remainingBehaviours.Count > 0 && totalWeight > 0) { // Get a random behaviour to assign weights to. BehaviourWeight currentBehaviour = (BehaviourWeight)GetBiasedVariable(remainingBehaviours.ToArray()); float weightForValue; // If the weight is too low to distribute with random, just assign the remaining weight. if (totalWeight > 0.3f) { // Assign random weight. // TODO: experiment with different dividers here. weightForValue = UnityEngine.Random.Range(0, totalWeight / 2); } else { // Assign left over value. weightForValue = totalWeight; // Divide leftover weight in unenqueued behaviours over enqueued behaviours, // to prevent having a bunch of weight sitting unused in unenqueued behaviours. for (int i = remainingBehaviours.Count - 1; i > -1; i--) { if (!remainingBehaviours[i].enqueued) { totalWeight += remainingBehaviours[i].currentValue; remainingBehaviours[i].currentValue = 0; remainingBehaviours.Remove(remainingBehaviours[i]); } } // If we removed all behaviours, redo loop with remaining behaviours // that could reach their threshold. if (remainingBehaviours.Count == 0) { remainingBehaviours = GetListOfPossibleBehaviours(totalWeight); } } // If this is an integer value, round to top to prevent getting stuck. if (currentBehaviour.isIntegerValue) { weightForValue = Mathf.Ceil(weightForValue); } // Assign the new weight to the dictionary entry to link later to the behaviour. currentBehaviour.currentValue += weightForValue; // When the value is higher than its max. if (currentBehaviour.currentValue > currentBehaviour.maxedWeight) { // Subtract the difference so it can be spent on other variables. float difference = currentBehaviour.currentValue - currentBehaviour.maxedWeight; weightForValue -= difference; // Clamp to max value. currentBehaviour.currentValue = currentBehaviour.maxedWeight; // Remove the current selected value from the remaining values so it won't be selected again. remainingBehaviours.Remove(currentBehaviour); } // Adjust the total weight left to distribute. totalWeight -= weightForValue; // Make sure the current behaviour isn't already enqueued. if (!currentBehaviour.enqueued) { // If the current behaviour has reached its threshold value, enqueue it // in the queue of selected behaviours. if (currentBehaviour.currentValue >= currentBehaviour.thresholdWeight) { selectedBehaviourNames.Enqueue(currentBehaviour.variableName); currentBehaviour.enqueued = true; } } if (increments > 1000) { Debug.LogError("Something went terribly wrong, leading to far too many loops in CalculateBehaviours()."); break; } increments++; } Debug.Log("Finished behaviour distribution after: " + increments + " increments"); // Generate queue of actual behaviours from the queue of behaviour names. while (selectedBehaviourNames.Count > 0) { selectedBehaviours.Enqueue(weaponBehaviourDict[selectedBehaviourNames.Dequeue()]); } // Return queue of selected behaviours. return(selectedBehaviours); }