summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/ShakingViewpoint.java
blob: f2b40e7d21a31183771f8c0e78ab4a3e5ab6d6af (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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.gamestate.mapview.viewpoint;

import com.fourisland.fourpuzzle.util.Interval;

/**
 *
 * @author hatkirby
 */
public class ShakingViewpoint implements Viewpoint {
    
    int sx;
    int length;
    int x;
    int y;
    ShakeSpeed speed;
    Interval in;
    Runnable callback;
    boolean back = false;
    long start;
    public ShakingViewpoint(int x, int y, ShakeSpeed speed, int length, Runnable callback)
    {
        this.sx = x;
        this.length = length;
        this.x = x;
        this.y = y;
        this.in = Interval.createMillisInterval(1000 / speed.getSpeed());
        this.speed = speed;
        this.callback = callback;
        this.start = System.currentTimeMillis();
    }
    
    private void refresh()
    {
        if (back)
        {
            x -= speed.getSpeed();
            
            if (x < sx - 16)
            {
                back = false;
            }
        } else {
            x += speed.getSpeed();
            
            if (x > sx + 16)
            {
                back = true;
            }
        }
        
        if (start + length <= System.currentTimeMillis())
        {
            callback.run();
        }
    }

    public int getX()
    {
        if (in.isElapsed())
        {
            refresh();
        }
        
        return x;
    }

    public int getY()
    {
        return y;
    }
    
    public static enum ShakeSpeed
    {
        Slow(16),
        Medium(24),
        Fast(32);
        
        private int speed;
        private ShakeSpeed(int speed)
        {
            this.speed = speed;
        }
        
        public int getSpeed()
        {
            return speed;
        }
    }

}