PART NINE
TOOLS
TOOLS
More Item classes
Add the following classes:
Tool (extends Item)
Bucket (extends Tool)
Hoe (extends Tool)
Set images for Bucket and Hoe.
Require Tools To Do Tasks
In the Dirt class, modify the click method so that...
You require item to be a Hoe to change dirt to soil
You require item to be a Bucket to wet the soil
CHECKPOINT
Run your code and see...
Actions can only be taken with the correct tool
Take a moment to realize how *fast* this was to do once you set up a solid framework to handle items. Building a quality, organized program allows expansion easily.
STAMINA
Stamina
In this program there isn't a specific Player object, so we'll simply manage Stamina in game alongside money.
In Game, create the following variables and methods
Variables and Constants
private static int curStamina
private static int maxStamina
public static final int BASE_STAMINA
This constant repersents the starting value for stamina; use it to intialize curStamina and maxStamina.
Methods
public static boolean hasStamina(int amount)
public static float getPercentStamina()
public static void expendStamina(int amount)
Using and Recovering Stamina
Each day, the reset curStamina to maxStamina
When planting a crop, tilling soil, or watering soil the user should expend some amount of stamina.
Rendering Stamina Bar
In Game, let's start by cleaning up the render method slightly. If you haven't already done so, move the code to render the user's money into a new method called renderMoney().
Write and call an additional method called renderStamina(). In this method, draw two rectangles that go across the top of the bottom bar.
A dark blue bar is the background for the bar and stretches across the width of the screen
A yellow bar that covers a percentage of the screen based on the percentage of stamina.
CHECKPOINT
Run your code and see...
A yellow and blue bar stretches across the bottom of the screen and changes when you expend stamina.
USER INTERFACE
Recolor Stamina Bar
An optional improvement to make your stamina bar feel more dynamic is to scale the color from GREEN to YELLOW to RED to help the user clearly see they're getting low on stamina.
To do so, you can use the percentStamina() method you already wrote, setting the Color as using percentile values:
g.setColor(new Color(1-getPercentStamina(), getPercentStamina(), 0));
Tracking Days
Create a variable and appropriate methods to track and display days for the user.
Highlighting Terrain
One way to make the game clearer and feel more responsive is to highlight which cell is currently being targeted by the mouse.
To do so, you'll want to write a few methods in Cell:
public int getXPixel() // returns the x coordinate of this cell in pixels
public int getYPixel() // returns the y coordinate of this cell in pixels
boolean isMouseOver() // returns if the mouse is over this cell
In class Cell, in the render() method
Draw something to show the cell is selected on top of the terrain but below the entity in the cell. In my example, this is a semi-transparent white rectangle, but you could could easily replace this with a different visualization (including images).
CHECKPOINT
Run your code and see...
Your program now highlights tiles the mouse is over
The number of days passed is displayed to the user
The stamina bar dynamically changes color