⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 m3gcanvas.java

📁 初学者游戏框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;

import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.m3g.*;

public class M3GCanvas
extends GameCanvas
implements Runnable {
    // Thread-control
    boolean running = false;
    boolean done = true;
    
    // If the game should end
    public static boolean gameOver = false;
    
    // Rendering hints
    public static final int STRONG_RENDERING_HINTS = Graphics3D.ANTIALIAS | Graphics3D.TRUE_COLOR | Graphics3D.DITHER;
    public static final int WEAK_RENDERING_HINTS = 0;
    public static int RENDERING_HINTS = STRONG_RENDERING_HINTS;
    
    // Key array
    boolean[] key = new boolean[5];
    
    // Key constants
    public static final int FIRE = 0;
    public static final int UP = FIRE + 1;
    public static final int DOWN = UP + 1;
    public static final int LEFT = DOWN + 1;
    public static final int RIGHT = LEFT + 1;
    
    // Global identity matrix
    Transform identity = new Transform();
    
    // Global Graphics3D object
    Graphics3D g3d = null;
    
    // The background
    Background back = null;
    
    // The global camera object
    Camera cam = null;
    
    // The particle system
    ParticleSystem ps = null;
    FountainEffect fx = null;
    
    // The playing field
    Mesh paddle;
    Group playingField;
    Group ballField;
    Ball ball;
    
    Group allField;

    // Transforms
    Transform trLeftWall, trRightWall, trTopWall, trBottomWall, trFrontWall;
    Transform trPaddle;
    Transform trCam = new Transform();
    
    // Paddle's coords
//    float[] paddleCoords = {0.0f, 0.0f, -5.0f};
    float[] paddleCoords = {0.0f, 0.0f, -2.0f};
    
    // Wall constants
    public static final int TOP_WALL = 0;
    public static final int LEFT_WALL = 1;
    public static final int RIGHT_WALL = 2;
    public static final int BOTTOM_WALL = 3;
    public static final int PADDLE_WALL = 4;
//    public static final int PLAYING_FIELD = 5;
    
    public static final int FRONT_WALL = 5;
    public static final int PLAYING_FIELD = 6;
    
    public static final int BALL_FIELD = 0;
    
    
    // Vectors for our walls
    // Explanation: Each wall holds two vectors that
    // define the plane (See linear algebra) and
    // the wall's normal vector.
    float[][][] wallVec = new float[PLAYING_FIELD][3][3];
    
    /** Constructs the canvas
     */
    public M3GCanvas(int fps)
    {
        // We don't want to capture keys normally
        super(true);
        
        // We want a fullscreen canvas
        setFullScreenMode(true);
        
        // Create our playing field
        createField();
        
        // Load our camera
        loadCamera();
        
        // Load our background
        loadBackground();
        
        // Set up graphics 3d
        setUp();
    }
    
    /** Prepares the Graphics3D engine for immediate mode rendering by adding a light */
    private void setUp()
    {
        // Get the instance
        g3d = Graphics3D.getInstance();
        
        // Add a light to our scene, so we can see something
        g3d.addLight(createAmbientLight(), identity);
    }
    
    
    /** Creates a simple ambient light */
    private Light createAmbientLight()
    {
        Light l = new Light();
        l.setMode(Light.AMBIENT);
        l.setIntensity(1.0f);
        return l;
    }

    /** When fullscreen mode is set, some devices will call
     * this method to notify us of the new width/height.
     * However, we don't really care about the width/height
     * in this tutorial so we just let it be
     */
    public void sizeChanged(int newWidth, int newHeight)
    {
        
    }
    
    /** Loads our camera */
    private void loadCamera()
    {
        // Create a new camera
        cam = new Camera();
        
        // Set the perspective of our camera (choose a pretty wide FoV for a nifty tube effect)
        cam.setPerspective(130.0f, (float)getWidth() / (float)getHeight(), 0.1f, 50.0f);
//        cam.setParallel( 130.0f, (float)getWidth() / (float)getHeight(), 0.1f, 50.0f);
        
//        trCam.postTranslate( 0.0f, 0.0f, 6.0f );
    }
    
    /** Loads the background */
    private void loadBackground()
    {
        // Create a new background, set bg color to black
        back = new Background();
        back.setColor(0);
    }
    
    /** Creates our playing field. It will instantiate the ball and the three
     * walls (fourth wall is the "screen"
     */
    private void createField()
    {
        try
        {
            loadBall();
            
            createPaddle();
            
            createWalls();  
            
            allField = new Group();
            allField.addChild( playingField );
            allField.addChild( ballField );
        }
        catch(IOException e)
        {
            System.out.println("Loading error: " + e);
        }
    }

    /**
     * 
     */
    private void createWalls() {
        // Create all planes with our nifty MeshFactory class (we need several for collision)
        Mesh wall1 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
        Mesh wall2 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
        Mesh wall3 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
        Mesh wall4 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
        
        Mesh wall5 = MeshFactory.createPlane("/res/wall.png", PolygonMode.CULL_BACK);
        
        // We want nice perspective correction here
        MeshOperator.setPerspectiveCorrection(wall1, true);
        MeshOperator.setPerspectiveCorrection(wall2, true);
        MeshOperator.setPerspectiveCorrection(wall3, true);
        MeshOperator.setPerspectiveCorrection(wall4, true);
        
        // Set the left wall at its true position
        trLeftWall = new Transform();         
        trLeftWall.postTranslate(-4.0f, 0.0f, -5.0f);
        trLeftWall.postRotate(90, 0.0f, 1.0f, 0.0f);
        trLeftWall.postScale(5.0f, 5.0f, 5.0f);     
        wall1.setTransform(trLeftWall);
        
        // Make its vectors
        float[] v = VectorOps.vector(0.0f, 1.0f, 0.0f);
        float[] u = VectorOps.vector(0.0f, 0.0f, 1.0f);
        float[] normVec = VectorOps.calcNormal(v, u);
        wallVec[LEFT_WALL][0] = v;
        wallVec[LEFT_WALL][1] = u;
        wallVec[LEFT_WALL][2] = normVec;
        
        // Set the right wall at its true position
        trRightWall = new Transform();         
        trRightWall.postTranslate(4.0f, 0.0f, -5.0f);
        trRightWall.postRotate(-90, 0.0f, 1.0f, 0.0f);
        trRightWall.postRotate(180, 0.0f, 0.0f, 1.0f);
        trRightWall.postScale(5.0f, 5.0f, 5.0f);
        wall2.setTransform(trRightWall);
        
        // Same vectors as the left wall
        wallVec[RIGHT_WALL][0] = v;
        wallVec[RIGHT_WALL][1] = u;
        wallVec[RIGHT_WALL][2] = normVec;
        
        // Set the top wall at its true position
        trTopWall = new Transform();
        trTopWall.postTranslate(0.0f, 4.0f, -5.0f);
        trTopWall.postRotate(90, 1.0f, 0.0f, 0.0f);
        trTopWall.postRotate(-90, 0.0f, 0.0f, 1.0f);
        trTopWall.postScale(5.0f, 5.0f, 5.0f);
        wall3.setTransform(trTopWall);
        
        // Make its vectors
        v = VectorOps.vector(1.0f, 0.0f, 0.0f);
        u = VectorOps.vector(0.0f, 0.0f, 1.0f);
        normVec = VectorOps.calcNormal(v, u);
        wallVec[TOP_WALL][0] = v;
        wallVec[TOP_WALL][1] = u;
        wallVec[TOP_WALL][2] = normVec;
        
        // Set the bottom wall at its true position
        trBottomWall = new Transform();
        trBottomWall.postTranslate(0.0f, -4.0f, -5.0f);
        trBottomWall.postRotate(-90, 1.0f, 0.0f, 0.0f);
        trBottomWall.postRotate(90, 0.0f, 0.0f, 1.0f);
        trBottomWall.postScale(5.0f, 5.0f, 5.0f);
        wall4.setTransform(trBottomWall);
        
        // Same vectors as top wall
        wallVec[BOTTOM_WALL][0] = v;
        wallVec[BOTTOM_WALL][1] = u;
        wallVec[BOTTOM_WALL][2] = normVec;
        
        
        
//      Set the bottom wall at its true position
        trFrontWall = new Transform();
        trFrontWall.postTranslate(0.0f, 0.0f, -10.0f);
        trFrontWall.postScale(5.0f, 5.0f, 5.0f);
        wall5.setTransform(trFrontWall);
        
//      Make its vectors
        v = VectorOps.vector(0.0f, 1.0f, 0.0f);
        u = VectorOps.vector(1.0f, 0.0f, 0.0f);
        normVec = VectorOps.calcNormal(v, u);
        wallVec[FRONT_WALL][0] = v;
        wallVec[FRONT_WALL][1] = u;
        wallVec[FRONT_WALL][2] = normVec;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -