My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.replica.replicaisland;

import android.view.KeyEvent;

public class InputGameInterface extends BaseObject {
private static final float ORIENTATION_DEAD_ZONE_MIN = 0.03f;
private static final float ORIENTATION_DEAD_ZONE_MAX = 0.1f;
private static final float ORIENTATION_DEAD_ZONE_SCALE = 0.75f;

private final static float ROLL_TIMEOUT = 0.1f;
private final static float ROLL_RESET_DELAY = 0.075f;

// Raw trackball input is filtered by this value. Increasing it will
// make the control more twitchy, while decreasing it will make the control more precise.
private final static float ROLL_FILTER = 0.4f;
private final static float ROLL_DECAY = 8.0f;

private final static float KEY_FILTER = 0.25f;
private final static float SLIDER_FILTER = 0.25f;


private InputButton mJumpButton = new InputButton();
private InputButton mAttackButton = new InputButton();
private InputXY mDirectionalPad = new InputXY();
private InputXY mTilt = new InputXY();

private int mLeftKeyCode = KeyEvent.KEYCODE_DPAD_LEFT;
private int mRightKeyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
private int mJumpKeyCode = KeyEvent.KEYCODE_SPACE;
private int mAttackKeyCode = KeyEvent.KEYCODE_SHIFT_LEFT;

private float mOrientationDeadZoneMin = ORIENTATION_DEAD_ZONE_MIN;
private float mOrientationDeadZoneMax = ORIENTATION_DEAD_ZONE_MAX;
private float mOrientationDeadZoneScale = ORIENTATION_DEAD_ZONE_SCALE;
private float mOrientationSensitivity = 1.0f;
private float mOrientationSensitivityFactor = 1.0f;
private float mMovementSensitivity = 1.0f;

private boolean mUseClickButtonForAttack = true;
private boolean mUseOrientationForMovement = false;
private boolean mUseOnScreenControls = false;

private float mLastRollTime;

public InputGameInterface() {
super();
reset();
}

@Override
public void reset() {
mJumpButton.release();
mAttackButton.release();
mDirectionalPad.release();
mTilt.release();
}



@Override
public void update(float timeDelta, BaseObject parent) {
InputSystem input = sSystemRegistry.inputSystem;
final InputButton[] keys = input.getKeyboard().getKeys();
final InputXY orientation = input.getOrientationSensor();

// tilt is easy
mTilt.clone(orientation);

final InputTouchScreen touch = input.getTouchScreen();
final float gameTime = sSystemRegistry.timeSystem.getGameTime();

float sliderOffset = 0;

// update movement inputs
if (mUseOnScreenControls) {
final InputXY sliderTouch = touch.findPointerInRegion(
ButtonConstants.MOVEMENT_SLIDER_REGION_X,
ButtonConstants.MOVEMENT_SLIDER_REGION_Y,
ButtonConstants.MOVEMENT_SLIDER_REGION_WIDTH,
ButtonConstants.MOVEMENT_SLIDER_REGION_HEIGHT);

if (sliderTouch != null) {
final float halfWidth = ButtonConstants.MOVEMENT_SLIDER_BAR_WIDTH / 2.0f;
final float center = ButtonConstants.MOVEMENT_SLIDER_X + halfWidth;
final float offset = sliderTouch.getX() - center;
float magnitudeRamp = Math.abs(offset) > halfWidth ? 1.0f : (Math.abs(offset) / halfWidth);

final float magnitude = magnitudeRamp * Utils.sign(offset) * SLIDER_FILTER * mMovementSensitivity;
sliderOffset = magnitudeRamp * Utils.sign(offset);
mDirectionalPad.press(gameTime, magnitude, 0.0f);
} else {
mDirectionalPad.release();
}
} else if (mUseOrientationForMovement) {
mDirectionalPad.clone(orientation);
mDirectionalPad.setMagnitude(
filterOrientationForMovement(orientation.getX()),
filterOrientationForMovement(orientation.getY()));
} else {
// keys or trackball
final InputXY trackball = input.getTrackball();
final InputButton left = keys[mLeftKeyCode];
final InputButton right = keys[mRightKeyCode];
final float leftPressedTime = left.getLastPressedTime();
final float rightPressedTime = right.getLastPressedTime();


if (trackball.getLastPressedTime() > Math.max(leftPressedTime, rightPressedTime)) {
// The trackball never goes "up", so force it to turn off if it wasn't triggered in the last frame.
// What follows is a bunch of code to filter trackball events into something like a dpad event.
// The goals here are:
// - For roll events that occur in quick succession to accumulate.
// - For roll events that occur with more time between them, lessen the impact of older events
// - In the absence of roll events, fade the roll out over time.
if (gameTime - trackball.getLastPressedTime() < ROLL_TIMEOUT) {
float newX;
float newY;
final float delay = Math.max(ROLL_RESET_DELAY, timeDelta);
if (gameTime - mLastRollTime <= delay) {
newX = mDirectionalPad.getX() + (trackball.getX() * ROLL_FILTER * mMovementSensitivity);
newY = mDirectionalPad.getY() + (trackball.getY() * ROLL_FILTER * mMovementSensitivity);
} else {
float oldX = mDirectionalPad.getX() != 0.0f ? mDirectionalPad.getX() / 2.0f : 0.0f;
float oldY = mDirectionalPad.getX() != 0.0f ? mDirectionalPad.getX() / 2.0f : 0.0f;
newX = oldX + (trackball.getX() * ROLL_FILTER * mMovementSensitivity);
newY = oldY + (trackball.getX() * ROLL_FILTER * mMovementSensitivity);
}

mDirectionalPad.press(gameTime, newX, newY);
mLastRollTime = gameTime;
trackball.release();
} else {
float x = mDirectionalPad.getX();
float y = mDirectionalPad.getY();
if (x != 0.0f) {
int sign = Utils.sign(x);
x = x - (sign * ROLL_DECAY * timeDelta);
if (Utils.sign(x) != sign) {
x = 0.0f;
}
}

if (y != 0.0f) {
int sign = Utils.sign(y);
y = y - (sign * ROLL_DECAY * timeDelta);
if (Utils.sign(x) != sign) {
y = 0.0f;
}
}


if (x == 0 && y == 0) {
mDirectionalPad.release();
} else {
mDirectionalPad.setMagnitude(x, y);
}
}

} else {
float xMagnitude = 0.0f;
float yMagnitude = 0.0f;
float pressTime = 0.0f;
// left and right are mutually exclusive
if (leftPressedTime > rightPressedTime) {
xMagnitude = -left.getMagnitude() * KEY_FILTER * mMovementSensitivity;
pressTime = leftPressedTime;
} else {
xMagnitude = right.getMagnitude() * KEY_FILTER * mMovementSensitivity;
pressTime = rightPressedTime;
}

if (xMagnitude != 0.0f) {
mDirectionalPad.press(pressTime, xMagnitude, yMagnitude);
} else {
mDirectionalPad.release();
}
}
}

// update other buttons
final InputButton jumpKey = keys[mJumpKeyCode];

// when on-screen movement controls are on, the fly and attack buttons are flipped.
float flyButtonRegionX = ButtonConstants.FLY_BUTTON_REGION_X;
float stompButtonRegionX = ButtonConstants.STOMP_BUTTON_REGION_X;

if (mUseOnScreenControls) {
ContextParameters params = sSystemRegistry.contextParameters;
flyButtonRegionX = params.gameWidth - ButtonConstants.FLY_BUTTON_REGION_WIDTH - ButtonConstants.FLY_BUTTON_REGION_X;
stompButtonRegionX = params.gameWidth - ButtonConstants.STOMP_BUTTON_REGION_WIDTH - ButtonConstants.STOMP_BUTTON_REGION_X;
}

final InputXY jumpTouch = touch.findPointerInRegion(
flyButtonRegionX,
ButtonConstants.FLY_BUTTON_REGION_Y,
ButtonConstants.FLY_BUTTON_REGION_WIDTH,
ButtonConstants.FLY_BUTTON_REGION_HEIGHT);

if (jumpKey.getPressed()) {
mJumpButton.press(jumpKey.getLastPressedTime(), jumpKey.getMagnitude());
} else if (jumpTouch != null) {
if (!mJumpButton.getPressed()) {
mJumpButton.press(jumpTouch.getLastPressedTime(), 1.0f);
}
} else {
mJumpButton.release();
}

final InputButton attackKey = keys[mAttackKeyCode];
final InputButton clickButton = keys[KeyEvent.KEYCODE_DPAD_CENTER]; // special case

final InputXY stompTouch = touch.findPointerInRegion(
stompButtonRegionX,
ButtonConstants.STOMP_BUTTON_REGION_Y,
ButtonConstants.STOMP_BUTTON_REGION_WIDTH,
ButtonConstants.STOMP_BUTTON_REGION_HEIGHT);

if (mUseClickButtonForAttack && clickButton.getPressed()) {
mAttackButton.press(clickButton.getLastPressedTime(), clickButton.getMagnitude());
} else if (attackKey.getPressed()) {
mAttackButton.press(attackKey.getLastPressedTime(), attackKey.getMagnitude());
} else if (stompTouch != null) {
// Since touch events come in constantly, we only want to press the attack button
// here if it's not already down. That makes it act like the other buttons (down once then up).
if (!mAttackButton.getPressed()) {
mAttackButton.press(stompTouch.getLastPressedTime(), 1.0f);
}
} else {
mAttackButton.release();
}

// This doesn't seem like exactly the right place to write to the HUD, but on the other hand,
// putting this code elsewhere causes dependencies between exact HUD content and physics, which
// we sometimes wish to avoid.
final HudSystem hud = sSystemRegistry.hudSystem;
if (hud != null) {
hud.setButtonState(mJumpButton.getPressed(), mAttackButton.getPressed(), mDirectionalPad.getPressed());
hud.setMovementSliderOffset(sliderOffset);
}
}


private float filterOrientationForMovement(float magnitude) {
float scaledMagnitude = magnitude * mOrientationSensitivityFactor;

return deadZoneFilter(scaledMagnitude, mOrientationDeadZoneMin, mOrientationDeadZoneMax, mOrientationDeadZoneScale);
}

private float deadZoneFilter(float magnitude, float min, float max, float scale) {
float smoothedMagnatude = magnitude;
if (Math.abs(magnitude) < min) {
smoothedMagnatude = 0.0f; // dead zone
} else if (Math.abs(magnitude) < max) {
smoothedMagnatude *= scale;
}

return smoothedMagnatude;
}


public final InputXY getDirectionalPad() {
return mDirectionalPad;
}

public final InputXY getTilt() {
return mTilt;
}

public final InputButton getJumpButton() {
return mJumpButton;
}

public final InputButton getAttackButton() {
return mAttackButton;
}

public void setKeys(int left, int right, int jump, int attack) {
mLeftKeyCode = left;
mRightKeyCode = right;
mJumpKeyCode = jump;
mAttackKeyCode = attack;
}

public void setUseClickForAttack(boolean click) {
mUseClickButtonForAttack = click;
}

public void setUseOrientationForMovement(boolean orientation) {
mUseOrientationForMovement = orientation;
}

public void setOrientationMovementSensitivity(float sensitivity) {
mOrientationSensitivity = sensitivity;
mOrientationSensitivityFactor = 2.9f * sensitivity + 0.1f;
}

public void setMovementSensitivity(float sensitivity) {
mMovementSensitivity = sensitivity;
}

public void setUseOnScreenControls(boolean onscreen) {
mUseOnScreenControls = onscreen;
}

}

Change log

r7 by sm0a9f4 on Jan 15, 2011   Diff
+ Fixed lots of bugs.  Most notably a
divide-by-zero case in the collision
system.
+ Add fades to all activity transitions
(seems unreliable, though; race
condition?)
+ Add Extras Menu, unlockable Linear Mode
and Level Select
+ Add difficulty settings: Baby, Kids, and
Adult
+ Add on-screen control interface; add
support for muli-touch input events
...
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by sm0a9f4 on May 2, 2010   Diff
Public release v1.3.
+ Rewrote input system to better
abstract different hardware from game.
Major rework to this section.
+ Added nefarious hacks via "Safe
...
All revisions of this file

File info

Size: 11479 bytes, 321 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting