|
6 | 6 | import android.os.Bundle;
|
7 | 7 | import android.support.annotation.Nullable;
|
8 | 8 | import android.support.v7.app.AppCompatActivity;
|
9 |
| -import android.widget.ImageView; |
| 9 | +import android.support.v7.widget.AppCompatImageView; |
10 | 10 | import android.widget.TextView;
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | import butterknife.BindView;
|
14 | 14 | import butterknife.ButterKnife;
|
15 | 15 | import butterknife.OnClick;
|
16 | 16 | import fr.m2ihm.a1819.shi_fu_me.R;
|
| 17 | +import fr.m2ihm.a1819.shi_fu_me.models.Game; |
| 18 | +import fr.m2ihm.a1819.shi_fu_me.p2p.Client; |
17 | 19 | import fr.m2ihm.a1819.shi_fu_me.p2p.Common;
|
| 20 | +import fr.m2ihm.a1819.shi_fu_me.p2p.Server; |
18 | 21 | import fr.m2ihm.a1819.shi_fu_me.p2p.WiFiDirectBroadcastReceiver;
|
19 |
| -import fr.m2ihm.a1819.shi_fu_me.utils.Choice; |
20 |
| - |
| 22 | +import fr.m2ihm.a1819.shi_fu_me.models.Choice; |
| 23 | +import fr.m2ihm.a1819.shi_fu_me.utils.AndroidDrawableResource; |
| 24 | +import fr.m2ihm.a1819.shi_fu_me.utils.AndroidImageViewDrawer; |
| 25 | +import fr.m2ihm.a1819.shi_fu_me.utils.Opponent; |
| 26 | +import fr.m2ihm.a1819.shi_fu_me.utils.OpponentFactory; |
| 27 | + |
| 28 | +/** |
| 29 | + * Activité affichée pendant le jeu |
| 30 | + * @version 1.0.0 |
| 31 | + */ |
21 | 32 | public class InGameActivity extends AppCompatActivity {
|
22 | 33 |
|
23 |
| - private Choice playerChoice; |
24 |
| - private Choice computerChoice; |
25 |
| - private int yourScore = 0; |
26 |
| - private int advScore = 0; |
| 34 | + final Game.GameType GAME_TYPE = Game.GameType.SINGLEPLAYER; |
| 35 | + |
| 36 | + Game game = new Game(); |
| 37 | + Opponent opponent = OpponentFactory.getOpponent(GAME_TYPE); |
| 38 | + |
| 39 | + AndroidImageViewDrawer imgDrawerPlayer; |
| 40 | + AndroidImageViewDrawer imgDrawerOpponent; |
| 41 | + |
| 42 | + @BindView(R.id.img_you) AppCompatImageView img_you; |
| 43 | + @BindView(R.id.img_adv) AppCompatImageView img_adv; |
27 | 44 |
|
28 |
| - @BindView(R.id.img_you) ImageView img_you; |
29 |
| - @BindView(R.id.img_adv) ImageView img_adv; |
30 | 45 | @BindView(R.id.txt_your_score) TextView txt_your_score;
|
31 | 46 | @BindView(R.id.txt_adv_score) TextView txt_adv_score;
|
32 |
| - private WifiP2pManager manager; |
33 |
| - private WifiP2pManager.Channel channel; |
34 |
| - private WiFiDirectBroadcastReceiver reciever; |
| 47 | + |
| 48 | + private WiFiDirectBroadcastReceiver receiver; |
35 | 49 | private IntentFilter intentFilter;
|
36 |
| - private Common clientServer; |
37 | 50 |
|
| 51 | + private Server server; |
| 52 | + private Client client; |
| 53 | + |
| 54 | + /** |
| 55 | + * Callback du clique sur le bouton ciseaux |
| 56 | + */ |
38 | 57 | @OnClick(R.id.btn_ciseaux) public void clickBtnCiseaux() {
|
39 |
| - playerChoice = Choice.CISEAUX; |
40 |
| - computerChoice = Choice.randomChoice(); |
41 |
| - setImages(); |
42 |
| - score(); |
| 58 | + game.setPlayerChoice(Choice.CISEAUX); |
| 59 | + game.setAdvChoice(opponent.getChoice()); |
| 60 | + game.updateScore(); |
| 61 | + updateUi(); |
43 | 62 | }
|
44 | 63 |
|
| 64 | + /** |
| 65 | + * Callback du clique sur le bouton feuille |
| 66 | + */ |
45 | 67 | @OnClick(R.id.btn_feuille) public void clickBtnFeuille() {
|
46 |
| - playerChoice = Choice.FEUILLE; |
47 |
| - computerChoice = Choice.randomChoice(); |
48 |
| - setImages(); |
49 |
| - score(); |
| 68 | + game.setPlayerChoice(Choice.FEUILLE); |
| 69 | + game.setAdvChoice(opponent.getChoice()); |
| 70 | + game.updateScore(); |
| 71 | + updateUi(); |
50 | 72 | }
|
51 | 73 |
|
| 74 | + /** |
| 75 | + * Callback du clique sur le boutton pierre |
| 76 | + */ |
52 | 77 | @OnClick(R.id.btn_pierre) public void clickBtnPierre() {
|
53 |
| - playerChoice = Choice.PIERRE; |
54 |
| - computerChoice = Choice.randomChoice(); |
55 |
| - setImages(); |
56 |
| - score(); |
| 78 | + game.setPlayerChoice(Choice.PIERRE); |
| 79 | + game.setAdvChoice(opponent.getChoice()); |
| 80 | + game.updateScore(); |
| 81 | + updateUi(); |
57 | 82 | }
|
58 | 83 |
|
59 |
| - private void setImages() { |
60 |
| - img_you.setImageResource(playerChoice.getDrawable()); |
61 |
| - img_adv.setImageResource(computerChoice.getDrawable()); |
62 |
| - } |
63 |
| - |
64 |
| - private void score() { |
65 |
| - if (this.playerChoice.win(this.computerChoice)) |
66 |
| - this.yourScore++; |
67 |
| - else if(this.computerChoice.win(this.playerChoice)) |
68 |
| - this.advScore++; |
69 |
| - |
| 84 | + /** |
| 85 | + * Met à jours l'affichage en fonction des scores et des choix des joueurs |
| 86 | + */ |
| 87 | + private void updateUi() { |
| 88 | + imgDrawerPlayer.drawResource((AndroidDrawableResource) game.getPlayerChoice().getRessource()); |
| 89 | + imgDrawerOpponent.drawResource((AndroidDrawableResource) game.getAdvChoice().getRessource()); |
70 | 90 | StringBuilder strB_adv = new StringBuilder();
|
71 | 91 | StringBuilder strB_you = new StringBuilder();
|
72 | 92 |
|
73 | 93 | strB_adv.append("Score adversaire ");
|
74 |
| - strB_adv.append(this.advScore); |
| 94 | + strB_adv.append(this.game.getAdvScore()); |
75 | 95 | this.txt_adv_score.setText(strB_adv);
|
76 | 96 |
|
77 | 97 | strB_you.append("Votre score ");
|
78 |
| - strB_you.append(this.yourScore); |
| 98 | + strB_you.append(this.game.getPlayerScore()); |
79 | 99 | this.txt_your_score.setText(strB_you);
|
80 | 100 | }
|
81 | 101 |
|
| 102 | + |
82 | 103 | @Override
|
83 | 104 | public void onCreate(@Nullable Bundle savedInstanceState) {
|
84 | 105 | super.onCreate(savedInstanceState);
|
85 | 106 | setContentView(R.layout.activity_in_game);
|
86 | 107 | ButterKnife.bind(this);
|
87 | 108 | setTitle(R.string.app_name);
|
88 | 109 |
|
89 |
| - manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); |
90 |
| - channel = manager.initialize(this, getMainLooper(), null); |
91 |
| - reciever = new WiFiDirectBroadcastReceiver(manager, channel, this); |
| 110 | + imgDrawerPlayer = new AndroidImageViewDrawer(img_you); |
| 111 | + imgDrawerOpponent = new AndroidImageViewDrawer(img_adv); |
| 112 | + |
| 113 | + WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE); |
| 114 | + assert manager != null; |
| 115 | + WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null); |
| 116 | + receiver = new WiFiDirectBroadcastReceiver(manager, channel, this); |
92 | 117 |
|
93 | 118 | intentFilter = new IntentFilter();
|
94 | 119 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
|
95 | 120 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
|
96 | 121 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
|
97 | 122 | intentFilter.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
|
98 | 123 |
|
99 |
| - img_adv.setRotation(180); |
100 |
| - this.txt_your_score.setText("Votre score "); |
101 |
| - this.txt_adv_score.setText("Score adversaire "); |
| 124 | + updateUi(); |
102 | 125 | }
|
103 | 126 |
|
104 | 127 | @Override
|
105 | 128 | protected void onResume() {
|
106 | 129 | super.onResume();
|
107 |
| - registerReceiver(reciever, intentFilter); |
| 130 | + registerReceiver(receiver, intentFilter); |
108 | 131 | }
|
109 | 132 |
|
110 | 133 | @Override
|
111 | 134 | protected void onPause() {
|
112 | 135 | super.onPause();
|
113 |
| - unregisterReceiver(reciever); |
| 136 | + unregisterReceiver(receiver); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + public AppCompatImageView getImg_you() { |
| 141 | + return img_you; |
| 142 | + } |
| 143 | + |
| 144 | + public AppCompatImageView getImg_adv() { |
| 145 | + return img_adv; |
| 146 | + } |
| 147 | + |
| 148 | + public TextView getTxt_your_score() { |
| 149 | + return txt_your_score; |
| 150 | + } |
| 151 | + |
| 152 | + public TextView getTxt_adv_score() { |
| 153 | + return txt_adv_score; |
| 154 | + } |
| 155 | + |
| 156 | + public Server getServer() { |
| 157 | + return server; |
| 158 | + } |
| 159 | + |
| 160 | + public void setServer(Server server) { |
| 161 | + this.server = server; |
114 | 162 | }
|
115 | 163 |
|
116 |
| - public Common getClientServer() { |
117 |
| - return clientServer; |
| 164 | + public Client getClient() { |
| 165 | + return client; |
118 | 166 | }
|
119 | 167 |
|
120 |
| - public void setClientServer(Common clientServer) { |
121 |
| - this.clientServer = clientServer; |
| 168 | + public void setClient(Client client) { |
| 169 | + this.client = client; |
122 | 170 | }
|
123 | 171 | }
|
0 commit comments