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).
This commit is contained in:
Andrew Tang 2022-06-27 01:40:01 +08:00
parent 2cb9448f73
commit e338cf1c47
13 changed files with 340 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

BIN
Phase2/.DS_Store vendored Normal file

Binary file not shown.

BIN
Phase2/Godot ===(Game Code)===/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,3 @@
source_md5="47313fa4c47a9963fddd764e1ec6e4a8"
dest_md5="26ea799ea0a3da9e753b3ebe822e0570"

View File

@ -0,0 +1,97 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://DieManager.gd" type="Script" id=1]
[ext_resource path="res://Die.gd" type="Script" id=2]
[node name="DieManager" type="Control"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
[node name="ColorRect" type="ColorRect" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
color = Color( 0, 0, 0, 1 )
[node name="Die" type="Button" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -95.0
margin_top = 2.0
margin_right = 92.0
margin_bottom = 91.0
text = "Roll Die/Dice"
script = ExtResource( 2 )
[node name="RolledValues" type="Label" parent="."]
visible = false
anchor_left = 0.5
anchor_right = 0.5
margin_left = -299.0
margin_top = 6.0
margin_right = 297.0
margin_bottom = 86.0
text = "Rolled Values:"
align = 1
autowrap = true
[node name="Outcome" type="Label" parent="."]
visible = false
anchor_left = 0.5
anchor_right = 0.5
margin_left = -296.0
margin_top = 173.0
margin_right = 300.0
margin_bottom = 253.0
text = "Success!
"
align = 1
autowrap = true
[node name="PercentNeeded" type="Label" parent="."]
visible = false
anchor_left = 0.5
anchor_right = 0.5
margin_left = -286.0
margin_top = 210.0
margin_right = 310.0
margin_bottom = 290.0
text = "Percent Needed to Pass: 0%"
align = 1
autowrap = true
[node name="PercentRolled" type="Label" parent="."]
visible = false
anchor_left = 0.5
anchor_right = 0.5
margin_left = -293.0
margin_top = 241.0
margin_right = 303.0
margin_bottom = 321.0
text = "Percent Rolled: 0%"
align = 1
autowrap = true
[node name="DegreeOfSuccess" type="Label" parent="."]
visible = false
anchor_left = 0.5
anchor_right = 0.5
margin_left = -295.0
margin_top = 272.0
margin_right = 301.0
margin_bottom = 352.0
text = "Degree of Success: 0%"
align = 1
autowrap = true
[node name="Reset" type="Button" parent="."]
margin_left = 457.0
margin_top = 421.0
margin_right = 567.0
margin_bottom = 469.0
text = "Reset"
[connection signal="button_down" from="Die" to="." method="_on_Die_button_down"]
[connection signal="pressed" from="Reset" to="." method="_on_Reset_pressed"]

View File

@ -0,0 +1,38 @@
#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

View File

@ -0,0 +1,126 @@
#This is the diemanager script that controls the rolling of the die/dice as well as calculates
#the end result
extends Control
export var desiredDice = [20, 8]
#diceUsed holds the dice objects that are rolled
var diceUsed = []
#diceInPlay holds the die objects as well.
#This and diceUsed are essentially the same arrays (holding the dice objects), but I had to use
#two arrays due to the parameters in returnDiePercentage
var diceInPlay = []
#rolledValues holds the integer value rolled from each die
var rolledValues = []
#User can select the percentage needed for a successful roll
export var neededPercentageToPass = .2
#boolean based on whether the overall roll passed or not
var passedRoll
#float holding the degree of success (rolledVal - neededPercentageToPass)
var degreeOfSuccess
#Load the diceInPlay array
func _ready():
for elem in desiredDice:
diceInPlay.append(Die.new(elem))
#This is the function that returns the percentage of a rolled die (rolled val / total sides)
#The UML showed that this function only had one parameter, "inputedDie," so I assumed that
#was the rolled integer value. However, in order to find the percetage we need the total sides as well
#As a result, I would get the total sides by ensuring that the inputedDie corresponds to the die at index 0
#of the diceInPlay array.
func returnDiePercentage(inputedDie):
var totalSides = diceInPlay[0].numFaces
#ensuring we don't lose the die objects in memory by storing them in another array
diceUsed.append(diceInPlay[0])
diceInPlay.remove(0)
var result = float(inputedDie) / float(totalSides)
return result
#Function to show all of the results on screen
func showOnUI(passResult, percentRolled) -> void:
if passResult:
$Outcome.text = "Successful Roll!"
else:
$Outcome.text = "Failed Roll!"
var diceResultText = "Rolled Values:\n"
#Prints the integer calues of each die rolled in the form: "D(num faces): (value rolled)"
for i in range(diceUsed.size()):
diceResultText += ("D" + str(diceUsed[i].numFaces) + ": " + str(rolledValues[i]) + "\n")
#NOTE: degree of success is always calculated regardlesss of success/failure. Let me know if this should be changed
degreeOfSuccess = percentRolled - neededPercentageToPass
#changing labels on screen
$RolledValues.text = diceResultText
$PercentNeeded.text = "Percent Needed to Pass: " + str(neededPercentageToPass * 100) + "%"
$PercentRolled.text = "Percent Rolled: " + str(percentRolled * 100) + "%"
$DegreeOfSuccess.text = "Degree of Success: " + str(degreeOfSuccess * 100) + "%"
#revealing labels to user
$Outcome.show()
$RolledValues.show()
$PercentNeeded.show()
$PercentRolled.show()
$DegreeOfSuccess.show()
#function for when "roll" button is clicked
func _on_Die_button_down():
#denominator will equal the total number of dice rolled
var denominator = 0
#sum of floats of all rolled die percentages
var sumOfPercentages = 0
#stores the rolled integer value of die in loop
var rolledVal
#remember that the size of diceInPlay decrements by 1 each time returnDiePercentage is called
while diceInPlay.size() > 0:
rolledVal = diceInPlay[0].rollDie()
#add rolled integer vaslue to array
rolledValues.append(rolledVal)
#add percentage to sum
sumOfPercentages += returnDiePercentage(rolledVal)
#increment denominator
denominator += 1
#result is average of sum of percentages
var result = sumOfPercentages / denominator
passedRoll = (result >= neededPercentageToPass)
showOnUI(passedRoll, result)
return result
#Reset stored values and text
func _on_Reset_pressed():
$Outcome.hide()
$PercentNeeded.hide()
$PercentRolled.hide()
$DegreeOfSuccess.hide()
$RolledValues.hide()
#Fill diceInPlay again and empty diceUsed
for die in diceUsed:
diceInPlay.append(die)
diceUsed = []
rolledValues = []

View File

@ -0,0 +1,7 @@
[gd_resource type="Environment" load_steps=2 format=2]
[sub_resource type="ProceduralSky" id=1]
[resource]
background_mode = 2
background_sky = SubResource( 1 )

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

View File

@ -0,0 +1,35 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://icon.png"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
process/normal_map_invert_y=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@ -0,0 +1,33 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=4
_global_script_classes=[ {
"base": "Button",
"class": "Die",
"language": "GDScript",
"path": "res://Die.gd"
} ]
_global_script_class_icons={
"Die": ""
}
[application]
config/name="GodotDiceRoller_Andrew"
run/main_scene="res://DiceRoller.tscn"
config/icon="res://icon.png"
[physics]
common/enable_pause_aware_picking=true
[rendering]
environment/default_environment="res://default_env.tres"