PART TWELVE
NATURE
HEALTH
Tracking Health
In class Entity,
Add the following variables
protected int curHealth
protected int maxHealth
Write a method takeDamage(int amount). It should:
Reduce curHealth by the set value
Make sure curHealth never drops below zero
ROCKS
Rock Class
Rocks are obstacles which block your farm, limiting the places you can plant crops. They can be destroyed, but take a lot of stamina and don't provide much value.
Create a Rock class that extends Entity
In the Rock constructor...
Set the rock as the focus
Set the image
Set the currentHealth and maxHealth (recommended: 5)
Set the value (recommended: 1)
A rock can be placed on any terrain
When clicked...
If the player has enough Stamina...
The player expends stamina.
A rock loses health.
If it is out of health
Remove it from the cell
Set the rock as expired
Player gains money equal to its value
Note: We'll update this later to require a tool.
World
If the map has a "r" in a cell
Make the terrain dirt
Place a Rock on that cell using the addEntity method
Edit your map to include some Rocks
CHECKPOINT
Run your code and see...
Rocks are being created on the map
When you click on rocks, they take damage. After a few clicks, the rock is destroyed and the player gains money
DAMAGE EFFECT
Tracking Damage Timing
We're going to add a flashing damage effect to objects when you damage them.
To start, we need to keep track of when an object has been damage and how long it's been since the last time it was hit.
In class Entity,
Add the following variables and constants
protected int damageTimer
protected boolean damaged
protected final int DAMAGE_EFFECT_DURATION
Revise takeDamage(int amount) to also
Set damageTimer to zero
Set damaged to true.
When clicked, the entity will flash a color briefly. In my program, this is about 10 frames. Experiment to find the right value!
Drawing Damage Timing
In class Entity, in the render() method...
Increment the damage timer
When the Entity is damaged and the damageTimer is less than the effect duration, set the image's color to a shade of orange/red. Otherwise, set the color to white.
To do so, use the setImageColor() method. Note that it takes floats as a parameter (percentile).
CHECKPOINT
Run your code and see...
Each time a rock takes damage it should flash for a brief period of time
TREES
Adding Classes
Create a subpackge for "tree" under plant
Create two classes
abstract class Tree (extends Plant)
class PineTree(extends Tree)
Tree
Trees can be harvested for wood, which gives the player money. We'll eventually add in the ability to plant them. Since they can grow on grass, they aren't obstacles to planting crops the way rocks are.
In the Tree constructor...
Use a parameter so the tree can start at any number of daysGrown
Set the tree as the focus
Trees only consider grass to be valid terrain
On a new day
When a tree grows, it heals all damage done to it on previous days.
As your tree grows, it should have more current and maximum health.
To do this, write a method called setHealth() that sets the tree's health based on daysGrown.
When clicked
If the player has enough Stamina...
The player expends stamina.
A tree loses health.
If it is out of health, remove it and the player gains money equal to its value times its maximum health.
Note: We'll update this later to require a tool.
Pine Tree
In the PineTree constructor
Add a parameter so the user can specify if the plant is fully grown or not
Set the maturity requirement
If the plant is fully grown, set daysGrown equal to maturity
Set the sheet
Set the pine tree's value
Call setImage() and setHealth() to initialize the tree
World
If the map has a "t" in a cell
Make the terrain grass
Place a PineTree on that cell using the addEntity method
Have your trees start at full maturity when first created on the map.
Edit your map to include some Pine Trees
CHECKPOINT
Run your code and see...
Pine Trees are being created on the map
When you click on trees, they take damage. After a few clicks, the tree is destroyed and the player gains money
PICK / AXE
Adding The Pick
Add the image for the pick from the media package
Create class Pick in the same way you created previous tools
Add the pick to the list of items in your shop
Unique Shop Items
The user won't ever need more than one pick! Let's add the ability to remove items from the shop once purchased.
In class Item, add a protected boolean variable called unique. Include an appropriate accessor method called isUnique().
In class Tool, write a constructor that sets unique to true
In class Shop, modify the keyPressed method. After buying an item, check if it is unique. If so, remove the item from your list of items.
Require The Pick
In class Rock, modify the clicked() method...
Only expend stamina and damage the rock if the user has a pick.
Repeat For Axe
Implement the same changes for the axe!
No more punching trees
CHECKPOINT
Run your code and see...
The user should be able to purchase a Pick and an Axe
Rocks can be damaged with a Pick and Trees can be damaged with an Axe.