Skip to content
This repository was archived by the owner on Dec 17, 2021. It is now read-only.

Commit 8a297a4

Browse files
committed
MVC correctly implemented, Vibration added
1 parent dac7a65 commit 8a297a4

6 files changed

Lines changed: 209 additions & 6 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ android {
55
buildToolsVersion "26.0.0"
66
defaultConfig {
77
applicationId "com.example.claudio.fency"
8-
minSdkVersion 15
8+
minSdkVersion 19
99
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.example.claudio.fency">
44

5+
<!-- abilita vibrazione -->
6+
<uses-permission android:name="android.permission.VIBRATE" />
7+
58
<application
69
android:allowBackup="true"
710
android:icon="@mipmap/ic_launcher"

app/src/main/java/com/example/claudio/fency/Player.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ public Player(PracticeModeActivity activity){
1313
}
1414

1515
public void changeState(int to){
16-
state = to; //LOL
16+
//if(state != to){
17+
state = to;
18+
19+
activity.updateView(this);
20+
//}
21+
22+
1723
}
1824

1925
public String toString(){
2026
String str = "Stato = "+ state;
2127
return str;
2228
}
29+
30+
public int getState() {
31+
return state;
32+
}
2333
}

app/src/main/java/com/example/claudio/fency/PracticeModeActivity.java

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.example.claudio.fency;
22

33
import android.os.Bundle;
4+
import android.os.Handler;
5+
import android.os.Vibrator;
46
import android.widget.ImageView;
57
import android.widget.TextView;
68

@@ -10,6 +12,9 @@
1012

1113
public class PracticeModeActivity extends FencyActivity {
1214

15+
private static final long ATTACK_ANIMATION_DELAY = 500; //milliseconds
16+
private final int vibrationLength = 100;
17+
1318
private Player discipulus;
1419
private Player magister;
1520
private SensorHandler discipuliArbiter;
@@ -18,7 +23,9 @@ public class PracticeModeActivity extends FencyActivity {
1823
private ImageView magistriIcon;
1924
private TextView imperium;
2025
private TextView approbatione;
21-
26+
private boolean attackAnimationIsOn = false;
27+
private Handler handler;
28+
private Vibrator vibrator;
2229

2330
@Override
2431
protected void onCreate(Bundle savedInstanceState){
@@ -27,6 +34,66 @@ protected void onCreate(Bundle savedInstanceState){
2734
cntFullScreen = findViewById(R.id.container_practice);
2835
goFullScreen();
2936

30-
//molte cose
37+
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
38+
handler = new Handler();
39+
discipulus = new Player(this);
40+
magister = new Player(this);
41+
disipuliIcon = (ImageView)findViewById(R.id.ivPlayerState);
42+
magistriIcon = (ImageView)findViewById(R.id.ivOpponentState);
43+
imperium = (TextView)findViewById(R.id.tvCommand);
44+
approbatione = (TextView)findViewById(R.id.tvCheck);
45+
46+
discipuliArbiter = new SensorHandler(this, discipulus);
47+
discipuliArbiter.registerListeners();
48+
magistriArbiter = new DummyHandler(this, magister);
49+
magistriArbiter.step();
50+
}
51+
52+
public void updateView(Player caller) {
53+
int state = caller.getState();
54+
ImageView icon = null;
55+
56+
if(caller.equals(discipulus)){
57+
icon = disipuliIcon;
58+
}
59+
else if(caller.equals(magister)){
60+
icon = magistriIcon;
61+
}
62+
63+
icon.setImageAlpha(255);
64+
65+
if(!attackAnimationIsOn) {
66+
//change player img
67+
switch (state) {
68+
case R.integer.HIGH_STAND:
69+
icon.setImageResource(R.mipmap.fency_high_stand);
70+
break;
71+
case R.integer.LOW_STAND:
72+
icon.setImageResource(R.mipmap.fency_low_stand);
73+
break;
74+
case R.integer.HIGH_ATTACK:
75+
icon.setImageResource(R.mipmap.fency_high_attack);
76+
break;
77+
case R.integer.LOW_ATTACK:
78+
icon.setImageResource(R.mipmap.fency_low_attack);
79+
break;
80+
case R.integer.INVALID:
81+
icon.setImageAlpha(80);
82+
break;
83+
}
84+
if (state==R.integer.HIGH_ATTACK || state==R.integer.LOW_ATTACK){
85+
vibrator.vibrate(vibrationLength);
86+
87+
attackAnimationIsOn = true;
88+
89+
handler.postDelayed(new Runnable() {
90+
@Override
91+
public void run() {
92+
// Allow player image change only after delay
93+
attackAnimationIsOn = false;
94+
}
95+
}, ATTACK_ANIMATION_DELAY);
96+
}
97+
}
3198
}
3299
}

app/src/main/java/com/example/claudio/fency/SensorHandler.java

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
public class SensorHandler extends FencyHandler implements SensorEventListener {
1313

14+
private final long attackMinLength = 350000000; //nanoseconds
15+
private final float sogliaY = 5.0f;
16+
private final int sogliaRoll = 55, pitch_upbound = -60, pitch_midbound = -5, pitch_lowbound = 60;
17+
1418
private SensorFusion sensorFusion;
1519
private SensorManager sensorManager;
1620
private Sensor sensorLinearAcceleration;
@@ -22,9 +26,14 @@ public class SensorHandler extends FencyHandler implements SensorEventListener {
2226

2327
public SensorHandler(FencyActivity context, Player player) {
2428
super(context, player);
29+
30+
sensorManager = (SensorManager)context.getSystemService(context.SENSOR_SERVICE);
31+
sensorLinearAcceleration = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
32+
sensorFusion = new SensorFusion();
33+
sensorFusion.setMode(SensorFusion.Mode.GYRO);
2534
}
2635

27-
public void registerSensorManagerListeners() {
36+
public void registerListeners() {
2837
// Register 3 sensors for SensorFusion
2938
sensorManager.registerListener(this,
3039
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
@@ -43,8 +52,114 @@ public void registerSensorManagerListeners() {
4352
}
4453

4554
@Override
46-
public void onSensorChanged(SensorEvent sensorEvent) {
55+
public void onSensorChanged(SensorEvent event) {
56+
int id = event.sensor.getType();
57+
//handle SensorFusion cases
58+
switch (id) {
59+
case Sensor.TYPE_ACCELEROMETER:
60+
sensorFusion.setAccel(event.values);
61+
sensorFusion.calculateAccMagOrientation();
62+
break;
63+
64+
case Sensor.TYPE_GYROSCOPE:
65+
sensorFusion.gyroFunction(event);
66+
break;
67+
68+
case Sensor.TYPE_MAGNETIC_FIELD:
69+
sensorFusion.setMagnet(event.values);
70+
break;
71+
}
72+
73+
if (id == Sensor.TYPE_LINEAR_ACCELERATION) {
74+
75+
float yCurrent = event.values[1];
76+
double pitch = sensorFusion.getPitch();
77+
double roll = sensorFusion.getRoll();
78+
79+
if (roll > sogliaRoll || roll < -sogliaRoll) {
80+
//("Invalid")
81+
player.changeState(R.integer.INVALID);
82+
} else if (pitch > pitch_upbound && pitch < pitch_midbound) {
83+
//("High Guard")
84+
player.changeState(R.integer.HIGH_STAND);
85+
} else if (pitch >= pitch_midbound && pitch < pitch_lowbound) {
86+
//("Low Guard")
87+
player.changeState(R.integer.LOW_STAND);
88+
} else {
89+
//("Invalid")
90+
player.changeState(R.integer.INVALID);
91+
}
92+
93+
if(start){
94+
// Initialize last y
95+
yPrevious = yCurrent;
96+
start = false;
97+
}
98+
else {
99+
yDelta = yCurrent - yPrevious;
47100

101+
switch (state) {
102+
case 0:
103+
if (yCurrent >= -sogliaY && yCurrent <= sogliaY) {
104+
//stay in state 0
105+
}
106+
else if (yCurrent > sogliaY && yDelta > 0
107+
&& player.getState() != R.integer.INVALID) {
108+
//save the starting time of the (possible) attack
109+
startingAttackTime = event.timestamp;
110+
//go to state 1
111+
state = 1;
112+
}
113+
else if (yCurrent < -sogliaY && yDelta < 0){
114+
//go to state -1
115+
state = -1;
116+
}
117+
break;
118+
case 1:
119+
//waiting for positive spike
120+
if (yDelta < 0) {
121+
peak = yCurrent;
122+
//go to state 2
123+
state = 2;
124+
}
125+
break;
126+
case 2:
127+
//waiting for negative spike
128+
if (yDelta > 0) {
129+
float spike2 = yCurrent;
130+
if (player.getState() != R.integer.INVALID &&
131+
(peak + spike2 < 0) && (event.timestamp- startingAttackTime > attackMinLength)) {
132+
//AFFONDO!
133+
if(player.getState()==R.integer.LOW_STAND)
134+
player.changeState(R.integer.LOW_ATTACK);
135+
else if (player.getState()==R.integer.HIGH_STAND)
136+
player.changeState(R.integer.HIGH_ATTACK);
137+
}
138+
startingAttackTime = 0;
139+
peak = 0;
140+
//go to state 0
141+
state = 0;
142+
}
143+
break;
144+
case -1:
145+
//waiting for negative spike
146+
if(yDelta>0){
147+
//go to state -2
148+
state = -2;
149+
}
150+
break;
151+
case -2:
152+
//waiting for positive spike
153+
if (yDelta<0){
154+
//back to state 0 (idle)
155+
state = 0;
156+
}
157+
break;
158+
default:
159+
break;
160+
}
161+
}
162+
}
48163
}
49164

50165
@Override

app/src/main/res/values/states.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<integer name="INVALID">-1</integer>
4+
<integer name="HIGH_STAND">0</integer>
5+
<integer name="LOW_STAND">1</integer>
6+
<integer name="HIGH_ATTACK">2</integer>
7+
<integer name="LOW_ATTACK">3</integer>
8+
</resources>

0 commit comments

Comments
 (0)