-
Notifications
You must be signed in to change notification settings - Fork 0
/
DungeonState.pde
327 lines (259 loc) · 8.4 KB
/
DungeonState.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
import ddf.minim.*;
class DungeonState extends State
{
final static int ATTACK_ROCK = 0;
final static int ATTACK_PAPER = 1;
final static int ATTACK_SCISSORS = 2;
final static int VICTOR_PLAYER = 0;
final static int VICTOR_ENEMY = 1;
final static int VICTOR_NONE = 3;
Raycaster caster;
Enemy enemy;
boolean fightMode = false;
boolean canAttack = true;
Player player;
int playerAttack = -1;
PImage rockImage;
PImage paperImage;
PImage scissorsImage;
PImage enemyHit;
PImage playerHit;
PImage combatBackground;
int enemyHitAlpha = 0;
int playerHitAlpha = 0;
PGraphics pg;
AudioPlayer step;
AudioPlayer defeat;
AudioPlayer hitYes;
AudioPlayer hitNo;
AudioPlayer hitNoOne;
void init()
{
caster = new Raycaster();
caster.renderAlpha = renderAlpha;
caster.init();
player = new Player();
rockImage = loadImage( "rock.png" );
paperImage = loadImage( "paper.png" );
scissorsImage = loadImage( "scissors.png" );
enemyHit = loadImage( "enemy_hit.png" );
playerHit = loadImage( "player_hit.png" );
combatBackground = loadImage( "combat_background.png" );
step = minim.loadFile( "step.wav" );
defeat = minim.loadFile( "explo.wav" );
hitYes = minim.loadFile( "hit-yes.wav" );
hitNo = minim.loadFile( "hit-no.wav" );
hitNoOne = minim.loadFile( "hit-noone.wav" );
pg = createGraphics( 800, 600 );
}
void handleKeys()
{
if ( keyPressed )
{
if ( fightMode ) return;
if ( key == 'W' || key == 'w' )
{
step.play();
caster.moveForward();
int randomNumber = (int)random( 50 );
if ( randomNumber == 25 )
startFight();
}
if ( key == 'A' || key == 'a' )
{
caster.rotateLeft();
}
if ( key == 'S' || key == 's' )
{
step.play();
caster.moveBackwards();
}
if ( key == 'D' || key == 'd' )
{
caster.rotateRight();
}
if ( key == CODED )
{
if ( keyCode == UP )
{
step.play();
caster.moveForward();
int randomNumber = (int)random( 50 );
if ( randomNumber == 25 )
startFight();
}
if ( keyCode == DOWN )
{
step.play();
caster.moveBackwards();
}
if ( keyCode == LEFT )
caster.rotateLeft();
if ( keyCode == RIGHT )
caster.rotateRight();
}
}
}
void handleMouse()
{
int victor = -1;
if ( !canAttack ) return;
if ( !fightMode ) return;
if ( fightMode )
{
// calculate collisions against "buttons"
if ( mouseX > 250 && mouseX < 250 + 77 && mouseY > 400 && mouseY < 400 + 77 )
{
playerAttack = ATTACK_ROCK;
canAttack = false;
victor = evalAttack();
}
if ( mouseX > 350 && mouseX < 350 + 77 && mouseY > 400 && mouseY < 400 + 77 )
{
playerAttack = ATTACK_PAPER;
canAttack = false;
victor = evalAttack();
}
if ( mouseX > 450 && mouseX < 450 + 77 && mouseY > 400 && mouseY < 400 + 77 )
{
playerAttack = ATTACK_SCISSORS;
canAttack = false;
victor = evalAttack();
}
}
switch ( victor )
{
case VICTOR_PLAYER:
enemy.hp -= (int) abs( player.str - enemy.def * 0.5 );
enemyHitAlpha = 255;
hitYes.play();
break;
case VICTOR_ENEMY:
player.hp -= (int) abs( enemy.str - player.def * 0.5 );
playerHitAlpha = 255;
hitNo.play();
break;
case VICTOR_NONE:
hitNoOne.play();
break;
}
if ( player.hp < 0 )
{
Babel.stateManager.switchState( StateManager.STATE_ENDING, false );
}
if ( enemy.hp < 0 )
{
defeat.play();
fightMode = false;
player.addXP( 10 * caster.currentFloor );
}
}
void update()
{
if ( caster.currentFloor == 8 )
Babel.stateManager.switchState( StateManager.STATE_ENDING, false );
if ( fightMode )
{
if ( enemyHitAlpha > 0 )
{
enemyHitAlpha -= 5;
}
else
{
enemyHitAlpha = 0;
}
if ( playerHitAlpha > 0 )
{
playerHitAlpha -= 5;
}
else
{
playerHitAlpha = 0;
}
if ( ( enemyHitAlpha == 0 ) && ( playerHitAlpha == 0 ) )
{
canAttack = true;
}
}
}
void startFight()
{
fightMode = true;
canAttack = true;
enemyHitAlpha = 0;
playerHitAlpha = 0;
// generate enemies and stuff
int enemyType = (int)random( 4 );
enemy = new Enemy( enemyType, caster.currentFloor );
}
void drawHUD()
{
stroke( 0, 0, 0 );
textFont( fontHUD, 12 );
text( "HP: " + player.hp, 10, 540 );
text( "LVL: " + player.level, 10, 555 );
text( "STR: " + player.str, 10, 570 );
text( "DEF: " + player.def, 10, 585 );
text( "BF" + ( caster.currentFloor + 1 ), 760, 585 );
}
void draw()
{
//background( 125, 125, 125 );
background( 145, 119, 137, renderAlpha );
fill( 249, 218, 166, renderAlpha );
rect( -1, -1, 801, 301 );
caster.renderAlpha = renderAlpha;
caster.draw();
if ( fightMode )
{
//stroke( 0, 0, 0 );
//fill( 255, 255, 255 );
//rect( 30, 40, 740, 500 );
image( combatBackground, 400 - ( combatBackground.width * 0.5 ), 300 - ( combatBackground.height * 0.5 ) );
image( enemy.image, 370 - ( enemy.image.width / 2 ), 200 );
tint(255, enemyHitAlpha);
image( enemyHit, 370 - ( enemyHit.width / 2 ), 200 );
noTint();
pg.beginDraw();
pg.background( 255, 255, 255, 0 );
pg.image( rockImage, 250, 400 );
pg.image( paperImage, 350, 400 );
pg.image( scissorsImage, 450, 400 );
if ( !canAttack )
pg.filter( GRAY );
pg.endDraw();
image( pg, 0, 0 );
tint( 255, playerHitAlpha );
image( playerHit, 40, 50 );
noTint();
}
drawHUD();
}
int evalAttack()
{
int enemyAttack = (int) random( 4 );
if ( enemyAttack == ATTACK_ROCK )
{
if ( playerAttack == ATTACK_PAPER )
return VICTOR_PLAYER;
if ( playerAttack == ATTACK_SCISSORS )
return VICTOR_ENEMY;
}
if ( enemyAttack == ATTACK_PAPER )
{
if ( playerAttack == ATTACK_ROCK )
return VICTOR_ENEMY;
if ( playerAttack == ATTACK_SCISSORS )
return VICTOR_PLAYER;
}
if ( enemyAttack == ATTACK_SCISSORS )
{
if ( playerAttack == ATTACK_ROCK )
return VICTOR_PLAYER;
if ( playerAttack == ATTACK_PAPER )
return VICTOR_ENEMY;
}
//canAttack = true;
return VICTOR_NONE;
}
}