Class Coin

java.lang.Object
  extended by Coin

public class Coin
extends java.lang.Object

This class represents a coin the player can pick up in the car game. It increases the player's score and time. The lifetime of the coin is measured in ticks. The time and score bonus decrease by specific amounts periodically after a specified number of ticks.

Author:
Dalibor Zeleny (dalibor@cs.wisc.edu)

Field Summary
 int ID
          A unique ID of this coin.
static double LENGTH
          The length of every coin will be the same.
static int MAX_ALPHA
          Transparency (alpha value) of this coin when displayed right after its creation
static int MAX_SCORE
          A maximum score threshold for the purpose of determining display color for a coin.
static java.awt.Color MAX_SCORE_COLOR
          The color a coin should have when displayed if its score bonus is above MAX_SCORE
static int MIN_ALPHA
          Transparency (alpha value) of this coin when displayed right before its expiration.
static int MIN_SCORE
          A minimum score threshold for the purpose of determining display color for a coin.
static java.awt.Color MIN_SCORE_COLOR
          The color a coin should have when displayed if its score bonus is below MIN_SCORE
 
Constructor Summary
Coin(double timeBonus, double timeDecrease, int scoreBonus, int scoreDecrease, long lifetimeInTicks, double positionOfFront, double velocity, long decreasePeriod)
          Creates a new coin with specified properties.
 
Method Summary
 void draw(java.awt.Graphics g, int x, int y)
          This method paints the coin using the provided Graphics coin g.
 boolean equals(java.lang.Object o)
          Two coins are equal to each other if and only if their IDs are the same.
 double getPositionOfFront()
          Returns how far the coin is along the highway.
 double getScoreBonus()
          Returns the score bonus this coin grants if picked up at the time this method is called.
 long getTimeAlive()
          Returns how many ticks this coin has been alive.
 double getTimeBonus()
          Returns the time bonus this coin grants if picked up at the time this method is called.
 double getVelocity()
          Returns how fast the coin is currently moving.
 boolean isExpired()
          Indicates whether this coin's lifetime is over.
 void tick(double time)
          Updates the state of the coin.
 java.lang.String toString()
          Returns a one-line string with some meaningful easy-to-read description of this coin's properties.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ID

public int ID
A unique ID of this coin.


LENGTH

public static double LENGTH
The length of every coin will be the same.


MAX_SCORE_COLOR

public static final java.awt.Color MAX_SCORE_COLOR
The color a coin should have when displayed if its score bonus is above MAX_SCORE


MIN_SCORE_COLOR

public static final java.awt.Color MIN_SCORE_COLOR
The color a coin should have when displayed if its score bonus is below MIN_SCORE


MIN_SCORE

public static final int MIN_SCORE
A minimum score threshold for the purpose of determining display color for a coin.

See Also:
Constant Field Values

MAX_SCORE

public static final int MAX_SCORE
A maximum score threshold for the purpose of determining display color for a coin.

See Also:
Constant Field Values

MAX_ALPHA

public static final int MAX_ALPHA
Transparency (alpha value) of this coin when displayed right after its creation

See Also:
Constant Field Values

MIN_ALPHA

public static final int MIN_ALPHA
Transparency (alpha value) of this coin when displayed right before its expiration.

See Also:
Constant Field Values
Constructor Detail

Coin

public Coin(double timeBonus,
            double timeDecrease,
            int scoreBonus,
            int scoreDecrease,
            long lifetimeInTicks,
            double positionOfFront,
            double velocity,
            long decreasePeriod)
Creates a new coin with specified properties.

Parameters:
timeBonus - The initial time bonus this coin grants to the player
timeDecrease - The time bonus is decreased periodically by this amount after decreasePeriod many ticks.
scoreBonus - The initial score bonus this coin grants to the player
scoreDecrease - The score bonus is decreased periodically by this amount after decreasePeriod many ticks.
lifetimeInTicks - The number of ticks for which this coin will exist.
positionOfFront - The starting position of the front of this coin on the highway.
velocity - The velocity at which this coin is moving.
decreasePeriod - Number of ticks after which bonuses periodically decrease.
Method Detail

getTimeBonus

public double getTimeBonus()
Returns the time bonus this coin grants if picked up at the time this method is called.

Returns:
the time bonus this coin grants if picked up at the time this method is called.

getScoreBonus

public double getScoreBonus()
Returns the score bonus this coin grants if picked up at the time this method is called.

Returns:
the score bonus this coin grants if picked up at the time this method is called.

getTimeAlive

public long getTimeAlive()
Returns how many ticks this coin has been alive.

Returns:
how many ticks this coin has been alive.

getPositionOfFront

public double getPositionOfFront()
Returns how far the coin is along the highway. The returned value tells us the position of the FRONT of the coin. The position of the rear of the coin is given by the return value of this method minus the value of the LENGTH constant.

Returns:
how far along the highway the coin is.

getVelocity

public double getVelocity()
Returns how fast the coin is currently moving.

Returns:
the speed at which the coin is moving

equals

public boolean equals(java.lang.Object o)
Two coins are equal to each other if and only if their IDs are the same.

Overrides:
equals in class java.lang.Object

tick

public void tick(double time)
Updates the state of the coin. This should increase the number of ticks this coin has been alive and update its distance based on its velocity and the tick length. Time and score bonuses also get decreased if the right number of ticks since the last time they were decreased has passed.

Parameters:
time - The tick length of the car game

isExpired

public boolean isExpired()
Indicates whether this coin's lifetime is over.

Returns:
whether this coin's lifetime is over.

draw

public void draw(java.awt.Graphics g,
                 int x,
                 int y)
This method paints the coin using the provided Graphics coin g. The color of the coin is governed by the score bonus and the transparency by how long it's been alive. The color is MAX_SCORE_COLOR if the score bonus is above MAX_SCORE, MIN_SCORE_COLOR if the score bonus is below MIN_SCORE, and it's a linear interpolation between those two colors when the score bonus is between MAX_SCORE and MIN_SCORE. The transparency (alpha value of the Color coin) is a linear interpolation between MAX_ALPHA and MIN_ALPHA depending on the ratio of the amount of ticks alive and the total lifetime (in ticks).

Parameters:
g - The graphics coin where this coin should be painted.
x - The x coordinate where the REAR of the coin should be painted.
y - The y coordinate where the TOP of the coin should be painted.

toString

public java.lang.String toString()
Returns a one-line string with some meaningful easy-to-read description of this coin's properties. Here is an example. ID: 3, at distance: 955.850000000004 with score bonus 796(-12 decrease), time bonus 6.300000000000006(-0.1 decrease) and decrease period 5. It has lived 88 out of 400 ticks

Overrides:
toString in class java.lang.Object