Source code: org/dma/ihm/game/Game.java
1 package org.dma.ihm.game;
2
3 import java.io.*;
4
5 import java.util.*;
6 import org.dma.ihm.controller.*;
7 import org.dma.ihm.game.calendar.*;
8 import org.dma.ihm.game.calendar.events.*;
9
10 import org.dma.ihm.game.league.*;
11 import org.dma.ihm.game.team.*;
12
13 /**
14 * The Game class extends Threads, and is responsible for the loop of events
15 * provided by the GameCalendar.
16 *
17 * @author Bernhard von Gunten & Arik Dasen
18 * @created December 29, 2001
19 */
20 public class Game extends Thread implements Serializable {
21
22 /** Listeners connected to the game class */
23 private Vector listener;
24 /** Flag if thread is suspended */
25 private volatile boolean suspended = false;
26
27
28 /** Constructor for the Game object */
29 public Game() { }
30
31
32 /** should only be used after a loadScenario() */
33 public synchronized void setSuspended() {
34 suspended = true;
35 }
36
37
38 /** Run method. A bit complicated, cause suspend() and resume() are depricated */
39 public void run() {
40 while (true) {
41 boolean stop = false;
42 if (suspended) {
43 synchronized (this) {
44 while (suspended) {
45 try {
46 wait();
47 } catch (InterruptedException e) {}
48 ;
49 }
50 }
51 } else {
52 GameCalendarEvent event = Controller.getScenario().getGameCalendar().getNextEvent();
53 stop = event.needsOnline();
54 event.play();
55 if (stop) {
56 synchronized (this) {
57 suspended = true;
58 }
59 processGameCalendarEvent(event);
60 }
61 }
62 }
63 }
64
65
66 /** MoveOn method called by the controller */
67 public synchronized void moveOn() {
68 suspended = false;
69 notify();
70 }
71
72
73 /**
74 * event-handling
75 *
76 * @param m The feature to be added to the GameCalendarListener attribute
77 */
78 public synchronized void addGameCalendarListener(GameCalendarListener m) {
79 if (listener == null) {
80 listener = new Vector(5, 5);
81 }
82 if (!listener.contains(m)) {
83 listener.addElement(m);
84 }
85 }
86
87
88 /**
89 * event-handling
90 *
91 * @param m GameCalendar event to be processed.
92 */
93 private synchronized void processGameCalendarEvent(GameCalendarEvent m) {
94 if (listener != null) {
95 for (Enumeration e = listener.elements(); e.hasMoreElements(); ) {
96 ((GameCalendarListener) (e.nextElement())).gameCalendarEventPerformed(m);
97 }
98 }
99 }
100 }