Projeto

Geral

Perfil

Estatísticas
| Revisão:

root / processing / blacklight / Cell.pde @ 19

Histórico | Ver | Anotar | Download (4,32 KB)

1
/*
2
BLACKLIGHT v1.0
3
 Project made for Mestrado de Comunicação Multimédia, Ramo Multimédia Interactivo at Departamento de Comunicação e Arte at Universidade de Aveiro.
4
 
5
 Author: Nuno Simaria - nsimaria@ua.pt - #23652
6
 Other workgroup members: Ana Filipa Lacerda, Daniela Rei, Renato Costa, Julien Cuenin
7
 */
8

    
9

    
10
class Cell {
11

    
12
  // Form
13
  int posX = 0;
14
  int posY = 0;
15
  int area = 0;
16

    
17
  int animX = 0;
18
  int animY = 0;
19
 
20
 int fillBlack = 0;
21
 int fillWhite = 255;
22
 int fillNeutral = 127;
23
 int opacity = 50;
24
 
25
  // Propreties
26
  int lifeBar = 127;
27
  Boolean animating = false;
28
  // 0 -> white 
29
  // 1 -> black 
30
  // 2-> null
31
  int side = 2;
32

    
33
  //Piece is a quad so receives x, y and quad area to deal later
34
  Cell ( int x, int y, int area) {
35
    this.posX = x;
36
    this.posY = y;
37
    this.area = area;
38
  }
39

    
40

    
41
  /**************************************************
42
   *
43
   * Upon creation it recieves the last lifeBar
44
   * and state from cell in same position
45
   *
46
   ***************************************************/
47
  void setup ( int lifeBar, Boolean animating, int cellArea ) {
48
    this.lifeBar = lifeBar;
49
    this.animating = animating;
50
    this.side = side;
51

    
52
    // Don't let lifebar escalate
53
    if (this.lifeBar >= 255) this.lifeBar = 255; 
54
    if (this.lifeBar <= 0) this.lifeBar = 0; 
55

    
56
    stroke (150, this.opacity);
57
    strokeWeight (2);
58

    
59
    // paints cell according to side
60
    switch(this.side) {
61
    case 2: 
62
      fill( this.fillNeutral, this.opacity ); 
63
      break;
64
    case 0: 
65
      fill( this.fillWhite, this.opacity ); 
66
      break;
67
    case 1: 
68
      fill( this.fillBlack, this.opacity ); 
69
      break;
70
    }
71
  };
72

    
73

    
74

    
75
  void draw() {
76

    
77
    // if is not between animation draw cell and circle color
78
    if ( this.animating == false ) {
79
      if (this.side == 0)
80
        fill (this.fillWhite, this.opacity + 70);
81
      else
82
        fill(lifeBar, this.opacity + 20 ) ;
83
      
84
      rect ( posX, posY, area, area );
85
      
86
    //  noStroke();
87
      // arc(this.posX + 40, this.posY + 40, 30, 30, 0, 360);
88
      //ellipse(this.posX + (this.area / 2 ), this.posY + ( this.area / 2), 30, 30);
89
    }
90

    
91
    // if white conquers plays animate white
92
    if (this.isWhite () && (this.side != 0)) {
93
      this.lifeBar = 255;
94
      animateCell( this.fillWhite, 0);
95
    }
96

    
97
    // if black conquers plays animate black
98
    if (this.isBlack () && (this.side != 1)) {
99
      this.lifeBar = 0;
100
      animateCell( this.fillBlack , 1);
101
    }
102
  }
103
  boolean isWhite ()
104
  {
105
    return (this.lifeBar >= 255);
106
  }
107

    
108
  boolean isBlack ()
109
  {
110
    return (this.lifeBar <= 0);
111
  }
112

    
113

    
114
  /**************************************************
115
   *
116
   * Updates cell conquer power
117
   *
118
   ***************************************************/
119
  void update (int player) {
120
    if ( this.animating == false ) {
121
      switch(player) {
122
      case 0: 
123
        updateWhite(); 
124
        break; 
125
      case 1: 
126
        updateBlack(); 
127
        break;
128
      }
129
    }
130
  };
131

    
132
  void updateWhite () {
133
    this.lifeBar += 5;
134
    //    println(lifeBar);
135
  };
136

    
137
  void updateBlack() {
138
    //    println("updateBlack");
139
    this.lifeBar -= 5;
140
    //    println(lifeBar);
141
  }
142

    
143
  /**************************************************
144
   *
145
   * Animates current cell
146
   *
147
   ***************************************************/
148
  void animateCell( int clr, int side ) {
149
    this.animating = true;
150
    if (  animX <= this.area) {
151
      fill(clr, this.opacity );  
152
      
153
      //this is a mess got to solve better
154
     /* if ( animX > area /2 ) {
155
        fill(clr, this.opacity );        
156
        //print("animX " + animX);
157
      } else if ( this.side != 2 ) {
158
        switch(this.side) {
159
        case 0: 
160
        println("fillWhite");
161
          fill( this.fillWhite, this.opacity );
162
          break;
163
        case 1: 
164
          fill( this.fillBlack, this.opacity ); 
165
          break;
166
        }
167
      } else {
168
        fill( this.fillNeutral, this.opacity );
169
      }*/
170
      
171
      noStroke();
172
      rect ( posX + animX, posY, area - animX * 2, area);
173
      
174
    } else {
175
      this.animating = false;
176
      this.side = side;
177
      animX = 0;
178

    
179
      // draw last frame animated to prevent blinking
180
      if ( this.side == 0 ) 
181
        fill( this.fillWhite, this.opacity + 20 );
182
      else if ( this.side == 1 )
183
        fill( this.fillBlack, this.opacity + 20);
184

    
185
      rect ( posX + animX, posY, area - animX * 2, area);
186
    }
187
    animX +=5*0.9;
188
    animY += 5;
189
  };
190
  
191
}