游戏开发之键盘控制类:GameAction_

package com.input;

/**
??? The GameAction class is an abstract to a user-initiated
??? action, like jumping or moving. GameActions can be mapped
??? to keys or the mouse with the InputManager.
*/
public class GameAction_ {

??? /**
??????? Normal behavior. The isPressed() method returns true
??????? as long as the key is held down.
??? */
??? public static final int NORMAL = 0;

??? /**
??????? Initial press behavior. The isPressed() method returns
??????? true only after the key is first pressed, and not again
??????? until the key is released and pressed again.
??? */
??? public static final int DETECT_INITAL_PRESS_ONLY = 1;

??? private static final int STATE_RELEASED = 0;
??? private static final int STATE_PRESSED = 1;
??? private static final int STATE_WAITING_FOR_RELEASE = 2;

??? private String name;
??? private int behavior;
??? private int amount;
??? private int state;

??? /**
??????? Create a new GameAction with the NORMAL behavior.
??? */
??? public GameAction(String name) {
??????? this(name, NORMAL);
??? }


??? /**
??????? Create a new GameAction with the specified behavior.
??? */
??? public GameAction(String name, int behavior) {
??????? this.name = name;
??????? this.behavior = behavior;
??????? reset();
??? }


??? /**
??????? Gets the name of this GameAction.
??? */
??? public String getName() {
??????? return name;
??? }


??? /**
??????? Resets this GameAction so that it appears like it hasn't
??????? been pressed.
??? */
??? public void reset() {
??????? state = STATE_RELEASED;
??????? amount = 0;
??? }


??? /**
??????? Taps this GameAction. Same as calling press() followed
??????? by release().
??? */
??? public synchronized void tap() {
??????? press();
??????? release();
??? }


??? /**
??????? Signals that the key was pressed.
??? */
??? public synchronized void press() {
??????? press(1);
??? }


??? /**
??????? Signals that the key was pressed a specified number of
??????? times, or that the mouse move a spcified distance.
??? */
??? public synchronized void press(int amount) {
??????? if (state != STATE_WAITING_FOR_RELEASE) {
??????????? this.amount+=amount;
??????????? state = STATE_PRESSED;
??????? }

??? }


??? /**
??????? Signals that the key was released
??? */
??? public synchronized void release() {
??????? state = STATE_RELEASED;
??? }


??? /**
??????? Returns whether the key was pressed or not since last
??????? checked.
??? */
??? public synchronized boolean isPressed() {
??????? return (getAmount() != 0);
??? }


??? /**
??????? For keys, this is the number of times the key was
??????? pressed since it was last checked.
??????? For mouse movement, this is the distance moved.
??? */
??? public synchronized int getAmount() {
??????? int retVal = amount;
??????? if (retVal != 0) {
??????????? if (state == STATE_RELEASED) {
??????????????? amount = 0;
??????????? }
??????????? else if (behavior == DETECT_INITAL_PRESS_ONLY) {
??????????????? state = STATE_WAITING_FOR_RELEASE;
??????????????? amount = 0;
??????????? }
??????? }
??????? return retVal;
??? }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值