Projeto

Geral

Perfil

Estatísticas
| Revisão:

root / processing / blacklight / Level.pde @ 19

Histórico | Ver | Anotar | Download (4,59 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
class Level
10
{
11
  Game game = null;
12
  Battlefield battlefield = null;
13
  Blacklord      blacklord   = null;
14
  Lightlord      lightlord   = null;
15
  Scoreboard scoreboard = null;
16
  int timer = 0;
17
  
18
  Serial         port = null;
19

    
20
  //  Blackmatter [] blackmatter = {};
21
  //  Lightmatter [] lightmatter = null;
22
  String name = "Level Name";
23
  int width = 0;
24
  int height = 0;
25
  int[] balances = {
26
    127, 127, 127, 127, 127, 127
27
  };
28
  int rate = 60;
29

    
30
  void setup ()
31
  {
32
    if (this.game == null)
33
    {
34
      println ("No Game defined for this Level");
35
      return;
36
    }  
37

    
38
    this.timer = 5000;
39
    
40
    this.battlefield = new Battlefield (1024, 768);
41
    this.lightlord = new Lightlord ();
42
    this.blacklord = new Blacklord ();
43
    this.scoreboard = new Scoreboard ();
44
    
45

    
46
    this.battlefield.level = this;
47
    this.lightlord.level = this;
48
    this.blacklord.level = this;  
49
    this.scoreboard.level = this;
50

    
51
    this.battlefield.setup ();
52
    this.lightlord.setup ();
53
    this.blacklord.setup ();
54
    this.scoreboard.setup ();
55
    
56

    
57
    String[] ports =  Serial.list ();
58

    
59
    if (ports.length == 1)
60
      return;
61

    
62
    if (_game_.leds != null)
63
    {
64
      this.port = _game_.leds;
65
    }
66
    
67
    
68
  }
69

    
70
  int balance ()
71
    {
72
    return this.battlefield.whites - this.battlefield.blacks;
73
    }
74

    
75
  void draw ()
76
  {
77
    this.battlefield.draw ();
78
    this.blacklord.draw ();
79
    this.lightlord.draw ();
80

    
81

    
82
    for (int i = 0; i < this.lightlord.minions [0].lighted.size (); i++)
83
    {
84
      int[] coords = (int []) this.lightlord.minions [0].lighted.get (i);
85

    
86
      Cell cell = this.battlefield.getCell (coords[0], coords[1]);
87
      //println ("balancing");
88
      cell.update(0);
89
    }
90

    
91
    for (int j = 0; j < this.blacklord.blackmatter.size (); j++)
92
    {
93
      Blackmatter b = (Blackmatter) this.blacklord.blackmatter.get (j);      
94
      if (b.check ())
95
        continue;
96
        
97
      Cell cell = this.battlefield.getCell (b.x, b.y);
98
      cell.update(1);
99
    }
100

    
101
    this.scoreboard.draw (); 
102

    
103
    //int balance = this.lightlord.balance ();
104

    
105
    // DESCOMENTAR!! LEDS
106
    //this.output (balance);
107
    if ( this.timer <= 0 ) this.outcome ();
108
    this.timer--;
109

    
110

    
111
    //    this.blacklord.attack ();
112

    
113
    //    this.lightlord.defend ();
114
  }
115

    
116
  void loadXml (XMLElement node)
117
  {
118
    this.name       = node.getChild ("name").getContent ();
119
    this.width      = int (node.getChild ("width").getContent ());
120
    this.height     = int (node.getChild ("height").getContent ());    
121

    
122
    println ("Level " + name + ": " + this.width + "x" + this.height);
123
  }
124

    
125
  void output (int value)
126
  {
127
    if (this.port != null)
128
      this.port.write (value);
129

    
130
    //  value = value + increment;
131

    
132
    //  if (value >= 255)
133
    //    increment = -1;
134
    //  if (value <= 0)
135
    //    increment = 1;
136

    
137
    //  println (value);
138
  }
139

    
140
  void outcome ()
141
  {
142
    int score = this.battlefield.blacks - this.battlefield.whites;
143
    
144
    if ( score > 0 ) {
145
       _game_.blackWins ();
146
    } else if ( score < 0 ) {
147
      _game_.lightWins (); 
148
    } else {
149
     this.timer += 1000; 
150
    }
151
      
152
      
153
    
154
/*    this.rate--;
155

    
156
    if (this.rate == 0)
157
    {
158

    
159
      for (int i = 1; i < this.balances.length; i++)
160
        this.balances [this.balances.length - i] = this.balances [this.balances.length - i - 1];
161

    
162
      this.balances [0] = balance;
163
      if (balance == 0)
164
        _game_.playBlackPower ();
165
      else if (balance > 155)
166
        _game_.playLightPower ();
167

    
168
      this.rate = 60;
169
    }
170

    
171
    int total = 0;
172
    int blackpower = 0;
173
    int lightpower = 0;
174
    boolean consecutiveBlack = true;
175
    boolean consecutiveLight = true;
176
    for (int i = 0; i < this.balances.length; i++)
177
    {
178
      total += this.balances [i];
179

    
180
      if ((this.balances [i] == 0) && consecutiveBlack)
181
      {
182
        blackpower++;
183
        consecutiveLight = false;
184
      }
185
      else if ((this.balances [i] > 155) && consecutiveLight)
186
      {
187
        lightpower++;
188
        consecutiveBlack = false;
189
      }
190
      else
191
      {
192
        consecutiveLight = false;
193
        consecutiveBlack = false;
194
      }
195
    }
196

    
197
    if (blackpower > 0)
198
    {
199
      _game_.score (blackpower, 0);
200
    }
201
    else if (lightpower > 0)
202
    {
203
      _game_.score (lightpower, 1);
204
    }
205

    
206

    
207
    total = total / this.balances.length;
208

    
209
    if (blackpower == 6)
210
      _game_.blackWins ();
211
    else if (lightpower == 6)
212
      _game_.lightWins ();*/
213
  }
214
}
215