summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/gamestate/mapview/viewpoint/AutomaticViewpoint.java
blob: 1f8a7965af779753c9a413fbf1ef0a8b92f18577 (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
/*
 * 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.gamestate.mapview.Map;
import com.fourisland.fourpuzzle.gamestate.mapview.event.HeroEvent;
import java.awt.Point;

/**
 *
 * @author hatkirby
 */
public class AutomaticViewpoint implements Viewpoint {
    
    private Map map;
    private Point heroLoc = new Point();
    private Point viewpoint;
    
    public AutomaticViewpoint(Map map)
    {
        this.map = map;
        
        refresh();
    }
    
    private void refresh()
    {
        int x,y;
        
        HeroEvent hero = Game.getHeroEvent();
        heroLoc.setLocation(hero.getLocation());
        
        Point endLoc = new Point(hero.getLocation());
        if (hero.isMoving())
        {
            switch (hero.getDirection())
            {
                case North: endLoc.translate(0, -1); break;
                case West: endLoc.translate(-1, 0); break;
                case South: endLoc.translate(0, 1); break;
                case East: endLoc.translate(1, 0); break;
            }
        }
        
        if (Math.max(endLoc.x,heroLoc.x) > 10)
        {
            if (Math.max(endLoc.x,heroLoc.x) < (map.getSize().width - 9))
            {
                x = (heroLoc.x - 10) * 16;
                x += hero.getMovingX();
            } else {
                x = (map.getSize().width - 20) * 16;
            }
        } else {
            x = 0;
        }
        
        if (Math.max(endLoc.y,heroLoc.y) > 7)
        {
            if (Math.max(endLoc.y,heroLoc.y) < (map.getSize().height - 7))
            {
                y = (heroLoc.y - 7) * 16;
                y += hero.getMovingY();
            } else {
                y = (map.getSize().height - 15) * 16;
            }
        } else {
            y = 0;
        }
        
        viewpoint = new Point(x,y);
    }

    public int getX()
    {
        if (!Game.getHeroEvent().getLocation().equals(heroLoc) || Game.getHeroEvent().isMoving())
        {
            refresh();
        }
        
        return viewpoint.x;
    }

    public int getY()
    {
        if (!Game.getHeroEvent().getLocation().equals(heroLoc) || Game.getHeroEvent().isMoving())
        {
            refresh();
        }
        
        return viewpoint.y;
    }

}