-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGamePanel.java
296 lines (261 loc) · 10.6 KB
/
GamePanel.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.awt.event.MouseEvent;
import java.awt.event.FocusListener;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Image;
//the JPanel which has the game in it
public class GamePanel extends JPanel implements KeyListener, MouseListener, FocusListener
{
//objects for rendering
private RenderingPanel renderingPanel; //the panel which preforms rendering
private Airplane airplane; //the airplane object
private Lighting lighting;
private Camera gameCamera; //the camera used in the game
private Terrain ground; //the terrain object which is just a mesh
private GameObject runway1; //a runway game object
private GameObject runway2;
private Image flightDials; //the image of the flight dials used on the side of the panel
private boolean paused; //is the game paused?
private SidePanel sidePanel; //the panel on the side of the screen which has dials and buttons
private Color skyColor = new Color(91, 215, 252);
//creates game objects and rendering related objects.
public GamePanel()
{
setLayout(new BorderLayout());
addKeyListener(this);
addFocusListener(this);
addMouseListener(this);
sidePanel = new SidePanel();
add(sidePanel, BorderLayout.EAST);
runway1 = new GameObject("runeway1", new Mesh("runway.obj", Color.DARK_GRAY, new Vector3(0, -0.09, 37), new EulerAngle(), 300, false, false), new Transform(new Vector3()));
runway2 = new GameObject("runeway2", new Mesh("runway.obj", Color.DARK_GRAY, new Vector3(0, -0.09, 2000), new EulerAngle(), 300, false, false), new Transform(new Vector3()));
flightDials = Utils.makeImage(new File(FlightSimulator.RESOURCES_FOLDER, "AirplaneDials.png"));
lighting = new Lighting(new Vector3(1, -1, 1), 30, 150);
gameCamera = new Camera(new Vector3(0, 0, -250), 100000, 100, 60);
airplane = new Airplane(this, gameCamera);
ground = new Terrain(-500, -200, 6000, 1000, 800, 300, 0.02, 30, new Color(1, 75, 148), new Color(15, 99, 0), new Color(200, 200, 210));
gameCamera.setOrbitControls(this, airplane, 1000, 10);
}
//sets up the rendering panel and starts the rendering updates.
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
requestFocusInWindow();
if (renderingPanel == null)
{
renderingPanel = new RenderingPanel(FlightSimulator.DEFAULT_WIDTH - FlightSimulator.DEFAULT_WIDTH/4, FlightSimulator.DEFAULT_HEIGHT);
gameCamera.setFov(FlightSimulator.user.getSettings().fov);
gameCamera.setSensitivity(FlightSimulator.user.getSettings().sensitivity);
airplane.setRenderPanel(renderingPanel);
airplane.startPhysics();
renderingPanel.setLighting(lighting);
renderingPanel.setCamera(gameCamera);
renderingPanel.setLighting(lighting);
renderingPanel.setFog(gameCamera.getFarClipDistancee()*0.6, gameCamera.getFarClipDistancee(), skyColor);
renderingPanel.addMesh(ground);
renderingPanel.addMesh(runway1.getMesh());
renderingPanel.addMesh(runway2.getMesh());
renderingPanel.setFPSlimit(150);
renderingPanel.start();
add(renderingPanel);
validate();
}
}
//the panel that contains dials
class SidePanel extends JPanel
{
public SidePanel()
{
setBackground(new Color(90, 94, 97));
setPreferredSize(new Dimension(FlightSimulator.DEFAULT_WIDTH/4, 100));
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
//make buttons
Button settingsButton = new Button("Settings", 20, 150, 50);
settingsButton.addActionListener(SettingsPanel.getSettingsSwitcher(GamePanel.name()));
Button controlsButton = new Button("Controls", 20, 150, 50);
controlsButton.addActionListener(ControlsPanel.getControlsSwitcher(GamePanel.name()));
Button resetButton = new Button("Reset", 20, 150, 50);
resetButton.addActionListener(new ResetButtonListener());
add(resetButton);
add(settingsButton);
add(controlsButton);
}
//repaint called in the airplane updater causes this to be called
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//convert to graphics 2d (used for line thickness)
Graphics2D g2d = (Graphics2D)g;
//draw the flight dials
g.drawImage(flightDials, FlightSimulator.DEFAULT_WIDTH/8, 400-FlightSimulator.DEFAULT_WIDTH/8, FlightSimulator.DEFAULT_WIDTH/4, 400, 250, 15, 480, 245, this);
g.drawImage(flightDials, 0, 400, FlightSimulator.DEFAULT_WIDTH/4, FlightSimulator.DEFAULT_WIDTH/4+400, 0, 245, 480, 728, this);
g2d.setStroke(new BasicStroke(4));
g2d.setColor(Color.WHITE);
//draw dial pointers
drawThrottle(g2d, 50, 230, airplane.getThrottle());
drawDialNeedle(g2d, 252, 315, 60, airplane.orientation().y); //compass
drawDialNeedle(g2d, 85, 487, 60, airplane.getSpeed()/75);//airspeed
drawTurnCoordinator(g2d, 85, 659, airplane.orientation().z); //turn coordinator
drawDialNeedle(g2d, 255, 489, 55, airplane.getAltitude()/300/Math.PI); //altimeter hundreds needle
drawDialNeedle(g2d, 255, 489, 30, airplane.getAltitude()/3000/Math.PI); //altimeter thousands needle
drawDialNeedle(g2d, 255, 659, 60, airplane.getVerticalClimb()/Math.PI/10 - Math.PI/2); //Vertical climb
}
//draws a pointer needle for a flight dial centered at the specified coordinates and rotated
//a specified amount.
public void drawDialNeedle(Graphics2D g2d, int centerX, int centerY, int length, double rotation)
{
rotation -= Math.PI/2;
int endPointX;
int endPointY;
endPointX = centerX+(int)(Math.cos(rotation)*length);
endPointY = centerY+(int)(Math.sin(rotation)*length);
g2d.drawLine(centerX, centerY, endPointX, endPointY);
}
//similar to the dial needles, just two and sideways for the airplane's turn coordinator
public void drawTurnCoordinator(Graphics2D g2d, int centerX, int centerY, double rotation)
{
int endPointX;
int endPointY;
endPointX = (int)(Math.cos(-rotation)*50);
endPointY = (int)(Math.sin(-rotation)*50);
g2d.drawLine(centerX, centerY, centerX+endPointX, centerY+endPointY);
g2d.drawLine(centerX, centerY, centerX-endPointX, centerY-endPointY);
}
//draws the throttle using two little rectangles and a string which labels the throttle.
public void drawThrottle(Graphics2D g2d, int topLeftX, int topLeftY, double throttleAmt)
{
throttleAmt = 1-throttleAmt;
g2d.setFont(new Font(FlightSimulator.FONTSTYLE, Font.PLAIN, 30));
g2d.drawString("Throttle", topLeftX + 20, topLeftY);
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(topLeftX, topLeftY, 10, 70);
g2d.setColor(Color.WHITE);
g2d.drawRect(topLeftX, (int)(topLeftY + (70*throttleAmt)) - 10, 10, 10);
}
}
public static String name()
{
return "GamePanel";
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
togglePause();
}
}
//toggles the pause used by the key pressed escape key
public void togglePause()
{
if (paused)
{
paused = false;
repaint();
airplane.startPhysics();
renderingPanel.start();
}
else
{
paused = true;
repaint();
airplane.stopPhysics();
renderingPanel.stopThread();
}
}
//pauses the game by stopping the physics and the rendering panel
public void pause()
{
if (!paused)
{
paused = true;
repaint();
airplane.stopPhysics();
renderingPanel.stopThread();
}
}
//inpuases the game by resuming physics and rendering
public void unpause()
{
if (paused)
{
paused = false;
repaint();
airplane.startPhysics();
renderingPanel.start();
}
}
//applies the user prefferences into reality by setting the fov and sensitivity.
public void updateSettings()
{
gameCamera.setFov(FlightSimulator.user.getSettings().fov);
gameCamera.setSensitivity(FlightSimulator.user.getSettings().sensitivity);
}
public boolean isPaused()
{
return paused;
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public static SwitchToGamePanelListener getGamePanelSwitcher()
{
return new SwitchToGamePanelListener();
}
static class SwitchToGamePanelListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FlightSimulator.flightSim.showPanel(name());
}
}
class ResetButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
airplane.reset();
}
}
class PauseListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
togglePause();
}
}
public void mouseClicked(MouseEvent e) {}
//neccessary for key input to work
public void mousePressed(MouseEvent e)
{
requestFocusInWindow();
}
public void mouseReleased(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
//automatically unpuases if the game panel gains focus, but also updates settings
//incase the user just returned from the settings panel
public void focusGained(FocusEvent e)
{
unpause();
repaint();
updateSettings();
}
//automatically pauses if focus is lost.
public void focusLost(FocusEvent e)
{
repaint();
pause();
}
public void mouseEntered(MouseEvent e) {}
}