Trying to make a grid-based game with dice spawning in random positions and orientations. Here's what I've got so far:
DiceSpawner in main scene:
extends Node3D
#This script will be universal in places where dice can spawn. It will first create a random timer
#and count to it with _process then generate a dice when it meets that timer
#There's probably a better way to do it but for now since it's a square map I'll
#generate coords by distance then direction with dice_where. I'll at least make
#a variable so I can change the dimensions simply for testing.
var mapsize = 6 #Mapsize is maximum distance from 0, not total width
var dice_position = Vector2(0,0) #This creates the variables we'll use to generate the dice later
var dice_orientation = Vector3(0,0,0)
func spawn_dice():
load("res://dice basic.tscn").instanciate()
func dice_where(): #this function is going to generate a dice at random coordinates
var dis_x = randi() % mapsize #Here we generate how far from center we spawn it
var dis_y = randi() % mapsize
var dir_x = randi() % 1 #This decides if we go that far positive or negative
var dir_y = randi() % 1
if dir_x == 1: #If dir is 1, we invert the corresponding variable
dis_x * -1
if dir_y == 1:
dis_y * -1
dice_position = Vector2(dis_x, dis_y) #Dice should spawn at this location
func dice_what(): #This function chooses the orientation of the dice as it spawns
var top = randi() % 5 #This decides the top number
var north = randi() % 3 #This rotates the rest of the numbers on the dice
var top_dice = [Vector3(0, north, 0), Vector3(-90, north, 0), Vector3(0, north, 90), Vector3(0, north, -90), Vector3(90, north, 0), Vector3(180, north, 0)]
north *= 90
dice_orientation = top_dice[top] #Each Vector3 in top_dice should be a different number on top, ordered numerically.
# Called when the node enters the scene tree for the first time.
func _ready():
preload("res://dice basic.tscn")
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
#We want to spawn dice every 8 to 15 seconds.
var last_dice_spawn = 0
#spawn_dice_in generates a timer to spawn the dice. Should loop when it spawns a dice.
var spawn_dice_in = randi() % 420; spawn_dice_in += 480
while last_dice_spawn <= spawn_dice_in:
last_dice_spawn += 1
if last_dice_spawn >= spawn_dice_in:
dice_where(); dice_what()
last_dice_spawn = 0; spawn_dice()
And Dice scene:
extends Node3D
# Called when the node enters the scene tree for the first time.
func _ready():
position.y = 0.5 #Offsets dice from ground, setting it as base makes rotating weird
position = dice_position
rotation = dice_orientation
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass