Skip to content
Snippets Groups Projects

Add: The user can now click on the die to roll

Merged Filip Jakobsson requested to merge FD-026 into main
1 file
+ 18
3
Compare changes
  • Side-by-side
  • Inline
+ 18
3
@@ -2,7 +2,7 @@ using Godot;
using System;
/// <summary>
/// This script is used to simulate a dice roll. It will display a random number between 1 and 6 when the player presses "R".
/// This script is used to simulate a dice roll. It will display a random number between 1 and 6 when the player presses "R" or clicks the dice.
/// This value is then displayed on the dice sprite.
/// The value can be sent to antoher script by using the "currentValue" variable in rollDice().
/// </summary>
@@ -34,17 +34,32 @@ public partial class Dice : Control
UpdateDiceDisplay();
}
public override void _Input(InputEvent @event)
{
// If the player presses "R", roll the dice, will probably change to an button later.
// Roll dice when the player presses the "R" key
if (@event.IsActionPressed("ui_roll_dice") && !HasRolled)
{
RollDice();
}
// Roll the dice when the player clicks on it
if (@event is InputEventMouseButton mouseEvent && mouseEvent.Pressed)
{
if (mouseEvent.ButtonIndex == MouseButton.Left) // Check for left-click
{
// Convert global mouse position to local position relative to the sprite
Vector2 localPosition = sprite.ToLocal(mouseEvent.Position);
// Check if the mouse is over the sprite
if (sprite.GetRect().HasPoint(localPosition) && !HasRolled)
{
RollDice();
}
}
}
}
private async void RollDice()
{
if (rolling)
Loading