Skip to content

Commit

Permalink
v0.7
Browse files Browse the repository at this point in the history
- Добавил звуки ударов
- Улучшил код
  • Loading branch information
InvalidNickname committed Mar 9, 2018
1 parent a245a90 commit 445b8b0
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 27 deletions.
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.6"
versionName "0.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
41 changes: 29 additions & 12 deletions app/src/main/java/hockey/airhockey/GameField.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.util.Log;
import android.util.SparseArray;
Expand All @@ -22,6 +23,8 @@ public class GameField extends SurfaceView implements Runnable {

private Thread thread;
private SurfaceHolder holder;
private SoundPool soundPool;
private int[] hitSound = new int[5];
private boolean isDrawing, isDragging1, isDragging2, isCollision1, isCollision2;
private VectorDrawableCompat background;
private int x, y;
Expand All @@ -34,19 +37,11 @@ public class GameField extends SurfaceView implements Runnable {
public GameField(Context context) {
super(context);
thread = new Thread();
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
loadMusic(context);
holder = getHolder();
activePointers = new SparseArray<>();
Paint paint = new Paint();
paint.setAntiAlias(true);
background = VectorDrawableCompat.create(context.getResources(), R.drawable.background, null);
if (background != null) {
background.setBounds(0, 0, width, MainActivity.height);
}
player1 = new Player(R.drawable.player, context, 1);
player2 = new Player(R.drawable.player, context, 2);
puck = new Puck(R.drawable.puck, context);
dragPointer1 = -1;
dragPointer2 = -1;
loadGraphics(context);
isCollision1 = false;
isCollision2 = false;
psec = System.currentTimeMillis();
Expand All @@ -64,6 +59,24 @@ private void update() {
psec = sec;
}

private void loadMusic(Context context) {
hitSound[0] = soundPool.load(context, R.raw.hit1, 1);
hitSound[1] = soundPool.load(context, R.raw.hit2, 1);
hitSound[2] = soundPool.load(context, R.raw.hit3, 1);
hitSound[3] = soundPool.load(context, R.raw.hit4, 1);
hitSound[4] = soundPool.load(context, R.raw.hit5, 1);
}

private void loadGraphics(Context context) {
background = VectorDrawableCompat.create(context.getResources(), R.drawable.background, null);
if (background != null) {
background.setBounds(0, 0, width, MainActivity.height);
}
player1 = new Player(R.drawable.player, context, 1);
player2 = new Player(R.drawable.player, context, 2);
puck = new Puck(R.drawable.puck, context);
}

private void checkCollision() {
if (puck.y < puckScale) {
puck.y = puckScale;
Expand Down Expand Up @@ -97,9 +110,13 @@ private void checkCollision() {

private void collisionWithPuck(Player player) {
// Увага! Нижче йде говнофізіка!
int random = (int) Math.round(Math.random() * 4);
soundPool.play(hitSound[random], 1, 1, 0, 0, 1);
Vector relative;
Vector collided = new Vector(0, 0);

System.out.println(" Speed before collision x: " + puck.v.x + " y: " + puck.v.y);

double alpha = Math.acos((player.x - puck.x) / Math.sqrt(Math.pow(puck.x - player.x, 2) + Math.pow(puck.y - player.y, 2)));
relative = puck.v.deductVector(player.v);
collided.y = -(relative.x * Math.cos(alpha) + relative.y * Math.sin(alpha));
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/hockey/airhockey/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class MainActivity extends AppCompatActivity {

GameField gameField;
public static final String TAG = "MAIN";
static int width, height, playerScale, puckScale;
static int width, height, playerScale, puckScale, frictionValue;
static boolean friction;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -17,6 +18,8 @@ protected void onCreate(Bundle savedInstanceState) {
height = getResources().getDisplayMetrics().heightPixels;
playerScale = getResources().getDisplayMetrics().widthPixels / getResources().getInteger(R.integer.player_scale);
puckScale = getResources().getDisplayMetrics().widthPixels / getResources().getInteger(R.integer.puck_scale);
friction = getResources().getBoolean(R.bool.friction);
frictionValue = getResources().getInteger(R.integer.friction_value);
gameField = new GameField(this);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(gameField);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/hockey/airhockey/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void update() {
void setV(long sec, long psec) {
v.x = (x - xp + 0.) / (sec - psec);
v.y = (y - yp + 0.) / (sec - psec);
v.setVector(v.x, v.y);
xp = x;
yp = y;
}
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/hockey/airhockey/Puck.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.graphics.Canvas;
import android.support.graphics.drawable.VectorDrawableCompat;

import static hockey.airhockey.MainActivity.friction;
import static hockey.airhockey.MainActivity.frictionValue;
import static hockey.airhockey.MainActivity.height;
import static hockey.airhockey.MainActivity.puckScale;
import static hockey.airhockey.MainActivity.width;
Expand All @@ -25,21 +27,12 @@ class Puck {
}

void update(long sec, long psec) {
v.setVector(v.x, v.y);
x += v.x * (sec - psec);
y += v.y * (sec - psec);
/* if (vX != 0 & vY != 0) {
boolean upOrDown = vY > 0;
double alpha = Math.acos(vX / Math.sqrt(Math.pow(vX, 2) + Math.pow(vY, 2)));
double v = Math.sqrt(Math.pow(vX, 2) + Math.pow(vY, 2));
vX = 0.999 * Math.cos(alpha) * v;
vY = 0.999 * Math.sin(alpha) * v;
if (!upOrDown) {
vY = -vY;
}
} else {
vY *= 0.999;
vX *= 0.999;
}*/
if (friction) {
v = v.multiplyVector(frictionValue);
}
drawable.setBounds(x - puckScale, y - puckScale, x + puckScale, y + puckScale);
}

Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/hockey/airhockey/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,22 @@ void setVector(double x, double y) {
Vector deductVector(Vector v) {
return new Vector(this.x - v.x, this.y - v.y);
}

Vector addVector(Vector v) {
return new Vector(this.x + v.x, this.y + v.y);
}

Vector getProjection(Vector v, double yAxisAngle) {
double x, y;
y = v.x * Math.cos(yAxisAngle) + v.y * Math.sin(yAxisAngle);
x = v.x * Math.sin(yAxisAngle) + v.y * Math.cos(yAxisAngle);
return new Vector(x, y);
}

Vector multiplyVector(double multiplier) {
double x, y;
x = multiplier * this.x / 1000;
y = multiplier * this.y / 1000;
return new Vector(x, y);
}
}
Binary file added app/src/main/res/raw/hit1.wav
Binary file not shown.
Binary file added app/src/main/res/raw/hit2.wav
Binary file not shown.
Binary file added app/src/main/res/raw/hit3.wav
Binary file not shown.
Binary file added app/src/main/res/raw/hit4.wav
Binary file not shown.
Binary file added app/src/main/res/raw/hit5.wav
Binary file not shown.
2 changes: 2 additions & 0 deletions app/src/main/res/values/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<resources>
<integer name="player_scale">8</integer>
<integer name="puck_scale">9</integer>
<bool name="friction">true</bool>
<integer name="friction_value">999</integer>
</resources>

0 comments on commit 445b8b0

Please sign in to comment.