Commit 9e01888d authored by Rominho15's avatar Rominho15
Browse files

Version 7.1 :

- Gestion du multijoueur
Next step : Version 7.2 : Gestion des niveaux en multijoueurs
parent f7824f4e
......@@ -25,14 +25,16 @@ public class GameController {
private GameThread gameThread;
private Context context;
private int niveau;
private int player;
public GameController(GameView gv, Context c, int n, int s) {
public GameController(GameView gv, Context c, int n, int s, int p) {
gameView = gv;
gameView.setGameControlleur(this);
context = c;
gameModel = new GameModel();
gameModel = new GameModel(p);
gameModel.getEvents().setScore(s);
niveau = n;
player = p;
init();
}
......@@ -53,7 +55,7 @@ public class GameController {
// place la batte en dessous de la balle
int batX = gameModel.getGround().getCenter().x;
int batY = gameModel.getGround().getCenter().y + gameModel.getGround().getCenter().y/2;
int batY = 3*gameModel.getGround().getCenter().y/2;
// met à jour la position de la batte dans le modèle
gameModel.getBat().setX(batX);
......@@ -61,6 +63,18 @@ public class GameController {
gameModel.getBat().setWidth((int) (size.x / 10));
gameModel.getBat().setHeight((int) (size.y / 60));
if(gameModel.getBat2() != null) {
// place la batte en dessous de la balle
int bat2X = gameModel.getGround().getCenter().x;
int bat2Y = gameModel.getGround().getCenter().y/2;
// met à jour la position de la batte dans le modèle
gameModel.getBat2().setX(bat2X);
gameModel.getBat2().setY(bat2Y);
gameModel.getBat2().setWidth((int) (size.x / 10));
gameModel.getBat2().setHeight((int) (size.y / 60));
}
if(niveau == 1) {
gameModel.initBricks1();
} else {
......@@ -84,7 +98,12 @@ public class GameController {
// teste la collision avec les bordures : si c'est le cas on change le signe de la composante vitesse correspondante (rebond sur les bords)
if(cx<=0 || cx>= gameModel.getGround().getWidth()) vx*=-1;
if(cy<=0 ) vy*=-1;
if(cy<=0) {
if(gameModel.getBat2() != null)
this.endGame(false);
else
vy*=-1;
}
// teste la collision avec la bordure du bas : la partie s'arrête
if(cy>=gameModel.getGround().getHeight()) {
this.endGame(false);
......@@ -93,12 +112,31 @@ public class GameController {
// teste la collision entre la batte et la balle (rebond sur la batte)
int col = collides(gameModel.getBall(), gameModel.getBat());
if(col != 0) {
if(col == 1)
vy*=-1;
int mod = xCompare(gameModel.getBall(),gameModel.getBat());
if(col == 1) {
vy *= -1;
vx += mod;
if(vx == 0) vx -= mod;
}
else
vx*=-1;
}
// Si il existe une Bat2, teste la collision entre la batte et la balle (rebond sur la batte)
if(gameModel.getBat2() != null) {
col = collides(gameModel.getBall(), gameModel.getBat2());
if(col != 0) {
int mod = xCompare(gameModel.getBall(),gameModel.getBat2());
if(col == 1) {
vy *= -1;
vx += mod;
if(vx == 0) vx -= mod;
}
else
vx*=-1;
}
}
// teste la collision entre les briques et la balle
for (int g=0 ; g < gameModel.getBrickGroupsSize() ; g++) {
BrickGroup brickGroup = gameModel.getBrickGroups().get(g);
......@@ -146,6 +184,7 @@ public class GameController {
i.putExtra("win",true);
}
i.putExtra("player",player);
i.putExtra("val_score",gameModel.getEvents().getScore());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
......@@ -165,6 +204,11 @@ public class GameController {
gameModel.getBat().setX(x);
}
public void moveBat2(int x) {
if(gameModel.getBat2() != null)
gameModel.getBat2().setX(x);
}
public void moveBatAdd(int x) {
moveBatAccelerometer(-x);
}
......@@ -172,7 +216,7 @@ public class GameController {
public void moveBatAccelerometer(float x) {
int addX = (int)-x;
int oldX = gameModel.getBat().getX() + gameModel.getBat().getWidth()/2;
gameModel.getBat().setX(oldX+addX);
gameModel.getBat().setX(oldX + addX);
}
public int collides(Ball ba, Bat bt) {
......@@ -218,6 +262,22 @@ public class GameController {
}
public int xCompare(Ball ba, Bat bt) {
int res = 0;
int positionBallByBat = ba.getX() - bt.getX();
int gap = bt.getWidth()/3;
int vitesse = ba.getVx();
Log.d("vitesse", "" + vitesse);
if(positionBallByBat < gap ) {
res = -1;
} else if (positionBallByBat >= gap*2){
res = 1;
}
return res;
}
public void tirer() {
}
......
......@@ -10,13 +10,20 @@ import java.util.ArrayList;
*/
public class GameModel{
private Bat bat;
private Bat bat2 = null;
private Ball ball;
private Ground ground;
private ArrayList<BrickGroup> brickGroups;
private Evenements events;
public GameModel() {
public GameModel(int p) {
bat = new Bat(50,50);
if(p == 2)
bat2 = new Bat(50,50);
Log.e("bat2",""+bat2);
ball = new Ball(15,15);
events = new Evenements();
brickGroups = new ArrayList<>();
......@@ -42,6 +49,14 @@ public class GameModel{
this.bat = bat;
}
public Bat getBat2() {
return bat2;
}
public void setBat2(Bat bat2) {
this.bat2 = bat2;
}
public Ball getBall() {
return ball;
}
......
......@@ -12,6 +12,7 @@ public class BetweenLevelActivity extends AppCompatActivity {
private int score;
private int methode;
private int player;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -21,6 +22,7 @@ public class BetweenLevelActivity extends AppCompatActivity {
Bundle extras = getIntent().getExtras();
score = extras.getInt("val_score");
methode = extras.getInt("method");
player = extras.getInt("player");
// Affichage score
TextView TVscore = (TextView) findViewById(R.id.score);
......@@ -32,6 +34,7 @@ public class BetweenLevelActivity extends AppCompatActivity {
i.putExtra("level",2);
i.putExtra("score",score);
i.putExtra("method",methode);
i.putExtra("player",player);
startActivity(i);
}
}
......@@ -112,6 +112,7 @@ public class EndActivity extends AppCompatActivity {
Intent i = new Intent(this,MainActivity.class);
i.putExtra("ini",true);
startActivity(i);
this.finish();
}
}
......@@ -18,18 +18,19 @@ public class GameActivity extends AppCompatActivity {
Bundle extras = getIntent().getExtras();
int niveau = extras.getInt("level");
int methode = extras.getInt("method");
int player = extras.getInt("player");
int score = 0;
if(niveau > 1)
score = extras.getInt("score");
initGame(niveau,methode,score);
initGame(niveau,methode,score,player);
}
public void initGame(int niveau, int methode, int score){
public void initGame(int niveau, int methode, int score, int player){
GameView gameView = (GameView)findViewById(R.id.gameview);
GameController gameController = new GameController(gameView,this.getApplicationContext(),niveau,score);
GameController gameController = new GameController(gameView,this.getApplicationContext(),niveau,score,player);
gameController.startGame();
gameView.setMethode(methode);
}
......
......@@ -6,6 +6,7 @@ import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.Toast;
......@@ -20,23 +21,45 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
}
public void onChangeMulti(View v) {
CheckBox multi = (CheckBox) findViewById(R.id.multi);
RadioGroup interaction = (RadioGroup) findViewById(R.id.interaction);
for (int i = 0; i < interaction.getChildCount(); i++) {
interaction.getChildAt(i).setEnabled(!multi.isChecked());
}
}
public void createGame(View v) {
int methode = 0;
RadioGroup interaction = (RadioGroup) findViewById(R.id.interaction);
switch (interaction.getCheckedRadioButtonId()) {
case R.id.cursor : methode = 0;
break;
case R.id.arrow : methode = 1;
break;
case R.id.rotation : methode = 2;
break;
default : methode = 0;
break;
CheckBox multi = (CheckBox) findViewById(R.id.multi);
Boolean isMulti = multi.isChecked();
int player = 2;
if(!isMulti) {
RadioGroup interaction = (RadioGroup) findViewById(R.id.interaction);
switch (interaction.getCheckedRadioButtonId()) {
case R.id.cursor:
methode = 0;
break;
case R.id.arrow:
methode = 1;
break;
case R.id.rotation:
methode = 2;
break;
default:
methode = 0;
break;
}
player = 1;
}
Intent i = new Intent(this,GameActivity.class);
i.putExtra("level",1);
i.putExtra("method",methode);
i.putExtra("player",player);
startActivity(i);
}
}
......@@ -31,6 +31,7 @@ public class GameView extends View {
Paint paintBall;
Paint paintBrick;
Paint paintBat;
Paint paintBat2;
GameController gameControlleur;
int methode;
Button left;
......@@ -53,6 +54,9 @@ public class GameView extends View {
paintBat=new Paint();
paintBat.setARGB(255, 0, 0, 255);
paintBat2=new Paint();
paintBat2.setARGB(255, 0, 255, 0);
}
public void setMethode(int id) {
......@@ -156,6 +160,9 @@ public class GameView extends View {
paintBat=new Paint();
paintBat.setARGB(255, 0, 0, 255);
paintBat2=new Paint();
paintBat2.setARGB(255, 0, 255, 0);
}
@Override
......@@ -173,7 +180,19 @@ public class GameView extends View {
canvas.drawRect(xBat, yBat, xBat + wBat, yBat + hBat, paintBat);
// Curseur
canvas.drawCircle(xBat+wBat/2,yBat+hBat+20,7,paintBall);
canvas.drawCircle(xBat + wBat / 2, yBat + hBat + 20, 7, paintBall);
if(gameControlleur.getGameModel().getBat2() != null) {
int xBat2 = gameControlleur.getGameModel().getBat2().getX();
int yBat2 = gameControlleur.getGameModel().getBat2().getY();
int wBat2 = gameControlleur.getGameModel().getBat2().getWidth();
int hBat2 = gameControlleur.getGameModel().getBat2().getHeight();
canvas.drawRect(xBat2, yBat2, xBat2 + wBat2, yBat2 + hBat2, paintBat2);
// Curseur
canvas.drawCircle(xBat2 + wBat2 / 2, yBat2 - hBat2 - 20, 7, paintBall);
}
for (int g=0 ; g < gameControlleur.getGameModel().getBrickGroupsSize() ; g++) {
BrickGroup brickGroup = gameControlleur.getGameModel().getBrickGroups().get(g);
......@@ -197,13 +216,18 @@ public class GameView extends View {
@Override
public boolean onTouchEvent(MotionEvent event) {
int yMax;
int yMin = 0;
//Log.d("View : ", "onTouchEvent");
switch (event.getAction()) {
case (MotionEvent.ACTION_MOVE):
//Log.d("View : ", "move " + event.getX() + " " +event.getY());
//Log.d("View : ", "move " + event.getX() + " " +event.getY());
int x = (int) event.getX();
int y = (int) event.getY();
if(gameControlleur.getGameModel().getBat2() != null)
yMin = gameControlleur.getGameModel().getBat2().getY() - gameControlleur.getGameModel().getBat().getHeight();
yMax = gameControlleur.getGameModel().getBat().getY() + gameControlleur.getGameModel().getBat().getHeight();
if(methode == 2) {
......@@ -211,6 +235,8 @@ public class GameView extends View {
} else if (methode == 0){
if (y > yMax) {
gameControlleur.moveBat(x);
} else if (y < yMin){
gameControlleur.moveBat2(x);
} else {
gameControlleur.tirer();
}
......@@ -221,6 +247,9 @@ public class GameView extends View {
x = (int) event.getX();
y = (int) event.getY();
if(gameControlleur.getGameModel().getBat2() != null)
yMin = gameControlleur.getGameModel().getBat2().getY() - gameControlleur.getGameModel().getBat().getHeight();
yMax = gameControlleur.getGameModel().getBat().getY() + gameControlleur.getGameModel().getBat().getHeight();
if(methode == 2) {
......@@ -228,6 +257,8 @@ public class GameView extends View {
} else if (methode == 0){
if (y > yMax) {
gameControlleur.moveBat(x);
} else if (y < yMin){
gameControlleur.moveBat2(x);
} else {
gameControlleur.tirer();
}
......
......@@ -10,6 +10,18 @@
android:orientation="vertical"
tools:context=".Screen.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Multiplayer"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Tplayers"
android:id="@+id/multi"
android:onClick="onChangeMulti"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......
......@@ -16,4 +16,6 @@
<string name="NoHS">Désolé, allez recommencez !</string>
<string name="Internet">Vous n\'avez pas de connexion Internet</string>
<string name="Name">Nom</string>
<string name="Multiplayer">Multijoueurs</string>
<string name="Tplayers">2 joueurs</string>
</resources>
\ No newline at end of file
......@@ -15,4 +15,6 @@
<string name="NoHS">Sorry, try again !</string>
<string name="Internet">You don\'t have any Internet access</string>
<string name="Name">Name</string>
<string name="Multiplayer">Multiplayer</string>
<string name="Tplayers">2 players</string>
</resources>
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