Start a new RPG Maker XP Project:
Lets name is Terrain Gen:
Okay now that we have started our new project lets get rid of a few things that are not at all needed:
Delete the Map001 That was created automatically, then Open up the script editor and delete all scripts but Main:
We are starting fresh, there is no need for any of the pre-made RPG Maker XP Scripts.
Alright, now that we have an empty scene setup how about a new Scene_Base
I'll be providing you with my own Scene_Base, please use it and love it ;)
Code:
#================================================================================
# Scene_Base > Handles Default Scene Procedures
#================================================================================
class Scene_Base
def initialize(*args)
set_variables
create_sprites
create_windows
create_bitmaps
end
def main
# This is the Transition Into the Scene
Graphics.transition
loop do
# Update Input and Graphics
Graphics.update
Input.update
update
# This Resets The frame speed after any strong processing, but should be used all the time IMO
Graphics.frame_reset
if scene_check
break
end
end
#commonly disposed material
Graphics.freeze
# This Resets The frame speed after any strong processing, but should be used all the time IMO
Graphics.frame_reset
dispose
return
end
def set_variables
# Common Variables
end
def create_sprites
# Any Common Sprites Would go here.
end
def create_windows
# Any Common Windows would go here
end
def create_bitmaps
# Common Bitmaps would go here
end
def update
# Common Update
end
def scene_check
if $scene != self
return true
else
return false
end
end
def dispose
# Common Dispose
end
def transition_check(val=nil, dur=10, filename="", vag=40)
# This Checks for a transition from 1 scene to another
if val != nil
# Does Scene Check ! = isnt
if $scene.is_a?(val)
# Checks if you want a Visual Transition or a fade
if filename != ""
Graphics.transition(dur, filename, vag)
else
Graphics.transition
end
Graphics.freeze
end
end
return
end
end
Alright, now that we have the Base scene out of the way lets work on our Main Scene, first Here is a Zip file with all the files you will need for this tutorial:
Resource Files
un-zip all the files from the zip to your Projects Graphics > Pictures Directory like so:
After you have unzipped the files, we can now finally begin to program the initial world Chunk Generation.
---------------------------------------------------
In Main in the scripts database replace the line
Code:
$scene = Scene_Title.new
#with
$scene = Scene_MapGen.new(500, 400, 16, 16)
In Scene_Base we allowed the initialize to take as many arguments as we wanted, so we are giving it 4, we will be programming it so that you will only need 2
but I will be showing you the way using 4 so that in your own world you can have blocks larger than 16x16.
Now Create a new Class/Scene under Scene_Base called Scene_MapGen, then input this code:
Code:
#===============================================================================
# Scene_MapGen - Generates Random Terrain
# -----------------------
# Created From azrith001's Terrain Gen Tutorial
#===============================================================================
class Scene_MapGen < Scene_Base
# Initialize Scene with 2 or 4 Arguments
def initialize(*args)
super(args)
end
end
Lol, because of the Scene_Base we only need to Initialize it to start Running the Scene.
Now inside def initialize add the following lines above the line containing super(*args)
Code:
# Check Args
if args[2] != nil and args[3] != nil
# Set Block Width & Height to Args 2 and 3
@blockWidth = args[2]
@blockHeight = args[3]
else
# Set Default Block Width & Height
@blockWidth = 16
@blockHeight = 16
end
# Set Map Width and Height for args
@mapWidth = args[0]
@mapHeight = args[1]
Your code should look like this:
Code:
# Initialize Scene with 2 or 4 Arguments
def initialize(*args)
# Check Args
if args[2] != nil and args[3] != nil
# Set Block Width & Height to Args 2 and 3
@blockWidth = args[2]
@blockHeight = args[3]
else
# Set Default Block Width & Height
@blockWidth = 16
@blockHeight = 16
end
# Set Map Width and Height for args
@mapWidth = args[0]
@mapHeight = args[1]
super(args)
end
Now we have initialize our first variables lets draw the world Depth Levels This will setup Space, Sky, Surface, Underground, Core(hell).
We will use these depth levels to determine where things spawn, gravity levels, and Cave/Ore/Mountain/Water/Lava Gen.
under def initialize add this code:
Code:
def create_sprites
# Setup Layer Heights Based on Height Percent
# Set Camera
@Camera = Viewport.new(0, 0, 640, 480)
# Set Percent of Heights
layerheights = []
layerheights[0] = [@mapheight*0.05, Color.new(75, 0, 138)]
layerheights[1] = [@mapheight*0.23, Color.new(22, 196, 212)]
layerheights[2] = [@mapheight*0.20, Color.new(121, 69, 18)]
layerheights[3] = [@mapheight*0.38, Color.new(131, 131, 131)]
layerheights[4] = [@mapheight*0.14, Color.new(136, 0, 0)]
# Create Sprite
@base = Sprite.new(@Camera)
mw = @mapwidth*@blockwidth
mh = @mapheight*@blockheight
@base.bitmap = Bitmap.new(mw, mh)
y = 0
# Setup Background
for l in 0...layerheights.size
# Create Visual World Base
lh = layerheights[l][0]*@blockheight
lc = layerheights[l][1]
# increase the Y for every layer after the first
if l > 0
y += layerheights[l-1][0]
end
@base.bitmap.fill_rect(0, y*@blockheight, @mapwidth*@blockwidth, lh, lc)
end
end
In this function we will be setting up the areas of color like so:
Space:
█ - RGB: (75, 0, 138)
Sky:
█ - RGB: (22, 196, 212)
Ground:
█ - RGB: (121, 69, 18)
Underground:
█ - RGB: (131, 131, 131)
Core:
█ - RGB: (136, 0, 0)
We do this so that we can grab the pixel area for a specific color and set biomes, mountain gen, etc in that area (we dont want caves generating in the sky do we? lol
To set up the height of each area we use a percent value of the area IE:
layerheights[0] = [@mapheight*0.10, Color.new(75, 0, 138)]
set the Space area to 10% of the total height of the world.
after we do that we can call for the layerheight (l) to set its y position after the previous item.
You should be able by now to run the game, and get a screen full of color :P
Just so you can see what the rest of it looks like we need to add an Input Update to Pan, and Zoom the map.
Under def create_sprites add this code:
Code:
def update
# Update Bitmap and Camera
@base.update
@Camera.update
# Get input and move Camera
if Input.repeat?(Input::UP)
@Camera.oy -= 20
elsif Input.repeat?(Input::DOWN)
@Camera.oy += 20
elsif Input.repeat?(Input::LEFT)
@Camera.ox -= 20
elsif Input.repeat?(Input::RIGHT)
@Camera.ox += 20
elsif Input.repeat?(Input::L)
@base.zoom_x -= 0.01
@base.zoom_y -= 0.01
elsif Input.repeat?(Input::R)
@base.zoom_x += 0.01
@base.zoom_y += 0.01
elsif Input.repeat?(Input::C)
@base.zoom_x = 1.0
@base.zoom_y = 1.0
end
end
You should have something like this ( im zoomed out)
This concludes the First Tutorial, in the next tutorial we will be going into how to setup the layers of block, using the graphics I provided :)
Thanks Leave any feed back you might have or requests of things to do in future tutorials.