Commit a904da3d authored by Lila NICKLER's avatar Lila NICKLER

Ajout de deux asteroides + animation

parent 3e84d2bb
......@@ -17,6 +17,7 @@ import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.PathInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
......@@ -33,6 +34,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
ImageView asteroid1;
ImageView asteroid2;
ImageView asteroid3;
ImageView asteroid4;
ImageView tie;
ImageView explosion;
private SensorManager mSensorManager;
......@@ -50,10 +52,17 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1);
asteroid2 = findViewById(R.id.asteroid2);
asteroid3 = findViewById(R.id.asteroid3);
asteroid4 = findViewById(R.id.asteroid4);
explosion=findViewById(R.id.explosion);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
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
float height = metrics.heightPixels;
float width = metrics.widthPixels;
......@@ -61,18 +70,13 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
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 arc1 = new Path();
//path.arcTo(0f, 0f, 800f, 1200f, -270f, -180f, true);
arc1.arcTo(oval,-80,180);
ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, arc1);
animator.setDuration(2000);
animator.setDuration(3050);
animator.setRepeatCount(Animation.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
......@@ -89,15 +93,14 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
animator.start();
Path line1 = new Path();
//path2.arcTo(100f, 50f, width/4f,(3*height)/5, -150f, -190f, true);
line1.lineTo((3*width)/4,(3*height)/5);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(asteroid2, View.X, View.Y, line1);
animator2.setDuration(2400);
animator2.setDuration(2350);
animator2.setRepeatCount(Animation.INFINITE);
animator2.setRepeatMode(ValueAnimator.REVERSE);
animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
public void onAnimationUpdate(ValueAnimator animation2) {
if(sontEnCollision(asteroid2,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
......@@ -108,32 +111,70 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
});
animator2.start();
Path arc2 = new Path();
arc2.arcTo(100f, 50f, width/3f,(3*height)/5, 300f, -200f, true);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(asteroid3, View.X, View.Y, arc2);
animator3.setDuration(2500);
animator3.setRepeatCount(Animation.INFINITE);
animator3.setRepeatMode(ValueAnimator.REVERSE);
animator3.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation3) {
if(sontEnCollision(asteroid3,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
animator3.start();
Path ovalPath = new Path();
RectF oval2 = new RectF(10,(height)/5, (2*width)/3, (2*height)/5);
ovalPath.addOval(oval2, Path.Direction.CW);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(asteroid4, View.X, View.Y, ovalPath);
animator4.setupStartValues();
animator4.setDuration(3200);
animator4.setRepeatCount(Animation.INFINITE);
animator4.setRepeatMode(ValueAnimator.REVERSE);
animator4.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation4) {
if(sontEnCollision(asteroid4,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
animator4.start();
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this,tie);
Handler handlerForMovingTie = new Handler();
Runnable movingTie = new Runnable() {
Runnable movingTieJoystick = new Runnable() {
@Override
public void run() {
// tie.setTranslationX(joyStickView.xDelta);
// tie.setTranslationY(joyStickView.yDelta);
Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed);
if (joyStickView.joystickIsPressed) {
Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show();
handlerForMovingTie.postDelayed(this, 10);
mainLayout.invalidate();
}
padCenter.setOnTouchListener(joyStickView.touchListener);
}
};
handlerForMovingTie.post(movingTie);
Thread threadTie = new Thread(movingTie);
Thread threadTie = new Thread(movingTieJoystick);
threadTie.start();
Log.i("TAG", "Thread:"+Thread.currentThread().getName());
Log.i("tag", "Hello");
padCenter.setOnTouchListener(joyStickView.touchListener);
Runnable movingTieSensor = new Runnable() {
@Override
public void run() {
mSensorManager.registerListener((SensorEventListener)MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
};
Thread threadTieSensor = new Thread(movingTieSensor);
threadTieSensor.start();
}
private void showExplosion() {
......@@ -160,7 +201,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
float gammaX = event.values[0], gammaY = event.values[1], gammaZ = event.values[2];
//move vaisseau
tie.setX(tie.getX()-((gammaX)*4));
tie.setX(tie.getX()-((gammaX)*4));
tie.setY(tie.getY()+((gammaY)*4));
if(tie.getX()<0){
......
......@@ -87,10 +87,32 @@
android:id="@+id/asteroid1"
android:layout_width="93dp"
android:layout_height="79dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="100dp"
android:layout_marginRight="100dp"
app:srcCompat="@drawable/asteroid1" />
android:layout_marginStart="110dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="50dp"
app:srcCompat="@drawable/asteroid1"
android:layout_marginLeft="150dp" />
<ImageView
android:id="@+id/asteroid3"
android:layout_width="98dp"
android:layout_height="60dp"
android:layout_marginStart="250dp"
android:layout_marginTop="250dp"
android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid3"
android:layout_marginLeft="200dp" />
<ImageView
android:id="@+id/asteroid4"
android:layout_width="80dp"
android:layout_height="74dp"
android:layout_marginStart="20dp"
android:layout_marginTop="290dp"
android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid4"
android:layout_marginLeft="120dp" />
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
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