-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBattleSystem.pde
338 lines (290 loc) · 8.31 KB
/
BattleSystem.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
class BattleSystem implements BattleSystemConstants {
// array of players (2)
BasicPlayer[] players;
final int PLAYER_COUNT = 2;
// references of players
BasicPlayer selectedPlayer;
BasicPlayer attackingPlayer;
BasicPlayer defendingPlayer;
// what state the game is in
int gameState;
// the text that will appear on screen, use it to communicate things
String textConsole = " ";
// x-osc stuffs
boolean oscPaired = false;
final boolean USE_XOSC = true;
float skillFloat;
int timer = 0;
public BattleSystem() {
initPlayers();
gameState = SETUP_STATE;//INTRO_STATE;// BATTLE_STATE;
}
private void initPlayers() {
players = new BasicPlayer[PLAYER_COUNT];
players[0] = new DefensivePlayer();
players[1] = new MajesticPlayer();
players[0].setOpponent(players[1]);
players[1].setOpponent(players[0]);
}
public void update() {
switch (gameState) {
case INTRO_STATE:
intro();
break;
case SETUP_STATE:
setup();
break;
case SKILL_STATE:
skillTest();
break;
case BATTLE_STATE:
battle();
break;
case GAME_OVER:
gameOver();
break;
}
}
/////////////////////////////////////////////////////////////
////// Game states
/////////////////////////////////////////////////////////////
private void intro() {
textConsole = "Press ENTER to start skilltest";
if (key == ENTER) startSkillTest();
}
private void setup() {
if (!pairXOSC() && USE_XOSC) {
pairXOSC();
} else {
gameState = INTRO_STATE;
};
}
private boolean pairXOSC() {
players[0].setIPaddr(XOSC_P1_IP);
players[1].setIPaddr(XOSC_P2_IP);
if (!players[0].isPaired()) {
selectedPlayer = players[0];
textConsole = "Pair XOSC for player 0";
return false;
} else if (!players[1].isPaired()) {
selectedPlayer = players[1];
textConsole = "Pair XOSC for player 1";
return false;
} else {
textConsole = "Players paired!";
oscPaired = true;
return true;
}
}
private void startSkillTest() {
skillFloat = 1.0;
gameState = SKILL_STATE;
players[0].setSkill(0.0);
players[1].setSkill(0.0);
}
private void skillTest() {
// set attackingPlayer
skillFloat -= 0.01;
if (skillFloat <= 0.0) startBattle();
}
private void startBattle() {
delay(1000);
if (players[0].getSkill() < players[1].getSkill()) {
attackingPlayer = players[1];
defendingPlayer = players[0];
} else {
attackingPlayer = players[0];
defendingPlayer = players[1];
}
players[0].selectWeapon(-1);
players[1].selectWeapon(-1);
gameState = BATTLE_STATE;
timer = 30*8;
textConsole = attackingPlayer.getName() + " is attacking " + defendingPlayer.getName() + "\nPick your Weapons!";
}
private void battle() {
if (timer > 0) {
timer--;
return;
}
// if (players[0].getSelectedWeapon() == null || players[1].getSelectedWeapon() == null) {
// return;
// }
/////
//
// Damage calculation based on attack power defense power and special powers active
//
/////
int p1WpCount = players[0].weapons.size();
int p2WpCount = players[1].weapons.size();
ArrayList<Integer> checkedwp1 = new ArrayList<Integer>();
ArrayList<Integer> checkedwp2 = new ArrayList<Integer>();
boolean roundEndsGame = false;
if(players[0].getSelectedWeapon() == null || players[1].getSelectedWeapon() == null){
if (players[0].getSelectedWeapon() == null) {
while(players[0].getSelectedWeapon() == null){
int rndWpId = int(random(p1WpCount));
if(! checkedwp1.contains(rndWpId) && players[0].weapons.get(rndWpId).isUseable()){
players[0].selectWeapon(rndWpId+1);
}else{
checkedwp1.add(rndWpId);
}
}
} else if (players[1].getSelectedWeapon() == null){
while(players[1].getSelectedWeapon() == null){
int rndWpId = int(random(p2WpCount));
if(! checkedwp2.contains(rndWpId) && players[1].weapons.get(rndWpId).isUseable()){
players[1].selectWeapon(rndWpId+1);
}else{
checkedwp2.add(rndWpId);
}
}
}
}
//Calculate damage based on super powers
//Basic Damage Calculation
int dammage = attackingPlayer.getAttackStrength();
int defense = defendingPlayer.getDefenseStrength();
players[0].useWeapon();
players[1].useWeapon();
boolean switchPlayers = false;
if(attackingPlayer.usingSp){
//Apply sP on priority
switch (attackingPlayer.name) {
case "Fierce" :
dammage += 4;
println("-->Fierce Attack Power!");
break;
case "Ludic" :
switchPlayers = true;
println("-->Mirror Attack Power!");
break;
default:
break;
}
attackingPlayer.usingSp = false;
}else if (defendingPlayer.usingSp){
//Apply sP on priority
switch (defendingPlayer.name) {
case "Fierce" :
println("-->Fierce Attack Missed!");
break;
case "Ludic" :
switchPlayers = true;
println("-->Mirror Attack Power!");
break;
default:
break;
}
defendingPlayer.usingSp = false;
}
if(switchPlayers) attackingPlayer.receiveAttack(dammage - defense);
else defendingPlayer.receiveAttack(dammage - defense);
println("Dammage = " + dammage + " Defense = " + defense);
// unselect weapons
players[0].selectWeapon(-1);
players[1].selectWeapon(-1);
//Check if there are still more weapons to use
for (BasicPlayer player : players) {
if(!player.hasWeapons()) roundEndsGame = true;
}
if(!attackingPlayer.isAlive() || !defendingPlayer.isAlive() || roundEndsGame){
// Game Over
gameState = GAME_OVER;
}else{
// Go to next state
gameState = INTRO_STATE;
}
}
public void gameOver(){
textConsole = "Game Over\n";
if(players[0].hitPoints > players[1].hitPoints){
textConsole += players[0].name + " defeats " + players[1].name + "!\n";
}else if(players[0].hitPoints == players[1].hitPoints){
textConsole += "It's a Draw!\n";
}else{
textConsole += players[1].name + " defeats " + players[0].name + "!\n";
}
textConsole += "Press ENTER to restart.\n";
if (key == ENTER) {
for (BasicPlayer player : players) {
player.init();
}
startSkillTest();
}
}
/////////////////////////////////////////////////////////////
////// Player input
/////////////////////////////////////////////////////////////
/**
* Simple input system
* @param player number
* @param button number
*/
public void playerInput(int _p, int _i) {
BasicPlayer _player = getPlayer(_p);
if (_player == null) {
println("Error: Player not found!");
return;
}
else if (gameState == BATTLE_STATE) {
if (_i == 5) {
_player.specialPower();
}
else {
_player.selectWeapon(_i);
}
} else if (gameState == SKILL_STATE) {
_player.setSkill(_player.getSkill()+0.01);
}else if(gameState == INTRO_STATE){
startSkillTest();
}
println("Got input " + _i + " from Player " + _player.name);
}
public void oscInput(OscMessage _mess) {
//println("OSC message " + _mess.addrPattern() + " From " + _mess.netAddress().address());
// if in paring mode
//println(_mess.addrPattern() == "/ping");
if (!oscPaired && _mess.checkAddrPattern("/ping")) {
selectedPlayer.setIPaddr(_mess.netAddress().address());
}else if(int(_mess.get(0).floatValue())==1){
if(_mess.checkAddrPattern("/Player1/1")){
battleSystem.playerInput(0,1);
}else if(_mess.checkAddrPattern("/Player1/2")){
battleSystem.playerInput(0,2);
}else if(_mess.checkAddrPattern("/Player1/3")){
battleSystem.playerInput(0,3);
}else if(_mess.checkAddrPattern("/Player1/4")){
battleSystem.playerInput(0,4);
}else if(_mess.checkAddrPattern("/Player1/5")){
battleSystem.playerInput(0,5);
}else if(_mess.checkAddrPattern("/Player2/1")){
battleSystem.playerInput(1,1);
}else if(_mess.checkAddrPattern("/Player2/2")){
battleSystem.playerInput(1,2);
}else if(_mess.checkAddrPattern("/Player2/3")){
battleSystem.playerInput(1,3);
}else if(_mess.checkAddrPattern("/Player2/4")){
battleSystem.playerInput(1,4);
}else if(_mess.checkAddrPattern("/Player2/5")){
battleSystem.playerInput(1,5);
}
}
}
/////////////////////////////////////////////////////////////
////// Accessors
/////////////////////////////////////////////////////////////
public float getSkillFloat() {
return skillFloat;
}
public BasicPlayer getPlayer(int _p) {
if (_p >= PLAYER_COUNT && _p < 0) return null;
else return players[_p];
}
public String getText() {
return textConsole;
}
public int getState() {
return gameState;
}
}