Level Generator:
extends Position2D
signal slip
# this is the space in between each block the block's sprite is 8 pixels
export(int) var grid_size = 8
# get the blocks to preload so we can instance them later
export(PackedScene) var block = preload("res://Scenes/Block.tscn")
export(PackedScene) var block_bottom = preload("res://Scenes/Block_Bottom.tscn")
export(PackedScene) var ice_block = preload("res://Scenes/Ice_Block.tscn")
export(PackedScene) var ice_block_bottom = preload("res://Scenes/Ice_Block_Bottom.tscn")
export(PackedScene) var finish = preload("res://Scenes/FinishLine.tscn")
# the number of blocks that you want to be generated (10 for testing, 300 for game (can be done in 1min apx))
export(int) var max_block_number = 100
# this variable is just the current amount of blocks that currently exits
var current_block_number = 0
# these variables are used to limit how high up and down the blocks can be created (60, 20 originally)
export(int) var max_position_limit = 100
export(int) var min_position_limit = 25
# misc variables
var rng = RandomNumberGenerator.new()
var rng2 = RandomNumberGenerator.new()
var dirt_on
var ice_on
# use a custom signal to tell the world scene to create blocks
# signals are usefull when instancing the block outside of this current scene
# this signal takes two arguments the block we want to create, and the location of the block
signal create_block(block, location)
func _ready():
rng2.randomize()
var which = rng2.randi_range(0,20)
print(which)
if which >= 3:
dirt_on = true
else:
ice_on = true
Events.emit_signal("slip")
func _process(delta):
# on/off switch for generator
if dirt_on == true:
#randomize the rng seed
rng.randomize()
# make the level generator continually move to the grid size
global_position.x += grid_size
# this action variable will decide through random numbers
# where the generator is going to move up or down (to create hills)
# this is where our "procedural" content comes in
# this project does not use noise textures
#var action = round(rand_range(0, 20)) (original code)
var action = rng.randi_range(0, 20)
# this action variable affects if it moves up
# change these values to make the terrain more flat or rough
if action == 0 and current_block_number >= 10:
global_position.x += grid_size
elif action >= 1 and action < 4:
# check to see if the position y is greater the minimum position limit
if global_position.y > min_position_limit:
global_position.y -= grid_size
# else move the position down a block
else:
global_position.y += grid_size
# or if it moves down
elif action >= 4 and action < 9:
# check to see if the position y is less than the maximum position limit
if global_position.y < max_position_limit:
global_position.y += grid_size
# else move the position up a block
else:
global_position.y -= grid_size
# create the blocks by emiting the signal which is caught by the world
# we set the block (argument 0) to the block that we want to create
# the block that we are creating is specified at the variable towards the top
# then we set the location (argument 1) to the generator's position
emit_signal("create_block", block, global_position)
# this creates another block below the one on top
emit_signal("create_block", block_bottom, Vector2(global_position.x, global_position.y + grid_size))
# add to the current block number
current_block_number += 1
# check if the current block number meets the max block number
if current_block_number >= max_block_number:
# adds the finish line to the end of the map
emit_signal("create_block", finish, global_position)
emit_signal("create_block", block_bottom, Vector2(global_position.x, global_position.y + grid_size))
# turns the generator off
dirt_on = false