71 lines
1.7 KiB
GDScript3
71 lines
1.7 KiB
GDScript3
|
extends Spatial
|
||
|
|
||
|
#var dice
|
||
|
var dices
|
||
|
var btn_roll
|
||
|
var lbl_debug
|
||
|
|
||
|
# Called when the node enters the scene tree for the first time.
|
||
|
func _ready():
|
||
|
dices = find_node("Dices")
|
||
|
# dice = find_node("Dice")
|
||
|
_init_roll_button()
|
||
|
_init_debug_labels()
|
||
|
|
||
|
func _init_roll_button():
|
||
|
btn_roll = Button.new()
|
||
|
btn_roll.text = "Roll"
|
||
|
btn_roll.connect("pressed", self, "_button_pressed")
|
||
|
add_child(btn_roll)
|
||
|
|
||
|
func _button_pressed():
|
||
|
print("applying force on dice")
|
||
|
for dice in dices.get_children():
|
||
|
if !dice.sleeping:
|
||
|
dice.apply_central_impulse(Vector3(0,50,0))
|
||
|
dice.apply_torque_impulse(Vector3(3,1,3))
|
||
|
|
||
|
func _process(_delta):
|
||
|
_update_debug_info()
|
||
|
for dice in dices.get_children():
|
||
|
if dice.global_transform.origin.y > 1:
|
||
|
dice.global_transform.origin.y = 1
|
||
|
|
||
|
## Debug
|
||
|
func _init_debug_labels():
|
||
|
lbl_debug = Label.new()
|
||
|
lbl_debug.text = "init"
|
||
|
lbl_debug.set_position(Vector2(10,50))
|
||
|
add_child(lbl_debug)
|
||
|
|
||
|
func _update_debug_info():
|
||
|
var debug_text = "Scores:\n"
|
||
|
var count = 1
|
||
|
var sum = 0
|
||
|
for dice in dices.get_children():
|
||
|
var result = _get_score_of_dice(dice)
|
||
|
sum += int(result)
|
||
|
debug_text += str("\n Dice ",count,": ",result)
|
||
|
count += 1
|
||
|
debug_text += str("\n Total Score: ",sum)
|
||
|
lbl_debug.text = str(debug_text)
|
||
|
|
||
|
func _get_score_of_dice(dice):
|
||
|
var dice_vector = dice.global_transform.basis.get_euler()
|
||
|
var x = stepify(dice_vector.x, 2)
|
||
|
var z = stepify(dice_vector.z, 2)
|
||
|
var current_result = ""
|
||
|
if x == 0 && z == 0:
|
||
|
current_result = "1"
|
||
|
elif x == 0 && z == 2:
|
||
|
current_result = "2"
|
||
|
elif x == 0 && (z == -4 || z == 4):
|
||
|
current_result = "3"
|
||
|
elif x == 0 && z == -2:
|
||
|
current_result = "4"
|
||
|
elif x == -2:
|
||
|
current_result = "5"
|
||
|
elif x == 2:
|
||
|
current_result = "6"
|
||
|
return current_result
|