Commit 288fded0 authored by Benjamin LEROUX's avatar Benjamin LEROUX

Gestion du joystick

parent 307c95c4
package com.imtm1.starwarsgame;
public class JoyStick {
import android.content.Context;
import android.graphics.Point;
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 {
float xDelta;
float yDelta;
float origineX;
float origineY;
ViewGroup mainLayout ;
Context context;
Boolean joystickIsPressed=false;
View tie;
public JoyStickView (ViewGroup mainLayout, Context context, View 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) {
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:
origineX=v.getX();
origineY=v.getY();
joystickIsPressed = true;
break;
case MotionEvent.ACTION_MOVE:
xDelta = event.getX() + v.getX() - halfPadSize;
yDelta = event.getY() + v.getY() - halfPadSize;
joystickIsPressed = true;
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:
v.setX(origineX);
v.setY(origineY);
joystickIsPressed = false;
break;
default:
return false;
}
mainLayout.invalidate();
return true;
}
};
}
package com.imtm1.starwarsgame;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Path;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
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.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView padExte;
ImageView padCenter;
ViewGroup mainLayout;
ImageView asteroid1;
ImageView asteroid2;
ImageView tie;
@SuppressLint("ClickableViewAccessibility")
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
padExte = findViewById(R.id.padExte);
padCenter = findViewById(R.id.padCenter);
mainLayout = (RelativeLayout) findViewById(R.id.main);
asteroid1 = findViewById(R.id.asteroid1);
asteroid2 = findViewById(R.id.asteroid2);
tie = findViewById(R.id.tie);
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(2000);
animator.setRepeatCount(Animation.INFINITE);
animator.start();
ObjectAnimator animator2 = ObjectAnimator.ofFloat(asteroid2, View.X, View.Y, path);
animator2.setDuration(5000);
animator2.setRepeatCount(Animation.INFINITE);
animator2.start();
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);
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();
}
}
};
handlerForMovingTie.post(movingTie);
Thread threadTie = new Thread(movingTie);
threadTie.start();
Log.i("TAG", "Thread:"+Thread.currentThread().getName());
Log.i("tag", "Hello");
padCenter.setOnTouchListener(joyStickView.touchListener);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:layout_marginBottom="4dp">
<ImageView
android:id="@+id/imageView4"
android:id="@+id/fond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
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" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="100dp"
android:layout_height="0dp"
android:layout_marginStart="27dp"
android:layout_marginLeft="27dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="448dp"
app:layout_constraintBottom_toTopOf="@+id/imageView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/asteroid1" />
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/imageView6"
android:layout_width="63dp"
android:layout_height="114dp"
android:layout_marginTop="143dp"
android:layout_marginEnd="46dp"
android:layout_marginRight="46dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/asteroid2" />
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
android:layout_width="match_parent"
android:layout_height="525dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp">
<ImageView
android:id="@+id/imageView7"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginTop="548dp"
android:layout_marginEnd="21dp"
android:layout_marginRight="21dp"
android:layout_marginBottom="55dp"
app:layout_constraintBottom_toTopOf="@+id/imageView8"
app:layout_constraintEnd_toEndOf="@+id/imageView8"
app:layout_constraintTop_toTopOf="parent"
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/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="31dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView7"
app:srcCompat="@drawable/pad_exterior" />
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/imageView9"
android:layout_width="106dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView8"
app:srcCompat="@drawable/pad_center" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
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>
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