Gate.java

/**
* Copyright (c) 2012-2013 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 java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

public class Gate {

    private Track track;
    private static Sprite lightSprite;  // sprite indicating if the gate is passed
    private boolean passed = false;
    /*
     * The first point of the gate
     */
    private int startX;
    private int startY;
    /*
     * The other point on the other side of the road
     */
    private int endX;
    private int endY;

    /**
     * Gate constructor. Loads light spire if not already loaded.
     *
     * @param track The track this gate is put
     * @param startX x coordinate of the first point of the gate
     * @param startY y coordinate of the first point of the gate
     * @param endX x coordinate of the point on the other side of the road
     * @param endY y coordinate of the point on the other side of the road
     */
    public Gate(Track track, int startX, int startY, int endX, int endY) {
        this.track = track;
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
        ImageLoader loader = ImageLoader.getInstance();
        if (lightSprite == null) {
            try {
                lightSprite = new Sprite(loader.loadImage("/light_sprite.png"),
                    12, 12);
            }
            catch (IOException ioe) {
                // Nothing to do here.
            }
        }
    }

    /**
     * Tells if the given point is near enough the gate.
     *
     * @param x x coordinate of the point
     * @param y y coordinate of the point
     * @return true if the point is near the given point, false otherwise.
     */
    public boolean isPointNear(int x, int y) {
        int rightX, leftX;
        int topY, bottomY;
        if (startX > endX) {
            rightX = startX + 5;
            leftX = endX - 5;
        }
        else {
            rightX = endX + 5;
            leftX = startX - 5;
        }
        if (startY > endY) {
            topY = startY + 5;
            bottomY = endY - 5;
        }
        else {
            topY = endY + 5;
            bottomY = startY - 5;
        }

        return x > leftX && x < rightX && y > bottomY && y < topY;
    }

    public void setPassed(boolean passed) {
        this.passed = passed;
    }

    /**
     * Renders the gate. That means, a light is drawn at both start and end
     * points of the gate. The light is green is the gate is passed.
     *
     * @param g Graphics object
     */
    public void render(Graphics g) {
        int trackX = track.getX();
        int trackY = track.getY();
        if (this.passed) {
            lightSprite.setFrame(1);
        }
        else {
            lightSprite.setFrame(0);
        }
        lightSprite.setPosition(startX + trackX, startY + trackY);
        lightSprite.paint(g);
        lightSprite.setPosition(endX + trackX, endY + trackY);
        lightSprite.paint(g);
    }
}