Repository navigation
Expand file tree
/
Copy pathMap.java
More file actions
195 lines (151 loc) · 4.4 KB
/
Copy pathMap.java
File metadata and controls
195 lines (151 loc) · 4.4 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.KeyEvent;
class Map{
final int sizex ,sizey;//
Isle[][] ilands;
int old_player_x, old_player_y;
static final int seed=23;
Player seaplayer;
public Map(){
sizey=sizex=20;
ilands=new Isle[sizex][sizey];
refreshAll();
seaplayer=new Player();
SeaCamera.initializeCamera(seaplayer.getX(), seaplayer.getY());
}
public void paint(Graphics g, int screenwidth , int screenheight){
Isle isleonposition=updateLanding();
if(isleonposition!=null){
isleonposition.paintOnLand(g,screenwidth,screenheight);
}else{
paintAtSea(g, screenwidth, screenheight);
}
}
public void paintAtSea(Graphics g, int screenwidth , int screenheight){
double scalex=screenwidth/sizex;
double scaley=screenheight/sizey;
int tmpx=(int)(scalex*(seaplayer.getX()-SeaCamera.getCameraX()));
int tmpy=(int)(scaley*(seaplayer.getY()-SeaCamera.getCameraY()));
g.setColor(Color.green.darker());
for(int i=0; i<sizex; i++){
for(int j=0; j<sizey; j++){
if(ilands[i][j]!=null){
ilands[i][j].paintAtSea(g , (int)((i-SeaCamera.getCameraX()%1)*scalex) , (int)((j-SeaCamera.getCameraY()%1)*scaley));
g.fillOval((int)((i+1-SeaCamera.getCameraX()%1)*scalex) , (int)((j+1-SeaCamera.getCameraY()%1)*scaley),4,4);
}
}
}
g.setColor(Color.black);
g.fillRect(395+tmpx,395+tmpy, 10 , 10);
g.drawLine(400+tmpx, 400+tmpy,
400+tmpx+(int)(10*Math.cos(seaplayer.returnTheta())), 400+tmpy+(int)(10*Math.sin(seaplayer.returnTheta())));
}
//a lot of quirky code in this class
public boolean update(){
Isle i=updateLanding();
if(i!=null){
if(i.updateLand()){
System.out.println("##############");
leaveIsland();
}
}else
refresh();
return i!=null;//returns true if onland
}
public void leaveIsland(){
seaplayer.x++;
SeaCamera.initializeCamera(seaplayer.getX(), seaplayer.getY());
refresh();//so that the movement takes effect
//potential teardown, not sure if needed
}
public void refresh(){
seaplayer.resetDirectionPosition(KeyBoard.returnSpeed() , sizex , sizey);
seaplayer.resetAngle(KeyBoard.returnTheta());
SeaCamera.update(seaplayer.getX(), seaplayer.getY());
int playerx=(int)SeaCamera.getCameraX();
int playery=(int)SeaCamera.getCameraY();
if(playerx>old_player_x){
old_player_x=playerx;
for(int i=0; i<sizex-1; i++){
ilands[i]=ilands[i+1];
}
ilands[sizex-1]=new Isle[sizey];
for(int j=0; j<sizey; j++){
refreshSquare(sizex-1 , j);
}
}
if(playerx<old_player_x){
old_player_x=playerx;
for(int i=sizex-1; i>0; i--){
ilands[i]=ilands[i-1];
}
ilands[0]=new Isle[sizey];
for(int j=0; j<sizey; j++){
refreshSquare(0 , j);
}
}
if(playery>old_player_y){
old_player_y=playery;
for(int j=0; j<sizey-1; j++){
for(int i=0; i<sizex; i++){
ilands[i][j]=ilands[i][j+1];
}
}
for(int i=0; i<sizex; i++){
refreshSquare(i , sizey-1);
}
}
if(playery<old_player_y){
old_player_y=playery;
for(int j=sizey-1; j>0; j--){
for(int i=0; i<sizey; i++){
ilands[i][j]=ilands[i][j-1];
}
}
for(int i=0; i<sizex; i++){
refreshSquare(i , 0);
}
}
}
public void refreshAll(){
for(int i=0; i<sizex; i++){
for(int j=0; j<sizey; j++){
refreshSquare(i,j);
}
}
}
public void refreshSquare(int x , int y){
if(randomNumber(x+old_player_x, y+old_player_y)%350==0){
ilands[x][y]=new Isle(x+old_player_x, y+old_player_y, 320);
new Thread(ilands[x][y]).start();
}else if(x+old_player_x==(sizex/2)-1 && y+old_player_y==(sizey/2)-1){
ilands[x][y]=new Isle(x+old_player_x, y+old_player_y, 160);
new Thread(ilands[x][y]).start();
}else{
ilands[x][y]=null;
}
}
public int getSizeX(){
return sizex;
}
public int getSizeY(){
return sizey;
}
//pseudo random number that assures that layouts doesn't change
public static long randomNumber(int x , int y){
String s=y+""+x+""+seed;
return s.hashCode();
}
/**
* this horribly named method returns true while the player is on land
* and initializes an isle if player comes ashore
*/
public Isle updateLanding(){
int tmpx=sizey/2-1+(int)(seaplayer.getX()-SeaCamera.getCameraX());
int tmpy=sizey/2-1+(int)(seaplayer.getY()-SeaCamera.getCameraY());
if(ilands[tmpx][tmpy]!=null)
ilands[tmpx][tmpy].initializeLand();
return ilands[tmpx][tmpy];
}
}