bcirpggithubmirror/Phase2/Godot ===(Game Code)===/DiceRoller/Die.gd
Andrew Tang e338cf1c47 Initial version of diceroller
Built a simple UI to test out the dice roller. Can handle multiple die and supports D4, D6, D8, D10, D12, and D20 (Let me know if there shouldn't be a restriction on the valid types of die).
2022-06-27 01:40:01 +08:00

39 lines
815 B
GDScript

#Die class
extends Button
class_name Die
#array of valid number of faces
var dieTypes = [4, 6, 8, 10, 12, 20]
#value of selected die type
var numFaces: int
#bool will be used to check if a valid die type was chosen
var validNumFaces = false
#Class constructor
func _init(value):
numFaces = value
#returns an integer value of the rolled result (assuming the die is a valid type)
func rollDie():
randomize()
#check if selected die type exists in dieTypes array
for value in dieTypes:
if value == numFaces:
validNumFaces = true
#if the selected die type exists, we can roll an integer value based on the number of sides.
if validNumFaces:
var rolledNum
rolledNum = randi() % numFaces + 1
return rolledNum
else:
print("You do not have a valid number of sides on the die!")
return