Projeto

Geral

Perfil

Revisão 11

Adicionado por Dinis Adriano Domingosmais de 9 anos atrás

Added a new conquer style grid system

Ver diferenças:

grid/grid.pde
1
/**************************************************
2
 *
3
 * Size of each cell calculated with a 
4
 * division of stage's width with a multiple of 8
5
 *
6
 * 1024 / 16 = 64
7
 * 768 / 16 = 43
8
 * 1280 / 16 = 80
9
 *
10
 **************************************************/
11
int videoScale = 80;
12
int width = 1280;
13
int height = 720;
14

  
15
int cols, rows;
16
Cell[][] cells = {
17
};
18

  
19
// Players x,y
20
int mX ;
21
int mY;
22

  
23
PFont _regular_;
24

  
25
BlackMinion blackminion;
26

  
27
void setup() {
28
  _regular_ = loadFont ("Code-Bold-48.vlw");
29
  size(width, height);  
30

  
31
  cols = width/videoScale;
32
  rows = height/videoScale;
33

  
34
  //noLoop();
35
  drawGrid();
36
  
37
  // Setup blackminion
38
  blackminion = new BlackMinion();
39
  blackminion.setup();
40
}
41

  
42
void draw() {
43
  background(159, 182, 205);
44

  
45
  for (int i = 0; i < this.cells.length; i++) {
46
    //rows
47
    for (int j = 0; j < this.cells[i].length; j++) {
48
      this.cells [i][j].setup ( this.cells[i][j].lifeBar, this.cells[i][j].animating );
49
      this.cells [i][j].draw();
50
    }
51
  }
52
  
53
  moveBlack();
54
  this.blackminion.draw();
55
}
56

  
57
/**************************************************
58
 *
59
 * Draw grid once
60
 *
61
 **************************************************/
62
void drawGrid () {
63
  for (int i = 0; i < cols; i++) {
64
    Cell [] column = new Cell [rows];
65
    for (int j = 0; j < rows; j++) {
66

  
67
      // Scaling up to draw a rectangle at (x,y)
68
      int x = i*videoScale;
69
      int y = j*videoScale;
70

  
71
      Cell cell = new Cell (x, y, videoScale);
72
      column [j] = cell;
73
      //cell.setup();
74
    }
75
    this.cells = (Cell[][]) append (this.cells, column);
76
  }
77
}
78

  
79
/**************************************************
80
 *
81
 * Returns cell affected by player
82
 *
83
 **************************************************/
84
Cell getCell (int x, int y)
85
{
86
  int cellX = floor (cols * x) / this.width;
87
  int cellY = floor (rows * y) / this.height;
88

  
89
  //println ("cellX " + cellX + ", cellY " + cellY);
90
  return this.cells [cellX][cellY];
91
}
92

  
93

  
94
/**************************************************
95
 *
96
 * Simulated White Player
97
 *
98
 **************************************************/
99
void mouseMoved() {
100
  mX = mouseX;
101
  mY = mouseY;
102
  Cell cell = this.getCell ( mX, mY);
103
  cell.update(0);
104
}
105

  
106

  
107
void moveBlack() {
108
  // Bounce
109
  if ( blackminion.posX <= 0 )  keyCode = RIGHT;
110
  if ( blackminion.posX >= 1270 )  keyCode = LEFT;
111
  if ( blackminion.posY <= 0 )  keyCode = DOWN;
112
  if ( blackminion.posY >= 710 )  keyCode = UP;
113

  
114

  
115
  switch(keyCode) {
116
  case UP: 
117
    blackminion.posY -= 5; 
118
    break;
119
  case DOWN: 
120
    blackminion.posY += 5; 
121
    break;
122
  case LEFT: 
123
    blackminion.posX -= 5; 
124
    break;
125
  case RIGHT: 
126
    blackminion.posX += 5; 
127
    break;
128
  }
129

  
130
  Cell cell = this.getCell ( blackminion.posX, blackminion.posY);
131
  cell.update(1);
132
}
133
/**************************************************
134
 *
135
 * Simulated Black Player
136
 *
137
 **************************************************/
138
class BlackMinion {
139
  int posX = 0;
140
  int posY = 0;
141

  
142
  void setup() {
143
  };
144

  
145
  void draw() {
146
    display();
147
  }
148

  
149

  
150
  void display() {
151
    println("posY" + posY);
152
    fill(0);
153
    ellipse (posX, posY, 20, 20);
154
  }
155
}; 
156

  
grid/Cell.pde
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

  

Também disponível em: Unified diff