changeset 60:4a984e3b27d2

Cleanups. Add a ad-hoc about box, it's ugly.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 22:59:38 +0200
parents fd10a9422b60
children ceaf645ed930
files game/Engine.java
diffstat 1 files changed, 84 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/game/Engine.java	Wed Feb 23 22:12:01 2011 +0200
+++ b/game/Engine.java	Wed Feb 23 22:59:38 2011 +0200
@@ -334,6 +334,9 @@
     SoundManager soundManager;
     Sound musa;
 
+    boolean aboutEnabled = false, aboutSecret = false;
+    BufferedImage aboutImg = null;
+
     IDMContainer widgets;
 
     public Engine()
@@ -350,6 +353,9 @@
             ResourceLoader res = new ResourceLoader("graphics/board.jpg");
             lautaBG = ImageIO.read(res.getStream());
 
+            res = new ResourceLoader("graphics/lavina.jpg");
+            aboutImg = ImageIO.read(res.getStream());
+
             try {
                 res = new ResourceLoader("graphics/font.ttf");
                 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
@@ -362,7 +368,7 @@
                 System.out.print("Could not initialize fonts.\n");
             }
             
-//            musa = soundManager.getSound("sounds/gamemusic.wav");
+            musa = soundManager.getSound("sounds/gamemusic.wav");
         }
         catch (IOException e)
         {
@@ -378,12 +384,14 @@
         widgets = new IDMContainer();
 
         widgets.add(new BtnSwapPiece(0.75f, 0.60f));
+        widgets.add(new BtnAbout(0.75f, 0.75f));
         widgets.add(new BtnNewGame(0.75f, 0.85f));
 
         lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63);
         widgets.add(lauta);
 
         // Game
+        aboutEnabled = false;
         startNewGame();
         
         // Initialize event listeners
@@ -409,6 +417,37 @@
         lauta.startNewGame();
     }
 
+    public void paintAbout(Graphics2D g)
+    {
+        int width = lautaDim.width / 2,
+            height = lautaDim.height / 4;
+        
+        int x = (lautaDim.width - width) / 2,
+            y = (lautaDim.height - height) / 2;
+        
+        g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f));
+        g.fill(new RoundRectangle2D.Float(x, y, width, height, 10, 10));
+
+
+        g.setFont(font1);
+        g.setPaint(Color.white);
+
+        g.drawString("RistiPolku v0.5", x + 10, y + 25);
+        if (aboutSecret)
+        {
+            g.drawImage(aboutImg, x + 15, y + 35, aboutImg.getWidth(), aboutImg.getHeight(), null); 
+            g.drawString("Dedicated to my", x + 225, y + 55);
+            g.drawString("favorite woman", x + 225, y + 85);
+            g.drawString("in the world.", x + 225, y + 115);
+            g.drawString("- Matti", x + 350, y + 155);
+        }
+        else
+        {
+            g.drawString("(c) Copyright 2011 Matti 'ccr' Hämäläinen", x + 10, y + 55);
+            g.drawString("Ohjelmointiprojekti Java-kurssille T740306", x + 10, y + 105);
+        }
+    }
+
     public void paintComponent(Graphics g)
     {
         Graphics2D g2 = (Graphics2D) g;
@@ -484,7 +523,11 @@
         g2.setFont(font1);
         long currTime = new Date().getTime();
         g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20);
-
+        
+        if (aboutEnabled)
+        {
+            paintAbout(g2);
+        }
     }
 
     public void startThreads()
@@ -519,23 +562,24 @@
 
     public void mousePressed(MouseEvent e)
     {
-        widgets.mousePressed(e);
+        if (!aboutEnabled)
+            widgets.mousePressed(e);
     }
 
     public void mouseReleased(MouseEvent e)
     {
-        widgets.mouseReleased(e);
+        if (!aboutEnabled)
+            widgets.mouseReleased(e);
     }
 
     public void mouseClicked(MouseEvent e)
     {
-        System.out.print("mouseClicked()\n");
         if (!hasFocus())
         {
-            System.out.print("requesting focus\n");
+            System.out.print("Requesting focus\n");
             requestFocus();
         }
-        else
+        if (!aboutEnabled)
         {
             lauta.mouseClicked(e);
         }
@@ -556,11 +600,26 @@
    
     public void keyPressed(KeyEvent e)
     {
-        // Handle keyboard input
-        if (lauta.keyPressed(e))
-            return;
-        
-        widgets.keyPressed(e);
+        if (aboutEnabled)
+        {
+            if (e.getKeyCode() == KeyEvent.VK_L)
+            {
+                aboutSecret = true;
+            }
+            else
+            {
+                aboutEnabled = false;
+                aboutSecret = false;
+            }
+        }
+        else
+        {
+            // Handle keyboard input
+            if (lauta.keyPressed(e))
+                return;
+
+            widgets.keyPressed(e);
+        }
     }
     
     public void run()
@@ -615,4 +674,17 @@
             lauta.pieceSwapCurrent();
         }
     }
+
+    class BtnAbout extends IDMButton
+    {
+        public BtnAbout(float x, float y)
+        {
+            super(x, y, KeyEvent.VK_A, font1, "Tietoja");
+        }
+
+        public void clicked()
+        {
+            aboutEnabled = true;
+        }
+    }
 }