Minggu, 12 Agustus 2012


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;


/**
 * @author kira
 */
public class Game extends GameCanvas implements Runnable{
    private static final int SPEED = 3;
    private static final int MIN_BUFFER = 20;
    private int viewPortX = 0;
    private int viewPortY = 0;
    Graphics g = getGraphics();
    GameDesign gd;
    Sprite Karakter;
    TiledLayer lantai,tembok,tlBase,pohon;
    LayerManager lm;
    private byte lastDirection = -1;
    private SpriteAnimationTask KarakterAnimator;
    private Timer timer;
   
    public Game(){
        super(true);
        init();
        new Thread(this).start();
    }
    private void init(){
       gd = new GameDesign();
       lm = new LayerManager();
       this.timer = new Timer();
       
        //define the reference in the midle of sprites frame so that transformations work well
       
        try {
            this.tlBase = this.gd.getLantai();
            Karakter= gd.getKar();
            tembok=gd.getTembok();
            pohon=gd.getPohon();
            this.Karakter.defineReferencePixel(8, 8);
        this.KarakterAnimator = new SpriteAnimationTask(this.Karakter, false);
        this.timer.scheduleAtFixedRate(this.KarakterAnimator, 0, gd.karbelakangDelay);
         
            gd.updateLayerManagerForDesain(lm);
           
           
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        while(true){
            //looping game
              //check for user input
            int keyState = getKeyStates();

            //if user is pressing the left button
            if ((keyState & LEFT_PRESSED) != 0) {
                //if the previous direction was other than left set the sequence
                //correct sequence & transform needed for walking to the left
                if (this.lastDirection != LEFT) {
                    this.lastDirection = LEFT;
                    this.Karakter.setFrameSequence(gd.karkiri);
                    this.Karakter.setTransform(Sprite.TRANS_NONE);
                    continue;
                }
                //assign the sequence playback direction
                this.KarakterAnimator.forward();
                //move the sprite to the left
                this.Karakter.move(-SPEED, 0);
                //if moving the sprite generates a collision return sprite back
                //to its original position
                if (this.spriteCollides(this.Karakter)) {
                    this.Karakter.move(SPEED,0);
                    continue;
                }
                //attempt to adjust the viewport to keep the sprite on the screen
                this.adjustViewport(this.viewPortX - SPEED, this.viewPortY);
            } else if ((keyState & RIGHT_PRESSED) != 0) {
                if (this.lastDirection != RIGHT) {
                    this.lastDirection = RIGHT;
                    this.Karakter.setFrameSequence(gd.karkanan);
                    this.Karakter.setTransform(Sprite.TRANS_NONE);
                    continue;
                }
                this.KarakterAnimator.forward();
                this.Karakter.move(SPEED, 0);
                if (this.spriteCollides(this.Karakter)) {
                    this.Karakter.move(-SPEED,0);
                    continue;
                }
                this.adjustViewport(this.viewPortX + SPEED, this.viewPortY);
            } else if ((keyState & UP_PRESSED) != 0) {
                if (this.lastDirection != UP) {
                    this.lastDirection = UP;
                    this.Karakter.setFrameSequence(gd.karbelakang);
                    this.Karakter.setTransform(Sprite.TRANS_NONE);
                    continue;
                }
                this.KarakterAnimator.forward();
                this.Karakter.move(0, -SPEED);
                if (this.spriteCollides(this.Karakter)) {
                    this.Karakter.move(0,SPEED);
                    continue;
                }
                this.adjustViewport(this.viewPortX, this.viewPortY - SPEED);
            } else if ((keyState & DOWN_PRESSED) != 0) {
                if (this.lastDirection != DOWN) {
                    this.lastDirection = DOWN;
                    this.Karakter.setFrameSequence(gd.kardepan);
                    this.Karakter.setTransform(Sprite.TRANS_NONE);
                    continue;
                }
                this.KarakterAnimator.forward();
                this.Karakter.move(0, SPEED);
               if (this.spriteCollides(this.Karakter)) {
                    this.Karakter.move(0,-SPEED);
                    continue;
                }
                this.adjustViewport(this.viewPortX, this.viewPortY + SPEED);
            } else {
                this.KarakterAnimator.setMoving(false);
            }

            this.lm.paint(g, 0, 0);
            flushGraphics(0, 0, this.getWidth(), this.getHeight());

            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }


 private class SpriteAnimationTask extends TimerTask {

        private boolean moving = false;
        private boolean forward = true;
        private Sprite sprite;

        public SpriteAnimationTask(Sprite sprite, boolean forward) {
            this.sprite = sprite;
            this.forward = forward;
        }

        public void run() {
            if (!this.moving) {
                return;
            }
            if (this.forward) {
                this.sprite.nextFrame();
            } else {
                this.sprite.prevFrame();
            }
        }

        public void forward() {
            this.forward = true;
            this.moving = true;
        }

        public void backward() {
            this.forward = false;
            this.moving = true;
        }

        public void setMoving(boolean isMoving) {
            this.moving = isMoving;
        }
    }

 private void adjustViewport(int x, int y) {

        int sx = this.Karakter.getX();
        int sy = this.Karakter.getY();

        int xmin = this.viewPortX + MIN_BUFFER;
        int xmax = this.viewPortX + this.getWidth() - this.Karakter.getWidth() - MIN_BUFFER;
        int ymin = this.viewPortY + MIN_BUFFER;
        int ymax = this.viewPortY + this.getHeight() - this.Karakter.getHeight() - MIN_BUFFER;

        //if the sprite is not near the any screen edges don't adjust
        if (sx >= xmin && sx <= xmax && sy >= ymin && sy <= ymax) {
            return;
        }

        //if the sprite is moving left but isn't near the left edge of the screen don't adjust
        if (this.lastDirection == LEFT && sx >= xmin) {
            return;
        }
        //if the sprite is moving right but isn't near the right edge of the screen don't adjust
        if (this.lastDirection == RIGHT && sx <= xmax) {
            return;
        }
        //if the sprite is moving up but isn't at near top edge of the screen don't adjust
        if (this.lastDirection == UP && sy >= ymin) {
            return;
        }
        //if the sprite is moving down but isn't at near bottom edge of the screen don't adjust
        if (this.lastDirection == DOWN && sy <= ymax) {
            return;
        }

        //only adjust x to values that ensure the base tiled layer remains visible
        //and no white space is shown
        if (x < this.tlBase.getX()) {
            this.viewPortX = this.tlBase.getX();
        } else if (x > this.tlBase.getX() + this.tlBase.getWidth() - this.getWidth()) {
            this.viewPortX = this.tlBase.getX() + this.tlBase.getWidth() - this.getWidth();
        } else {
            this.viewPortX = x;
        }

        //only adjust y to values that ensure the base tiled layer remains visible
        //and no white space is shown
        if (y < this.tlBase.getY()) {
            this.viewPortY = this.tlBase.getY();
        } else if (y > this.tlBase.getY() + this.tlBase.getHeight() - this.getHeight()) {
            this.viewPortY = this.tlBase.getY() + this.tlBase.getHeight() - this.getHeight();
        } else {
            this.viewPortY = y;
        }

        //adjust the viewport
        this.lm.setViewWindow(this.viewPortX, this.viewPortY, this.getWidth(), this.getHeight());
    }

     public boolean spriteCollides(Sprite sprite) {
        return sprite.collidesWith(this.tembok, true)
            || sprite.collidesWith(this.pohon, true)
            || sprite.getX() < 0 || sprite.getY() < 0
            || sprite.getX() > (this.tlBase.getWidth() - sprite.getWidth())
            || sprite.getY() > (this.tlBase.getHeight() - sprite.getHeight()
            );
    }


}

Tidak ada komentar:

Posting Komentar