Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Jump Instruction #7

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.ArrayList;
import java.util.Random;

import ca.unb.mobiledev.reflexrevolution.sensors.JumpDetector;
import ca.unb.mobiledev.reflexrevolution.utils.Difficulty;
import ca.unb.mobiledev.reflexrevolution.utils.GameMode;
import ca.unb.mobiledev.reflexrevolution.utils.Instruction;
Expand All @@ -41,8 +42,10 @@ public class GameActivity extends AppCompatActivity {
private Difficulty difficulty;

private SensorManager sensorManager;
private Sensor accelerometer;
private Sensor accelerationSensor;
private Sensor gravitySensor;
private ShakeDetector shakeDetector;
private JumpDetector jumpDetector;

private int timeCount;
private int score;
Expand Down Expand Up @@ -73,16 +76,17 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void initializeSensors() {
if (instructions.contains(Instruction.SHAKE)) {
// Get accelerometer sensor
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);

// Initialize shake detector
shakeDetector = new ShakeDetector();
shakeDetector.setOnShakeListener(count -> {
if (count > 1) detectInput(Instruction.SHAKE);
});
}
// Get sensors
accelerationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

// Initialize detectors
shakeDetector = new ShakeDetector();
shakeDetector.setOnShakeListener(count -> {
if (count > 1) detectInput(Instruction.SHAKE);
});
jumpDetector = new JumpDetector();
jumpDetector.setOnJumpListener(() -> detectInput(Instruction.JUMP));
}

private void updateTimerText(){
Expand Down Expand Up @@ -159,6 +163,7 @@ private void gameLoop() {

//Display UI elements for the current instruction, and set up any necessary input receivers
private void displayInstruction() {
TextView label;
switch (currentInstruction){
case BUTTON:
Button button = new Button(this);
Expand All @@ -167,15 +172,21 @@ private void displayInstruction() {
button.setOnClickListener(v -> detectInput(Instruction.BUTTON));
layout.addView(button);
break;

case SHAKE:
TextView label = new TextView(this);
label = new TextView(this);
label.setText("SHAKE");
label.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 34);
layout.addView(label);
break;

default:
case JUMP:
label = new TextView(this);
label.setText("JUMP");
label.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
label.setTextSize(TypedValue.COMPLEX_UNIT_SP, 34);
layout.addView(label);
break;
}
}
Expand Down Expand Up @@ -213,7 +224,11 @@ private void registerListeners() {
if (currentInstruction == null) return;
switch (currentInstruction) {
case SHAKE:
sensorManager.registerListener(shakeDetector, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(shakeDetector, accelerationSensor, SensorManager.SENSOR_DELAY_NORMAL);
break;
case JUMP:
sensorManager.registerListener(jumpDetector, accelerationSensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(jumpDetector, gravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
break;
}
}
Expand All @@ -224,6 +239,9 @@ private void unregisterListeners() {
case SHAKE:
sensorManager.unregisterListener(shakeDetector);
break;
case JUMP:
sensorManager.unregisterListener(jumpDetector);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ca.unb.mobiledev.reflexrevolution.activities.tests;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import ca.unb.mobiledev.reflexrevolution.R;
import ca.unb.mobiledev.reflexrevolution.sensors.JumpDetector;

public class JumpTestActivity extends AppCompatActivity {

private TextView mainLabel;
private SensorManager sensorManager;
private Sensor accelerometer;
private Sensor gravitySensor;
private JumpDetector jumpDetector;
private int count;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

mainLabel = findViewById(R.id.mainLabel);
count = 0;

// Get sensors
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

// Initialize jump detector
mainLabel.setText("Jump Detected: 0");
jumpDetector = new JumpDetector();
jumpDetector.setOnJumpListener(() -> mainLabel.setText("Jump Detected: "+(++count)));
}

@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(jumpDetector, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(jumpDetector, gravitySensor, SensorManager.SENSOR_DELAY_NORMAL);
}

@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(jumpDetector);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class ShakeTestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shake_test);
setContentView(R.layout.activity_test);

shakeText = findViewById(R.id.shakeText);
shakeText = findViewById(R.id.mainLabel);

// Get accelerometer sensor
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ca.unb.mobiledev.reflexrevolution.sensors;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.util.Log;

public class JumpDetector implements SensorEventListener {

private static final float JUMP_THRESHOLD_FORCE = 4.0f;

private OnJumpListener mListener;
private float[] upVector;

public void setOnJumpListener(OnJumpListener listener) {
this.mListener = listener;
}

public interface OnJumpListener {
void onJump();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Can do something if accuracy of the sensor changes
}

@Override
public void onSensorChanged(SensorEvent event) {
if (mListener != null) {
int sensorType = event.sensor.getType();
if (sensorType == Sensor.TYPE_GRAVITY) {
// Get a normalized up vector based on gravity
float[] gravityVector = event.values;
vectorNormalize(gravityVector);
vectorInverse(gravityVector);
upVector = gravityVector;
}
else if (sensorType == Sensor.TYPE_LINEAR_ACCELERATION) {
if (upVector == null) return;

// Scalar projection of the acceleration onto gravity vector
float[] acceleration = event.values;
float upComponent = Math.abs(vectorDotProduct(acceleration, upVector));

if (upComponent > JUMP_THRESHOLD_FORCE) {
mListener.onJump();
}
}
}
}

private double vectorMagnitude(float[] v) {
return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}

private void vectorNormalize(float[] v) {
double magnitude = vectorMagnitude(v);
v[0] = (float) (v[0] / magnitude);
v[1] = (float) (v[1] / magnitude);
v[2] = (float) (v[2] / magnitude);
}

private void vectorInverse(float[] v) {
v[0] = -v[0];
v[1] = -v[1];
v[2] = -v[2];
}

private float vectorDotProduct(float[] v1, float[] v2) {
return (v1[0]*v2[0]) + (v1[1]*v2[1]) + (v1[2]*v2[2]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum Instruction {
BUTTON, //Temp instruction for testing
TAP,
SWIPE,
SHAKE
SHAKE,
JUMP
// Will add more later
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ public class InstructionUtil {

public static ArrayList<Instruction> createInstructions(GameMode mode, Context context){
SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
Sensor gravitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);

ArrayList<Instruction> instructions = new ArrayList<>();

//Construct list of instructions based on gamemode
switch(mode){
case BASIC:
instructions.add(Instruction.BUTTON);
Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
if (accelerometer != null) instructions.add(Instruction.SHAKE);
if (accelerometer != null && gravitySensor != null) instructions.add(Instruction.JUMP);
break;

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
tools:context=".activities.tests.ShakeTestActivity">

<TextView
android:id="@+id/shakeText"
android:id="@+id/mainLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
Expand Down