-
Notifications
You must be signed in to change notification settings - Fork 0
/
Civilization.java
141 lines (112 loc) · 3.63 KB
/
Civilization.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package civ.core;
import static civ.core.instance.IData.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import civ.core.input.KeyboardHandler;
import civ.core.input.MouseHandler;
public class Civilization extends BaseCivilization {
private static final long serialVersionUID = -5800474980111119057L;
private JFrame f;
private JPanel p;
private transient Game game;
private transient MouseHandler m;
private transient KeyboardHandler k;
private static final String TITLE = "Civilization";
private static final double TARGET_UPS = 60.0D;
private static final double ONE_NANO = 1E9D;
private boolean running = false;
public static void main (String[] args) {
String lcOSName = System.getProperty("os.name");
boolean isMac = lcOSName.contains("OS X");
if (isMac) {
//place menu items on the mac toolbar
System.setProperty("apple.laf.useScreenMenuBar", "true");
//use smoother fonts
System.setProperty("apple.awt.textantialiasing", "true");
//ref: http://developer.apple.com/releasenotes/Java/Java142RNTiger/1_NewFeatures/chapter_2_section_3.html
System.setProperty("apple.awt.graphics.EnableQ2DX","true");
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
EventQueue.invokeLater(() -> new Civilization().start());
}
public Civilization() {
f = new JFrame();
m = new MouseHandler();
k = new KeyboardHandler();
p = new MapPanel();
}
public synchronized void start() {
running = true;
init();
new Thread(this, "Game").start();
}
private void init() {
game = new Game(1);
f.setTitle(TITLE);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.addKeyListener(k);
f.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
class MapPanel extends JPanel {
private static final long serialVersionUID = -5681252766728523060L;
public MapPanel() {
setBorder(BorderFactory.createEmptyBorder());
setBackground(Color.BLACK);
addMouseListener(m);
addMouseMotionListener(m);
addMouseWheelListener(m);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
game.draw(g2d);
}
}
private void render() {
f.repaint();
}
private void update() {
game.update();
}
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
long now = 0;
final double ns = ONE_NANO / TARGET_UPS;
double delta = 0;
while (running) {
now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
update();
render();
delta--;
}
if ((System.currentTimeMillis() - timer) > 1000)
timer += 1000;
}
}
}