This repository was archived by the owner on Aug 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 413
Expand file tree
/
Copy pathEngine.java
More file actions
266 lines (232 loc) · 8.53 KB
/
Engine.java
File metadata and controls
266 lines (232 loc) · 8.53 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.snatik.matches.engine;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.widget.ImageView;
import com.snatik.matches.R;
import com.snatik.matches.common.Memory;
import com.snatik.matches.common.Music;
import com.snatik.matches.common.Shared;
import com.snatik.matches.engine.ScreenController.Screen;
import com.snatik.matches.events.EventObserverAdapter;
import com.snatik.matches.events.engine.FlipDownCardsEvent;
import com.snatik.matches.events.engine.GameWonEvent;
import com.snatik.matches.events.engine.HidePairCardsEvent;
import com.snatik.matches.events.ui.BackGameEvent;
import com.snatik.matches.events.ui.DifficultySelectedEvent;
import com.snatik.matches.events.ui.FlipCardEvent;
import com.snatik.matches.events.ui.NextGameEvent;
import com.snatik.matches.events.ui.ResetBackgroundEvent;
import com.snatik.matches.events.ui.StartEvent;
import com.snatik.matches.events.ui.ThemeSelectedEvent;
import com.snatik.matches.model.BoardArrangment;
import com.snatik.matches.model.BoardConfiguration;
import com.snatik.matches.model.Game;
import com.snatik.matches.model.GameState;
import com.snatik.matches.themes.Theme;
import com.snatik.matches.themes.Themes;
import com.snatik.matches.ui.PopupManager;
import com.snatik.matches.utils.Clock;
import com.snatik.matches.utils.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class Engine extends EventObserverAdapter {
private static Engine mInstance = null;
private Game mPlayingGame = null;
private int mFlippedId = -1;
private int mToFlip = -1;
private ScreenController mScreenController;
private Theme mSelectedTheme;
private ImageView mBackgroundImage;
private Handler mHandler;
private Engine() {
mScreenController = ScreenController.getInstance();
mHandler = new Handler();
}
public static Engine getInstance() {
if (mInstance == null) {
mInstance = new Engine();
}
return mInstance;
}
public void start() {
Shared.eventBus.listen(DifficultySelectedEvent.TYPE, this);
Shared.eventBus.listen(FlipCardEvent.TYPE, this);
Shared.eventBus.listen(StartEvent.TYPE, this);
Shared.eventBus.listen(ThemeSelectedEvent.TYPE, this);
Shared.eventBus.listen(BackGameEvent.TYPE, this);
Shared.eventBus.listen(NextGameEvent.TYPE, this);
Shared.eventBus.listen(ResetBackgroundEvent.TYPE, this);
}
public void stop() {
mPlayingGame = null;
mBackgroundImage.setImageDrawable(null);
mBackgroundImage = null;
mHandler.removeCallbacksAndMessages(null);
mHandler = null;
Shared.eventBus.unlisten(DifficultySelectedEvent.TYPE, this);
Shared.eventBus.unlisten(FlipCardEvent.TYPE, this);
Shared.eventBus.unlisten(StartEvent.TYPE, this);
Shared.eventBus.unlisten(ThemeSelectedEvent.TYPE, this);
Shared.eventBus.unlisten(BackGameEvent.TYPE, this);
Shared.eventBus.unlisten(NextGameEvent.TYPE, this);
Shared.eventBus.unlisten(ResetBackgroundEvent.TYPE, this);
mInstance = null;
}
@Override
public void onEvent(ResetBackgroundEvent event) {
Drawable drawable = mBackgroundImage.getDrawable();
if (drawable != null) {
((TransitionDrawable) drawable).reverseTransition(2000);
} else {
Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
mBackgroundImage.setImageBitmap(bitmap);
}
}
@Override
public void onEvent(StartEvent event) {
mScreenController.openScreen(Screen.THEME_SELECT);
}
@Override
public void onEvent(NextGameEvent event) {
PopupManager.closePopup();
int difficulty = mPlayingGame.boardConfiguration.difficulty;
if (mPlayingGame.gameState.achievedStars == 3 && difficulty < 6) {
difficulty++;
}
Shared.eventBus.notify(new DifficultySelectedEvent(difficulty));
}
@Override
public void onEvent(BackGameEvent event) {
PopupManager.closePopup();
mScreenController.openScreen(Screen.DIFFICULTY);
}
@Override
public void onEvent(ThemeSelectedEvent event) {
mSelectedTheme = event.theme;
mScreenController.openScreen(Screen.DIFFICULTY);
Bitmap bitmap = Utils.scaleDown(R.drawable.background, Utils.screenWidth(), Utils.screenHeight());
Bitmap backgroundImage = Themes.getBackgroundImage(mSelectedTheme);
backgroundImage = Utils.crop(backgroundImage, Utils.screenHeight(), Utils.screenWidth());
Drawable[] backgrounds = new Drawable[2];
backgrounds[0] = new BitmapDrawable(Shared.context.getResources(), bitmap);
backgrounds[1] = new BitmapDrawable(Shared.context.getResources(), backgroundImage);
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
mBackgroundImage.setImageDrawable(crossfader);
crossfader.startTransition(2000);
}
@Override
public void onEvent(DifficultySelectedEvent event) {
mFlippedId = -1;
mPlayingGame = new Game();
mPlayingGame.boardConfiguration = new BoardConfiguration(event.difficulty);
mPlayingGame.theme = mSelectedTheme;
mToFlip = mPlayingGame.boardConfiguration.numTiles;
// arrange board
arrangeBoard();
// start the screen
mScreenController.openScreen(Screen.GAME);
}
private void arrangeBoard() {
BoardConfiguration boardConfiguration = mPlayingGame.boardConfiguration;
BoardArrangment boardArrangment = new BoardArrangment();
// build pairs
// result {0,1,2,...n} // n-number of tiles
List<Integer> ids = new ArrayList<Integer>();
for (int i = 0; i < boardConfiguration.numTiles; i++) {
ids.add(i);
}
// shuffle
// result {4,10,2,39,...}
Collections.shuffle(ids);
// place the board
List<String> tileImageUrls = mPlayingGame.theme.tileImageUrls;
Collections.shuffle(tileImageUrls);
boardArrangment.pairs = new HashMap<Integer, Integer>();
boardArrangment.tileUrls = new HashMap<Integer, String>();
int j = 0;
for (int i = 0; i < ids.size(); i++) {
if (i + 1 < ids.size()) {
// {4,10}, {2,39}, ...
boardArrangment.pairs.put(ids.get(i), ids.get(i + 1));
// {10,4}, {39,2}, ...
boardArrangment.pairs.put(ids.get(i + 1), ids.get(i));
// {4,
boardArrangment.tileUrls.put(ids.get(i), tileImageUrls.get(j));
boardArrangment.tileUrls.put(ids.get(i + 1), tileImageUrls.get(j));
i++;
j++;
}
}
mPlayingGame.boardArrangment = boardArrangment;
}
@Override
public void onEvent(FlipCardEvent event) {
// Log.i("my_tag", "Flip: " + event.id);
int id = event.id;
if (mFlippedId == -1) {
mFlippedId = id;
// Log.i("my_tag", "Flip: mFlippedId: " + event.id);
} else {
if (mPlayingGame.boardArrangment.isPair(mFlippedId, id)) {
// Log.i("my_tag", "Flip: is pair: " + mFlippedId + ", " + id);
// send event - hide id1, id2
Shared.eventBus.notify(new HidePairCardsEvent(mFlippedId, id), 1000);
// play music
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Music.playCorrent();
}
}, 1000);
mToFlip -= 2;
if (mToFlip == 0) {
int passedSeconds = (int) (Clock.getInstance().getPassedTime() / 1000);
Clock.getInstance().pause();
int totalTime = mPlayingGame.boardConfiguration.time;
GameState gameState = new GameState();
mPlayingGame.gameState = gameState;
// remained seconds
gameState.remainedSeconds = totalTime - passedSeconds;
gameState.passedSeconds = passedSeconds;
// calc stars
if (passedSeconds <= totalTime / 2) {
gameState.achievedStars = 3;
} else if (passedSeconds <= totalTime - totalTime / 5) {
gameState.achievedStars = 2;
} else if (passedSeconds < totalTime) {
gameState.achievedStars = 1;
} else {
gameState.achievedStars = 0;
}
// calc score
gameState.achievedScore = mPlayingGame.boardConfiguration.difficulty * gameState.remainedSeconds * mPlayingGame.theme.id;
// save to memory
Memory.save(mPlayingGame.theme.id, mPlayingGame.boardConfiguration.difficulty, gameState.achievedStars);
Memory.saveTime(mPlayingGame.theme.id, mPlayingGame.boardConfiguration.difficulty ,gameState.passedSeconds);
Shared.eventBus.notify(new GameWonEvent(gameState), 1200);
}
} else {
// Log.i("my_tag", "Flip: all down");
// send event - flip all down
Shared.eventBus.notify(new FlipDownCardsEvent(), 1000);
}
mFlippedId = -1;
// Log.i("my_tag", "Flip: mFlippedId: " + mFlippedId);
}
}
public Game getActiveGame() {
return mPlayingGame;
}
public Theme getSelectedTheme() {
return mSelectedTheme;
}
public void setBackgroundImageView(ImageView backgroundImage) {
mBackgroundImage = backgroundImage;
}
}