Mercurial > hg > ristipolku
view game/Engine.java @ 32:e480579cc460
More work on debugging the path resolving.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Feb 2011 18:22:16 +0200 |
parents | 60a4579a79df |
children | aa15b2c556b4 |
line wrap: on
line source
/* * Ristipolku Game Engine * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> */ package game; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.image.*; import java.awt.event.*; import java.awt.font.*; import javax.imageio.*; import javax.swing.*; import java.util.*; import java.io.*; import game.*; class PathInfo { public int in, inX, inY, out, outX, outY; public PathInfo(int in, int inX, int inY, int out, int outX, int outY) { this.in = in; this.inX = inX; this.inY = inY; this.out = out; this.outX = outX; this.outY = outY; } public void print() { System.out.print("PathInfo: inP="+in+", inX="+inX+", inY="+inY+"\n"); System.out.print(" outP="+out+", outX="+outX+", outY="+outY+"\n"); } } /* class AnimatedElement { float x, y, stime, value; Interpolate lerp; boolean active; public AnimatedElement(float x, float y, ) { stime = 0; this.x = x; this.y = y; } public animate(float time) { if (!active) { active = true; stime = time; } float t = (time - stime) / 10.0f; if (t < 100) value = lerp.getValue(t); else { } } public paint(Graphics2D g, ); { } } */ class IDMWidget { public IDMWidget() { } public void paint(Graphics2D g) { } public boolean hit(float x, float y) { return false; } } class IDMButton { BufferedImage imgUp, imgPressed; public IDMButton(float x, float y, String text) { } public void paint(Graphics2D g) { } public boolean hit(float x, float y) { return false; } } class GameBoard { public static final int boardSize = 9; public static final int boardMiddle = 4; Piece[][] board; Piece current; public boolean flagGameOver; int moveX, moveY, movePoint; public GameBoard() { board = new Piece[boardSize][boardSize]; board[boardMiddle][boardMiddle] = new Piece(PieceType.START); moveX = boardMiddle; moveY = boardMiddle - 1; movePoint = 5; pieceFinishTurn(); flagGameOver = false; } public void paint(Graphics2D g, int sx, int sy, float scale) { for (int y = 0; y < boardSize; y++) for (int x = 0; x < boardSize; x++) if (board[x][y] != null) { AffineTransform save = g.getTransform(); board[x][y].paint(g, sx + (x * scale), sy + (y * scale), scale - scale / 10); g.setTransform(save); } } public void animate(float time) { for (int y = 0; y < boardSize; y++) for (int x = 0; x < boardSize; x++) if (board[x][y] != null) { board[x][y].animate(time); } } private boolean isEmpty(int x, int y) { return (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x][y] == null); } public void pieceRotate(Piece.RotateDir dir) { if (current != null) current.rotate(dir); } public PathInfo resolvePath(int startX, int startY, int startPoint, boolean mark) { int x = startX, y = startY; int point = startPoint; Piece curr = board[x][y]; do { if (x >= 0 && x < boardSize && y >= 0 && y < boardSize) { curr = board[x][y]; if (curr == null) break; if (curr.getType() == PieceType.START) { // Hit center starting piece return null; } else { // Mark, if needed if (mark) { curr.setType(PieceType.LOCKED); curr.setActiveConnection(point); } // Get next piece point = curr.getConnection(point); 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; } } } else { // Outside of the board return null; } } while (curr != null); return new PathInfo(startPoint, startX, startY, point, x, y); } public void pieceFinishTurn() { if (current != null) { current.setType(PieceType.LOCKED); PathInfo i = resolvePath(moveX, moveY, movePoint, true); if (i != null) { System.out.print("moveX="+moveX+", moveY="+moveY+", movePoint="+movePoint+"\n"); i.print(); moveX = i.outX; moveY = i.outY; movePoint = i.out; } } current = new Piece(PieceType.ACTIVE); if (isEmpty(moveX, moveY)) { board[moveX][moveY] = current; } else { PathInfo i = resolvePath(moveX, moveY, movePoint, true); if (i != null) board[moveX][moveY] = current; else { System.out.print("pieceFinishTurn(): Game Over!\n"); flagGameOver = true; } } } } public class Engine extends JPanel implements Runnable, KeyListener, MouseListener { long startTime; float gameClock, gameFrames; Thread animThread; boolean animEnable = false; Font fontMain, font1, font2; GameBoard lauta = null; BufferedImage lautaBG = null, lautaBGScaled = null; Dimension lautaDim; SoundElement[] sounds; Piece testi; int con = 0; public SoundElement snd(Sound snd) { return sounds[snd.ordinal()]; } public Engine() { // Initialize globals System.out.print("Engine() constructor\n"); gameClock = 0; gameFrames = 0; startTime = new Date().getTime(); testi = new Piece(PieceType.LOCKED); // Load resources try { ResourceLoader res = new ResourceLoader("graphics/board.jpg"); lautaBG = ImageIO.read(res.getStream()); try { res = new ResourceLoader("graphics/font.ttf"); fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); font1 = fontMain.deriveFont(24f); font2 = fontMain.deriveFont(32f); } catch (FontFormatException e) { 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()); } } catch (IOException e) { /* JOptionPane.showMessageDialog(null, e.getMessage(), "Initialization error", JOptionPane.ERROR_MESSAGE); */ System.out.print(e.getMessage()); } lauta = new GameBoard(); addKeyListener(this); addMouseListener(this); // Get initial focus if (!hasFocus()) { System.out.print("Engine(): requesting focus\n"); requestFocus(); } snd(Sound.MUSIC_GAME1).loop(-1); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Use antialiasing when rendering the game elements g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // Rescale background if component size has changed Dimension dim = getSize(); if (lautaDim == null || !dim.equals(lautaDim)) { lautaBGScaled = new BufferedImage(dim.width, dim.height, BufferedImage.TYPE_INT_ARGB); Graphics2D gimg = lautaBGScaled.createGraphics(); gimg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); lautaDim = dim; System.out.print("scale changed\n"); } // Background, pieces 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); // 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() { System.out.print("startThreads()\n"); if (animThread == null) { animThread = new Thread(this); animEnable = true; animThread.start(); } } public void stopThreads() { System.out.print("stopThreads()\n"); if (animThread != null) { animThread.interrupt(); animEnable = false; animThread = null; } for (Sound s : Sound.values()) { if (snd(s) != null) snd(s).stop(); } } public void mousePressed(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { System.out.print("mouseClicked()\n"); if (!hasFocus()) { System.out.print("requesting focus\n"); requestFocus(); } } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { } public void keyPressed(KeyEvent e) { // Handle keyboard input switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: case KeyEvent.VK_UP: lauta.pieceRotate(Piece.RotateDir.LEFT); break; case KeyEvent.VK_RIGHT: case KeyEvent.VK_DOWN: lauta.pieceRotate(Piece.RotateDir.RIGHT); break; case KeyEvent.VK_ENTER: lauta.pieceFinishTurn(); snd(Sound.PIECE_PLACED).stop(); snd(Sound.PIECE_PLACED).play(); break; } } public void run() { while (animEnable) { // Progress game animation clock gameClock++; // Animate components lauta.animate(gameClock); // Repaint with a frame limiter if (gameClock % 3 == 1) { repaint(); gameFrames++; } // debugataan con = ((int) (gameClock / 100)) & 7; testi.clearActiveConnections(); testi.setActiveConnection(con); // Sleep for a moment try { Thread.sleep(10); } catch (InterruptedException x) { } } } }