Commit f4ea1cff authored by Benjamin LEROUX's avatar Benjamin LEROUX

Restart & Game Over

parent 61d102eb
......@@ -16,6 +16,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameOver" android:theme="@style/Theme.GameOver">
</activity>
</application>
</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;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
class JoyStickView {
float xDelta;
float yDelta;
float origineX;
......@@ -32,13 +30,11 @@ class JoyStickView {
Boolean joystickIsPressed=false;
ImageView tie;
public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie)
{
this.mainLayout = mainLayout;
this.context = context;
this.tie=tie;
}
final View.OnTouchListener touchListener = new View.OnTouchListener() {
......@@ -74,8 +70,9 @@ class JoyStickView {
v.setY(yDelta);
//move vaisseau
tie.setX(tie.getX()+((xDelta - origineX)/20)); //on divise pour reduire la vitesse
tie.setY(tie.getY()+((yDelta - origineY)/20));
tie.setX(tie.getX()+((xDelta - origineX)/15)); //on divise pour reduire la vitesse
tie.setY(tie.getY()+((yDelta - origineY)/15));
if(tie.getX()<0){
tie.setX(0);
......
......@@ -6,6 +6,8 @@ import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
......@@ -15,14 +17,17 @@ import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.DisplayMetrics;
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.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
......@@ -42,6 +47,16 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
ImageView asteroid4;
ImageView tie;
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 Sensor accelerometer;
......@@ -59,7 +74,19 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
asteroid2 = findViewById(R.id.asteroid2);
asteroid3 = findViewById(R.id.asteroid3);
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);
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
sExplosion = sounds.load(this, R.raw.hit, 1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
......@@ -68,16 +95,26 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// 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.setVolume(100,100);
music.setVolume(50,50);
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 width = metrics.widthPixels;
RectF oval = new RectF(100,100, width-180, height/2);
tie = findViewById(R.id.tie);
Path arc1 = new Path();
arc1.arcTo(oval,-80,180);
ObjectAnimator animator = ObjectAnimator.ofFloat(asteroid1, View.X, View.Y, arc1);
......@@ -88,11 +125,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid1,tie)){
Log.i("collision", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
......@@ -108,11 +143,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override
public void onAnimationUpdate(ValueAnimator animation2) {
if(sontEnCollision(asteroid2,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
......@@ -128,11 +161,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override
public void onAnimationUpdate(ValueAnimator animation3) {
if(sontEnCollision(asteroid3,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
......@@ -150,11 +181,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
@Override
public void onAnimationUpdate(ValueAnimator animation4) {
if(sontEnCollision(asteroid4,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
......@@ -166,7 +195,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
Runnable movingTieJoystick = new Runnable() {
@Override
public void run() {
padCenter.setOnTouchListener(joyStickView.touchListener);
}
};
......@@ -177,7 +205,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
Runnable movingTieSensor = new Runnable() {
@Override
public void run() {
mSensorManager.registerListener((SensorEventListener)MainActivity.this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
};
......@@ -186,22 +213,45 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
threadTieSensor.start();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showExplosion() {
explosion.setVisibility(View.VISIBLE);
SoundPool sounds;
int sExplosion;
sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0);
sExplosion = sounds.load(this, R.raw.hit, 1);
if(!isInvincible && isPlaying){
explosion.setVisibility(View.VISIBLE);
isInvincible=true;
sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);
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() {
@Override
public void run() {
explosion.postDelayed(new Runnable() {
@Override
public void run() {
explosion.setVisibility(View.INVISIBLE);
}
}, 500); // produce 0.5 sec delay in button visibility
explosion.setVisibility(View.INVISIBLE);
// sounds.play(sExplosion, 1.0f, 1.0f, 0, 0, 1.5f);
;
}
}, 500); // produce 5 sec delay in button visibility
explosion.postDelayed(new Runnable() {
@Override
public void run() {
isInvincible=false;
tie.setColorFilter(0);
}
}, 1500);
}
}
@Override
......@@ -217,8 +267,10 @@ 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.setY(tie.getY()+((gammaY)*4));
if(!isPad && isPlaying){
tie.setX(tie.getX()-((gammaX)*4));
tie.setY(tie.getY()+((gammaY)*4));
}
if(tie.getX()<0){
tie.setX(0);
......@@ -227,8 +279,8 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
}
if(tie.getY()<0){
tie.setY(0);
}else if(tie.getY()>=(height*0.6)){
tie.setY((float) (height*0.6));
}else if(tie.getY()>=(height-(padCenter.getHeight()*3))){
tie.setY((float) (height-(padCenter.getHeight()*3)));
}
}
......@@ -239,7 +291,6 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
firstView .getLocationOnScreen(firstPosition);
secondView.getLocationOnScreen(secondPosition);
Rect rectFirstView = new Rect(firstPosition [0], firstPosition [1],
firstPosition [0] + firstView .getMeasuredWidth()/2, firstPosition [1] + firstView .getMeasuredHeight()/2);
Rect rectSecondView = new Rect(secondPosition[0], secondPosition[1],
......
......@@ -49,15 +49,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:text="Joystick"
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"
android:text="Capteurs ON"
app:backgroundTint="#9C27B0" />
</FrameLayout>
......@@ -70,8 +62,8 @@
<ImageView
android:id="@+id/tie"
android:layout_width="110dp"
android:layout_height="55dp"
android:layout_width="76dp"
android:layout_height="46dp"
android:layout_gravity="center"
android:layout_marginTop="150dp"
app:srcCompat="@drawable/tie" />
......@@ -85,8 +77,8 @@
<ImageView
android:id="@+id/asteroid2"
android:layout_width="84dp"
android:layout_height="72dp"
android:layout_width="76dp"
android:layout_height="70dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="150dp"
......@@ -94,40 +86,40 @@
<ImageView
android:id="@+id/asteroid1"
android:layout_width="93dp"
android:layout_height="79dp"
android:layout_width="68dp"
android:layout_height="65dp"
android:layout_marginStart="110dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="50dp"
app:srcCompat="@drawable/asteroid1"
android:layout_marginLeft="150dp" />
app:srcCompat="@drawable/asteroid1" />
<ImageView
android:id="@+id/asteroid3"
android:layout_width="98dp"
android:layout_height="60dp"
android:layout_width="80dp"
android:layout_height="52dp"
android:layout_marginStart="250dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="250dp"
android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid3"
android:layout_marginLeft="200dp" />
app:srcCompat="@drawable/asteroid3" />
<ImageView
android:id="@+id/asteroid4"
android:layout_width="80dp"
android:layout_height="74dp"
android:layout_width="51dp"
android:layout_height="73dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="120dp"
android:layout_marginTop="290dp"
android:layout_marginRight="0dp"
app:srcCompat="@drawable/asteroid4"
android:layout_marginLeft="120dp" />
app:srcCompat="@drawable/asteroid4" />
<TextView
android:id="@+id/vie"
android:layout_width="103dp"
android:layout_height="36dp"
android:layout_gravity="right"
android:layout_gravity="left"
android:text="Vie"
android:textSize="30dp"
......@@ -139,9 +131,10 @@
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Score"
android:text="Score:"
android:textSize="30dp"
android:textStyle="bold" />
</FrameLayout>
</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 @@
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.GameOver" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<item name="android:windowIsTranslucent">true</item>
</style>
</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