summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/event/specialmove/MoveEventThread.java
blob: 2395dd05372452e6d5036f8ad9aca06502dfca62 (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
/*
 * 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.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hatkirby
 */
public class MoveEventThread implements Runnable {
        
    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 run()
    {
        try {
            moveEventWait.acquire();
        } catch (InterruptedException ex) {
            Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        while (ev.isMoving())
        {
            try {
                Thread.sleep(2);
            } catch (InterruptedException ex) {
                Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        events.add(ev);
        
        for (MoveEvent action : actions)
        {
            action.doAction(ev);
        }

        events.remove(ev);
        moveEventWait.release();
    }
    
    /* TODO Rename the two following methods (isHeroMoving and isOtherMoving)
     * to isHeroActive and isOtherActive respectively.
     */
    
    public static boolean isHeroMoving()
    {
        return (events.contains(Game.getHeroEvent()));
    }
    
    public static boolean isOtherMoving(LayerEvent event)
    {
        return (events.contains(event));
    }
    
    public static void moveAll()
    {
        try {
            moveEventWait.acquire(100);
            moveEventWait.release(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(MoveEventThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}