-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTile.java
More file actions
75 lines (63 loc) · 3.55 KB
/
Copy pathTile.java
File metadata and controls
75 lines (63 loc) · 3.55 KB
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
package coloris;
import java.util.HashMap;
import java.util.Random;
import javafx.animation.TranslateTransition;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;
public class Tile extends Rectangle
{
private static final Color[] COLORS = new Color[] {Color.RED, Color.BLUE, Color.PURPLE, Color.YELLOW, Color.WHITE};
private static final HashMap<Color, String> SPRITE_MAPPINGS = new HashMap<Color, String>() {{
put(Color.RED, "/coloris/sprites/red.png");
put(Color.BLUE, "/coloris/sprites/blue.png");
put(Color.PURPLE, "/coloris/sprites/purple.png");
put(Color.YELLOW, "/coloris/sprites/yellow.png");
put(Color.WHITE, "/coloris/sprites/white.png");
}};
private static final HashMap<Color, String> DESTROY_MAPPINGS = new HashMap<Color,String>() {{
put(Color.RED, "/coloris/sprites/destroy_red.png");
put(Color.BLUE, "/coloris/sprites/destroy_blue.png");
put(Color.PURPLE, "/coloris/sprites/destroy_purple.png");
put(Color.YELLOW, "/coloris/sprites/destroy_yellow.png");
put(Color.WHITE, "/coloris/sprites/destroy_white.png");
}};
private static final double FALL_ONE_ROW_IN_MILLIS = 100.0;
private final Color color;
public Tile(double dimension) //treba da dodam i offset, boardWidth i boardHeight
{
super(dimension, dimension);
Random random = new Random();
int index = (int)(random.nextDouble() * COLORS.length);
color = COLORS[index];
String path = SPRITE_MAPPINGS.get(color);
Image sprite = new Image(getClass().getResource(path).toString());
setFill(new ImagePattern(sprite, 0, 0, 1, 1, true));
}
public TranslateTransition createTranslation(int noPlaces)
{
TranslateTransition tt = new TranslateTransition();
tt.setNode(this);
tt.setDuration(Duration.millis(FALL_ONE_ROW_IN_MILLIS * noPlaces));
tt.setByY(getWidth() * noPlaces);
return tt;
}
public Color getColor() {return color;}
public boolean sameColor(Tile tile) {return color.equals(tile.getColor());}
public void setDestroySprite()
{
String path = DESTROY_MAPPINGS.get(color);
Image sprite = new Image(getClass().getResource(path).toString());
setFill(new ImagePattern(sprite, 0, 0, 1, 1, true));
}
@Override
public String toString()
{
String path = SPRITE_MAPPINGS.get(getColor());
int start = path.lastIndexOf('/');
char c = path.charAt(start + 1);
return String.valueOf(c).toUpperCase();
}
}