rotateaction.java.svn-base

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

SVN-BASE
76
字号
/*
 * RotateLeftAction.java
 *
 * Created on 31. Dezember 2005, 14:26
 *
 * 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.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.scene.CameraNode;

/**
 * <code>KeyRotateLeftAction</code> performs the action of rotating a camera a
 * certain angle. This angle is determined by the speed at which the camera can
 * turn and the time between frames.
 * 
 * @author Mark Powell
 * @version $Id: KeyRotateLeftAction.java,v 1.14 2005/09/15 17:13:57 renanse Exp $
 */
public class RotateAction extends InputAction {
    
    public enum Direction { 
        LEFT(1,0), RIGHT(-1,0), UP(0,-1), DOWN(0,1);
        
        private int v;
        private int h;
               
        Direction(int h, int v) {
            this.h = h;
            this.v = v;
        }
        
        public int getHorizontal() {
            return h;
        }
        
        public int getVertical() {
            return v;
        }
        
    }

    private CameraNode node;
    
    private Direction direction;
    private static float hAngle = 0;
    private static float vAngle = 0;

    public RotateAction(CameraNode node, float speed, Direction direction) {
        this.node = node;
        this.speed = speed;
        this.direction = direction;
    }

    /**
     * <code>performAction</code> rotates the camera a certain angle.
     * 
     * @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
        hAngle += speed * evt.getTime() * direction.getHorizontal();
        float newVAngle = vAngle + speed * evt.getTime() * direction.getVertical();
        if (-0.4 < newVAngle && newVAngle < 0.4) {
            vAngle = newVAngle;
        }
        node.setLocalRotation(new Quaternion(new float[]{vAngle, hAngle, 0f}));
    }
}

⌨️ 快捷键说明

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