PART ONE
TERRAIN
SETTING UP THE PROJECT
A NOTE ON YOUR NOTES
This first section will provide a lot of direct code examples to get you set up! As the project moves onward, you'll be doing a lot more on your own.
Setting Up A New Project
Copy your Project Template and build a new project, naming it FarmSim
As you set up these files, make sure that your packages are lowercase and your classes start with capital letters, as shown to the right.
At the start, most of these files will be empty except for Game and Main, which should have the code from the template.
Making Packages
We can organize our code into packages, which are like folders.
To create a package, right click on your project and select create package.
Note that to create a subpackage like terrain, you'll need to create a package called "world.terrain"
PROJECT ORGANIZATION
Composition "has-a"
Game
World
Cell (2D Array)
Terrain
Inheritance "is-a"
Terrain
Dirt
Grass
Terrain
Create protected variables called cell and color
Write a constructor that initializes color to gray
Write a setCell() method
Dirt and Grass
Each of these classes should extend class Terrain
In the constructor, initialize color to brown or green respectively
Hint: You'll need to create a custom color using RGB for brown
Cell
Create private variables called x, y, and terrain.
Create a constructor that initializes your class-level variables
Notice that we use the setTerrain() method that we are about to write! Code reuse makes things more efficient.
Write the setTerrain() method
This assigns terrain based on the parameter
It also lets the terrain know which cell it belongs to.
Make accessor methods for getX(), getY(), and getTerrain()
WATCH OUT
Did you write the accessor methods requested above? Note that not all of the code will be provided!
World
Create constants to represent WIDTH and HEIGHT of the world. These values refer to the number of cells in each dimension.
Since this is a graphical program, these names can be clearer than using rows and columns to help us visualize the positions of cells
Declare and initialize a 2D array of cells
Initialize each cell
Game
Declare a private World object named world
In the init() method, initialize world
CHECKPOINT
At this point, your code should compile. When you run it... you'll get a blank screen. Let's make things more exciting.
DISPLAYING THE WORLD
Setting Cell Size
We're going to work toward making each cell render itself. In order to do so, we'll need to know the size of each cell. We'll need to add two methods to class Cell.
At the default resolution for 1920x1080, all cells will be square and 64x64. However, we want this program to work at least "okay" on other resolutions.
This means we'll need a seperate method for width and height.
All cells will be the same size, so we can make these methods static.
Notice that in the code to the right, we subtract 128. This provides space for a bar at the bottom of the screen.
You should make this value a constant, rather than having a set number hard-coded into a random line.
Note that this will distort the ratios on other resolutions. This is okay for simplicity here - your project only needs to look good on the school computers.
The Render Chain
Write a render method in Terrain
Set the color to the color of the terrain
Draws a box based on the cell's x and y position.
Write a render method in Cell
Tells the terrain to render itself
Write a render method in the World
Tells each cell to render itself
Edit the render method in Game
Tells the world to render itself
Woah, that's a lot of work for to just draw a few squares! Why so many layers?
As this project goes on, each of these layers will be responsible for multiple jobs. Be patient, this will pay off!
Hardcoding Starting Terrain
Add code to your World constructor that sets the terrain at a few specific cells. This should provide us with something to see when we run the program.
CHECKPOINT
Run your code... at this point, we' should now be able to see
Two dirt tiles
One grass tile.
A lot of gray, undefined tiles
A black bar at the bottom of the screen