Commit 20d67e34 authored by Benjamin LEROUX's avatar Benjamin LEROUX

Sensor & explosion commit

parent 3beb84ce
...@@ -3,31 +3,32 @@ package com.imtm1.starwarsgame; ...@@ -3,31 +3,32 @@ package com.imtm1.starwarsgame;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.Animation; import android.view.animation.Animation;
import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.Toast; import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements SensorEventListener{
ImageView padExte; ImageView padExte;
ImageView padCenter; ImageView padCenter;
...@@ -35,6 +36,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -35,6 +36,9 @@ public class MainActivity extends AppCompatActivity {
ImageView asteroid1; ImageView asteroid1;
ImageView asteroid2; ImageView asteroid2;
ImageView tie; ImageView tie;
ImageView explosion;
private SensorManager mSensorManager;
private Sensor accelerometer;
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
...@@ -48,8 +52,17 @@ public class MainActivity extends AppCompatActivity { ...@@ -48,8 +52,17 @@ public class MainActivity extends AppCompatActivity {
mainLayout = (RelativeLayout) findViewById(R.id.main); mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1); asteroid1 = findViewById(R.id.asteroid1);
asteroid2 = findViewById(R.id.asteroid2); asteroid2 = findViewById(R.id.asteroid2);
explosion=findViewById(R.id.explosion);
tie = findViewById(R.id.tie); tie = findViewById(R.id.tie);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
// on recupere l accelerometre a partir du SensorManager
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// on associe l ecouteur d’evenements au SensorManager
mSensorManager.registerListener((SensorEventListener) this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
Path path = new Path(); Path path = new Path();
path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true); path.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, path); ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, path);
...@@ -60,6 +73,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -60,6 +73,9 @@ public class MainActivity extends AppCompatActivity {
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid1,tie)){ if(sontEnCollision(asteroid1,tie)){
Log.i("collision", "true"); Log.i("collision", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
} }
} }
}); });
...@@ -73,6 +89,9 @@ public class MainActivity extends AppCompatActivity { ...@@ -73,6 +89,9 @@ public class MainActivity extends AppCompatActivity {
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid2,tie)){ if(sontEnCollision(asteroid2,tie)){
Log.i("collision2", "true"); Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
} }
} }
}); });
...@@ -106,6 +125,44 @@ public class MainActivity extends AppCompatActivity { ...@@ -106,6 +125,44 @@ public class MainActivity extends AppCompatActivity {
} }
private void showExplosion() {
explosion.setVisibility(View.VISIBLE);
explosion.postDelayed(new Runnable() {
@Override
public void run() {
explosion.setVisibility(View.INVISIBLE);
}
}, 500); // produce 5 sec delay in button visibility
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {// TODO Auto−generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {// TODO Auto−generated method stub
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
float gammaX = event.values[0], gammaY = event.values[1], gammaZ = event.values[2];
//move vaisseau
tie.setX(tie.getX()-((gammaX)*4));
tie.setY(tie.getY()+((gammaY)*4));
if(tie.getX()<0){
tie.setX(0);
}else if(tie.getX()>=(width-tie.getWidth())){
tie.setX(width-tie.getWidth());
}
if(tie.getY()<0){
tie.setY(0);
}else if(tie.getY()>=(height*0.6)){
tie.setY((float) (height*0.6));
}
}
boolean sontEnCollision(ImageView firstView, ImageView secondView) { boolean sontEnCollision(ImageView firstView, ImageView secondView) {
int [] firstPosition = new int[2]; int [] firstPosition = new int[2];
......
...@@ -67,6 +67,13 @@ ...@@ -67,6 +67,13 @@
android:layout_marginTop="150dp" android:layout_marginTop="150dp"
app:srcCompat="@drawable/tie" /> app:srcCompat="@drawable/tie" />
<ImageView
android:id="@+id/explosion"
android:layout_width="135dp"
android:layout_height="113dp"
android:visibility="invisible"
app:srcCompat="@drawable/explosion" />
<ImageView <ImageView
android:id="@+id/asteroid2" android:id="@+id/asteroid2"
android:layout_width="84dp" android:layout_width="84dp"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment