summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/MovingViewpoint.java
blob: e6db32b6295101facf400c38a780183f9cc2a262 (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
/*
 * 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.Game;
import com.fourisland.fourpuzzle.util.Interval;

/**
 *
 * @author hatkirby
 */
public class MovingViewpoint implements Viewpoint {
    
    private double x;
    private double y;
    private int sx;
    private int sy;
    private int dx;
    private int dy;
    private double speed;
    private int xdist;
    private int ydist;
    private Runnable callback;
    private Interval in;
    
    public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback)
    {
        this(sx, sy, dx, dy, callback, 1000);
    }
    
    public MovingViewpoint(int sx, int sy, int dx, int dy, Runnable callback, int length)
    {
        this.x = sx;
        this.y = sy;
        this.sx = sx;
        this.sy = sy;
        this.dx = dx;
        this.dy = dy;
        this.speed = length / Game.FPS;
        this.xdist = dx - sx;
        this.ydist = dy - sy;
        this.callback = callback;
        this.in = Interval.createMillisInterval((int) speed);
    }
    
    private void refresh()
    {
        x += (xdist / speed);
        y += (ydist / speed);
        
        if (((sx < dx) && (x > dx)) || ((sx > dx) && (x < dx)))
        {
            x = dx;
        }
        
        if (((sy < dy) && (y > dy)) || ((sy > dy) && (y < dy)))
        {
            y = dy;
        }
        
        if ((x == dx) && (y == dy))
        {
            callback.run();
        }
    }

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

    public int getY()
    {
        if (in.isElapsed())
        {
            refresh();
        }
        
        return (int) y;
    }
    
}