Mercurial > hg > ristipolku
comparison game/Engine.java @ 37:3dc5ae9f1c80
Work on game logic.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 18 Feb 2011 04:58:30 +0200 |
parents | aa15b2c556b4 |
children | e682b623aea9 |
comparison
equal
deleted
inserted
replaced
36:a43587e14485 | 37:3dc5ae9f1c80 |
---|---|
13 import javax.imageio.*; | 13 import javax.imageio.*; |
14 import javax.swing.*; | 14 import javax.swing.*; |
15 import java.util.*; | 15 import java.util.*; |
16 import java.io.*; | 16 import java.io.*; |
17 import game.*; | 17 import game.*; |
18 | 18 import javax.sound.sampled.*; |
19 | 19 |
20 class PathInfo | 20 class PathInfo |
21 { | 21 { |
22 public int in, inX, inY, out, outX, outY; | 22 public int in, inX, inY, out, outX, outY; |
23 | 23 |
203 curr.setActiveConnection(point); | 203 curr.setActiveConnection(point); |
204 } | 204 } |
205 | 205 |
206 // Get next piece | 206 // Get next piece |
207 point = curr.getConnection(point); | 207 point = curr.getConnection(point); |
208 int npoint = -1; | |
208 switch (point) | 209 switch (point) |
209 { | 210 { |
210 case 0: y--; break; | 211 case 0: y--; npoint = 5; break; |
211 case 1: y--; break; | 212 case 1: y--; npoint = 4; break; |
212 case 2: x++; break; | 213 |
213 case 3: x++; break; | 214 case 2: x++; npoint = 7; break; |
214 case 4: y++; break; | 215 case 3: x++; npoint = 6; break; |
215 case 5: y++; break; | 216 |
216 case 6: x--; break; | 217 case 4: y++; npoint = 1; break; |
217 case 7: x--; break; | 218 case 5: y++; npoint = 0; break; |
219 | |
220 case 6: x--; npoint = 3; break; | |
221 case 7: x--; npoint = 2; break; | |
218 } | 222 } |
223 point = npoint; | |
219 } | 224 } |
220 } | 225 } |
221 else | 226 else |
222 { | 227 { |
223 // Outside of the board | 228 // Outside of the board |
285 Font fontMain, font1, font2; | 290 Font fontMain, font1, font2; |
286 GameBoard lauta = null; | 291 GameBoard lauta = null; |
287 BufferedImage lautaBG = null, lautaBGScaled = null; | 292 BufferedImage lautaBG = null, lautaBGScaled = null; |
288 Dimension lautaDim; | 293 Dimension lautaDim; |
289 | 294 |
290 SoundElement[] sounds; | 295 |
291 | 296 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); |
292 Piece testi; int con = 0; | 297 SoundManager smgr; |
293 | 298 Sound musa, placed; |
294 public SoundElement snd(Sound snd) | |
295 { | |
296 return sounds[snd.ordinal()]; | |
297 } | |
298 | 299 |
299 public Engine() | 300 public Engine() |
300 { | 301 { |
301 // Initialize globals | 302 // Initialize globals |
302 System.out.print("Engine() constructor\n"); | 303 System.out.print("Engine() constructor\n"); |
303 | 304 |
304 gameClock = 0; | 305 gameClock = 0; |
305 gameFrames = 0; | 306 gameFrames = 0; |
306 startTime = new Date().getTime(); | 307 startTime = new Date().getTime(); |
307 | 308 |
308 testi = new Piece(PieceType.LOCKED); | 309 // Sound system |
309 | 310 smgr = new SoundManager(sfmt, 16); |
310 | 311 |
311 // Load resources | 312 // Load resources |
312 try | 313 try |
313 { | 314 { |
314 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); | 315 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); |
322 } | 323 } |
323 catch (FontFormatException e) | 324 catch (FontFormatException e) |
324 { | 325 { |
325 System.out.print("Could not initialize fonts.\n"); | 326 System.out.print("Could not initialize fonts.\n"); |
326 } | 327 } |
327 | 328 |
328 sounds = new SoundElement[16]; | 329 musa = smgr.getSound("sounds/gamemusic.wav"); |
329 for (Sound s : Sound.values()) | 330 // placed = smgr.getSound("sounds/placed.wav"); |
330 { | |
331 System.out.print(s +" = "+ s.ordinal() +"\n"); | |
332 sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isStreaming()); | |
333 } | |
334 } | 331 } |
335 catch (IOException e) | 332 catch (IOException e) |
336 { | 333 { |
337 /* | |
338 JOptionPane.showMessageDialog(null, | 334 JOptionPane.showMessageDialog(null, |
339 e.getMessage(), | 335 e.getMessage(), |
340 "Initialization error", | 336 "Initialization error", |
341 JOptionPane.ERROR_MESSAGE); | 337 JOptionPane.ERROR_MESSAGE); |
342 */ | 338 |
343 System.out.print(e.getMessage()); | 339 System.out.print(e.getMessage()); |
344 } | 340 } |
345 | 341 |
342 // Initialize game components | |
346 lauta = new GameBoard(); | 343 lauta = new GameBoard(); |
347 addKeyListener(this); | 344 addKeyListener(this); |
348 addMouseListener(this); | 345 addMouseListener(this); |
349 | 346 |
350 // Get initial focus | 347 // Get initial focus |
351 if (!hasFocus()) | 348 if (!hasFocus()) |
352 { | 349 { |
353 System.out.print("Engine(): requesting focus\n"); | 350 System.out.print("Engine(): requesting focus\n"); |
354 requestFocus(); | 351 requestFocus(); |
355 } | 352 } |
356 | 353 |
357 snd(Sound.MUSIC_GAME1).loop(-1); | 354 smgr.play(musa); |
358 } | 355 } |
359 | 356 |
360 | 357 |
361 public void paintComponent(Graphics g) | 358 public void paintComponent(Graphics g) |
362 { | 359 { |
386 | 383 |
387 // Background, pieces | 384 // Background, pieces |
388 g2.drawImage(lautaBGScaled, 0, 0, null); | 385 g2.drawImage(lautaBGScaled, 0, 0, null); |
389 lauta.paint(g2, 100, 150, 60); | 386 lauta.paint(g2, 100, 150, 60); |
390 | 387 |
391 testi.paint(g2, 50f,50f,100f); | |
392 | |
393 // Scores | 388 // Scores |
394 g2.setFont(font1); | 389 g2.setFont(font1); |
395 g2.setPaint(Color.white); | 390 g2.setPaint(Color.white); |
396 | 391 |
397 | 392 |
398 // Other elements | 393 // Other elements |
399 long currTime = new Date().getTime(); | 394 long currTime = new Date().getTime(); |
400 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); | 395 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); |
401 | |
402 g2.drawString("con = "+ con, 20, 30); | |
403 } | 396 } |
404 | 397 |
405 public void startThreads() | 398 public void startThreads() |
406 { | 399 { |
407 System.out.print("startThreads()\n"); | 400 System.out.print("startThreads()\n"); |
414 } | 407 } |
415 | 408 |
416 public void stopThreads() | 409 public void stopThreads() |
417 { | 410 { |
418 System.out.print("stopThreads()\n"); | 411 System.out.print("stopThreads()\n"); |
412 | |
413 // Stop animations | |
419 if (animThread != null) | 414 if (animThread != null) |
420 { | 415 { |
421 animThread.interrupt(); | 416 animThread.interrupt(); |
422 animEnable = false; | 417 animEnable = false; |
423 animThread = null; | 418 animThread = null; |
424 } | 419 } |
425 | 420 |
426 for (Sound s : Sound.values()) | 421 // Shut down sound manager |
427 { | 422 smgr.close(); |
428 if (snd(s) != null) | |
429 snd(s).stop(); | |
430 } | |
431 } | 423 } |
432 | 424 |
433 public void mousePressed(MouseEvent e) { } | 425 public void mousePressed(MouseEvent e) { } |
434 public void mouseEntered(MouseEvent e) { } | 426 public void mouseEntered(MouseEvent e) { } |
435 public void mouseExited(MouseEvent e) { } | 427 public void mouseExited(MouseEvent e) { } |
467 case KeyEvent.VK_DOWN: | 459 case KeyEvent.VK_DOWN: |
468 lauta.pieceRotate(Piece.RotateDir.RIGHT); | 460 lauta.pieceRotate(Piece.RotateDir.RIGHT); |
469 break; | 461 break; |
470 | 462 |
471 case KeyEvent.VK_ENTER: | 463 case KeyEvent.VK_ENTER: |
464 smgr.play(placed); | |
472 lauta.pieceFinishTurn(); | 465 lauta.pieceFinishTurn(); |
473 snd(Sound.PIECE_PLACED).stop(); | |
474 snd(Sound.PIECE_PLACED).play(); | |
475 break; | 466 break; |
476 | 467 |
477 case KeyEvent.VK_ESCAPE: | 468 case KeyEvent.VK_ESCAPE: |
478 break; | 469 break; |
479 } | 470 } |
494 { | 485 { |
495 repaint(); | 486 repaint(); |
496 gameFrames++; | 487 gameFrames++; |
497 } | 488 } |
498 | 489 |
499 // debugataan | |
500 con = ((int) (gameClock / 100)) & 7; | |
501 testi.clearActiveConnections(); | |
502 testi.setActiveConnection(con); | |
503 | |
504 // Sleep for a moment | 490 // Sleep for a moment |
505 try { | 491 try { |
506 Thread.sleep(10); | 492 Thread.sleep(10); |
507 } | 493 } |
508 catch (InterruptedException x) { | 494 catch (InterruptedException x) { |