Projeto

Geral

Perfil

Estatísticas
| Revisão:

root / processing / blacklight / Cell.pde @ 15

Histórico | Ver | Anotar | Download (3,71 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
  // Propreties
21
  int lifeBar = 127;
22
  Boolean animating = false;
23
  // 0 -> white 
24
  // 1 -> black 
25
  // 2-> null
26
  int side = 2;
27

    
28
  //Piece is a quad so receives x, y and quad area to deal later
29
  Cell ( int x, int y, int area) {
30
    this.posX = x;
31
    this.posY = y;
32
    this.area = area;
33
  }
34

    
35

    
36
  /**************************************************
37
   *
38
   * Upon creation it recieves the last lifeBar
39
   * and state from cell in same position
40
   *
41
   ***************************************************/
42
  void setup ( int lifeBar, Boolean animating ) {
43
    this.lifeBar = lifeBar;
44
    this.animating = animating;
45
    this.side = side;
46

    
47
    // Don't let lifebar escalate
48
    if (this.lifeBar >= 255) this.lifeBar = 255; 
49
    if (this.lifeBar <= 0) this.lifeBar = 0; 
50

    
51
    stroke (159, 182, 205);
52
    strokeWeight (2);
53

    
54
    // paints cell according to side
55
    switch(this.side) {
56
    case 2: 
57
      fill(127); 
58
      break;
59
    case 0: 
60
      fill(255); 
61
      break;
62
    case 1: 
63
      fill(0); 
64
      break;
65
    }
66
  };
67

    
68

    
69

    
70
  void draw() {
71

    
72
    // if is not between animation draw cell and circle color
73
    if ( this.animating == false ) {
74
      rect ( posX, posY, area, area );
75
      fill(lifeBar) ;
76
      noStroke();
77
      // arc(this.posX + 40, this.posY + 40, 30, 30, 0, 360);
78
      ellipse(this.posX + 40, this.posY + 40, 30, 30);
79
    }
80

    
81
    // if white conquers plays animate white
82
    if (this.isWhite () && (this.side != 0)) {
83
      this.lifeBar = 255;
84
      animateCell(255, 0);
85
    }
86

    
87
    // if black conquers plays animate black
88
    if (this.isBlack () && (this.side != 1)) {
89
      this.lifeBar = 0;
90
      animateCell(0, 1);
91
    }
92
  }
93
  boolean isWhite ()
94
  {
95
    return (this.lifeBar >= 255);
96
  }
97

    
98
  boolean isBlack ()
99
  {
100
    return (this.lifeBar <= 0);
101
  }
102

    
103

    
104
  /**************************************************
105
   *
106
   * Updates cell conquer power
107
   *
108
   ***************************************************/
109
  void update (int player) {
110
    if ( this.animating == false ) {
111
      switch(player) {
112
      case 0: 
113
        updateWhite(); 
114
        break; 
115
      case 1: 
116
        updateBlack(); 
117
        break;
118
      }
119
    }
120
  };
121

    
122
  void updateWhite () {
123
    this.lifeBar += 5;
124
    //    println(lifeBar);
125
  };
126

    
127
  void updateBlack() {
128
    //    println("updateBlack");
129
    this.lifeBar -= 5;
130
    //    println(lifeBar);
131
  }
132

    
133
  /**************************************************
134
   *
135
   * Animates current cell
136
   *
137
   ***************************************************/
138
  void animateCell( int clr, int side ) {
139
    this.animating = true;
140
    if (  animX <= this.area) {
141
      
142
      //this is a mess got to solve better
143
      if ( animX > area /2 ) {
144
        fill(clr);        
145
        //print("animX " + animX);
146
      } else if ( this.side != 2 ) {
147
        switch(this.side) {
148
        case 0: 
149
          fill(255); 
150
          break;
151
        case 1: 
152
          fill(0); 
153
          break;
154
        }
155
      } else {
156
        fill(127);
157
      }
158
      
159
      rect ( posX + animX, posY, area - animX * 2, area);
160
      
161
    } else {
162
      this.animating = false;
163
      this.side = side;
164
      animX = 0;
165

    
166
      // draw last frame animated to prevent blinking
167
      if ( this.side == 0 ) 
168
        fill(255);
169
      else if ( this.side == 1 )
170
        fill(0);
171

    
172
      rect ( posX + animX, posY, area - animX * 2, area);
173
    }
174
    animX +=3*0.7;
175
    animY += 3;
176
  };
177
  
178
}