moveaction.java.svn-base

来自「一个JAVA程序员的游戏」· SVN-BASE 代码 · 共 146 行

SVN-BASE
146
字号
/*
 * ForwardAction.java
 *
 * Created on 31. Dezember 2005, 14:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kanjitori.action;

import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.Node;
import com.jme.system.DisplaySystem;
import java.awt.Point;
import kanjitori.map.Content;
import kanjitori.map.Map;

public class MoveAction extends InputAction {
    
    public enum Direction {FORWARD, BACKWARD, LEFT, RIGHT };
    
    private static float SLIDE_DIST = 0.6f;
    
    //temp holder for the multiplication of the direction and time
    private static final Vector3f tempVa = new Vector3f();
    //the camera to manipulate
    private Node node;
    private float angle;
    private Direction direction;
    
    private static Map map;
    
    public static void setLevelMap(Map map) {
        MoveAction.map = map;
    }
    
    /**
     * Constructor instantiates a new <code>KeyForwardAction</code> object.
     *
     * @param camera
     *            the camera that will be affected by this action.
     * @param speed
     *            the speed at which the camera can move.
     */
    public MoveAction(Node node, float speed, Direction direction) {
        this.node = node;
        this.speed = speed;
        this.direction = direction;
    }
    
    /**
     * <code>performAction</code> moves the camera along it's positive
     * direction vector at a speed of movement speed * time. Where time is the
     * time between frames and 1 corresponds to 1 second.
     *
     * @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
        Vector3f loc = node.getLocalTranslation();
        Vector3f dir = null;
        Camera camera = DisplaySystem.getDisplaySystem().getRenderer().getCamera();
        switch(direction) {
            case FORWARD : dir = camera.getDirection(); break;
            case BACKWARD : dir = camera.getDirection().mult(-1); break;
            case LEFT : dir = camera.getLeft(); break;
            case RIGHT : dir = camera.getLeft().mult(-1); break;
        }
        
        loc.addLocal(dir.mult(speed * evt.getTime(), tempVa));
        loc.y = 0;
        slide(loc);
        node.setLocalTranslation(loc);
    }
    
    private void slide(Vector3f loc) {
        float xpos = loc.x / 2;
        float ypos = loc.z / 2;
        int x = (int) Math.round(xpos);
        int y = (int) Math.round(ypos);
        
        float xd = xpos - x;
        if (xd < -SLIDE_DIST/2) {
            if (map.getContent(x - 1, y) == Content.SOLID) {
                loc.x = 2 * x - SLIDE_DIST;
            }
        } else if (xd > SLIDE_DIST/2) {
            if (map.getContent(x+1, y) == Content.SOLID) {
                loc.x = 2 * x + SLIDE_DIST;
            }
        }
        
        float yd = ypos - y;
        if (yd < -SLIDE_DIST/2) {
            if (map.getContent(x, y - 1) == Content.SOLID) {
                loc.z = 2 * y - SLIDE_DIST;
            }
        } else if (yd > SLIDE_DIST/2) {
            if (map.getContent(x, y + 1) == Content.SOLID) {
                loc.z = 2 * y + SLIDE_DIST;
            }
        }
        
        //the "corner" case
        xd = loc.x / 2 - x;
        yd = loc.z / 2 - y;
        
        if (xd < - SLIDE_DIST/2 && yd < - SLIDE_DIST/2) {
            if (map.getContent(x - 1, y - 1) == Content.SOLID) {
                if (xd > yd) {
                    loc.x = 2 * x - SLIDE_DIST;
                } else {
                    loc.z = 2 * y - SLIDE_DIST;
                }
            }
        } else if (xd > SLIDE_DIST/2 && yd < - SLIDE_DIST/2) {
            if (map.getContent(x + 1, y - 1) == Content.SOLID) {
                if (-xd > yd) {
                    loc.x = 2 * x + SLIDE_DIST;
                } else {
                    loc.z = 2 * y - SLIDE_DIST;
                }
            }
        } else if (xd < - SLIDE_DIST/2 && yd > SLIDE_DIST/2) {
            if (map.getContent(x - 1, y + 1) == Content.SOLID) {
                if (xd > -yd) {
                    loc.x = 2 * x - SLIDE_DIST;
                } else {
                    loc.z = 2 * y + SLIDE_DIST;
                }
            }
        } else if (xd > SLIDE_DIST/2 && yd > SLIDE_DIST/2) {
            if (map.getContent(x + 1, y + 1) == Content.SOLID) {
                if (xd < yd) {
                    loc.x = 2 * x + SLIDE_DIST;
                } else {
                    loc.z = 2 * y + SLIDE_DIST;
                }
            }
        }
        
    }
}

⌨️ 快捷键说明

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