-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameplayScene.java
More file actions
180 lines (171 loc) · 8.27 KB
/
GameplayScene.java
File metadata and controls
180 lines (171 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.example.jason.a4x4;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.MotionEvent;
import static com.example.jason.a4x4.SceneManager.HEIGHT;
import static com.example.jason.a4x4.SceneManager.WIDTH;
/**
* Created by JASON on 9/4/2018.
*/
public class GameplayScene implements Scene{
private Context context;
private DatabaseHelper db;
private Rect bgBounds = new Rect(0, 0, WIDTH, HEIGHT +(int)(HEIGHT * 0.075));
private Drawable bg;
private GameBoard board;
private static final int SWIPE_THRESHOLD = 100;
private float DownX = 0, DownY = 0;
private boolean win;
private Paint winScreenColor = new Paint();
private Rect winScreen = new Rect(0,0, WIDTH, HEIGHT);
private Rect resetBound = new Rect((int)(WIDTH * 0.77), (int)(HEIGHT * 0.0263), (int)(WIDTH * 0.95), (int)(HEIGHT * 0.05));
private Drawable resetButton;
private Rect backBound = new Rect((int)(WIDTH * 0.05), (int)(HEIGHT * 0.0263), (int)(WIDTH * 0.23), (int)(HEIGHT * 0.05));
private Drawable backButton;
private boolean postScore;
GameplayScene(Context context){
this.context = context;
db = new DatabaseHelper(context);
bg = ContextCompat.getDrawable(context, R.drawable.test_bg);
bg.setBounds(bgBounds);
resetButton = ContextCompat.getDrawable(context, R.drawable.resetbutton);
resetButton.setBounds(resetBound);
backButton = ContextCompat.getDrawable(context, R.drawable.backutton);
backButton.setBounds(backBound);
board = new GameBoard(context);
win = false;
postScore = false;
winScreenColor.setColor(Color.BLACK);
winScreenColor.setAlpha(100);
winScreenColor.setMaskFilter(new BlurMaskFilter(100, BlurMaskFilter.Blur.NORMAL));
}
@Override
public void update(){
board.update();
}
@Override
public void receiveTouch(MotionEvent event){
float UpX, UpY, Xdif, Ydif;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
DownX = event.getX();
DownY = event.getY();
break;
case MotionEvent.ACTION_UP:
UpX = event.getX();
UpY = event.getY();
Ydif = DownY - UpY;
Xdif = UpX - DownX;
if (!win && Math.abs(Xdif) > Math.abs(Ydif)) {
if (Math.abs(Xdif) > SWIPE_THRESHOLD) {
if (DownX > WIDTH * 0.22222 && DownX < WIDTH * 0.77774 && DownY > HEIGHT * 0.5 && DownY < HEIGHT * 0.5 + 4 * WIDTH * 0.13888) {
if (Xdif > 0) {
if (DownY > HEIGHT * 0.5 && DownY < HEIGHT * 0.5 + WIDTH * 0.13888) {
board.swipeRight(0);
} else if (DownY > HEIGHT * 0.5 + WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 2 * WIDTH * 0.13888) {
board.swipeRight(1);
} else if (DownY > HEIGHT * 0.5 + 2 * WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 3 * WIDTH * 0.13888) {
board.swipeRight(2);
} else if (DownY > HEIGHT * 0.5 + 3 * WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 4 * WIDTH * 0.13888) {
board.swipeRight(3);
}
} else {
if (DownY > HEIGHT * 0.5 && DownY < HEIGHT * 0.5 + WIDTH * 0.13888) {
board.swipeLeft(0);
} else if (DownY > HEIGHT * 0.5 + WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 2 * WIDTH * 0.13888) {
board.swipeLeft(1);
} else if (DownY > HEIGHT * 0.5 + 2 * WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 3 * WIDTH * 0.13888) {
board.swipeLeft(2);
} else if (DownY > HEIGHT * 0.5 + 3 * WIDTH * 0.13888 && DownY < HEIGHT * 0.5 + 4 * WIDTH * 0.13888) {
board.swipeLeft(3);
}
}
board.startGame();
if (board.checkWin()) {
board.stopGame();
win = true;
}
}
}
} else if (!win && Math.abs(Ydif) > SWIPE_THRESHOLD) {
if (DownY > HEIGHT * 0.5 && DownY < HEIGHT * 0.5 + 4 * WIDTH * 0.13888) {
if (Ydif > 0) {
if (DownX > WIDTH * 0.2222 && DownX < WIDTH * 0.3611) {
board.swipeUp(0);
} else if (DownX > WIDTH * 0.3611 && DownX < WIDTH * 0.49998) {
board.swipeUp(1);
} else if (DownX > WIDTH * 0.49998 && DownX < WIDTH * 0.63886) {
board.swipeUp(2);
} else if (DownX > WIDTH * 0.63886 && DownX < WIDTH * 0.77774) {
board.swipeUp(3);
}
board.startGame();
} else {
if (DownX > WIDTH * 0.2222 && DownX < WIDTH * 0.3611) {
board.swipeDown(0);
} else if (DownX > WIDTH * 0.3611 && DownX < WIDTH * 0.49998) {
board.swipeDown(1);
} else if (DownX > WIDTH * 0.49998 && DownX < WIDTH * 0.63886) {
board.swipeDown(2);
} else if (DownX > WIDTH * 0.63886 && DownX < WIDTH * 0.77774) {
board.swipeDown(3);
}
board.startGame();
}
board.startGame();
if (board.checkWin()) {
board.stopGame();
win = true;
}
}
}
if( //reset button
(DownX >= (WIDTH * 0.73) && DownX <= (WIDTH * 0.99)) && //check in resetBounds
(DownY >= (HEIGHT * 0.02) && DownY <= (HEIGHT * 0.06)) &&
(UpX >= (WIDTH * 0.73) && UpX <= (WIDTH * 0.99)) &&
(UpY >= (HEIGHT * 0.02) && UpY <= (HEIGHT * 0.06))
){
board = new GameBoard(context);
win = false;
postScore = false;
}
if((DownX >= 0 && DownX <= (WIDTH * 0.28)) && //back button
(DownY >= (HEIGHT * 0.02) && DownY <= (HEIGHT * 0.06)) &&
(UpX >= 0 && UpX <= (WIDTH * 0.28)) &&
(UpY >= (HEIGHT * 0.02) && UpY <= (HEIGHT * 0.06))
){
board = new GameBoard(context); //also resets game
win = false;
postScore = false;
ChangeToMainMenuScene();
}
}
}
@Override
public void draw(Canvas canvas){
bg.draw(canvas);
board.draw(canvas);
backButton.draw(canvas);
resetButton.draw(canvas);
if(win){
canvas.drawRect( winScreen, winScreenColor);
if(!postScore) { //post only once, until reset
db.insert(board.getScore());
postScore = true;
}
}
}
@Override
public void terminate(){
SceneManager.ACTIVE_SCENE = 0;
}
public void ChangeToMainMenuScene(){
SceneManager.ACTIVE_SCENE = 0;
}
}