test/Game.java

Thu, 23 Jan 2014 14:17:06 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Jan 2014 14:17:06 +0100
changeset 22
f463693b5eeb
parent 17
7ea86024aef0
permissions
-rw-r--r--

added command line parameters for header and footer file

17
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 package de.uapcore.threelittlestars;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 import java.awt.BorderLayout;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 import java.io.IOException;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 import javax.swing.JFrame;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 import javax.swing.JOptionPane;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 import de.uapcore.threelittlestars.entities.Player;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 import de.uapcore.threelittlestars.entities.Player.PlayerCharacter;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 import de.uapcore.threelittlestars.managers.AssetManager;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 import de.uapcore.threelittlestars.managers.InputManager;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 import de.uapcore.threelittlestars.managers.WorldManager;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 import de.uapcore.threelittlestars.renderers.GameRenderer;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 public class Game implements Runnable {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 public static final int TICK_RATE = 32;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 private InputManager im;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 private AssetManager am;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 private WorldManager wm;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 private MainFrame frame;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 private MainPanel canvas;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 public Game(MainFrame frame, PlayerCharacter playerCharacter) {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 this.frame = frame;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 canvas = new MainPanel();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33 am = new AssetManager(canvas);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 wm = new WorldManager(am);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 im = new InputManager();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 canvas.setRenderer(new GameRenderer(wm));
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 canvas.addKeyListener(im);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 frame.add(canvas, BorderLayout.CENTER);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 frame.pack();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 canvas.requestFocusInWindow();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 try {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 startGame(playerCharacter);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 } catch (IOException e) {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 JOptionPane.showMessageDialog(frame, "Die Weltdaten sind beschÃĪdigt.",
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 frame.getTitle(), JOptionPane.ERROR_MESSAGE);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 System.exit(1);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 public void startGame(PlayerCharacter character) throws IOException {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 Player.setCharacter(character);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 // TODO: asset / savegame loading
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 wm.loadAsset("testworld"); // just testing here
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 new Thread(this).start();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 @Override
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 public void run() {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 long lastTick = System.currentTimeMillis();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 do {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 long currentTick = System.currentTimeMillis();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 if (currentTick - lastTick >= TICK_RATE) {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 lastTick += TICK_RATE;
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 // Deliver buffered input events
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 im.deliverEvents(wm);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 // Call updates
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 wm.update();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 // Catch other key events
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 if (im.isEscapePressed()) {
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 frame.setVisible(false);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 // Repaint canvas
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 canvas.repaint();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 Thread.yield();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 } while (frame.isVisible());
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 // Cleanup stuff
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 frame.dispose();
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 System.exit(0);
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93
7ea86024aef0 implemented java highlighting
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 }

mercurial