Commit f4ea1cff authored by Benjamin LEROUX's avatar Benjamin LEROUX

Restart & Game Over

parent 61d102eb
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".GameOver" android:theme="@style/Theme.GameOver">
</activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.imtm1.starwarsgame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
public class GameOver extends Activity {
Button buttonReco;
Button buttonLeave;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameover);
buttonReco = findViewById(R.id.backButton);
buttonLeave = findViewById(R.id.leaveButton);
buttonReco.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((ResultReceiver)getIntent().getParcelableExtra("finisher")).send(1, new Bundle()); //ferme l'activité précédente
startActivity(new Intent(GameOver.this,MainActivity.class)); //commence une nouvelle activité
finish();
}
});
buttonLeave.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
DisplayMetrics metrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height=metrics.heightPixels;
int width=metrics.widthPixels;
getWindow().setLayout((int)(width*0.9),(int)(height*0.3));
}
}
...@@ -17,12 +17,10 @@ import android.view.WindowManager; ...@@ -17,12 +17,10 @@ import android.view.WindowManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
class JoyStickView { class JoyStickView {
float xDelta; float xDelta;
float yDelta; float yDelta;
float origineX; float origineX;
...@@ -32,13 +30,11 @@ class JoyStickView { ...@@ -32,13 +30,11 @@ class JoyStickView {
Boolean joystickIsPressed=false; Boolean joystickIsPressed=false;
ImageView tie; ImageView tie;
public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie) public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie)
{ {
this.mainLayout = mainLayout; this.mainLayout = mainLayout;
this.context = context; this.context = context;
this.tie=tie; this.tie=tie;
} }
final View.OnTouchListener touchListener = new View.OnTouchListener() { final View.OnTouchListener touchListener = new View.OnTouchListener() {
...@@ -74,8 +70,9 @@ class JoyStickView { ...@@ -74,8 +70,9 @@ class JoyStickView {
v.setY(yDelta); v.setY(yDelta);
//move vaisseau //move vaisseau
tie.setX(tie.getX()+((xDelta - origineX)/20)); //on divise pour reduire la vitesse tie.setX(tie.getX()+((xDelta - origineX)/15)); //on divise pour reduire la vitesse
tie.setY(tie.getY()+((yDelta - origineY)/20)); tie.setY(tie.getY()+((yDelta - origineY)/15));
if(tie.getX()<0){ if(tie.getX()<0){
tie.setX(0); tie.setX(0);
......
...@@ -6,6 +6,8 @@ import android.animation.ObjectAnimator; ...@@ -6,6 +6,8 @@ import android.animation.ObjectAnimator;
import android.animation.ValueAnimator; import android.animation.ValueAnimator;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
...@@ -15,14 +17,17 @@ import android.media.SoundPool; ...@@ -15,14 +17,17 @@ import android.media.SoundPool;
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.ResultReceiver;
import android.util.DisplayMetrics; 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.view.animation.PathInterpolator;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.hardware.Sensor; import android.hardware.Sensor;
import android.hardware.SensorEvent; import android.hardware.SensorEvent;
...@@ -42,6 +47,16 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -42,6 +47,16 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
ImageView asteroid4; ImageView asteroid4;
ImageView tie; ImageView tie;
ImageView explosion; ImageView explosion;
MediaPlayer music;
TextView score;
TextView vie;
Button button;
SoundPool sounds;
int sExplosion;
int nbrVies;
boolean isInvincible;
boolean isPad;
boolean isPlaying;
private SensorManager mSensorManager; private SensorManager mSensorManager;
private Sensor accelerometer; private Sensor accelerometer;
...@@ -59,7 +74,19 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -59,7 +74,19 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
asteroid2 = findViewById(R.id.asteroid2); asteroid2 = findViewById(R.id.asteroid2);
asteroid3 = findViewById(R.id.asteroid3); asteroid3 = findViewById(R.id.asteroid3);
asteroid4 = findViewById(R.id.asteroid4); asteroid4 = findViewById(R.id.asteroid4);
tie = findViewById(R.id.tie);
button = findViewById(R.id.button);
nbrVies=5;
isInvincible=false;
isPad=true;
isPlaying=true;
score =findViewById(R.id.score);
score.setVisibility(View.INVISIBLE);
vie =findViewById(R.id.vie);
vie.setText("Vie: "+nbrVies);
explosion=findViewById(R.id.explosion); explosion=findViewById(R.id.explosion);
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
sExplosion = sounds.load(this, R.raw.hit, 1);
DisplayMetrics metrics = new DisplayMetrics(); DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics); getWindowManager().getDefaultDisplay().getMetrics(metrics);
...@@ -68,16 +95,26 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -68,16 +95,26 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// on associe l ecouteur d’evenements au SensorManager // on associe l ecouteur d’evenements au SensorManager
MediaPlayer music= MediaPlayer.create(MainActivity.this,R.raw.music); music= MediaPlayer.create(MainActivity.this,R.raw.music);
music.setLooping(true); music.setLooping(true);
music.setVolume(100,100); music.setVolume(50,50);
music.start(); music.start();
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isPad=!isPad;
if(isPad){
button.setText("Capteurs ON");
}else{
button.setText("Capteurs OFF");
}
}
});
float height = metrics.heightPixels; float height = metrics.heightPixels;
float width = metrics.widthPixels; float width = metrics.widthPixels;
RectF oval = new RectF(100,100, width-180, height/2); RectF oval = new RectF(100,100, width-180, height/2);
tie = findViewById(R.id.tie);
Path arc1 = new Path(); Path arc1 = new Path();
arc1.arcTo(oval,-80,180); arc1.arcTo(oval,-80,180);
ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, arc1); ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, arc1);
...@@ -88,11 +125,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -88,11 +125,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override @Override
public void onAnimationUpdate(ValueAnimator animation) { public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid1,tie)){ if(sontEnCollision(asteroid1,tie)){
Log.i("collision", "true");
explosion.setX(tie.getX()); explosion.setX(tie.getX());
explosion.setY(tie.getY()); explosion.setY(tie.getY());
showExplosion(); showExplosion();
} }
} }
}); });
...@@ -108,11 +143,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -108,11 +143,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override @Override
public void onAnimationUpdate(ValueAnimator animation2) { public void onAnimationUpdate(ValueAnimator animation2) {
if(sontEnCollision(asteroid2,tie)){ if(sontEnCollision(asteroid2,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX()); explosion.setX(tie.getX());
explosion.setY(tie.getY()); explosion.setY(tie.getY());
showExplosion(); showExplosion();
} }
} }
}); });
...@@ -128,11 +161,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -128,11 +161,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override @Override
public void onAnimationUpdate(ValueAnimator animation3) { public void onAnimationUpdate(ValueAnimator animation3) {
if(sontEnCollision(asteroid3,tie)){ if(sontEnCollision(asteroid3,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX()); explosion.setX(tie.getX());
explosion.setY(tie.getY()); explosion.setY(tie.getY());
showExplosion(); showExplosion();
} }
} }
}); });
...@@ -150,11 +181,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -150,11 +181,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override @Override
public void onAnimationUpdate(ValueAnimator animation4) { public void onAnimationUpdate(ValueAnimator animation4) {
if(sontEnCollision(asteroid4,tie)){ if(sontEnCollision(asteroid4,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX()); explosion.setX(tie.getX());
explosion.setY(tie.getY()); explosion.setY(tie.getY());
showExplosion(); showExplosion();
} }
} }
}); });
...@@ -166,7 +195,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -166,7 +195,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
Runnable movingTieJoystick = new Runnable() { Runnable movingTieJoystick = new Runnable() {
@Override @Override
public void run() { public void run() {
padCenter.setOnTouchListener(joyStickView.touchListener); padCenter.setOnTouchListener(joyStickView.touchListener);
} }
}; };
...@@ -177,7 +205,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -177,7 +205,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
Runnable movingTieSensor = new Runnable() { Runnable movingTieSensor = new Runnable() {
@Override @Override
public void run() { public void run() {
mSensorManager.registerListener((SensorEventListener)MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL); mSensorManager.registerListener((SensorEventListener)MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
} }
}; };
...@@ -186,22 +213,45 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -186,22 +213,45 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
threadTieSensor.start(); threadTieSensor.start();
} }
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showExplosion() { private void showExplosion() {
explosion.setVisibility(View.VISIBLE); if(!isInvincible && isPlaying){
SoundPool sounds; explosion.setVisibility(View.VISIBLE);
int sExplosion; isInvincible=true;
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0); sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);
sExplosion = sounds.load(this, R.raw.hit, 1); if(nbrVies>0){
nbrVies--;
vie.setText("Vie: "+nbrVies);
if(nbrVies==0){
music.stop();
isPlaying=false;
Intent GoToGameOver=new Intent(MainActivity.this,GameOver.class);
GoToGameOver.putExtra("finisher", new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
MainActivity.this.finish();
}
});
startActivityForResult(GoToGameOver,1);
}
}
tie.setColorFilter(Color.RED);
explosion.postDelayed(new Runnable() { explosion.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
explosion.setVisibility(View.INVISIBLE);
}
}, 500); // produce 0.5 sec delay in button visibility
explosion.setVisibility(View.INVISIBLE); explosion.postDelayed(new Runnable() {
// sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f); @Override
; public void run() {
} isInvincible=false;
}, 500); // produce 5 sec delay in button visibility tie.setColorFilter(0);
}
}, 1500);
}
} }
@Override @Override
...@@ -217,8 +267,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -217,8 +267,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
float gammaX = event.values[0], gammaY = event.values[1], gammaZ = event.values[2]; float gammaX = event.values[0], gammaY = event.values[1], gammaZ = event.values[2];
//move vaisseau //move vaisseau
tie.setX(tie.getX()-((gammaX)*4)); if(!isPad && isPlaying){
tie.setY(tie.getY()+((gammaY)*4)); tie.setX(tie.getX()-((gammaX)*4));
tie.setY(tie.getY()+((gammaY)*4));
}
if(tie.getX()<0){ if(tie.getX()<0){
tie.setX(0); tie.setX(0);
...@@ -227,8 +279,8 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -227,8 +279,8 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
} }
if(tie.getY()<0){ if(tie.getY()<0){
tie.setY(0); tie.setY(0);
}else if(tie.getY()>=(height*0.6)){ }else if(tie.getY()>=(height-(padCenter.getHeight()*3))){
tie.setY((float) (height*0.6)); tie.setY((float) (height-(padCenter.getHeight()*3)));
} }
} }
...@@ -239,7 +291,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen ...@@ -239,7 +291,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
firstView .getLocationOnScreen(firstPosition); firstView .getLocationOnScreen(firstPosition);
secondView.getLocationOnScreen(secondPosition); secondView.getLocationOnScreen(secondPosition);
Rect rectFirstView = new Rect(firstPosition [0], firstPosition [1], Rect rectFirstView = new Rect(firstPosition [0], firstPosition [1],
firstPosition [0] + firstView .getMeasuredWidth()/2, firstPosition [1] + firstView .getMeasuredHeight()/2); firstPosition [0] + firstView .getMeasuredWidth()/2, firstPosition [1] + firstView .getMeasuredHeight()/2);
Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1], Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1],
......
...@@ -49,15 +49,7 @@ ...@@ -49,15 +49,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|left" android:layout_gravity="bottom|left"
android:text="Joystick" android:text="Capteurs ON"
app:backgroundTint="#9C27B0" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="Capteurs"
app:backgroundTint="#9C27B0" /> app:backgroundTint="#9C27B0" />
</FrameLayout> </FrameLayout>
...@@ -70,8 +62,8 @@ ...@@ -70,8 +62,8 @@
<ImageView <ImageView
android:id="@+id/tie" android:id="@+id/tie"
android:layout_width="110dp" android:layout_width="76dp"
android:layout_height="55dp" android:layout_height="46dp"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginTop="150dp" android:layout_marginTop="150dp"
app:srcCompat="@drawable/tie" /> app:srcCompat="@drawable/tie" />
...@@ -85,8 +77,8 @@ ...@@ -85,8 +77,8 @@
<ImageView <ImageView
android:id="@+id/asteroid2" android:id="@+id/asteroid2"
android:layout_width="84dp" android:layout_width="76dp"
android:layout_height="72dp" android:layout_height="70dp"
android:layout_marginStart="50dp" android:layout_marginStart="50dp"
android:layout_marginLeft="80dp" android:layout_marginLeft="80dp"
android:layout_marginTop="150dp" android:layout_marginTop="150dp"
...@@ -94,40 +86,40 @@ ...@@ -94,40 +86,40 @@
<ImageView <ImageView
android:id="@+id/asteroid1" android:id="@+id/asteroid1"
android:layout_width="93dp" android:layout_width="68dp"
android:layout_height="79dp" android:layout_height="65dp"
android:layout_marginStart="110dp" android:layout_marginStart="110dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="10dp" android:layout_marginTop="10dp"
android:layout_marginEnd="10dp" android:layout_marginEnd="10dp"
android:layout_marginRight="50dp" android:layout_marginRight="50dp"
app:srcCompat="@drawable/asteroid1" app:srcCompat="@drawable/asteroid1" />
android:layout_marginLeft="150dp" />
<ImageView <ImageView
android:id="@+id/asteroid3" android:id="@+id/asteroid3"
android:layout_width="98dp" android:layout_width="80dp"
android:layout_height="60dp" android:layout_height="52dp"
android:layout_marginStart="250dp" android:layout_marginStart="250dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="250dp" android:layout_marginTop="250dp"
android:layout_marginRight="0dp" android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid3" app:srcCompat="@drawable/asteroid3" />
android:layout_marginLeft="200dp" />
<ImageView <ImageView
android:id="@+id/asteroid4" android:id="@+id/asteroid4"
android:layout_width="80dp" android:layout_width="51dp"
android:layout_height="74dp" android:layout_height="73dp"
android:layout_marginStart="20dp" android:layout_marginStart="20dp"
android:layout_marginLeft="120dp"
android:layout_marginTop="290dp" android:layout_marginTop="290dp"
android:layout_marginRight="0dp" android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid4" app:srcCompat="@drawable/asteroid4" />
android:layout_marginLeft="120dp" />
<TextView <TextView
android:id="@+id/vie" android:id="@+id/vie"
android:layout_width="103dp" android:layout_width="103dp"
android:layout_height="36dp" android:layout_height="36dp"
android:layout_gravity="right" android:layout_gravity="left"
android:text="Vie" android:text="Vie"
android:textSize="30dp" android:textSize="30dp"
...@@ -139,9 +131,10 @@ ...@@ -139,9 +131,10 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="left" android:layout_gravity="left"
android:text="Score" android:text="Score:"
android:textSize="30dp" android:textSize="30dp"
android:textStyle="bold" /> android:textStyle="bold" />
</FrameLayout> </FrameLayout>
</RelativeLayout> </RelativeLayout>
</RelativeLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="80dp"
android:text="DEFAITE !"
android:textSize="30dp"
android:textStyle="bold" />
<Button
android:id="@+id/backButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="120dp"
android:text="RECOMMENCER" />
<Button
android:id="@+id/leaveButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="190dp"
android:layout_marginTop="120dp"
android:text="QUITTER" />
</RelativeLayout>
\ No newline at end of file
...@@ -13,4 +13,8 @@ ...@@ -13,4 +13,8 @@
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. --> <!-- Customize your theme here. -->
</style> </style>
<style name="Theme.GameOver" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:windowIsTranslucent">true</item>
</style>
</resources> </resources>
\ No newline at end of file
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