summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java
blob: 233c415aec484a9adde7ad35ab13f36bb44c91fb (plain) (blame)
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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.gamestate.mapview.event.specialmove;

import com.fourisland.fourpuzzle.Game;
import com.fourisland.fourpuzzle.gamestate.mapview.event.Event;
import com.fourisland.fourpuzzle.gamestate.mapview.event.LayerEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;

/**
 *
 * @author hatkirby
 */
public class MoveEventThread implements Runnable {
    
    private static ExecutorService moveEventExecutor = Executors.newCachedThreadPool();

    private static volatile List<Event> events = new Vector<Event>();
    private static volatile Semaphore moveEventWait = new Semaphore(100);
    private static volatile List<Future> eventThreads = new ArrayList<Future>();
    
    private Event ev;
    private MoveEvent[] actions;
    
    public MoveEventThread(Event ev, MoveEvent[] actions)
    {
        this.ev = ev;
        this.actions = actions;
    }
    
    public void start()
    {
        for (Future f : eventThreads)
        {
            if (f.isDone())
            {
                eventThreads.remove(f);
            }
        }
        
        eventThreads.add(moveEventExecutor.submit(this));
    }

    public void run()
    {
        try {
            moveEventWait.acquire();
        } catch (InterruptedException ex) {
            return;
        }
        
        while (ev.isMoving())
        {
            try {
                Thread.sleep(2);
            } catch (InterruptedException ex) {
                return;
            }
        }
        
        events.add(ev);
        
        try
        {
            for (MoveEvent action : actions)
            {
                action.doAction(ev);

                if (Thread.currentThread().isInterrupted())
                {
                    throw new InterruptedException();
                }
            }
        } catch (InterruptedException ex)
        {
            /* Swallow the interrupt because execution will drop to the finally
             * and then the method will end anyway */
        } finally {
            events.remove(ev);
            moveEventWait.release();
        }
    }
    
    public static void moveAll() throws InterruptedException
    {
        try
        {
            moveEventWait.acquire(100);
        } catch (InterruptedException ex)
        {
            for (Future f : eventThreads)
            {
                f.cancel(true);
            }
            
            throw ex;
        } finally {
            moveEventWait.release(100);
        }
    }
    
    public static boolean isHeroActive()
    {
        return (events.contains(Game.getHeroEvent()));
    }
    
    public static boolean isOtherActive(LayerEvent event)
    {
        return (events.contains(event));
    }

}