/
Level.pde
122 lines (110 loc) · 3.37 KB
/
Level.pde
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
public class Level{
String lines[];
char blocks[][];
public int BLOCKSIZE;
int sizes[]; //numbers of blocks in each row.
int blength; //number of blocks horizontal.
public int originx;
public int originy;
CloneGuard game;
public Level(int num, CloneGuard game){
BLOCKSIZE = 60;
blocks = new char[100][20];
load(num);
this.game = game;
}
public void load(int num){
lines = loadStrings("level" + num + ".txt");
for(int i = 0; i < lines.length; i++){
char temp[] = new char[100];
for(int j = 0; j < lines[i].length(); j++){
//NB: X AND Y ARE BACKWARDS...........
blocks[j][i] = lines[i].charAt(j);
}
}
blength = blocks.length;
sizes = new int[blength];
for(int i = 0; i < blength; i++){
sizes[i] = blocks[i].length;
}
}
public void initPaint(){
int blength = blocks.length;
//IMPR: only draw what's on-screen?
for(int i = 0; i < blength; i++){
for(int j = 0; j < sizes[i]; j++){
if(blocks[i][j] == '-'){
fill(255);
}else if(blocks[i][j] == 'x'){
fill(100);
}else if(blocks[i][j] == 'o'){
originx = i*BLOCKSIZE;
originy = j*BLOCKSIZE;
fill(255);
}else if(blocks[i][j] == 'E'){
fill(0,50,50);
game.addEnemy(i*BLOCKSIZE,(j+1)*BLOCKSIZE);
}else
fill(0);
rect(i*BLOCKSIZE,j*BLOCKSIZE,BLOCKSIZE,BLOCKSIZE);
}
}
}
public void paint(){
int blength = blocks.length;
//IMPR: only draw what's on-screen?
for(int i = 0; i < blength; i++){
for(int j = 0; j < sizes[i]; j++){
if(blocks[i][j] == '-'){
fill(255);
}else if(blocks[i][j] == 'x'){
fill(100);
}else if(blocks[i][j] == 'o'){
originx = i*BLOCKSIZE;
originy = j*BLOCKSIZE;
fill(255);
}else if(blocks[i][j] == 'E'){
fill(0,50,50);
}else
fill(0);
rect(i*BLOCKSIZE,j*BLOCKSIZE,BLOCKSIZE,BLOCKSIZE);
}
}
}
//to see if the x,y point is within a block.
public boolean isSolidBlock(double x, double y){
if(x < 0 || x > levelWidth() - 1)
return true;
if(y < 0)
return false;
//NB: (this will have to be changed if ever the character is falling REALLY fast)
return blocks[floor((float)x/BLOCKSIZE)][floor((float)y/BLOCKSIZE)] == 'x';
}
public int roundUpToBlockTop(int y){
return floor(y/BLOCKSIZE)*BLOCKSIZE;
}
//direction should be 1 or -1
public int roundToBlockSide(int x, int direction){
return floor(x/BLOCKSIZE)*BLOCKSIZE + BLOCKSIZE/2 - direction*BLOCKSIZE/2;
}
public int levelWidth(){
return blength*BLOCKSIZE;
}
boolean wallCollision(int x, int y, int width, int height){
//checks to the side of all 4 corners
return level.isSolidBlock(x, y) ||
level.isSolidBlock(x + width, y) ||
level.isSolidBlock(x, y + height - 1) ||
level.isSolidBlock(x + width, y + height - 1);
}
boolean ceilingCollision(double x, double y, int width){
//checks left and right points of sprite
return level.isSolidBlock(x,y) ||
level.isSolidBlock(x + width,y);
}
boolean groundCollision(double x, double y, int width, int height){
//checks left and right points of sprite
return level.isSolidBlock(x, y + height) ||
level.isSolidBlock(x + width, y + height);
}
}