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 pathGameFragment.java
More file actions
114 lines (96 loc) · 3.35 KB
/
GameFragment.java
File metadata and controls
114 lines (96 loc) · 3.35 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
package com.snatik.matches.fragments;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.snatik.matches.R;
import com.snatik.matches.common.Music;
import com.snatik.matches.common.Shared;
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.model.Game;
import com.snatik.matches.ui.BoardView;
import com.snatik.matches.ui.PopupManager;
import com.snatik.matches.utils.Clock;
import com.snatik.matches.utils.Clock.OnTimerCount;
import com.snatik.matches.utils.FontLoader;
import com.snatik.matches.utils.FontLoader.Font;
public class GameFragment extends BaseFragment{
private BoardView mBoardView;
private TextView mTime;
private ImageView mTimeImage;
private LinearLayout ads;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate(R.layout.game_fragment, container, false);
view.setClipChildren(false);
((ViewGroup)view.findViewById(R.id.game_board)).setClipChildren(false);
mTime = (TextView) view.findViewById(R.id.time_bar_text);
mTimeImage = (ImageView) view.findViewById(R.id.time_bar_image);
FontLoader.setTypeface(Shared.context, new TextView[] {mTime}, Font.GROBOLD);
mBoardView = BoardView.fromXml(getActivity().getApplicationContext(), view);
FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.game_container);
frameLayout.addView(mBoardView);
frameLayout.setClipChildren(false);
// build board
buildBoard();
Shared.eventBus.listen(FlipDownCardsEvent.TYPE, this);
Shared.eventBus.listen(HidePairCardsEvent.TYPE, this);
Shared.eventBus.listen(GameWonEvent.TYPE, this);
return view;
}
@Override
public void onDestroy() {
Shared.eventBus.unlisten(FlipDownCardsEvent.TYPE, this);
Shared.eventBus.unlisten(HidePairCardsEvent.TYPE, this);
Shared.eventBus.unlisten(GameWonEvent.TYPE, this);
super.onDestroy();
}
private void buildBoard() {
Game game = Shared.engine.getActiveGame();
int time = game.boardConfiguration.time;
setTime(time);
mBoardView.setBoard(game);
startClock(time);
}
private void setTime(int time) {
int min = time / 60;
int sec = time - min*60;
mTime.setText(" " + String.format("%02d", min) + ":" + String.format("%02d", sec));
}
private void startClock(int sec) {
Clock clock = Clock.getInstance();
clock.startTimer(sec*1000, 1000, new OnTimerCount() {
@Override
public void onTick(long millisUntilFinished) {
setTime((int) (millisUntilFinished/1000));
}
@Override
public void onFinish() {
setTime(0);
}
});
}
@Override
public void onEvent(GameWonEvent event) {
mTime.setVisibility(View.GONE);
mTimeImage.setVisibility(View.GONE);
PopupManager.showPopupWon(event.gameState);
}
@Override
public void onEvent(FlipDownCardsEvent event) {
mBoardView.flipDownAll();
}
@Override
public void onEvent(HidePairCardsEvent event) {
mBoardView.hideCards(event.id1, event.id2);
}
}