changeset 37:3dc5ae9f1c80

Work on game logic.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Feb 2011 04:58:30 +0200
parents a43587e14485
children 736de7b28701
files game/Engine.java game/Piece.java
diffstat 2 files changed, 36 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/game/Engine.java	Thu Feb 17 22:05:48 2011 +0200
+++ b/game/Engine.java	Fri Feb 18 04:58:30 2011 +0200
@@ -15,7 +15,7 @@
 import java.util.*;
 import java.io.*;
 import game.*;
-
+import javax.sound.sampled.*;
 
 class PathInfo
 {
@@ -205,17 +205,22 @@
                     
                     // Get next piece
                     point = curr.getConnection(point);
+                    int npoint = -1;
                     switch (point)
                     {
-                        case 0: y--; break;
-                        case 1: y--; break;
-                        case 2: x++; break;
-                        case 3: x++; break;
-                        case 4: y++; break;
-                        case 5: y++; break;
-                        case 6: x--; break;
-                        case 7: x--; break;
+                        case 0: y--; npoint = 5; break;
+                        case 1: y--; npoint = 4; break;
+                        
+                        case 2: x++; npoint = 7; break;
+                        case 3: x++; npoint = 6; break;
+                        
+                        case 4: y++; npoint = 1; break;
+                        case 5: y++; npoint = 0; break;
+                        
+                        case 6: x--; npoint = 3; break;
+                        case 7: x--; npoint = 2; break;
                     }
+                    point = npoint;
                 }
             }
             else
@@ -287,14 +292,10 @@
     BufferedImage lautaBG = null, lautaBGScaled = null;
     Dimension lautaDim;
 
-    SoundElement[] sounds;
 
-    Piece testi; int con = 0;
-
-    public SoundElement snd(Sound snd)
-    {
-        return sounds[snd.ordinal()];
-    }
+    static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
+    SoundManager smgr;
+    Sound musa, placed;
 
     public Engine()
     {
@@ -305,8 +306,8 @@
         gameFrames = 0;
         startTime = new Date().getTime();
 
-        testi = new Piece(PieceType.LOCKED);
-
+        // Sound system
+        smgr = new SoundManager(sfmt, 16);
 
         // Load resources
         try
@@ -324,25 +325,21 @@
             {
                 System.out.print("Could not initialize fonts.\n");
             }
-
-            sounds = new SoundElement[16];
-            for (Sound s : Sound.values())
-            {
-                System.out.print(s +" = "+ s.ordinal() +"\n");
-                sounds[s.ordinal()] = new SoundElement("sounds/" + s.getName(), s.isStreaming());
-            }
+            
+            musa = smgr.getSound("sounds/gamemusic.wav");
+//            placed = smgr.getSound("sounds/placed.wav");
         }
         catch (IOException e)
         {
-/*
             JOptionPane.showMessageDialog(null,
                 e.getMessage(),
                 "Initialization error",
                 JOptionPane.ERROR_MESSAGE);
-*/
+
             System.out.print(e.getMessage());
         }
 
+        // Initialize game components
         lauta = new GameBoard();
         addKeyListener(this);
         addMouseListener(this);
@@ -353,8 +350,8 @@
             System.out.print("Engine(): requesting focus\n");
             requestFocus();
         }
-
-        snd(Sound.MUSIC_GAME1).loop(-1);
+        
+        smgr.play(musa);
     }
 
 
@@ -388,8 +385,6 @@
         g2.drawImage(lautaBGScaled, 0, 0, null);
         lauta.paint(g2, 100, 150, 60);
 
-        testi.paint(g2, 50f,50f,100f);
-
         // Scores
         g2.setFont(font1);
         g2.setPaint(Color.white);
@@ -398,8 +393,6 @@
         // Other elements
         long currTime = new Date().getTime();
         g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20);
-        
-        g2.drawString("con = "+ con, 20, 30);
     }
 
     public void startThreads()
@@ -416,6 +409,8 @@
     public void stopThreads()
     {
         System.out.print("stopThreads()\n");
+        
+        // Stop animations
         if (animThread != null)
         {
             animThread.interrupt();
@@ -423,11 +418,8 @@
             animThread = null;
         }
 
-        for (Sound s : Sound.values())
-        {
-            if (snd(s) != null)
-                snd(s).stop();
-        }
+        // Shut down sound manager
+        smgr.close();
     }
 
     public void mousePressed(MouseEvent e) { }
@@ -469,9 +461,8 @@
                 break;
 
             case KeyEvent.VK_ENTER:
+                smgr.play(placed);
                 lauta.pieceFinishTurn();
-                snd(Sound.PIECE_PLACED).stop();
-                snd(Sound.PIECE_PLACED).play();
                 break;
             
             case KeyEvent.VK_ESCAPE:
@@ -496,11 +487,6 @@
                 gameFrames++;
             }
             
-            // debugataan
-                con = ((int) (gameClock / 100)) & 7;
-                testi.clearActiveConnections();
-                testi.setActiveConnection(con);
-
             // Sleep for a moment
             try {
                 Thread.sleep(10);
--- a/game/Piece.java	Thu Feb 17 22:05:48 2011 +0200
+++ b/game/Piece.java	Fri Feb 18 04:58:30 2011 +0200
@@ -86,7 +86,11 @@
 
     public int getConnection(int in)
     {
-        return connections[(in + (currRotation * 2)) % 8];
+        int offs = (in + (currRotation * 2)) % 8;
+        if (offs < 0)
+            offs = 8 + offs;
+
+        return connections[offs];
     }
     
     public void rotate(RotateDir dir)