summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/TurnTimer.java
blob: 789bf1a301602da399dae06f2d864573210910e0 (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
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;

public class TurnTimer implements ActionListener, ITurnTimer {
	private IView view;
	private int timeLeft = 60;
	private Timer timer;
	private Event timeRunOutEvent = new Event();

	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;
	}
}