Skip to content

Commit

Permalink
v0.11
Browse files Browse the repository at this point in the history
- Доделал физику столкновения с воротами
- Добавил анимацию при забивании гола
  • Loading branch information
InvalidNickname committed Apr 3, 2018
1 parent 3c54509 commit 06e33f7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 76 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "0.10"
versionName "0.11"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
131 changes: 63 additions & 68 deletions app/src/main/java/hockey/airhockey/GameField.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package hockey.airhockey;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -29,7 +28,7 @@ public class GameField extends SurfaceView implements Runnable {
private SurfaceHolder holder;
private SoundPool soundPool;
private int[] hitSound = new int[5];
private boolean isDrawing, isDragging1, isDragging2, isCollision1, isCollision2;
private boolean isDrawing, isDragging1, isDragging2, isCollision1, isCollision2, isGame;
private VectorDrawableCompat background;
private Player player1, player2;
private Gate upperGate, lowerGate;
Expand All @@ -54,8 +53,7 @@ public GameField(Context context) {
paint.setColor(Color.BLUE);
paint.setTextSize(height / 3.5f);
paint.setAlpha(50);
Typeface base = Typeface.createFromAsset(context.getAssets(), "fonts/aldrich.ttf");
paint.setTypeface(base);
paint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/aldrich.ttf"));
loadGraphics();
startGame();
thread.start();
Expand All @@ -66,37 +64,39 @@ private void update() {
long sec = System.currentTimeMillis();
delta = sec - psec;
psec = sec;
player1.setV(delta);
player2.setV(delta);
checkGoal();
player1.update();
player2.update();
checkCollision();
if (isGame) {
checkCollision();
player1.setV(delta);
player2.setV(delta);
checkGoal();
}
player1.update(delta, isGame);
player2.update(delta, isGame);
upperGate.update();
lowerGate.update();
puck.update(delta);
puck.update(delta, isGame);
if (!isGame & length(puck.x, puck.y, width / 2, height / 2) <= puckScale / 2) {
startGame();
}
}

// проверка гола
private void checkGoal() {
if (puck.y < 0) {
count2++;
playGoal();
startGame();
} else if (puck.y > height) {
count1++;
playGoal();
startGame();
}
}

// TODO анимация перемещения шайбы и бит на изначальные позиции после гола
// анимация перемещения шайбы и бит на изначальные позиции после гола
private void playGoal() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
isGame = false;
puck.v.setVector((width / 2 - puck.x) / 1000d, (height / 2 - puck.y) / 1000d);
player1.v.setVector((width / 2 - player1.x) / 1000d, (1.4 * playerScale - player1.y) / 1000d);
player2.v.setVector((width / 2 - player2.x) / 1000d, (height - 1.4 * playerScale - player2.y) / 1000d);
}

// начало новой игры
Expand All @@ -106,6 +106,7 @@ private void startGame() {
isCollision2 = false;
psec = System.currentTimeMillis();
loadGraphics();
isGame = true;
}

// загрузка звуков
Expand Down Expand Up @@ -146,25 +147,25 @@ private void drawOnCanvas(Canvas canvas) {
puck.drawPuck(canvas);
}

// провера столковения шайбы
private double length(double x1, double y1, double x2, double y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

// проверка столковения шайбы
private void checkCollision() {
// проверка столкновения с верхней/нижней стенкой, пролетит ли в ворота
if (puck.x < lowerGate.rightCorner & puck.x > lowerGate.leftCorner) {
if (puck.y < puckScale) {
if (Math.sqrt(Math.pow(puck.x - lowerGate.leftCorner, 2) + Math.pow(puck.y, 2)) < playerScale) {
double alpha = Math.acos((puck.x - upperGate.leftCorner) / Math.sqrt(Math.pow(puck.x - upperGate.leftCorner, 2) + Math.pow(puck.y, 2)));
collisionWithGates(alpha);
} else if (Math.sqrt(Math.pow(puck.x - lowerGate.rightCorner, 2) + Math.pow(puck.y, 2)) < playerScale) {
double alpha = Math.acos((puck.x - upperGate.rightCorner) / Math.sqrt(Math.pow(puck.x - upperGate.rightCorner, 2) + Math.pow(puck.y, 2)));
collisionWithGates(alpha);
if (length(puck.x, puck.y, upperGate.leftCorner, 0) < playerScale) {
collision(new Vector(0, 0), Math.acos((puck.x - upperGate.leftCorner) / length(puck.x, puck.y, upperGate.leftCorner, 0)));
} else if (length(puck.x, puck.y, upperGate.rightCorner, 0) < playerScale) {
collision(new Vector(0, 0), Math.acos((puck.x - upperGate.rightCorner) / length(puck.x, puck.y, upperGate.rightCorner, 0)));
}
} else if (puck.y > height - puckScale) {
if (Math.sqrt(Math.pow(puck.x - lowerGate.leftCorner, 2) + Math.pow(puck.y - height, 2)) < playerScale) {
double alpha = Math.acos((puck.x - lowerGate.leftCorner) / Math.sqrt(Math.pow(puck.x - lowerGate.leftCorner, 2) + Math.pow(puck.y - height, 2)));
collisionWithGates(alpha);
} else if (Math.sqrt(Math.pow(puck.x - lowerGate.rightCorner, 2) + Math.pow(puck.y - height, 2)) < playerScale) {
double alpha = Math.acos((puck.x - lowerGate.rightCorner) / Math.sqrt(Math.pow(puck.x - lowerGate.rightCorner, 2) + Math.pow(puck.y - height, 2)));
collisionWithGates(alpha);
if (length(puck.x, puck.y, lowerGate.leftCorner, height) < playerScale) {
collision(new Vector(0, 0), Math.acos((puck.x - lowerGate.leftCorner) / length(puck.x, puck.y, lowerGate.leftCorner, height)));
} else if (length(puck.x, puck.y, lowerGate.rightCorner, height) < playerScale) {
collision(new Vector(0, 0), Math.acos((puck.x - lowerGate.rightCorner) / length(puck.x, puck.y, lowerGate.rightCorner, height)));
}
}
} else {
Expand Down Expand Up @@ -193,40 +194,38 @@ private void checkCollision() {
ply2 = player2.y + player2.v.y * delta / 5 * i;
px = puck.x + puck.v.x * delta / 5 * i;
py = puck.y + puck.v.y * delta / 5 * i;
boolean c1 = Math.sqrt(Math.pow(px - plx1, 2) + Math.pow(py - ply1, 2)) < playerScale + puckScale - 5;
boolean c2 = Math.sqrt(Math.pow(px - plx2, 2) + Math.pow(py - ply2, 2)) < playerScale + puckScale - 5;
if (c1 & !isCollision1) {
if ((length(px, py, plx1, ply1) < playerScale + puckScale - 5) & !isCollision1) {
if (ply1 <= py) {
collisionWithPuck(player1, Math.acos((px - plx1) / Math.sqrt(Math.pow(px - plx1, 2) + Math.pow(py - ply1, 2))));
collision(player1.v, Math.acos((px - plx1) / length(px, py, plx1, ply1)));
} else {
collisionWithPuck(player1, Math.acos((plx1 - px) / Math.sqrt(Math.pow(px - plx1, 2) + Math.pow(py - ply1, 2))));
collision(player1.v, Math.acos((plx1 - px) / length(px, py, plx1, ply1)));
}
isCollision1 = true;
}
if (!c1) {
if (!(length(px, py, plx1, ply1) < playerScale + puckScale - 5)) {
isCollision1 = false;
}
if (c2 & !isCollision2) {
if ((length(px, py, plx2, ply2) < playerScale + puckScale - 5) & !isCollision2) {
if (ply2 <= py) {
collisionWithPuck(player2, Math.acos((px - plx2) / Math.sqrt(Math.pow(px - plx2, 2) + Math.pow(py - ply2, 2))));
collision(player2.v, Math.acos((px - plx2) / length(px, py, plx2, ply2)));
} else {
collisionWithPuck(player2, Math.acos((plx2 - px) / Math.sqrt(Math.pow(px - plx2, 2) + Math.pow(py - ply2, 2))));
collision(player2.v, Math.acos((plx2 - px) / length(px, py, plx2, ply2)));
}
isCollision2 = true;
}
if (!c2) {
if (!(length(px, py, plx2, ply2) < playerScale + puckScale - 5)) {
isCollision2 = false;
}
}
}

// столкновение биты с шайбой, alpha - угол наклона прямой, соединяющей центры шайбы и биты к прямой x
private void collisionWithPuck(Player player, double alpha) {
private void collision(Vector speed, double alpha) {
int random = (int) Math.round(Math.random() * 4);
soundPool.play(hitSound[random], 1, 1, 0, 0, 1);
Vector relative, collided = new Vector(0, 0);
// нахождение скорости шайбы относительно биты
relative = puck.v.deductVector(player.v);
relative = puck.v.deductVector(speed);
// проецирование скорости шайбы в систему координат с наклоном прямой x на угол alpha, отражение vy
collided.y = -(relative.x * Math.cos(alpha) + relative.y * Math.sin(alpha));
collided.x = relative.x * Math.sin(alpha) - relative.y * Math.cos(alpha);
Expand All @@ -235,17 +234,6 @@ private void collisionWithPuck(Player player, double alpha) {
puck.v.y = -collided.x * Math.cos(alpha) + collided.y * Math.sin(alpha);
}

// TODO столкновение шайбы с воротами
private void collisionWithGates(double alpha) {
Vector collided = new Vector(0, 0);
// проецирование скорости шайбы в систему координат с наклоном прямой x на угол alpha, отражение vy
collided.y = -(puck.v.x * Math.cos(alpha) + puck.v.y * Math.sin(alpha));
collided.x = puck.v.x * Math.sin(alpha) - puck.v.y * Math.cos(alpha);
// проецирование отраженной скорости на нормальную систему координат
puck.v.x = collided.x * Math.sin(alpha) + collided.y * Math.cos(alpha);
puck.v.y = -collided.x * Math.cos(alpha) + collided.y * Math.sin(alpha);
}

@Override
public void run() {
while (isDrawing) {
Expand Down Expand Up @@ -273,7 +261,6 @@ private int checkX(int playerX) {
return playerX;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
int pointerIndex = event.getActionIndex();
Expand Down Expand Up @@ -302,7 +289,7 @@ public boolean onTouchEvent(MotionEvent event) {
pointerIndex = i;
pointerId = event.getPointerId(pointerIndex);
PointF point = activePointers.get(event.getPointerId(i));
if (point != null) {
if (point != null & isGame) {
point.x = event.getX(i);
point.y = event.getY(i);
if (isDragging1 & (pointerId == dragPointer1)) {
Expand Down Expand Up @@ -331,21 +318,29 @@ public boolean onTouchEvent(MotionEvent event) {
}
break;
}
player1.x = checkX(player1.x);
if (player1.y < playerScale) {
player1.y = playerScale;
} else if (player1.y > height / 2 - playerScale) {
player1.y = height / 2 - playerScale;
}
player2.x = checkX(player2.x);
if (player2.y > height - playerScale) {
player2.y = height - playerScale;
} else if (player2.y < height / 2 + playerScale) {
player2.y = height / 2 + playerScale;
if (isGame) {
player1.x = checkX(player1.x);
if (player1.y < playerScale) {
player1.y = playerScale;
} else if (player1.y > height / 2 - playerScale) {
player1.y = height / 2 - playerScale;
}
player2.x = checkX(player2.x);
if (player2.y > height - playerScale) {
player2.y = height - playerScale;
} else if (player2.y < height / 2 + playerScale) {
player2.y = height / 2 + playerScale;
}
}
performClick();
return true;
}

@Override
public boolean performClick() {
return super.performClick();
}

public void pauseDrawing() {
isDrawing = false;
try {
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/hockey/airhockey/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ void drawPlayer(Canvas canvas) {
drawable.draw(canvas);
}

void update() {
void update(long delta, boolean isGame) {
if (!isGame) {
x += v.x * delta;
y += v.y * delta;
}
drawable.setBounds(x - playerScale, y - playerScale, x + playerScale, y + playerScale);
}

Expand Down
8 changes: 2 additions & 6 deletions app/src/main/java/hockey/airhockey/Puck.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static hockey.airhockey.GameActivity.friction;
import static hockey.airhockey.GameActivity.frictionValue;
import static hockey.airhockey.GameActivity.height;
import static hockey.airhockey.GameActivity.playerScale;
import static hockey.airhockey.GameActivity.puckScale;
import static hockey.airhockey.GameActivity.width;

Expand All @@ -27,16 +26,13 @@ class Puck {
}
}

void update(long delta) {
void update(long delta, boolean isGame) {
v.setVector(v.x, v.y);
x += v.x * delta;
y += v.y * delta;
if (friction) {
if (friction & isGame) {
v = v.multiplyVector(frictionValue);
}
if (v.v > playerScale * 2) {
v.setVector((playerScale * 2) * Math.cos(v.angle), (playerScale * 2 * delta) * Math.sin(v.angle));
}
drawable.setBounds(x - puckScale, y - puckScale, x + puckScale, y + puckScale);
}

Expand Down

0 comments on commit 06e33f7

Please sign in to comment.