-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloneGuard.pde
239 lines (206 loc) · 4.64 KB
/
CloneGuard.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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
PImage sprite;
PImage spriteMan[];
int spriteHeight;
int spriteWidth;
int drawWidth;
//MOVEMENT
int xpos;
int ypos;
int yvel;
int yacc;
int xsp; //current speed in the x direction.
int XVELOCITY; //the starting walk speed.
int YACCEL;
boolean facingLeft; //ISSUE:
//SHOOTING
Shot shots[];
int shotcount;
int MAXSHOTS;
int shotTimer;
//INPUT
int keys[]; //ints (should be booleans)
//LEVEL
Level level;
//ENEMIES
public Enemy enemies[];
public int enemyNum;
void setup(){
size(600,400);
enemyNum = 0;
level = new Level(0, this);
//ENEMY
enemies = new Enemy[20];
//SPRITE
sprite = loadImage("images/1.png");
spriteMan = new PImage[4];
for(int i = 0; i < 4; i+=2){
spriteMan[i] = loadImage("images/a" + i + ".png");
spriteMan[i+1] = loadImage("images/a" + i + "b.png");
}
spriteHeight = 40;
spriteWidth = 20;
drawWidth = 40;
//xpos = 401;
xsp = 0;
XVELOCITY = 5;
//ypos = 201;
yvel = 0;
YACCEL = 1;
yacc = YACCEL;
facingLeft = true;
//INPUT
keys = new int[4];
for(int i = 0; i < 4; i++){
keys[i] = 0;
}
//LEVEL
level.initPaint();
level.paint();
//Position (based on level)
xpos = level.originx;
ypos = level.originy;
//SHOOTING
MAXSHOTS = 30;
shots = new Shot[MAXSHOTS];
shotcount = 0;
for(int i = 0; i < MAXSHOTS; i++){
shots[i] = new Shot(level);
}
shotTimer = 100;
}
void draw(){
translate(0 -xpos + (width/2), 0 - ypos + (height/2));
if(xsp > 0)
facingLeft = false;
else if (xsp < 0)
facingLeft = true;
background(000);
level.paint();
fill(255);
//rect(xpos,ypos,spriteWidth,spriteHeight);
drawSprite();
//WALKING AND SIDE COLLISION
if((keys[0] > 0 || keys[1] > 0) && !level.wallCollision(xpos + xsp,ypos,spriteWidth,spriteHeight)){
xpos += xsp;
}
//JUMPING AND VERTICAL COLLISIONS
yvel += yacc;
if(!level.groundCollision(xpos,ypos + yvel,spriteWidth,spriteHeight) && !level.ceilingCollision(xpos,ypos + yvel,spriteWidth)){ //if (if we move) there is no floor or ceiling collision
ypos += yvel;
yacc = YACCEL;
}else if(level.ceilingCollision(xpos,ypos + yvel,spriteWidth)){ //ceiling collision
ypos = level.roundUpToBlockTop(ypos) + 1; //align top of sprite with ceiling
yvel = 0;
}else{ //floor collision
ypos = level.roundUpToBlockTop(ypos + yvel + spriteHeight) - spriteHeight; //stand on floor
yacc = 0;
yvel = 0;
}
//SHOOTING
for(int i = 0; i < MAXSHOTS; i++){
shots[i].paint();
}
if (keys[2] > 0){
shotTimer++;
if (shotTimer > 9){
shotTimer = 0;
shoot();
}
}
for(int i = 0; i < enemyNum; i++){
enemies[i].paint();
}
}
void keyPressed(){
char k = (char)key;
switch(k){
case 'j':
if(keys[0] == 0){
xsp -= XVELOCITY;
}
keys[0] = 1;
break;
case 'l':
if(keys[1] == 0){
xsp += XVELOCITY;
}
keys[1] = 1;
break;
case 'x':
if(keys[2] == 0)
keys[2] = 1;
break;
case 'z':
//JUMP!
if(yacc == 0){
yacc = YACCEL;
yvel = -20;
}
keys[3] = 1;
break;
default:
break;
}
}
void keyReleased(){
char k = (char)key;
switch(k){
case 'j':
if(keys[0] > 0)
xsp += XVELOCITY;
keys[0] = 0;
break;
case 'l':
if(keys[1] > 0)
xsp -= XVELOCITY;
keys[1] = 0;
break;
case 'x':
if (keys[2] > 0){
keys[2] = 0;
shotTimer = 10;
}
break;
case 'z':
//STOP ACCELERATING UPWARDS (JUMPING)
keys[3] = 0;
if(yvel <= -5){ //set these to zero to have a completely immediate response
yvel = -5; //a velocity that still gives a little bit of arc
}
break;
default:
break;
}
}
void shoot(){
shots[shotcount].set(xpos,ypos + (int)(spriteHeight/2),true);
if(!facingLeft){
shots[shotcount].shootRight(); //sets direction of shot.
shots[shotcount].x += (int)spriteWidth/2;
}else{
shots[shotcount].shootLeft();
shots[shotcount].x -= (int)spriteWidth/2;
}
shotcount = (shotcount + 1) % MAXSHOTS;
}
void drawSprite(){
if(facingLeft && yacc != 0)
image(spriteMan[2], xpos - spriteWidth/2, ypos,drawWidth,spriteHeight);
if(facingLeft && yacc == 0)
image(spriteMan[0], xpos - spriteWidth/2, ypos,drawWidth,spriteHeight);
if(!facingLeft && yacc != 0)
image(spriteMan[3], xpos - spriteWidth/2,ypos,drawWidth,spriteHeight);
if(!facingLeft && yacc == 0)
image(spriteMan[1], xpos - spriteWidth/2,ypos,drawWidth,spriteHeight);
}
void addEnemy(int x, int y){
if(enemies.length >= enemyNum){
Enemy temp[] = new Enemy[enemyNum + 5];
for(int i = 0; i < enemyNum; i++){
temp[i] = enemies[i];
}
enemies = temp;
}
enemies[enemyNum] = new Enemy(x,y,level);
enemyNum += 1;
}