Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
Star Wars Game
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Benjamin LEROUX
Star Wars Game
Commits
288fded0
Commit
288fded0
authored
Feb 25, 2021
by
Benjamin LEROUX
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Gestion du joystick
parent
307c95c4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
256 additions
and
67 deletions
+256
-67
JoyStick.java
app/src/main/java/com/imtm1/starwarsgame/JoyStick.java
+106
-1
MainActivity.java
app/src/main/java/com/imtm1/starwarsgame/MainActivity.java
+73
-1
activity_main.xml
app/src/main/res/layout/activity_main.xml
+77
-65
No files found.
app/src/main/java/com/imtm1/starwarsgame/JoyStick.java
View file @
288fded0
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
;
}
};
}
app/src/main/java/com/imtm1/starwarsgame/MainActivity.java
View file @
288fded0
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
(
0
f
,
0
f
,
1000
f
,
1000
f
,
270
f
,
-
180
f
,
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
);
}
}
app/src/main/res/layout/activity_main.xml
View file @
288fded0
<?xml version="1.0" encoding="utf-8"?>
<
androidx.constraintlayout.widget.Constraint
Layout
xmlns:android=
"http://schemas.android.com/apk/res/android"
<
Relative
Layout
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=
"16
dp"
app:layout_constraintBottom_toBottomOf=
"parent
"
app:layout_constraintStart_toStartOf=
"parent
"
android:layout_marginEnd=
"0
dp"
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>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment