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 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
/.idea/deploymentTargetDropDown.xml
.DS_Store
/build
/captures
Expand Down
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
android:supportsRtl="true"
android:theme="@style/Theme.ReflexRevolution">
<activity
android:name=".MainActivity"
android:name=".activities.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameActivity" />
<activity android:name=".GameOverActivity" />
<activity android:name=".activities.GameActivity" />
<activity android:name=".activities.GameOverActivity" />
</application>

</manifest>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package ca.unb.mobiledev.reflexrevolution;
package ca.unb.mobiledev.reflexrevolution.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
Expand All @@ -13,6 +17,14 @@
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;
import ca.unb.mobiledev.reflexrevolution.utils.InstructionUtil;
import ca.unb.mobiledev.reflexrevolution.R;
import ca.unb.mobiledev.reflexrevolution.sensors.ShakeDetector;

public class GameActivity extends AppCompatActivity {
//Time in ms
//Could get rid of this and do random intervals or scale it off of score
Expand All @@ -29,6 +41,12 @@ public class GameActivity extends AppCompatActivity {
private GameMode gameMode;
private Difficulty difficulty;

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

private int timeCount;
private int score;

Expand All @@ -49,12 +67,28 @@ protected void onCreate(Bundle savedInstanceState) {
layout = findViewById(R.id.layout);
score = 0;

instructions = InstructionUtil.createInstructions(gameMode);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
instructions = InstructionUtil.createInstructions(gameMode, this);
initializeSensors();
updateTimerText();
updateScoreText();
resetTimer();
}

private void initializeSensors() {
// 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(){
timeText.setText("Timer: " + timeCount);
}
Expand Down Expand Up @@ -120,6 +154,7 @@ public void onFinish() {
private void gameLoop() {
currentInstruction = getRandomInstruction();
displayInstruction();
registerListeners();
newTimer();
}

Expand All @@ -128,21 +163,30 @@ 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);
button.setText("Press");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
detectInput(Instruction.BUTTON);
}
});

button.setText("PRESS");
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 34);
button.setOnClickListener(v -> detectInput(Instruction.BUTTON));
layout.addView(button);
break;

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

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 All @@ -164,31 +208,55 @@ private void detectInput(Instruction instruction) {
//Stop timer and clear UI, wait one second, then start the gameloop (in resetTimer)
resetTimer();
resetUI();
unregisterListeners();
}
}

//Scuffed function that will give a scaled timer based on score.
//Starts at ~3000ms and min value is ~1000ms
//I literally came up with this from playing around in desmos so we might want to rework this later
//Probably adjust this based on difficulty later
private int scaleTimerFromScore(){
int scaledTimer = (int)Math.pow(2, -0.04*score + 11) + 1000;
return scaledTimer;
private int scaleTimerFromScore() {
return (int)Math.pow(2, -0.04*score + 11) + 1000;
}

private void registerListeners() {
if (currentInstruction == null) return;
switch (currentInstruction) {
case SHAKE:
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;
}
}

private void unregisterListeners() {
if (currentInstruction == null) return;
switch (currentInstruction) {
case SHAKE:
sensorManager.unregisterListener(shakeDetector);
break;
case JUMP:
sensorManager.unregisterListener(jumpDetector);
}
}

//Resume timer if app was closed
@Override
protected void onResume() {
super.onResume();
if(timer != null) {
resumeTimer();
}
registerListeners();
if(timer != null) resumeTimer();
}

//Make sure timer doesn't keep going with app closed
@Override
protected void onPause() {
super.onPause();
unregisterListeners();
timer.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ca.unb.mobiledev.reflexrevolution;
package ca.unb.mobiledev.reflexrevolution.activities;

import android.content.Intent;
import android.os.Bundle;
Expand All @@ -8,6 +8,10 @@

import androidx.appcompat.app.AppCompatActivity;

import ca.unb.mobiledev.reflexrevolution.utils.Difficulty;
import ca.unb.mobiledev.reflexrevolution.utils.GameMode;
import ca.unb.mobiledev.reflexrevolution.R;

public class GameOverActivity extends AppCompatActivity {

private TextView scoreText;
Expand All @@ -34,23 +38,15 @@ protected void onCreate(Bundle savedInstanceState) {
}

//Start activity for new game if "Play Again" is hit
replayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(GameOverActivity.this, GameActivity.class);
intent.putExtra("GameMode", gameMode);
intent.putExtra("Difficulty", difficulty);
startActivity(intent);
finish();
}
replayButton.setOnClickListener(v -> {
Intent intent = new Intent(GameOverActivity.this, GameActivity.class);
intent.putExtra("GameMode", gameMode);
intent.putExtra("Difficulty", difficulty);
startActivity(intent);
finish();
});

//Close this activity if "Menu" button is hit, returning to menu
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
menuButton.setOnClickListener(v -> finish());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ca.unb.mobiledev.reflexrevolution.activities;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import ca.unb.mobiledev.reflexrevolution.utils.Difficulty;
import ca.unb.mobiledev.reflexrevolution.utils.GameMode;
import ca.unb.mobiledev.reflexrevolution.R;

public class MainActivity extends AppCompatActivity {

private Button startButton;

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

startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, GameActivity.class);
intent.putExtra("GameMode", GameMode.BASIC);
intent.putExtra("Difficulty", Difficulty.NORMAL);
startActivity(intent);
});
}
}