Track.java
/*
* Copyright © 2012 Nokia Corporation. All rights reserved.
* Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
* Oracle and Java are trademarks or registered trademarks of Oracle and/or its
* affiliates. Other product and company names mentioned herein may be trademarks
* or trade names of their respective owners.
* See LICENSE.TXT for license information.
*/
package com.nokia.example.racer.views.components;
import com.nokia.example.racer.helpers.ImageLoader;
import com.nokia.example.racer.views.tracks.LapListener;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class Track {
private Vector gates; // gates which car must pass to get a lap driven
private Image track;
private Gate currentGate = null;
private int currentGateIndex = 0;
/*
* screen relative coordinates
*/
private int x;
private int y;
private int laps = 0; // laps driven
private LapListener listener;
private int width;
private int height;
/*
* track relative coordinates for starting point of the car
*/
private int carStartX = 0;
private int carStartY = 0;
private int carStartFrame = 0;
public Track(LapListener listener) {
this.listener = listener;
gates = new Vector();
}
/**
* Loads the image at imagePath and sets it for the track
*
* @param imagePath Path to the image
*/
public void setTrack(String imagePath) {
try {
track = ImageLoader.getInstance().loadImage(imagePath);
width = track.getWidth();
height = track.getHeight();
}
catch (IOException e) {
}
}
/**
* Renders the track and its gates.
*
* @param g Graphics object
*/
public void render(Graphics g) {
g.drawImage(track, x, y, g.TOP | g.LEFT);
for (int i = 0; i < gates.size(); i++) {
((Gate) gates.elementAt(i)).render(g);
}
}
/**
* Sets position of the track relative to screen
*
* @param x x coordinate of the position
* @param y y coordinate of the position
*/
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Add gates to track. The last gate added will also be the last gate
* required to pass
*
* @param gate Gate to be added
*/
public void addGate(Gate gate) {
gates.addElement(gate);
}
/**
* Checks if the given point (car) is near enough next gate to mark it
* passed. If all gates are passed, they are all reseted (set to non-passed)
*
* @param x x coordinate of the point
* @param y y coordinate of the point
*/
public void checkGatePass(int x, int y) {
if (currentGate == null) {
currentGate = (Gate) gates.elementAt(currentGateIndex);
}
if (currentGate.isPointNear(x, y)) {
currentGate.setPassed(true);
currentGateIndex++;
if (currentGateIndex > gates.size() - 1) {
laps++;
listener.lapDriven(laps);
currentGateIndex = 0;
/*
* set the last gate passed for a moment before resetting
*/
new Thread() {
public void run() {
try {
sleep(500);
}
catch (InterruptedException e) {
}
resetGates();
}
}.start();
}
currentGate = (Gate) gates.elementAt(currentGateIndex);
}
}
/**
* Sets all gates non-passed.
*/
public void resetGates() {
for (int i = 0; i < gates.size(); i++) {
((Gate) gates.elementAt(i)).setPassed(false);
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getLaps() {
return laps;
}
public void resetLaps() {
laps = 0;
currentGate = (Gate) gates.firstElement();
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Image getTrackImage() {
return track;
}
/**
* Sets the point where car is positioned in the beginning of this track
*
* @param x x coordinate of the point
* @param y y coordinate of the point
*/
public void setCarStartPosition(int x, int y) {
this.carStartX = x;
this.carStartY = y;
}
public int getCarStartFrame() {
return carStartFrame;
}
public void setCarStartFrame(int carStartFrame) {
this.carStartFrame = carStartFrame;
}
public int getCarStartX() {
return carStartX;
}
public int getCarStartY() {
return carStartY;
}
}