Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 157 lines (123 sloc) 3.995 kb
47ed482 @boxysean second commit
authored
1 class Player {
2 int id;
3 String name;
4 int colour;
5 int hp = HP_MAX;
6 LinkedList bullets = new LinkedList();
7 boolean sideLeft;
40d10ea @boxysean Added data and libraries
authored
8 float y = height/2;
9 int nextShootTime;
188892d @boxysean added maxlink
authored
10
47ed482 @boxysean second commit
authored
11 Player(int id, String name, int colour, boolean sideLeft) {
12 this.id = id;
13 this.name = name;
14 this.colour = colour;
15 this.sideLeft = sideLeft;
16 }
188892d @boxysean added maxlink
authored
17
47ed482 @boxysean second commit
authored
18 void drawPaddle() {
19 noStroke();
20 fill(colour);
188892d @boxysean added maxlink
authored
21
47ed482 @boxysean second commit
authored
22 if (sideLeft) {
233d346 @boxysean next commit
authored
23 image(playerImage[id], HP_BAR_WIDTH + BAR_BUFFER, y - (PADDLE_HEIGHT/2));
24 // rect(HP_BAR_WIDTH + BAR_BUFFER, y - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT);
188892d @boxysean added maxlink
authored
25 }
26 else {
233d346 @boxysean next commit
authored
27 image(playerImage[id], width - HP_BAR_WIDTH - PADDLE_WIDTH - BAR_BUFFER, y - (PADDLE_HEIGHT/2));
28 // rect(width - HP_BAR_WIDTH - PADDLE_WIDTH - BAR_BUFFER, y - (PADDLE_HEIGHT/2), PADDLE_WIDTH, PADDLE_HEIGHT);
47ed482 @boxysean second commit
authored
29 }
30 }
188892d @boxysean added maxlink
authored
31
47ed482 @boxysean second commit
authored
32 void drawHP() {
33 noStroke();
34 fill(#00FF00);
35
233d346 @boxysean next commit
authored
36 float y = map(hp, 0, HP_MAX, glassHP[id].height, 0);
188892d @boxysean added maxlink
authored
37
47ed482 @boxysean second commit
authored
38 if (sideLeft) {
233d346 @boxysean next commit
authored
39 image(barHP, -1, y+10, barHP.width, height-bottomHP[id].height - 10-y);
40 image(bottomHP[id], -1, height-bottomHP[id].height - 10);
41 image(glassHP[id], 0, 0);
42
43 // rect(0, y, HP_BAR_WIDTH, height);
188892d @boxysean added maxlink
authored
44 }
45 else {
233d346 @boxysean next commit
authored
46 // image(barHP, 0, 0);
47 // image(barHP, width-bottomHP[id].width+1, 50);
48 // image(barHP, width-bottomHP[id].width+1, height-bottomHP[id].height - y, barHP.width, y);
49
50 image(barHP, width-bottomHP[id].width+1, y+10, barHP.width, height-bottomHP[id].height - 10-y);
51
52 image(bottomHP[id], width-bottomHP[id].width+1, height-bottomHP[id].height - 10);
53 image(glassHP[id], width-glassHP[id].width, 0);
54 // rect(width - HP_BAR_WIDTH, y, width, height);
47ed482 @boxysean second commit
authored
55 }
56 }
188892d @boxysean added maxlink
authored
57
47ed482 @boxysean second commit
authored
58 void shootBullet() {
59 int x = 0;
eeddd9b @boxysean latest
authored
60 int speed = game.BULLET_SPEED;
188892d @boxysean added maxlink
authored
61
47ed482 @boxysean second commit
authored
62 if (sideLeft) {
63 x = HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH;
188892d @boxysean added maxlink
authored
64 }
65 else {
47ed482 @boxysean second commit
authored
66 x = width - (HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH);
67 speed *= -1;
68 }
188892d @boxysean added maxlink
authored
69
47ed482 @boxysean second commit
authored
70 Bullet bullet = new Bullet(x, y, speed, id);
188892d @boxysean added maxlink
authored
71
47ed482 @boxysean second commit
authored
72 bullets.add(bullet);
73 }
188892d @boxysean added maxlink
authored
74
47ed482 @boxysean second commit
authored
75 void up() {
76 y = max(y-PADDLE_SPEED, 0);
77 }
188892d @boxysean added maxlink
authored
78
47ed482 @boxysean second commit
authored
79 void down() {
80 y = min(y+PADDLE_SPEED, height);
81 }
188892d @boxysean added maxlink
authored
82
47ed482 @boxysean second commit
authored
83 void moveTo(float y) {
84 // update y
85 this.y = y;
b6df167 @boxysean added collision
authored
86
87 // collision detect bullets, destroy if collided
88 ListIterator ii = game.players[1-id].bullets.listIterator();
89
90 ArrayList destroy = new ArrayList();
91
92 int zz = 0;
93
94 while (ii.hasNext()) {
95 Bullet bullet = (Bullet) ii.next();
96
97 float bx[] = new float[2];
98 float by[] = new float[2];
99 float px[] = new float[2];
100 float py[] = new float[2];
101
233d346 @boxysean next commit
authored
102 bx[0] = bullet.x - (BULLET_WIDTH / 2.0f);
103 by[0] = bullet.y - (BULLET_HEIGHT / 2.0f);
104 bx[1] = bullet.x + (BULLET_WIDTH / 2.0f);
105 by[1] = bullet.y + (BULLET_HEIGHT / 2.0f);
b6df167 @boxysean added collision
authored
106
107 if (sideLeft) {
108 px[0] = HP_BAR_WIDTH + BAR_BUFFER;
109 px[1] = HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH;
110 } else {
111 px[0] = width - (HP_BAR_WIDTH + BAR_BUFFER + PADDLE_WIDTH);
112 px[1] = width - (HP_BAR_WIDTH + BAR_BUFFER);
113 }
114
115 py[0] = this.y - (PADDLE_HEIGHT / 2.0);
116 py[1] = this.y + (PADDLE_HEIGHT / 2.0);
117
118 // bx[0], by[0]
119 // bx[0], by[1]
120 // bx[1], by[0]
121 // bx[1], by[1]
122
123 all: for (int i = 0; i < 2; i++) {
124 for (int j = 0; j < 2; j++) {
125 if ((px[0] <= bx[i] && bx[i] <= px[1]) && (py[0] <= by[j] && by[j] <= py[1])) {
126 // collides, destroy bullet
127 destroy.add(bullet);
128 break all;
129 }
130 }
131 }
132 }
133
134
135 for (int i = 0; i < destroy.size(); i++) {
136 Bullet bullet = (Bullet) destroy.get(i);
137 boolean done = game.players[1-id].bullets.remove(bullet);
138
bdaabd4 @boxysean changed difficulty
authored
139 link.output(7, "block");
b6df167 @boxysean added collision
authored
140 }
47ed482 @boxysean second commit
authored
141 }
188892d @boxysean added maxlink
authored
142
47ed482 @boxysean second commit
authored
143 void hit() {
144 hp--;
188892d @boxysean added maxlink
authored
145
b6df167 @boxysean added collision
authored
146 link.output(1, "hit");
147 link.output(2, id);
148 link.output(3, hp);
233d346 @boxysean next commit
authored
149
150 if (hp <= 0) {
151 game.gameOver();
eeddd9b @boxysean latest
authored
152 game.winnerId = 1-id;
233d346 @boxysean next commit
authored
153 }
47ed482 @boxysean second commit
authored
154 }
155 }
188892d @boxysean added maxlink
authored
156
Something went wrong with that request. Please try again.