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
20d67e34
Commit
20d67e34
authored
Feb 28, 2021
by
Benjamin LEROUX
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sensor & explosion commit
parent
3beb84ce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
70 additions
and
6 deletions
+70
-6
MainActivity.java
app/src/main/java/com/imtm1/starwarsgame/MainActivity.java
+63
-6
activity_main.xml
app/src/main/res/layout/activity_main.xml
+7
-0
No files found.
app/src/main/java/com/imtm1/starwarsgame/MainActivity.java
View file @
20d67e34
...
@@ -3,31 +3,32 @@ package com.imtm1.starwarsgame;
...
@@ -3,31 +3,32 @@ package com.imtm1.starwarsgame;
import
androidx.annotation.RequiresApi
;
import
androidx.annotation.RequiresApi
;
import
androidx.appcompat.app.AppCompatActivity
;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.animation.Animator
;
import
android.animation.ObjectAnimator
;
import
android.animation.ObjectAnimator
;
import
android.animation.ValueAnimator
;
import
android.animation.ValueAnimator
;
import
android.annotation.SuppressLint
;
import
android.annotation.SuppressLint
;
import
android.app.Activity
;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.Path
;
import
android.graphics.Path
;
import
android.graphics.Rect
;
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.Animation
;
import
android.view.animation.Animation
;
import
android.view.animation.PathInterpolator
;
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
;
...
@@ -35,6 +36,9 @@ public class MainActivity extends AppCompatActivity {
...
@@ -35,6 +36,9 @@ public class MainActivity extends AppCompatActivity {
ImageView
asteroid1
;
ImageView
asteroid1
;
ImageView
asteroid2
;
ImageView
asteroid2
;
ImageView
tie
;
ImageView
tie
;
ImageView
explosion
;
private
SensorManager
mSensorManager
;
private
Sensor
accelerometer
;
@SuppressLint
(
"ClickableViewAccessibility"
)
@SuppressLint
(
"ClickableViewAccessibility"
)
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
LOLLIPOP
)
@RequiresApi
(
api
=
Build
.
VERSION_CODES
.
LOLLIPOP
)
...
@@ -48,8 +52,17 @@ public class MainActivity extends AppCompatActivity {
...
@@ -48,8 +52,17 @@ public class MainActivity extends AppCompatActivity {
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
);
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
(
0
f
,
0
f
,
1000
f
,
1000
f
,
270
f
,
-
180
f
,
true
);
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
);
ObjectAnimator
animator
=
ObjectAnimator
.
ofFloat
(
asteroid1
,
View
.
X
,
View
.
Y
,
path
);
...
@@ -60,6 +73,9 @@ public class MainActivity extends AppCompatActivity {
...
@@ -60,6 +73,9 @@ public class MainActivity extends AppCompatActivity {
public
void
onAnimationUpdate
(
ValueAnimator
animation
)
{
public
void
onAnimationUpdate
(
ValueAnimator
animation
)
{
if
(
sontEnCollision
(
asteroid1
,
tie
)){
if
(
sontEnCollision
(
asteroid1
,
tie
)){
Log
.
i
(
"collision"
,
"true"
);
Log
.
i
(
"collision"
,
"true"
);
explosion
.
setX
(
tie
.
getX
());
explosion
.
setY
(
tie
.
getY
());
showExplosion
();
}
}
}
}
});
});
...
@@ -73,6 +89,9 @@ public class MainActivity extends AppCompatActivity {
...
@@ -73,6 +89,9 @@ public class MainActivity extends AppCompatActivity {
public
void
onAnimationUpdate
(
ValueAnimator
animation
)
{
public
void
onAnimationUpdate
(
ValueAnimator
animation
)
{
if
(
sontEnCollision
(
asteroid2
,
tie
)){
if
(
sontEnCollision
(
asteroid2
,
tie
)){
Log
.
i
(
"collision2"
,
"true"
);
Log
.
i
(
"collision2"
,
"true"
);
explosion
.
setX
(
tie
.
getX
());
explosion
.
setY
(
tie
.
getY
());
showExplosion
();
}
}
}
}
});
});
...
@@ -106,6 +125,44 @@ public class MainActivity extends AppCompatActivity {
...
@@ -106,6 +125,44 @@ public class MainActivity extends AppCompatActivity {
}
}
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
)
{
boolean
sontEnCollision
(
ImageView
firstView
,
ImageView
secondView
)
{
int
[]
firstPosition
=
new
int
[
2
];
int
[]
firstPosition
=
new
int
[
2
];
...
...
app/src/main/res/layout/activity_main.xml
View file @
20d67e34
...
@@ -67,6 +67,13 @@
...
@@ -67,6 +67,13 @@
android:layout_marginTop=
"150dp"
android:layout_marginTop=
"150dp"
app:srcCompat=
"@drawable/tie"
/>
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
<ImageView
android:id=
"@+id/asteroid2"
android:id=
"@+id/asteroid2"
android:layout_width=
"84dp"
android:layout_width=
"84dp"
...
...
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