summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/TurnTimer.java
blob: 17bdf0f373604cee9dd771f2b40b49b5866570aa (plain)
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
package jrummikub.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.view.IView;

/**
 * Count-down timer used to limit the turn time
 */
public class TurnTimer implements ActionListener, ITurnTimer {
	private IView view;
	private int timeLeft = 60;
	private Timer timer;
	private Event timeRunOutEvent = new Event();

	/**
	 * Create a new timer using a given view to display the current time left
	 * 
	 * @param view
	 *            view to display
	 */
	public TurnTimer(IView view) {
		this.view = view;
		timer = new Timer(1000, this);
		timer.setRepeats(true);
		timer.setCoalesce(false);
		view.getPlayerPanel().setTimeLeft(timeLeft);
	}

	@Override
	public void startTimer() {
		timer.start();
	}

	@Override
	public void stopTimer() {
		timer.stop();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		timeLeft--;
		view.getPlayerPanel().setTimeLeft(timeLeft);
		if (timeLeft == 0) {
			timer.stop();
			timeRunOutEvent.emit();
		}
	}

	@Override
	public IEvent getTimeRunOutEvent() {
		return timeRunOutEvent;
	}
}