-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
130 lines (106 loc) · 3.62 KB
/
Copy pathMap.java
File metadata and controls
130 lines (106 loc) · 3.62 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
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
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.*;
public class Map {
private TreeRockStrip firstStrip;
private Strip[] map;
private LocationGrid grid;
private BufferedImage finalImage;
private BufferedImage[] objImages;
private final int WIDTH = 1000;
private final int HEIGHT = 700;
public Map(LocationGrid g){
grid = g;
this.firstStrip = new TreeRockStrip();
grid.update(firstStrip);
map = new Strip[WIDTH/50];
map[0] = firstStrip;
for (int i = 1; i < map.length-1; i++){
if (randomInt() == 0){
map[i] = new TreeRockStrip(map[i-1],i);
} else {
map[i] = new RoadStrip(i,grid);
}
grid.update(map[i]);
}
map[map.length-1] = new TreeRockStrip(map[map.length-2],map.length-1);
grid.update(map[map.length-1]);
objImages = new BufferedImage[4];
objImages[1] = loadImage("tree");
objImages[2] = loadImage("rock");
objImages[3] = loadImage("road");
}
public Map(TreeRockStrip firstStrip, LocationGrid g){
grid = g;
this.firstStrip = firstStrip;
this.firstStrip.setIndex(0);
grid.update(firstStrip);
map = new Strip[WIDTH/50];
map[0] = firstStrip;
for (int i = 1; i < map.length-1; i++){
if (randomInt() == 0){
map[i] = new TreeRockStrip(map[i-1],i);
} else {
map[i] = new RoadStrip(i,grid);
}
grid.update(map[i]);
}
map[map.length-1] = new TreeRockStrip(map[map.length-2],map.length-1);
grid.update(map[map.length-1]);
objImages = new BufferedImage[4];
objImages[1] = loadImage("tree");
objImages[2] = loadImage("rock");
objImages[3] = loadImage("road");
}
//returns 0 or 1 based on probability function
public int randomInt(){
int pick = (int)(Math.random()*10);
if (pick < 6) return 1;
return 0;
}
public static BufferedImage loadImage(String file) {
BufferedImage sprite = null;
try {
sprite = ImageIO.read(new File(file + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
return sprite;
}
public BufferedImage addMapObjects(BufferedImage img){
BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = out.createGraphics();
g.drawImage(img,0,0,null);
for (int i = 0; i < map.length; i++){
out = map[i].addStrip(this,out);
}
g.dispose();
return out;
}
public BufferedImage addMapBackground(BufferedImage img){
BufferedImage out = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = out.createGraphics();
g.drawImage(img,0,0,null);
for (int i = 0; i < map.length; i++){
int[] objGrid = map[i].getObjects();
for (int j = 0; j < objGrid.length; j++){
if (objGrid[j] == 3){
g.drawImage(objImages[3],50*i,50*j,null);
}
}
}
g.dispose();
return out;
}
public BufferedImage getMap(){
return finalImage;
}
public Strip getStrip(int i){
return map[i];
}
public BufferedImage[] getObjects(){
return objImages;
}
}