summary refs log tree commit diff stats
path: root/src/com/fourisland/fourpuzzle/transition/SlideTransition.java
blob: aa91061884cf8c7277d4b49ed1f53a7c7cba3da8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fourisland.fourpuzzle.transition;

import com.fourisland.fourpuzzle.Direction;
import com.fourisland.fourpuzzle.Game;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author hatkirby
 */
public class SlideTransition implements MultidirectionalTransition {

    TransitionDirection direction;
    Direction d;
    public SlideTransition(TransitionDirection direction, Direction d)
    {
        this.direction = direction;
        this.d = d;
        
        if (d == Direction.North)
        {
            max = -(Game.HEIGHT);
            way = SlideDirection.UpOrDown;
        } else if (d == Direction.West)
        {
            max = -(Game.WIDTH);
            way = SlideDirection.SideToSide;
        } else if (d == Direction.South)
        {
            max = Game.HEIGHT;
            way = SlideDirection.UpOrDown;
        } else if (d == Direction.East)
        {
            max = Game.WIDTH;
            way = SlideDirection.SideToSide;
        }
        
        wait = Math.round(max / (Game.FPS/2));
    }
    
    public TransitionDirection getDirection()
    {
        return direction;
    }

    int tick;
    int wait;
    int max;
    SlideDirection way;
    public boolean render(Graphics2D g)
    {   
        if (max > 0)
        {
            tick = Math.min(tick + wait, max);
        } else {
            tick = Math.max(tick + wait, max);
        }
        
        if (direction == TransitionDirection.Out)
        {
            g.setColor(Color.BLACK);
            
            if (way == SlideDirection.SideToSide)
            {
                g.drawImage(preTransition, tick, 0, null);
                
                if (max > 0)
                {
                    g.fillRect(0, 0, tick, Game.HEIGHT);
                } else {
                    g.fillRect(Game.WIDTH+tick, 0, -tick, Game.HEIGHT);
                }
            } else {
                g.drawImage(preTransition, 0, tick, null);
                
                if (max > 0)
                {
                    g.fillRect(0, 0, Game.WIDTH, tick);
                } else {
                    g.fillRect(0, Game.HEIGHT+tick, Game.WIDTH, -tick);
                }
            }
        } else if (direction == TransitionDirection.In)
        {
            if (way == SlideDirection.SideToSide)
            {
                g.drawImage(preTransition, tick, 0, null);
                
                if (max > 0)
                {
                    g.drawImage(postTransition, tick - Game.WIDTH, 0, null);
                } else {
                    g.drawImage(postTransition, Game.WIDTH + tick, 0, null);
                }
            } else {
                g.drawImage(preTransition, 0, tick, null);
                
                if (max > 0)
                {
                    g.drawImage(postTransition, 0, tick - Game.HEIGHT, null);
                } else {
                    g.drawImage(postTransition, 0, Game.HEIGHT + tick, null);
                }
            }
        }
        
        if (tick == max)
        {
            return true;
        }
        
        return false;
    }

    private BufferedImage preTransition;
    public void setPreTransition(BufferedImage preTransition)
    {
        this.preTransition = preTransition;
    }
    
    private BufferedImage postTransition;
    public void setPostTransition(BufferedImage postTransition)
    {
        this.postTransition = postTransition;
    }
    
    private enum SlideDirection
    {
        SideToSide,
        UpOrDown
    }

    public Transition copy()
    {
        return new SlideTransition(direction, d);
    }

}