DesktopDevice.java :  » Game » jbots » com » zonski » jbots » desktop » client » Java Open Source

Java Open Source » Game » jbots 
jbots » com » zonski » jbots » desktop » client » DesktopDevice.java
package com.zonski.jbots.desktop.client;

import java.util.Random;

import com.zonski.jbots.engine.Device;
import com.zonski.jbots.engine.InputStreamFactory;

public abstract class DesktopDevice implements Device
{
    private static final int MAX_OFFSET = 4;
    
    private PainterCanvas canvas;

    private Random random;
    private int currentUpdate;
    private int vibrateFinishUpdate;

    public DesktopDevice()
    {
        this.random = new Random();
    }

    public PainterCanvas getCanvas()
    {
        return this.canvas;
    }

    public void setCanvas(PainterCanvas canvas)
    {
        this.canvas = canvas;
    }

    public void vibrate(int duration)
    {
        this.vibrateFinishUpdate = 
            Math.max(this.currentUpdate + duration, 
                    this.vibrateFinishUpdate);
    }

    /**
     * Implementation specific mechanism to play sounds
     * @param sound the name of the sound to be played
     * @param times the number of times to play the sound
     */
    public abstract void play(String sound, int times);

    public void update()
    {
        this.currentUpdate++;
        if(this.vibrateFinishUpdate != 0)
        {
            if(this.currentUpdate > this.vibrateFinishUpdate)
            {
                this.canvas.setOffset(0, 0);
                this.vibrateFinishUpdate = 0;
            }else{
                int offsetX = this.random.nextInt()%MAX_OFFSET;
                int offsetY = this.random.nextInt()%MAX_OFFSET;
                this.canvas.setOffset(offsetX, offsetY);
            }
        }
    }

    public void stopAll()
    {
        this.vibrateFinishUpdate = 0;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.