bcirpggithubmirror/Phase2/Godot ===(Game Code)===/DiceRoller/DieManager.gd

129 lines
3.6 KiB
GDScript3
Raw Permalink Normal View History

#This is the diemanager script that controls the rolling of the die/dice as well as calculates
#the end result
class_name DieManager
extends Node2D
#Array of the desired dice values to mod god wants
var desiredDice: Array
2022-07-03 16:13:15 +00:00
#User can select the percentage needed for a successful roll
var neededPercentageToPass: float
2022-07-03 16:13:15 +00:00
var validDieTypes = [4, 6, 8, 10, 12, 20]
2022-07-03 16:13:15 +00:00
#boolean for if a percentageroll is taking place
#we need a boolean for this because the way a percentage roll is calculated
#with two D10s is different than if one were using other dice
var isPercentageRoll = false
#diceUsed holds the dice objects that are rolled
var diceUsed = []
#rolledValues holds the integer value rolled from each die
var rolledValues = []
#boolean based on whether the overall roll passed or not
var passedRoll
#float holding the degree of success (rolledVal - neededPercentageToPass)
var degreeOfSuccess
#Constructor for diemanager class
func _init(dice, percent):
desiredDice = dice
neededPercentageToPass = percent
loadData()
#set values of diemanager
func setDieManager(dice, percent):
desiredDice = dice
neededPercentageToPass = percent
loadData()
#Load the diceInPlay array
func loadData():
for elem in desiredDice:
2022-07-03 16:13:15 +00:00
if elem in validDieTypes:
diceUsed.append(Die.new(elem))
2022-07-03 16:13:15 +00:00
#conditional to check if two D10s are being used
#if so, we know that a percentage roll is taking place
if len(desiredDice) == 2 && desiredDice[0] == 10 && desiredDice[1] == 10:
isPercentageRoll = true
#Resets the data in the script
func clearData():
isPercentageRoll = false
rolledValues = []
desiredDice = []
diceUsed = []
neededPercentageToPass = 0
2022-07-03 16:13:15 +00:00
#Returns the percent value of an individual die
#Stores the rolled value in rolledValues
func returnDiePercentage(inputedDie):
#In case this method is being called on no dice
if len(diceUsed) == 0:
push_error("Cannot roll without any dice!")
var rolledVal = inputedDie.rollDie()
#add rolled integer value to array
rolledValues.append(rolledVal)
2022-07-03 16:13:15 +00:00
#Checks if a percentageroll is being done
if isPercentageRoll:
#This conditional is used to detemrine if the rolled value is
#for the tens or ones digit
return float(rolledVal % 10)
return float(rolledVal) / float(inputedDie.numFaces)
#Rolls all of the dice in diceUsed
#returns the average of all the percentages
func rollDice():
#In case this method is being called on no dice
if len(diceUsed) == 0:
push_error("Cannot roll without any dice!")
#denominator will equal the total number of dice rolled
var denominator = 0
#sum of floats of all rolled die percentages
var sumOfPercentages = 0
if isPercentageRoll:
sumOfPercentages += (returnDiePercentage(diceUsed[0]) / 10.0) + (returnDiePercentage(diceUsed[1]) / 100.0)
else:
for die in diceUsed:
sumOfPercentages += returnDiePercentage(die)
denominator += 1
var result = []
result.append(rolledValues)
2022-07-03 16:13:15 +00:00
if isPercentageRoll:
#Percentage roll result remains the sum of the rolls
result.append(sumOfPercentages)
2022-07-03 16:13:15 +00:00
else:
if denominator == 0:
result.append(0)
#result is average of sum of percentages otherwise rounded to 2 decimcal places
result.append(stepify((float(sumOfPercentages) / float(denominator)), 0.0001))
passedRoll = (result[1] >= neededPercentageToPass)
#NOTE: degree of success is always calculated regardlesss of success/failure. Let me know if this should be changed
degreeOfSuccess = result[1] - neededPercentageToPass
result.append(passedRoll)
result.append(neededPercentageToPass)
result.append(degreeOfSuccess)
result.append(desiredDice)
#rollDice result: [[rolledValues], percentRolled, passResult, neededPercent, degreeOfSuccess, dice]
return result