Projet Darts  0.2
BTS SNIR LaSalle Avignon 2020
ActivitePartie.java
Aller à la documentation de ce fichier.
1 package projet.lasalle84.darts;
7 import androidx.appcompat.app.AppCompatActivity;
8 
9 import android.bluetooth.BluetoothAdapter;
10 import android.content.Intent;
11 import android.os.Bundle;
12 import android.os.Handler;
13 import android.os.Message;
14 import android.os.Parcelable;
15 import android.util.Log;
16 import android.view.View;
17 import android.widget.ArrayAdapter;
18 import android.widget.Button;
19 import android.widget.ListView;
20 import android.widget.TextView;
21 import android.widget.Toast;
22 
23 import java.io.Serializable;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 
32 public class ActivitePartie extends AppCompatActivity implements View.OnClickListener
33 {
37  private final static String TAG = "IHMPartie";
38  private final static int REQUEST_CODE_ENABLE_BLUETOOTH = 0;
39 
42  private Button boutonTirManque = null;
43  private Button boutonLancerPartie = null;
44  private ArrayList<Joueur> mesJoueurs = null;
45  private BluetoothAdapter bluetoothAdapter = null;
46  private Partie maPartie = null;
47  private TypeJeu modeJeu = null;
48  private ArrayAdapter<String> adapteur = null;
49  private ListView listJoueur = null;
50  private TextView affichageImpact = null;
51  private Button boutonPause = null;
52  private boolean AfficheRegle;
53 
60  @Override
61  protected void onCreate(Bundle savedInstanceState)
62  {
63  Log.d(TAG,"onCreate()");
64  super.onCreate(savedInstanceState);
65  setContentView(R.layout.activity_partie);
71  maPartie = new Partie(mesJoueurs, modeJeu, AfficheRegle, bluetoothAdapter, handlerUI);
72  }
73 
79  public void ChargerLesParametre()
80  {
81  mesJoueurs = new ArrayList<Joueur>();
82  mesJoueurs = (ArrayList<Joueur>) getIntent().getSerializableExtra("LesJoueurs");
83  Iterator<Joueur> it = mesJoueurs.iterator();
84 
85  while(it.hasNext())
86  {
87  Log.d(TAG, "le joueur "+ it.next().getNom() + "est chargé");
88  }
89  modeJeu = new TypeJeu(getIntent().getIntExtra("TypeMode",0));
90  AfficheRegle = getIntent().getBooleanExtra("AfficheRegle", true);
91  }
92 
99  @Override
100  public void onClick(View element)
101  {
102  Log.d(TAG, "onClick() " + element.toString());
103 
104  if (element == boutonLancerPartie)
105  {
106  new Thread()
107  {
108  @Override
109  public void run() {
110  super.run();
111  maPartie.demarrer();
112  }
113  }.start();
114  boutonLancerPartie.setVisibility(View.INVISIBLE);
115  boutonTirManque.setVisibility(View.VISIBLE);
116  }
117  else if (element == boutonTirManque)
118  {
119  maPartie.cibleManquer();
120  }
121  else if (element == boutonPause)
122  {
123  if (boutonPause.getText().equals("Pause"))
124  {
125  boutonPause.setText("Reprendre");
126  }
127  else
128  {
129  boutonPause.setText("Pause");
130  }
131  }
132 
133 
134  }
135  public void activerBluetooth()
136  {
137  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
138  if (bluetoothAdapter == null)
139  {
140  Toast.makeText(getApplicationContext(), "Bluetooth non activé !", Toast.LENGTH_SHORT).show();
141  }
142  else
143  {
144  if (!bluetoothAdapter.isEnabled())
145  {
146  Toast.makeText(getApplicationContext(), "Bluetooth non activé !", Toast.LENGTH_SHORT).show();
147  Intent activeBlueTooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
148  startActivityForResult(activeBlueTooth, REQUEST_CODE_ENABLE_BLUETOOTH);
149  }
150  else
151  {
152  Toast.makeText(getApplicationContext(), "Bluetooth activé", Toast.LENGTH_SHORT).show();
153  }
154  }
155  }
156 
157 
164  public void recupererWidgets()
165  {
166  Log.d(TAG,"recupererWidgets()");
167  boutonTirManque = (Button) findViewById(R.id.TirManque);
168  boutonLancerPartie = (Button) findViewById(R.id.LancerPartie);
169  listJoueur = (ListView) findViewById(R.id.MesJoueur);
170  affichageImpact = (TextView) findViewById(R.id.Impact);
171  boutonPause = (Button) findViewById(R.id.Pause);
172  }
173 
180  public void initialiserWidgets()
181  {
182  Log.d(TAG,"initialiserWidgets()");
183  boutonTirManque.setOnClickListener(this);
184  boutonTirManque.setVisibility(View.INVISIBLE);
185  boutonLancerPartie.setVisibility(View.INVISIBLE);
186  boutonLancerPartie.setOnClickListener(this);
187  boutonPause.setVisibility(View.INVISIBLE); // Fonction Pause non-fonctionnel
188  boutonPause.setOnClickListener(this);
189  }
190 
197  @Override
198  protected void onActivityResult(int requestCode, int resultCode, Intent data)
199  {
200  Log.d(TAG,"onActivityResult()");
201  super.onActivityResult(requestCode, resultCode, data);
202  if (requestCode != REQUEST_CODE_ENABLE_BLUETOOTH)
203  return;
204  if (resultCode == RESULT_OK)
205  {
206  Toast.makeText(getApplicationContext(), "Bluetooth activé", Toast.LENGTH_SHORT).show();
207  }
208  else
209  {
210  Toast.makeText(getApplicationContext(), "Bluetooth non activé !", Toast.LENGTH_SHORT).show();
211  }
212  }
213 
214  final private Handler handlerUI = new Handler()
215  {
216  @Override
217  public void handleMessage(Message msg)
218  {
219  super.handleMessage(msg);
220  Bundle b = msg.getData();
221 
222  switch (b.getInt("action"))
223  {
224  case Partie.JOUEUR_SUIVANT:
225  Log.d(TAG, "JOUEUR_SUIVANT" + "Joueur: " + b.getString("joueur"));
226  AfficheQuiDoitJouer(b.getString("joueur"));
227  break;
228  case Partie.SET_SCORE:
229  Log.d(TAG, "SET_SCORE " + "Joueur: " + b.getString("joueur") + " Score: " + b.getInt("score"));
230  actualiserScoreIHM(b.getString("joueur"), b.getInt("score"));
231  break;
232  case Partie.IMPACT:
233  Log.d(TAG, "IMPACT " + "Joueur: " + b.getString("joueur") + " TypePoint: " + b.getInt("typePoint") + " NumeroCible: " + b.getInt("numeroCible"));
234  b.getInt("typePoint");
235  b.getInt("numeroCible");
236  afficherImpact( b.getInt("typePoint"),b.getInt("numeroCible"));
237  break;
238  case Partie.GAGNANT:
239  Log.d(TAG, "GAGNANT " + b.getString("gagnant"));
240  afficheGagnant(b.getString("gagnant"));
241  break;
242  case Partie.CONNEXION_CIBLE:
243  Log.d(TAG, "CONNEXION_CIBLE ");
244  boutonLancerPartie.setVisibility(View.VISIBLE);
245  break;
246  }
247  }
248  };
249 
257  {
258  Log.d(TAG, "initialiserListeJoueur()");
259  adapteur = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
260  for (int i = 0; i < mesJoueurs.size(); i++)
261  {
262  adapteur.add(mesJoueurs.get(i).getNom());
263  }
264  listJoueur.setAdapter(adapteur);
265  }
266 
273  public void actualiserScoreIHM(String joueur, int score)
274  {
275  Log.d(TAG, "actualiserScoreIHM()");
276  int idJoueur = 0;
277 
278  for (int i = 0; i < mesJoueurs.size(); i++)
279  {
280  if (mesJoueurs.get(i).getNom().equals(joueur))
281  {
282  idJoueur = i;
283  }
284  }
285  mesJoueurs.get(idJoueur).setScore(score);
286  String chaineCaractere = mesJoueurs.get(idJoueur).getNom() + ": " + mesJoueurs.get(idJoueur).getScore();
287  adapteur.remove(adapteur.getItem(idJoueur));
288  adapteur.insert(chaineCaractere, idJoueur);
289  }
290 
297  public void AfficheQuiDoitJouer(String joueur)
298  {
299  Log.d(TAG, "AfficheQuiDoitJouer()");
300  String chaineCaractere;
301  for (int i = 0; i < mesJoueurs.size(); i++)
302  {
303  if (mesJoueurs.get(i).getNom().equals(joueur))
304  {
305  chaineCaractere = "-> " + mesJoueurs.get(i).getNom() + ": " + mesJoueurs.get(i).getScore();
306  }
307  else {
308  chaineCaractere = mesJoueurs.get(i).getNom() + ": " + mesJoueurs.get(i).getScore();
309  }
310  adapteur.remove(adapteur.getItem(i));
311  adapteur.insert(chaineCaractere, i);
312 
313  }
314  affichageImpact.setText("");
315  }
316 
323  public void afficherImpact(int typeImpact, int numeroImpact)
324  {
325  Log.d(TAG, "afficherImpact()");
326  String lettreTypeImpact = new String();
327  switch (typeImpact)
328  {
329  case 0:
330  lettreTypeImpact = "MISS";
331  break;
332  case 1:
333  lettreTypeImpact = "S";
334  break;
335  case 2:
336  lettreTypeImpact = "D";
337  break;
338  case 3:
339  lettreTypeImpact = "T";
340  break;
341  }
342  if (lettreTypeImpact == "MISS")
343  {
344  affichageImpact.setText(affichageImpact.getText().toString().concat("MISS "));
345  }
346  else
347  {
348  affichageImpact.setText(affichageImpact.getText().toString().concat(lettreTypeImpact + Integer.toString(numeroImpact) + " "));
349  }
350  }
351 
358  public void afficheGagnant(String gagnant)
359  {
360  Log.d(TAG, "afficheGagnant()");
361  Intent IHMGagnant = new Intent(ActivitePartie.this, ActiviteFinPartie.class);
362  List<Joueur>lesJoueur = new ArrayList<Joueur>();
363  for (int i = 0; i < mesJoueurs.size(); i++)
364  {
365  if (mesJoueurs.get(i).getNom().equals(gagnant))
366  {
367  IHMGagnant.putExtra("gagnant", mesJoueurs.get(i).getNom());
368  }
369  else
370  {
371  lesJoueur.add(mesJoueurs.get(i));
372  }
373 
374  IHMGagnant.putExtra("joueurs", (Serializable) lesJoueur);
375  startActivity(IHMGagnant);
376  finish();
377  }
378  }
379 
380  @Override
381  public void finish()
382  {
383  super.finish();
385  }
386 }
387 
static final int JOUEUR_SUIVANT
Definition: Partie.java:38
static final int IMPACT
Definition: Partie.java:40
void AfficheQuiDoitJouer(String joueur)
Méthode qui affiche qui doit jouer.
void ChargerLesParametre()
Méthode qui permet charger des joueurs et les paramètre de la partie.
void onCreate(Bundle savedInstanceState)
Méthode appelée à la création de l&#39;activité
static final String TAG
Tag pour Log.
void afficherImpact(int typeImpact, int numeroImpact)
Méthode qui affiche l&#39;impact de la volley actuelle.
void onClick(View element)
Méthode appelée lors d&#39;un clic sur un widget de l&#39;activité
Button boutonLancerPartie
Bouton pour lancer la partie.
ArrayList< Joueur > mesJoueurs
liste de Joueur
void deconnecterPeripheriquesBluetooth()
Deconnecter les periphériques bluetooth.
Definition: Partie.java:138
Déclaration de la classe Partie.
Definition: Partie.java:26
void initialiserWidgets()
Récupère les widgets du layout de l&#39;activité
static final int REQUEST_CODE_ENABLE_BLUETOOTH
Code que le bluetooth est activé
Déclaration de la classe TypeJeu.
Definition: TypeJeu.java:15
static final int CONNEXION_CIBLE
Definition: Partie.java:42
TypeJeu modeJeu
Object Type de Jeu.
ArrayAdapter< String > adapteur
Adapteur qui affiche quelle joueur doit joueur et score.
static final int SET_SCORE
Definition: Partie.java:39
void recupererWidgets()
Récupère les widgets du layout de l&#39;activité
void actualiserScoreIHM(String joueur, int score)
Méthode qui actualise le score du joueur sur IHM.
void onActivityResult(int requestCode, int resultCode, Intent data)
Méthode qui permet d&#39;avoir le retour d&#39;une activité
Déclaration de la classe ActivitePartie.
void demarrer()
Démarrer la partie.
Definition: Partie.java:172
static final int GAGNANT
Definition: Partie.java:41
Button boutonTirManque
Le bouton si la cible a était manquée.
TextView affichageImpact
Texte qui affiche la volley actuelle.
BluetoothAdapter bluetoothAdapter
Bluetooth Adapteur.