root / grid / Cell.pde @ 11
Histórico | Ver | Anotar | Download (3,16 KB)
| 1 |
class Cell {
|
|---|---|
| 2 |
|
| 3 |
// Form |
| 4 |
int posX = 0; |
| 5 |
int posY = 0; |
| 6 |
int area = 0; |
| 7 |
|
| 8 |
int animX = 0; |
| 9 |
|
| 10 |
// Propreties |
| 11 |
int lifeBar = 127; |
| 12 |
Boolean animating = false; |
| 13 |
// 0 -> white |
| 14 |
// 1 -> black |
| 15 |
// 2-> null |
| 16 |
int side = 2; |
| 17 |
|
| 18 |
//Piece is a quad so receives x, y and quad area to deal later |
| 19 |
Cell ( int x, int y, int area) {
|
| 20 |
this.posX = x; |
| 21 |
this.posY = y; |
| 22 |
this.area = area; |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
/************************************************** |
| 27 |
* |
| 28 |
* Upon creation it recieves the last lifeBar |
| 29 |
* and state from cell in same position |
| 30 |
* |
| 31 |
***************************************************/ |
| 32 |
void setup ( int lifeBar, Boolean animating ) {
|
| 33 |
this.lifeBar = lifeBar; |
| 34 |
this.animating = animating; |
| 35 |
|
| 36 |
// Don't let lifebar escalate |
| 37 |
if (this.lifeBar >= 255) this.lifeBar = 255; |
| 38 |
if (this.lifeBar <= 0) this.lifeBar = 0; |
| 39 |
|
| 40 |
stroke (159, 182, 205); |
| 41 |
strokeWeight (2); |
| 42 |
|
| 43 |
// paints cell according to side |
| 44 |
switch(side) {
|
| 45 |
case 2: |
| 46 |
fill(127); |
| 47 |
break; |
| 48 |
case 0: |
| 49 |
fill(255); |
| 50 |
break; |
| 51 |
case 1: |
| 52 |
fill(0); |
| 53 |
break; |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
void draw() {
|
| 60 |
// debug for cell |
| 61 |
//textFont (_regular_, 48); |
| 62 |
//text ( this.num , posX, posY ); |
| 63 |
|
| 64 |
// if is not between animation draw cell and circle color |
| 65 |
if ( this.animating == false ) {
|
| 66 |
quad( |
| 67 |
this.posX, |
| 68 |
this.posY, |
| 69 |
|
| 70 |
this.posX + area, |
| 71 |
this.posY, |
| 72 |
|
| 73 |
this.posX + area, |
| 74 |
this.posY + area, |
| 75 |
|
| 76 |
this.posX, |
| 77 |
this.posY + area); |
| 78 |
if ( this.side == 1) {
|
| 79 |
fill(255); |
| 80 |
} |
| 81 |
|
| 82 |
fill(lifeBar) ; |
| 83 |
noStroke(); |
| 84 |
arc(this.posX + 40, this.posY + 40, 30, 30, 0, 360); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
// if white conquers plays animate white |
| 89 |
if (this.lifeBar >= 255 && this.side != 0) {
|
| 90 |
this.lifeBar = 255; |
| 91 |
animateCell(255, 0); |
| 92 |
} |
| 93 |
|
| 94 |
// if black conquers plays animate black |
| 95 |
if (this.lifeBar <= 0 && this.side != 1) {
|
| 96 |
this.lifeBar = 0; |
| 97 |
animateCell(0, 1); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
/************************************************** |
| 102 |
* |
| 103 |
* Updates cell conquer power |
| 104 |
* |
| 105 |
***************************************************/ |
| 106 |
void update (int player) {
|
| 107 |
if ( this.animating == false ) {
|
| 108 |
switch(player) {
|
| 109 |
case 0: |
| 110 |
updateWhite(); |
| 111 |
break; |
| 112 |
case 1: |
| 113 |
updateBlack(); |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
}; |
| 118 |
|
| 119 |
void updateWhite () {
|
| 120 |
this.lifeBar += 5; |
| 121 |
println(lifeBar); |
| 122 |
}; |
| 123 |
|
| 124 |
void updateBlack() {
|
| 125 |
println("updateBlack");
|
| 126 |
this.lifeBar -= 5; |
| 127 |
println(lifeBar); |
| 128 |
} |
| 129 |
|
| 130 |
/************************************************** |
| 131 |
* |
| 132 |
* Animates current cell |
| 133 |
* |
| 134 |
***************************************************/ |
| 135 |
void animateCell( int clr, int side ) {
|
| 136 |
this.animating = true; |
| 137 |
if ( animX <= this.area) {
|
| 138 |
if ( animX > area /2 ) {
|
| 139 |
fill(clr); |
| 140 |
print("animX " + animX);
|
| 141 |
} |
| 142 |
else {
|
| 143 |
fill(127); |
| 144 |
} |
| 145 |
quad( |
| 146 |
this.posX+animX, |
| 147 |
this.posY, |
| 148 |
|
| 149 |
(this.posX + area) - animX, |
| 150 |
this.posY, |
| 151 |
|
| 152 |
(this.posX + area) -animX, |
| 153 |
this.posY + area, |
| 154 |
|
| 155 |
this.posX + animX, |
| 156 |
this.posY + area); |
| 157 |
} |
| 158 |
else {
|
| 159 |
this.animating = false; |
| 160 |
this.side = side; |
| 161 |
animX = 0; |
| 162 |
} |
| 163 |
animX +=3*0.7; |
| 164 |
}; |
| 165 |
} |
| 166 |
|