Random Level Generator For Squidio

05 Feb, 2018 - 00:00:00

By

Matthew Deig

Tags:

Code

    function new_world ()
        obs = 0
        level_num_obs = level_num_obs + level_mul
        if new_game == 1 then
            starting_pixels = 350
            while obs <= level_num_obs do

                world_build_check = math.random() * 100

                if ( ( world_build_check - 40 ) < 0 ) then
                    new_block_speed = math.random(-1 * block.max_velocity, block.max_velocity)
                    if ( new_block_speed < 0 and new_block_speed > -1 * block.min_velocity ) then
                        new_block_speed = math.random( -1 * block.max_velocity, -1 * block.min_velocity)
                    elseif ( new_block_speed > 0 and new_block_speed < block.min_velocity ) then
                        new_block_speed = math.random( block.min_velocity, block.max_velocity )
                    else
                        new_block_speed = block.min_velocity
                    end
                    table.insert ( world, { ['name'] = 'block', ['x'] = math.random(block.width, love.graphics.getWidth() - block.width),
                                            ['y'] = starting_pixels, ['width'] = block.width, ['height'] = block.height, ['velocity'] = new_block_speed,
                                            ['sprite'] = block.sprite} )
                    obs = obs + 1
                elseif ( ( world_build_check - 50 ) < 0 ) then
                    world_build_check = math.random() * 100
                    if ( ( world_build_check - 30 ) < 0 ) then
                        table.insert ( world, { ['name'] = 'left_stream', ['x'] = love.graphics.getWidth() - left_stream.width,
                                                ['y'] = starting_pixels, ['width'] = left_stream.width, ['height'] = left_stream.height, ['velocity'] = math.random(left_stream.min_force, left_stream.max_force),
                                                ['sprite'] = left_stream.sprite} )
                    else
                        table.insert ( world, { ['name'] = 'right_stream', ['x'] = 0,
                                                ['y'] = starting_pixels, ['width'] = right_stream.width, ['height'] = right_stream.height, ['velocity'] = math.random(right_stream.min_force, right_stream.max_force),
                                                ['sprite'] = right_stream.sprite} )
                    end
                    obs = obs + 1
                end

                    starting_pixels = starting_pixels - 62
            end

            starting_pixels = starting_pixels - 124
            table.insert ( world, { ['name'] = 'goal', ['x'] = goal.width,
                                            ['y'] = starting_pixels, ['width'] = goal.width, ['height'] = goal.height, ['velocity'] = 0,
                                            ['sprite'] = goal.sprite} )
            table.insert ( world, { ['name'] = 'goal2', ['x'] = love.graphics.getWidth() - goal.width,
                                            ['y'] = starting_pixels, ['width'] = goal.width, ['height'] = goal.height, ['velocity'] = 0,
                                            ['sprite'] = goal.sprite} )
        else
            starting_pixels = -62
            while obs <= level_num_obs do

                world_build_check = math.random() * 100
                if ( ( world_build_check - 40 ) < 0 ) then
                    new_block_speed = math.random(-1 * block.max_velocity, block.max_velocity)
                    if ( new_block_speed < 0 and new_block_speed > -1 * block.min_velocity ) then
                        new_block_speed = math.random( -1 * block.max_velocity, -1 * block.min_velocity)
                    elseif ( new_block_speed > 0 and new_block_speed < block.min_velocity ) then
                        new_block_speed = math.random( block.min_velocity, block.max_velocity )
                    else
                        new_block_speed = block.min_velocity
                    end


                    table.insert ( world, { ['name'] = 'block', ['x'] = math.random(block.width, love.graphics.getWidth() - block.width),
                                            ['y'] = starting_pixels, ['width'] = block.width, ['height'] = block.height, ['velocity'] = new_block_speed,
                                            ['sprite'] = block.sprite} )
                    obs = obs + 1
                elseif ( ( world_build_check - 50 ) < 0 ) then
                    world_build_check = math.random() * 100
                    if ( ( world_build_check - 30 ) < 0 ) then
                        table.insert ( world, { ['name'] = 'left_stream', ['x'] = love.graphics.getWidth() - left_stream.width,
                                                ['y'] = starting_pixels, ['width'] = left_stream.width, ['height'] = left_stream.height, ['velocity'] = math.random(left_stream.min_force, left_stream.max_force),
                                                ['sprite'] = left_stream.sprite} )
                    else
                        table.insert ( world, { ['name'] = 'right_stream', ['x'] = 0,
                                                ['y'] = starting_pixels, ['width'] = right_stream.width, ['height'] = right_stream.height, ['velocity'] = math.random(right_stream.min_force, right_stream.max_force),
                                                ['sprite'] = right_stream.sprite} )
                    end
                    obs = obs + 1

                end
                starting_pixels = starting_pixels - 62

            end

            starting_pixels = starting_pixels - 124
            table.insert ( world, { ['name'] = 'goal', ['x'] = goal.width,
                                            ['y'] = starting_pixels, ['width'] = goal.width, ['height'] = goal.height, ['velocity'] = 0,
                                            ['sprite'] = goal.sprite} )
            table.insert ( world, { ['name'] = 'goal2', ['x'] = love.graphics.getWidth() - goal.width,
                                            ['y'] = starting_pixels, ['width'] = goal.width, ['height'] = goal.height, ['velocity'] = 0,
                                            ['sprite'] = goal.sprite} )

        end


    end

What does it do

This function for the game will generate new table objects for the level after the goal marker shows up on the screen. I wanted it to be a weighted random so I just did some random check and minus out the chance of it happening. Some of my older version used a range check on the random number, but the results were not desired of what I wanted.

It places the hit object, stream object, and the goal object. The player is spawn at the bottom of the screen. Then I take the starting pixel which is roughly the middle of the screen and go up the screen until I hit the maxium of objects for the level.

I feel the function is a little wielding but it got the job done.