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;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
class JoyStickView {
int xDelta;
int yDelta;
float xDelta;
float yDelta;
float origineX;
float origineY;
ViewGroup mainLayout ;
Context context;
Boolean joystickIsPressed = false;
Boolean joystickIsPressed=false;
ImageView tie;
public JoyStickView (ViewGroup mainLayout, Context context)
public JoyStickView (ViewGroup mainLayout, Context context, ImageView tie)
{
this.mainLayout = mainLayout;
this.context = context;
this.tie=tie;
}
final View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int x = (int) event.getRawX();
final int y = (int) event.getRawY();
joystickIsPressed = false;
float halfPadSize = (v.getHeight()/2);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams laParams = (FrameLayout.LayoutParams) v.getLayoutParams();
xDelta = x - laParams.leftMargin;
yDelta = y - laParams.topMargin;
Log.i("tag", "Hello");
origineX=v.getX();
origineY=v.getY();
joystickIsPressed = true;
break;
case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) v.getLayoutParams();
lParams.leftMargin = x - xDelta;
lParams.topMargin = y - yDelta;
lParams.rightMargin = 0;
lParams.bottomMargin = 0;
xDelta = event.getX() + v.getX() - halfPadSize;
yDelta = event.getY() + v.getY() - halfPadSize;
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;
case MotionEvent.ACTION_UP:
FrameLayout.LayoutParams layParams = (FrameLayout.LayoutParams) v.getLayoutParams();
//layParams.leftMargin = xDelta;
//layParams.topMargin = yDelta ;
v.setLayoutParams(layParams);
v.setX(origineX);
v.setY(origineY);
joystickIsPressed = false;
break;
......@@ -77,5 +108,3 @@ class JoyStickView {
}
......@@ -4,30 +4,43 @@ import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.PathInterpolator;
import android.widget.FrameLayout;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
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 padCenter;
ViewGroup mainLayout;
ImageView asteroid1;
ImageView asteroid2;
ImageView tie;
ImageView explosion;
private SensorManager mSensorManager;
private Sensor accelerometer;
@SuppressLint("ClickableViewAccessibility")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
......@@ -38,23 +51,62 @@ public class MainActivity extends AppCompatActivity {
padCenter = findViewById(R.id.padCenter);
mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1);
asteroid2 = findViewById(R.id.asteroid2);
explosion=findViewById(R.id.explosion);
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.arcTo(0f, 0f, 1000f, 1000f, 270f, -180f, true);
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();
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();
Runnable movingTie = new Runnable() {
@Override
public void run() {
tie.setTranslationX(joyStickView.xDelta);
tie.setTranslationY(joyStickView.yDelta);
@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();
......@@ -62,7 +114,7 @@ public class MainActivity extends AppCompatActivity {
mainLayout.invalidate();
}
}
}
};
handlerForMovingTie.post(movingTie);
Thread threadTie = new Thread(movingTie);
......@@ -71,10 +123,64 @@ public class MainActivity extends AppCompatActivity {
Log.i("tag", "Hello");
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));
}
}
\ No newline at end of file
}
......@@ -12,80 +12,85 @@
android:id="@+id/main"
android:layout_marginBottom="4dp">
<ImageView
android:id="@+id/fond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:srcCompat="@drawable/etoilefond" />
<ImageView
android:id="@+id/fond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
app:srcCompat="@drawable/etoilefond" />
<FrameLayout
android:id="@+id/gameSpace"
android:layout_width="match_parent"
android:layout_height="199dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp" >
<FrameLayout
android:id="@+id/gameSpace"
android:layout_width="match_parent"
android:layout_height="199dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp" >
<ImageView
android:id="@+id/padCenter"
android:layout_width="168dp"
android:layout_height="102dp"
android:layout_gravity="center"
app:srcCompat="@drawable/pad_center" />
<ImageView
android:id="@+id/padCenter"
android:layout_width="168dp"
android:layout_height="102dp"
android:layout_gravity="center"
app:srcCompat="@drawable/pad_center" />
<ImageView
android:id="@+id/padExte"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:keepScreenOn="false"
android:visibility="visible"
app:srcCompat="@drawable/pad_exterior" />
<ImageView
android:id="@+id/padExte"
android:layout_width="179dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:keepScreenOn="false"
android:visibility="visible"
app:srcCompat="@drawable/pad_exterior" />
</FrameLayout>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
<ImageView
android:id="@+id/tie"
android:layout_width="110dp"
android:layout_height="55dp"
android:layout_gravity="center"
android:layout_marginTop="150dp"
app:srcCompat="@drawable/tie" />
<ImageView
android:id="@+id/tie"
android:layout_width="110dp"
android:layout_height="55dp"
android:layout_marginStart="150dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="450dp"
android:layout_marginRight="180dp"
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
android:id="@+id/asteroid2"
android:layout_width="138dp"
android:layout_height="113dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="150dp"
app:srcCompat="@drawable/asteroid2" />
<ImageView
android:id="@+id/asteroid2"
android:layout_width="84dp"
android:layout_height="72dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="150dp"
app:srcCompat="@drawable/asteroid2" />
<ImageView
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" />
</FrameLayout>
<ImageView
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" />
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
\ No newline at end of file
</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