Commit ea6eeb94 authored by Lila NICKLER's avatar Lila NICKLER

Merge branches 'ben' and 'master' of...

Merge branches 'ben' and 'master' of http://gvipers.imt-lille-douai.fr/benjamin.leroux/star-wars-game into master

 Conflicts:
	app/src/main/java/com/imtm1/starwarsgame/JoyStick.java
	app/src/main/java/com/imtm1/starwarsgame/MainActivity.java
	app/src/main/res/layout/activity_main.xml
parents 3eab8cc5 20d67e34
...@@ -2,66 +2,97 @@ package com.imtm1.starwarsgame; ...@@ -2,66 +2,97 @@ package com.imtm1.starwarsgame;
import android.content.Context; import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler; import android.os.Handler;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.Display;
import android.view.Gravity; import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
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.Toast; import android.widget.Toast;
class JoyStickView { class JoyStickView {
int xDelta; float xDelta;
int yDelta; float yDelta;
float origineX;
float origineY;
ViewGroup mainLayout ; ViewGroup mainLayout ;
Context context; Context context;
Boolean joystickIsPressed = false; Boolean joystickIsPressed=false;
ImageView tie;
public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie)
public JoyStickView (ViewGroup mainLayout, Context context)
{ {
this.mainLayout = mainLayout; this.mainLayout = mainLayout;
this.context = context; this.context = context;
this.tie=tie;
} }
final View.OnTouchListener touchListener = new View.OnTouchListener() { final View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getRawX(); float halfPadSize = (v.getHeight()/2);
final int y = (int) event.getRawY(); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
joystickIsPressed = false; Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
switch (event.getAction()) switch (event.getAction())
{ {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams laParams = (FrameLayout.LayoutParams) v.getLayoutParams(); origineX=v.getX();
xDelta = x - laParams.leftMargin; origineY=v.getY();
yDelta = y - laParams.topMargin;
Log.i("tag", "Hello");
joystickIsPressed = true; joystickIsPressed = true;
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v.getLayoutParams(); xDelta = event.getX() + v.getX() - halfPadSize;
lParams.leftMargin = x - xDelta; yDelta = event.getY() + v.getY() - halfPadSize;
lParams.topMargin = y - yDelta;
lParams.rightMargin = 0;
lParams.bottomMargin = 0;
joystickIsPressed = true; joystickIsPressed = true;
v.setLayoutParams(lParams); float displacement = (float) Math.sqrt(Math.pow(xDelta - origineX, 2) + Math.pow(yDelta - origineY, 2));
if(displacement > halfPadSize){ //si deplacement dans le cercle possible
xDelta=((xDelta-origineX)/displacement*(halfPadSize)+origineX);
yDelta=((yDelta-origineY)/displacement*(halfPadSize)+origineY);
}
v.setX(xDelta);
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));
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-v.getHeight()*3)){
tie.setY((float) (height-v.getHeight()*3));
}
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
FrameLayout.LayoutParams layParams = (FrameLayout.LayoutParams) v.getLayoutParams(); v.setX(origineX);
//layParams.leftMargin = xDelta; v.setY(origineY);
//layParams.topMargin = yDelta ;
v.setLayoutParams(layParams);
joystickIsPressed = false; joystickIsPressed = false;
break; break;
...@@ -77,5 +108,3 @@ class JoyStickView { ...@@ -77,5 +108,3 @@ class JoyStickView {
} }
...@@ -4,30 +4,43 @@ import androidx.annotation.RequiresApi; ...@@ -4,30 +4,43 @@ import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.app.Activity; import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.graphics.Path; import android.graphics.Path;
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.PathInterpolator; import android.view.animation.Animation;
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;
ViewGroup mainLayout; ViewGroup mainLayout;
ImageView asteroid1; ImageView asteroid1;
ImageView asteroid2;
ImageView tie; ImageView tie;
ImageView explosion;
private SensorManager mSensorManager;
private Sensor accelerometer;
@SuppressLint("ClickableViewAccessibility")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
...@@ -38,23 +51,62 @@ public class MainActivity extends AppCompatActivity { ...@@ -38,23 +51,62 @@ public class MainActivity extends AppCompatActivity {
padCenter = findViewById(R.id.padCenter); padCenter = findViewById(R.id.padCenter);
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);
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);
animator.setDuration(20000); animator.setDuration(2000);
animator.setRepeatCount(Animation.INFINITE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid1,tie)){
Log.i("collision", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
animator.start(); animator.start();
ObjectAnimator animator2 = ObjectAnimator.ofFloat(asteroid2, View.X, View.Y, path);
animator2.setDuration(5000);
animator2.setRepeatCount(Animation.INFINITE);
animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if(sontEnCollision(asteroid2,tie)){
Log.i("collision2", "true");
explosion.setX(tie.getX());
explosion.setY(tie.getY());
showExplosion();
}
}
});
animator2.start();
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this);
JoyStickView joyStickView = new JoyStickView(mainLayout,MainActivity.this,tie);
Handler handlerForMovingTie = new Handler(); Handler handlerForMovingTie = new Handler();
Runnable movingTie = new Runnable() { Runnable movingTie = new Runnable() {
@Override @Override
public void run() { public void run() {
tie.setTranslationX(joyStickView.xDelta); // tie.setTranslationX(joyStickView.xDelta);
tie.setTranslationY(joyStickView.yDelta); // tie.setTranslationY(joyStickView.yDelta);
Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed); Log.i( "TAG","Bool:"+ joyStickView.joystickIsPressed);
if (joyStickView.joystickIsPressed) { if (joyStickView.joystickIsPressed) {
Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "joysitck is pressed", Toast.LENGTH_SHORT).show();
...@@ -71,10 +123,64 @@ public class MainActivity extends AppCompatActivity { ...@@ -71,10 +123,64 @@ public class MainActivity extends AppCompatActivity {
Log.i("tag", "Hello"); Log.i("tag", "Hello");
padCenter.setOnTouchListener(joyStickView.touchListener); padCenter.setOnTouchListener(joyStickView.touchListener);
}
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) {
int [] firstPosition = new int[2];
int [] secondPosition = new int[2];
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],
secondPosition[0] + secondView.getMeasuredWidth(), secondPosition[1] + secondView.getMeasuredHeight());
float x_inter = Math.max(0, Math.min(rectFirstView.right, rectSecondView.right) - Math.max(rectFirstView.left, rectSecondView.left));
float y_inter = Math.max(0, Math.min(rectFirstView.bottom, rectSecondView.bottom) - Math.max(rectFirstView.top, rectSecondView.top));
float aireInter = x_inter * y_inter;
return(rectFirstView. intersect (rectSecondView));
} }
} }
...@@ -63,16 +63,21 @@ ...@@ -63,16 +63,21 @@
android:id="@+id/tie" android:id="@+id/tie"
android:layout_width="110dp" android:layout_width="110dp"
android:layout_height="55dp" android:layout_height="55dp"
android:layout_marginStart="150dp" android:layout_gravity="center"
android:layout_marginLeft="50dp" android:layout_marginTop="150dp"
android:layout_marginTop="450dp"
android:layout_marginRight="180dp"
app:srcCompat="@drawable/tie" /> app:srcCompat="@drawable/tie" />
<ImageView <ImageView
android:id="@+id/asteroid2" android:id="@+id/explosion"
android:layout_width="138dp" android:layout_width="135dp"
android:layout_height="113dp" android:layout_height="113dp"
android:visibility="invisible"
app:srcCompat="@drawable/explosion" />
<ImageView
android:id="@+id/asteroid2"
android:layout_width="84dp"
android:layout_height="72dp"
android:layout_marginStart="50dp" android:layout_marginStart="50dp"
android:layout_marginLeft="80dp" android:layout_marginLeft="80dp"
android:layout_marginTop="150dp" android:layout_marginTop="150dp"
......
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