Track.java
/*
* Copyright © 2011 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;
private Image track;
private Image debugTrack;
private Gate currentGate = null;
private int currentGateIndex = 0;
private int x;
private int y;
private int start_x;
private int start_y;
private int laps = 0;
private int width;
private int height;
public static boolean debug = false;
private LapListener listener;
private int carXPosition = 0;
private int carYPosition = 0;
private int carStartFrame = 0;
public Track(LapListener listener) {
this.listener = listener;
gates = new Vector();
}
public void setTrack(String image_path) {
try {
track = ImageLoader.getInstance().loadImage(image_path);
width = track.getWidth();
height = track.getHeight();
if(debug) {
debugTrack = Image.createImage(width, height);
debugTrack.getGraphics().drawImage(track, 0, 0, Graphics.LEFT|Graphics.TOP);
}
}catch(IOException e) {
System.out.println("unable to load image IOerror: " + e.getMessage());
}
}
public void render(Graphics g) {
if (debug) {
g.drawImage(debugTrack, x, y, g.TOP | g.LEFT);
g.drawString("currentGate:" + currentGateIndex, 0, 200, g.LEFT | g.TOP);
if (currentGate != null) {
currentGate.render(debugTrack.getGraphics());
}
} else {
g.drawImage(track, x, y, g.TOP | g.LEFT);
}
}
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
* @param gate
*/
public void addGate(Gate gate) {
gates.addElement(gate);
}
public void checkGatePass(int x, int y) {
if(currentGate == null) {
currentGate = (Gate) gates.elementAt(currentGateIndex);
}
if(currentGate.isPointNear(x, y)) {
currentGateIndex++;
if(currentGateIndex > gates.size() -1) {
laps++;
listener.lapDriven(laps);
currentGateIndex = 0;
}
currentGate = (Gate) gates.elementAt(currentGateIndex);
}
}
public int getLaps() {
return laps;
}
public void resetLaps() {
laps = 0;
}
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;
}
public int getCarXPosition() {
return carXPosition;
}
public int getCarYPosition() {
return carYPosition;
}
/**
* Set car starting position relative to screen
* @param x
* @param y
*/
public void setCarPosition(int x, int y) {
this.carXPosition = x;
this.carYPosition = y;
}
public int getCarStartFrame() {
return carStartFrame;
}
public void setCarStartFrame(int carStartFrame) {
this.carStartFrame = carStartFrame;
}
public int getStartX() {
return start_x;
}
public int getStartY() {
return start_y;
}
public void setStartPosition(int x, int y) {
start_x = x;
start_y = y;
}
public void resetPosition() {
x = start_x;
y = start_y;
}
}