blob: 488331aa7715d13de67cc16ffce78f31a7e831f4 (
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
|
/*
* 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.List;
import java.util.Vector;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
*
* @author hatkirby
*/
public class MoveEventThread implements Runnable {
private static Executor moveEventExecutor = Executors.newCachedThreadPool();
private static volatile List<Event> events = new Vector<Event>();
private static volatile Semaphore moveEventWait = new Semaphore(100);
private Event ev;
private MoveEvent[] actions;
public MoveEventThread(Event ev, MoveEvent[] actions)
{
this.ev = ev;
this.actions = actions;
}
public void start()
{
moveEventExecutor.execute(this);
}
public void run()
{
try {
moveEventWait.acquire();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
while (ev.isMoving())
{
try {
Thread.sleep(2);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
events.add(ev);
for (MoveEvent action : actions)
{
action.doAction(ev);
}
events.remove(ev);
moveEventWait.release();
}
public static void moveAll() throws InterruptedException
{
moveEventWait.acquire(100);
moveEventWait.release(100);
}
public static boolean isHeroActive()
{
return (events.contains(Game.getHeroEvent()));
}
public static boolean isOtherActive(LayerEvent event)
{
return (events.contains(event));
}
}
|