www.pudn.com > 20061029005.rar > GameCanvas.java


// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov. 
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html 
// Decompiler options: packimports(3) fieldsfirst ansi  
 
package org.cesar.inmotion.sea; 
 
import java.util.Random; 
import javax.microedition.lcdui.*; 
import org.cesar.inmotion.sea.basic.Diver; 
import org.cesar.inmotion.sea.basic.Fire; 
import org.cesar.inmotion.sea.basic.Fish; 
import org.cesar.inmotion.util.*; 
 
// Referenced classes of package org.cesar.inmotion.sea: 
//            Attack, GameController, Constants, SeaPersistence 
 
public class GameCanvas extends Canvas 
    implements CommandListener 
{ 
 
    private boolean isPaused; 
    private boolean drawHiScore; 
    private static GameCanvas instance; 
    private boolean isGameOver; 
    private Diver player; 
    public static Attack attack; 
    public static int frame; 
    public static int ScreenHeight; 
    public static int ScreenWidth; 
    public static int SurfaceY; 
    public static byte wave_index; 
    public static int clouds_x; 
    public static int clouds_y; 
    public static byte level = 1; 
 
    public GameCanvas() 
    { 
        instance = this; 
        Constants.lives = 5; 
        ScreenHeight = getHeight(); 
        ScreenWidth = getWidth(); 
        SurfaceY = (ScreenHeight * 30) / 100; 
        player = new Diver(Constants.img_diver[0][1]); 
    } 
 
    private void cleanAll(Graphics g) 
    { 
        g.setColor(0, 210, 255); 
        g.fillRect(0, 0, g.getClipWidth(), g.getClipHeight()); 
    } 
 
    public void init() 
    { 
        isPaused = false; 
        isGameOver = false; 
        removeCommand(Constants.pauseCommand); 
        removeCommand(Constants.exitCommand); 
        removeCommand(Constants.startCommand); 
        addCommand(Constants.pauseCommand); 
        addCommand(Constants.exitCommand); 
        setCommandListener(this); 
        player.init(); 
        attack = new Attack(); 
    } 
 
    public void update() 
    { 
        frame++; 
        player.update(); 
        if(!attack.isActive()) 
        { 
            attack.init(); 
        } else 
        { 
            if(Constants.score > 2500 && level == 4) 
                attack.setLevel(++level); 
            else 
            if(Constants.score > 1500 && level == 3) 
                attack.setLevel(++level); 
            else 
            if(Constants.score > 1000 && level == 2) 
                attack.setLevel(++level); 
            else 
            if(Constants.score > 100 && level == 1) 
                attack.setLevel(++level); 
            attack.update(); 
        } 
        if(player.getFire() != null && player.getFire().isVisible()) 
            player.getFire().update(); 
    } 
 
    public void paint(Graphics g) 
    { 
        if(!isGameOver) 
        { 
            cleanAll(g); 
            drawBackground(g); 
            drawAttack(g); 
            drawSub(g); 
            drawFires(g); 
            drawDetails(g); 
        } else 
        if(drawHiScore) 
            drawHiScore(); 
        else 
            drawGameover(g); 
    } 
 
    public void keyPressed(int i) 
    { 
        int j = getGameAction(i); 
        switch(j) 
        { 
        case 3: // '\003' 
        case 4: // '\004' 
        default: 
            break; 
 
        case 2: // '\002' 
            if(player.repeatRight) 
                player.repeatRight = false; 
            else 
                player.repeatLeft = true; 
            break; 
 
        case 5: // '\005' 
            if(player.repeatLeft) 
                player.repeatLeft = false; 
            else 
                player.repeatRight = true; 
            break; 
 
        case 1: // '\001' 
            if(player.repeatDown) 
                player.repeatDown = false; 
            else 
                player.repeatUp = true; 
            break; 
 
        case 6: // '\006' 
            if(player.repeatUp) 
                player.repeatUp = false; 
            else 
                player.repeatDown = true; 
            break; 
        } 
    } 
 
    public void keyReleased(int i) 
    { 
        player.repeatLeft = false; 
        player.repeatRight = false; 
        player.repeatDown = false; 
        player.repeatUp = false; 
    } 
 
    public void keyRepeated(int i) 
    { 
        keyPressed(i); 
    } 
 
    public boolean isPaused() 
    { 
        return isPaused; 
    } 
 
    public boolean isGameOver() 
    { 
        return isGameOver; 
    } 
 
    public void die() 
    { 
        if(Constants.lives > 0) 
        { 
            player.setFishcount((byte)0); 
            player.breath = 0; 
            attack.setActive(false); 
            player.atSurface = true; 
            Constants.lives--; 
            init(); 
        } else 
        { 
            isGameOver = true; 
        } 
    } 
 
    public static GameCanvas getInstance() 
    { 
        return instance; 
    } 
 
    public Diver getDiver() 
    { 
        return player; 
    } 
 
    public void drawAttack(Graphics g) 
    { 
        for(byte byte0 = 0; byte0 < attack.getMaxitems(); byte0++) 
        { 
            Fish fish = attack.getShark(byte0); 
            if(fish.isVisible() && !fish.isDead()) 
                g.drawImage(fish.getImage(), fish.getX(), fish.getY(), 20); 
            Fish fish1 = attack.getFish(byte0); 
            if(fish1.isVisible() && !fish1.isDead()) 
                g.drawImage(fish1.getImage(), fish1.getX(), fish1.getY(), 20); 
        } 
 
    } 
 
    public void drawSub(Graphics g) 
    { 
        g.drawImage(player.getImage(), player.getX(), player.getY(), 20); 
    } 
 
    public void drawFires(Graphics g) 
    { 
        Fire fire = player.getFire(); 
        if(fire != null && fire.isVisible()) 
            g.drawImage(fire.getImage(), fire.getX(), fire.getY(), 0x2 | 0x1); 
    } 
 
    public void drawBackground(Graphics g) 
    { 
        g.setColor(0, 80, 189); 
        g.fillRect(0, SurfaceY, ScreenWidth, ScreenHeight); 
        if(frame % 2 == 0) 
        { 
            wave_index = (byte)(++wave_index % 4); 
            clouds_x -= 3; 
            if(clouds_x <= -Constants.img_clouds.getWidth()) 
            { 
                clouds_x = ScreenWidth; 
                clouds_y = Math.abs((new Random()).nextInt()) % (SurfaceY - Constants.img_clouds.getHeight()); 
            } 
        } 
        g.drawImage(Constants.img_clouds, clouds_x, clouds_y, 0x10 | 0x4); 
        g.drawImage(Constants.img_wave[wave_index], 0, SurfaceY, 0x10 | 0x4); 
        g.drawImage(Constants.img_wave[wave_index], Constants.img_wave[wave_index].getWidth(), SurfaceY, 0x10 | 0x4); 
        g.drawImage(Constants.img_ground, 0, ScreenHeight - Constants.img_ground.getHeight(), 0x10 | 0x4); 
        g.drawImage(Constants.img_ground, Constants.img_ground.getWidth(), ScreenHeight - Constants.img_ground.getHeight(), 0x10 | 0x4); 
    } 
 
    public void drawDetails(Graphics g) 
    { 
        g.setFont(Constants.FONT); 
        g.setColor(255, 255, 255); 
        g.drawString(String.valueOf(Constants.score), ScreenWidth - 1, 3, 0x8 | 0x10); 
        g.setColor(0, 0, 0); 
        g.drawString(String.valueOf(Constants.score), ScreenWidth - 2, 2, 0x8 | 0x10); 
        if(player.breath < 300) 
            g.setColor(255, 255, 255); 
        else 
            g.setColor(255, 255, 0); 
        g.fillRoundRect(2, 2, (player.breath * ((ScreenWidth * 60) / 100)) / 1000, 5, 2, 2); 
        g.setColor(0, 0, 0); 
        g.drawRoundRect(2, 2, (player.breath * ((ScreenWidth * 60) / 100)) / 1000, 5, 2, 2); 
        if(player.getFishcount() == 6) 
        { 
            if(frame % 10 < 5) 
            { 
                for(int i = 0; i < player.getFishcount(); i++) 
                    g.drawImage(Constants.img_fish[0], 2 + i * (Constants.img_fish[0].getWidth() + 3), 10, 0x4 | 0x10); 
 
            } 
        } else 
        { 
            for(int j = 0; j < player.getFishcount(); j++) 
                g.drawImage(Constants.img_fish[0], 2 + j * (Constants.img_fish[0].getWidth() + 3), 10, 0x4 | 0x10); 
 
        } 
        for(int k = 0; k < Constants.lives; k++) 
            g.drawImage(Constants.img_life, 2 + k * (Constants.img_life.getWidth() + 3), 18, 0x4 | 0x10); 
 
    } 
 
    public void drawGameover(Graphics g) 
    { 
        removeCommand(Constants.pauseCommand); 
        g.setColor(255, 255, 255); 
        g.fillRect(0, 0, ScreenWidth, ScreenHeight); 
        short word0 = (short)(g.getClipWidth() / 2 - Constants.gameOverImg.getWidth() / 2); 
        short word1 = (short)(g.getClipHeight() / 2 - Constants.gameOverImg.getHeight() / 2); 
        g.drawImage(Constants.gameOverImg, word0, word1, 20); 
    } 
 
    public void drawHiScore() 
    { 
        Form form = (Form)ApplicationController.getInstance(AbstractMIDlet.getAppClassName()).getScreen(6); 
        String s = Constants.TITLE_SCORE_STR; 
        if(form.size() > 1) 
            form.delete(form.size() - 1); 
        form.append(s + " " + Integer.toString(Constants.score)); 
        ApplicationController.getInstance(AbstractMIDlet.getAppClassName()).changeScreen(6); 
    } 
 
    public void commandAction(Command command, Displayable displayable) 
    { 
        ApplicationController applicationcontroller = ApplicationController.getInstance(AbstractMIDlet.getAppClassName()); 
        Form form = (Form)applicationcontroller.getScreen(2); 
        if(displayable == form) 
        { 
            if(command == Constants.yesCommand) 
            { 
                isGameOver = true; 
                applicationcontroller.changeScreen(4); 
            } else 
            if(command == Constants.noCommand) 
            { 
                addCommand(Constants.pauseCommand); 
                removeCommand(Constants.yesCommand); 
                applicationcontroller.changeScreen(1); 
                isPaused = false; 
            } 
        } else 
        if(displayable == applicationcontroller.getScreen(6)) 
        { 
            String s = ((TextField)((Form)applicationcontroller.getScreen(6)).get(0)).getString(); 
            SeaPersistence.getInstance().setScore(s, Constants.score); 
            ((GameController)applicationcontroller).writeHiScore(); 
            applicationcontroller.getScreen(5).removeCommand(Constants.backCommand); 
            applicationcontroller.getScreen(5).removeCommand(Constants.emptyCommand); 
            applicationcontroller.getScreen(5).addCommand(Constants.okCommand); 
            applicationcontroller.getScreen(5).addCommand(Constants.emptyCommand); 
            applicationcontroller.changeScreen(5); 
        } else 
        if(displayable == applicationcontroller.getScreen(5)) 
        { 
            if(command == Constants.okCommand) 
            { 
                applicationcontroller.getScreen(5).removeCommand(Constants.okCommand); 
                applicationcontroller.getScreen(5).addCommand(Constants.backCommand); 
                applicationcontroller.changeScreen(4); 
            } else 
            if(command == Constants.backCommand) 
                applicationcontroller.changeScreen(4); 
        } else 
        if(command == Constants.exitCommand) 
        { 
            if(isPaused) 
                removeCommand(Constants.resumeCommand); 
            if(isGameOver) 
            { 
                if(SeaPersistence.getInstance().getScoreValue(SeaPersistence.getInstance().getScoreNumber() - 1) < Constants.score) 
                { 
                    drawHiScore = true; 
                    repaint(); 
                    serviceRepaints(); 
                } else 
                { 
                    ApplicationController.getInstance(AbstractMIDlet.getAppClassName()).changeScreen(4); 
                } 
            } else 
            { 
                ApplicationController.getInstance(AbstractMIDlet.getAppClassName()).changeScreen(2); 
            } 
            isPaused = true; 
        } else 
        if(command == Constants.pauseCommand) 
        { 
            removeCommand(Constants.pauseCommand); 
            addCommand(Constants.resumeCommand); 
            isPaused = true; 
        } else 
        if(command == Constants.resumeCommand) 
        { 
            removeCommand(Constants.resumeCommand); 
            addCommand(Constants.pauseCommand); 
            isPaused = false; 
        } else 
        if(command == Constants.okCommand) 
            ApplicationController.getInstance(AbstractMIDlet.getAppClassName()).changeScreen(4); 
    } 
 
}