|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package game; |
| 7 | + |
| 8 | +import java.awt.BorderLayout; |
| 9 | +import map.Tiles; |
| 10 | +import java.awt.Color; |
| 11 | +import java.awt.Dimension; |
| 12 | +import java.awt.Graphics; |
| 13 | +import java.awt.Graphics2D; |
| 14 | +import java.awt.GraphicsConfiguration; |
| 15 | +import java.awt.GraphicsDevice; |
| 16 | +import java.awt.GraphicsEnvironment; |
| 17 | +import java.awt.GridLayout; |
| 18 | +import java.awt.Image; |
| 19 | +import java.awt.Rectangle; |
| 20 | +import java.awt.RenderingHints; |
| 21 | +import java.awt.Transparency; |
| 22 | +import java.awt.event.KeyAdapter; |
| 23 | +import java.awt.event.KeyEvent; |
| 24 | +import java.awt.geom.AffineTransform; |
| 25 | +import java.awt.image.BufferedImage; |
| 26 | +import java.io.File; |
| 27 | +import java.io.FileNotFoundException; |
| 28 | +import java.io.IOException; |
| 29 | +import java.net.URL; |
| 30 | +import java.util.Observer; |
| 31 | +import java.util.logging.Level; |
| 32 | +import java.util.logging.Logger; |
| 33 | +import javax.imageio.ImageIO; |
| 34 | +import javax.sound.sampled.AudioInputStream; |
| 35 | +import javax.sound.sampled.AudioSystem; |
| 36 | +import javax.sound.sampled.Clip; |
| 37 | +import javax.swing.ImageIcon; |
| 38 | +import javax.swing.JFrame; |
| 39 | +import javax.swing.JPanel; |
| 40 | +import map.CharacterPiece; |
| 41 | +import map.GamePiece; |
| 42 | +import map.Map; |
| 43 | +import map.ObstaclePiece; |
| 44 | +import map.Tile; |
| 45 | + |
| 46 | +/** |
| 47 | + * |
| 48 | + * @author awmil_000 |
| 49 | + */ |
| 50 | +public class Game extends JFrame implements Runnable{ |
| 51 | + public static Dimension ZERO_VECTOR = new Dimension(0,0); |
| 52 | + public static AffineTransform ZERO_ROTATION = new AffineTransform(); |
| 53 | + public static int FRAMES_PER_SECOND = 30; |
| 54 | + private static double FRAME_PERIOD_MILLIS = (1000/Game.FRAMES_PER_SECOND); |
| 55 | + private long framePeriod; |
| 56 | + public static boolean DRAW_DEBUG_LINES = false; |
| 57 | + static GraphicsConfiguration config; |
| 58 | + MiniMap miniMap; |
| 59 | + Dimension screenSplitLocation; |
| 60 | + public static final boolean ENABLE_MUSIC = false; |
| 61 | + |
| 62 | + static{ |
| 63 | + GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 64 | + GraphicsDevice device = env.getDefaultScreenDevice(); |
| 65 | + config = device.getDefaultConfiguration(); |
| 66 | + } |
| 67 | + |
| 68 | + public JFrame otherframe; |
| 69 | + public JPanel otherpanel; |
| 70 | + |
| 71 | + public static Rectangle getRectCollider(Dimension origin, Dimension size) { |
| 72 | + return new Rectangle(origin.width,origin.height,size.width,size.height); |
| 73 | + } |
| 74 | + |
| 75 | + public static int distance(Dimension d1, Dimension d2) { |
| 76 | + int dx = Math.abs(d1.width - d2.width); |
| 77 | + int dy = Math.abs(d1.height - d2.height); |
| 78 | + return dx+dy; |
| 79 | + } |
| 80 | + |
| 81 | + public static Dimension rotate(int magnitude, double heading) { |
| 82 | + float x = magnitude; |
| 83 | + float y = magnitude; |
| 84 | + x = (float) -(x*Math.cos(heading)); |
| 85 | + y = (float) -(y*Math.sin(heading)); |
| 86 | + return new Dimension(Math.round(x),Math.round(y)); |
| 87 | + } |
| 88 | + |
| 89 | + private Thread thread; |
| 90 | + |
| 91 | + Map gameMap; |
| 92 | + MapView camera1; |
| 93 | + static final int TILES_PER_DIMENSION = 10; |
| 94 | + static final Dimension SCREENSIZE = new Dimension(6,6); |
| 95 | + static final char[] controls1 = {'w','a','s','d',' '}; |
| 96 | + static final char[] controls2 = {'i','j','k','l','b'}; |
| 97 | + KeyController player1Controller = new KeyController(controls1); |
| 98 | + GamePiece player1; |
| 99 | + |
| 100 | + KeyController player2Controller = new KeyController(controls2); |
| 101 | + GamePiece player2; |
| 102 | + MapView camera2; |
| 103 | + private final GamePanel gameView; |
| 104 | + private int player2Score=0; |
| 105 | + private int player1Score=0; |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + public Game(int sqrtMapTiles){ |
| 110 | + |
| 111 | + BufferedImage bimg; |
| 112 | + try { |
| 113 | + //gameMap stores persistant world data |
| 114 | + //constructor creates a square map with the given side length |
| 115 | + |
| 116 | + URL url = Game.class.getResource("/res/tank1.map"); |
| 117 | + gameMap = new Map("tank1.map"); |
| 118 | + } catch (Exception ex) { |
| 119 | + Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex); |
| 120 | + } |
| 121 | + |
| 122 | + bimg = (BufferedImage) getSprite("/res/tank1_strip60.png"); |
| 123 | + player1 = new CharacterPiece(bimg,player1Controller,new Dimension(300,300)); |
| 124 | + player2 = new CharacterPiece(bimg,player2Controller,new Dimension(600,600)); |
| 125 | + player1 = gameMap.add(player1); |
| 126 | + |
| 127 | + |
| 128 | + player2 = gameMap.add(player2); |
| 129 | + |
| 130 | + //camera is a view into the gameMap |
| 131 | + //currently this should show the whole map |
| 132 | + camera1 = new MapView(gameMap,new Dimension(500,500),player1); |
| 133 | + |
| 134 | + camera2 = new MapView(gameMap,new Dimension(500,500),player2); |
| 135 | + |
| 136 | + gameView = new GamePanel(new Dimension(1000,500)); |
| 137 | + |
| 138 | + screenSplitLocation = new Dimension(500,500); |
| 139 | + |
| 140 | + miniMap = new MiniMap(gameMap,10); |
| 141 | + |
| 142 | + add(gameView); |
| 143 | + |
| 144 | + setSize(new Dimension(1035,540)); |
| 145 | + |
| 146 | + setTitle("Application"); |
| 147 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 148 | + setLocationRelativeTo(null); |
| 149 | + addKeyListener(player1Controller); |
| 150 | + addKeyListener(player2Controller); |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + public static void main(String args[]) { |
| 155 | + JFrame runner = new Game(TILES_PER_DIMENSION); |
| 156 | +// runner.pack(); |
| 157 | + runner.setFocusable(true); |
| 158 | + runner.setVisible(true); |
| 159 | +// ((Game)runner).otherframe.setVisible(true); |
| 160 | +// ((Game)runner).otherframe.setFocusable(true); |
| 161 | + Thread game = new Thread((Runnable) runner); |
| 162 | + game.start(); |
| 163 | + |
| 164 | + if(ENABLE_MUSIC) |
| 165 | + try { |
| 166 | + URL defaultSound = Game.class.getResource("/res/Music.mid"); |
| 167 | + //System.out.print("\n"+defaultSound.toString()); |
| 168 | + // getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav"); |
| 169 | +// File soundFile = new File(defaultSound.toURI()); |
| 170 | + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound); |
| 171 | + Clip clip = AudioSystem.getClip(); |
| 172 | + clip.open(audioInputStream); |
| 173 | + clip.start(); |
| 174 | + } catch (Exception e) { |
| 175 | + System.out.print("\n"+e.toString()); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + |
| 180 | + @Override |
| 181 | + public void run() { |
| 182 | + while(true){ |
| 183 | + long ping = System.currentTimeMillis(); |
| 184 | + gameMap.moveAll(); |
| 185 | +// camera1.repaint(); |
| 186 | +// camera2.repaint(); |
| 187 | + gameView.repaint(); |
| 188 | + gameMap.cleanUp(); |
| 189 | + |
| 190 | + if(!gameMap.getObjects().contains(player1)){ |
| 191 | + if(player1.getRespawn() == 0){ |
| 192 | + gameMap.spawnPlayer(player1); |
| 193 | + player2.addScore(1); |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if(!gameMap.getObjects().contains(player2)){ |
| 198 | + if(player2.getRespawn() == 0){ |
| 199 | + gameMap.spawnPlayer(player2); |
| 200 | + player1.addScore(1); |
| 201 | + } |
| 202 | + } |
| 203 | + try { |
| 204 | + Thread.sleep((long) framePeriod); |
| 205 | + ping = System.currentTimeMillis() - ping; |
| 206 | + framePeriod += (FRAME_PERIOD_MILLIS - ping); |
| 207 | + framePeriod = framePeriod > 0 ? framePeriod : 0; |
| 208 | +// System.out.printf("Frame Rate: %f\n", (float)(1000/((float)framePeriod))); |
| 209 | + } catch (InterruptedException ex) { |
| 210 | + System.out.print(ex); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + Dimension offset = new Dimension(520,0); |
| 216 | + public void paintEverthing(Graphics2D g2d){ |
| 217 | + camera1.drawView(g2d,new Dimension(0,0)); |
| 218 | + camera2.drawView(g2d,offset); |
| 219 | + miniMap.draw(g2d,screenSplitLocation); |
| 220 | + } |
| 221 | + |
| 222 | + public static BufferedImage getSprite(String path) |
| 223 | + { |
| 224 | + BufferedImage img = null; |
| 225 | + URL url = Game.class.getResource(path); |
| 226 | + try { |
| 227 | + BufferedImage paintable = ImageIO.read(url); |
| 228 | + img = Game.getCompatImage(img,paintable.getWidth(),paintable.getHeight()); |
| 229 | + Graphics2D g2d = img.createGraphics(); |
| 230 | + g2d.drawImage(paintable,0,0, null); |
| 231 | + g2d.dispose(); |
| 232 | +// System.out.print(url); |
| 233 | + } catch (IOException ex) { |
| 234 | + Logger.getLogger(Tile.class.getName()).log(Level.SEVERE, null, ex); |
| 235 | + } |
| 236 | + |
| 237 | + return img; |
| 238 | + } |
| 239 | + |
| 240 | + public static Dimension add(Dimension d1,Dimension d2){ |
| 241 | + Dimension result = new Dimension( |
| 242 | + d1.width+d2.width, |
| 243 | + d1.height+d2.height |
| 244 | + ); |
| 245 | + return result; |
| 246 | + } |
| 247 | + |
| 248 | + public static BufferedImage getCompatImage(BufferedImage img,int wd, int ht){ |
| 249 | + img = config.createCompatibleImage(wd, ht, Transparency.TRANSLUCENT); |
| 250 | + img.setAccelerationPriority(1); |
| 251 | + return img; |
| 252 | + } |
| 253 | + |
| 254 | + private class GamePanel extends JPanel{ |
| 255 | + public GamePanel(Dimension size){ |
| 256 | + super(); |
| 257 | + setSize(size); |
| 258 | + setOpaque(true); |
| 259 | + setBackground(Color.BLACK); |
| 260 | + } |
| 261 | + |
| 262 | + @Override |
| 263 | + public void paint(Graphics g){ |
| 264 | + super.paint(g); |
| 265 | + |
| 266 | + Graphics2D g2d = (Graphics2D)g; |
| 267 | + paintEverthing(g2d); |
| 268 | + |
| 269 | + } |
| 270 | + |
| 271 | + } |
| 272 | + |
| 273 | + public static void playClip(String path){ |
| 274 | + try { |
| 275 | + URL defaultSound = Game.class.getResource(path); |
| 276 | +// System.out.print("\n"+defaultSound.toString()); |
| 277 | + // getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav"); |
| 278 | +// File soundFile = new File(defaultSound.toURI()); |
| 279 | + AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound); |
| 280 | + Clip clip = AudioSystem.getClip(); |
| 281 | + clip.open(audioInputStream); |
| 282 | + clip.start(); |
| 283 | + } catch (Exception e) { |
| 284 | + System.out.print("\n"+e.toString()); |
| 285 | + } |
| 286 | + } |
| 287 | + |
| 288 | +} |
| 289 | + |
0 commit comments