-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingObjectsPanel.java
More file actions
152 lines (125 loc) · 4.19 KB
/
MovingObjectsPanel.java
File metadata and controls
152 lines (125 loc) · 4.19 KB
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
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class MovingObjectsPanel extends JPanel {
final Dimension defaultDim;// = new Dimension(800,600);
DiepIOMap gm;
private Timer t;
public MovingObjectsPanel() {
this( new Dimension(800,600));
}
public MovingObjectsPanel(Dimension dim) {
defaultDim = dim;
this.setPreferredSize(defaultDim);
makeGameMap();
t.start();// start the timer which starts the "ticking"
}
private void makeGameMap() {
gm = new DiepIOMap(this.defaultDim);// let the map know what dim is
setUpKeyMappings();
t = new Timer(100, new ActionListener() {// fires off every 10 ms
@Override
public void actionPerformed(ActionEvent arg0) {
gm.tick();// I tell the GameMap to tick... do what
// you do every time the clock goes off.
//animation
repaint();// naturally, we want to see the new view
if ((gm.TankBot.getHealth() <=0 && gm.TankBot2.getHealth()<=0) || gm.gameOver){
gm.tick();
t.stop();
}
}
}
);// this semicolon is here because it is the end of the new Timer construction...
}
private void setUpKeyMappings() {
// maps keys with actions...
// The code below maps a KeyStroke to an action to be performed
// In this case I mapped the space bar key to the action named "shoot"
// Whenever someone hits the Space Bar the action shoot is sent out
this.getInputMap().put(KeyStroke.getKeyStroke("SPACE"),"shoot");
this.getInputMap().put(KeyStroke.getKeyStroke("W"), "moveUp");
this.getInputMap().put(KeyStroke.getKeyStroke("S"), "moveDown");
this.getInputMap().put(KeyStroke.getKeyStroke("D"), "moveRight");
this.getInputMap().put(KeyStroke.getKeyStroke("A"), "moveLeft");
this.getInputMap().put(KeyStroke.getKeyStroke("released W"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released S"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released D"), "stop");
this.getInputMap().put(KeyStroke.getKeyStroke("released A"), "stop");
// This associates the command shoot with some action. In this
// case, the action triggers a shoot command invoked on my GameMap. In general, whatever
// goes in the actionPerformed method will be executed when a shoot command
// is sent...
this.getActionMap().put("shoot",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
gm.shoot();
}
});
this.getActionMap().put("stop",new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e) {
gm.stopTank();
}
});
this.getActionMap().put("moveUp", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
gm.moveUp();
}
});
this.getActionMap().put("moveDown", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
gm.moveDown();
}
});
this.getActionMap().put("moveRight", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
gm.moveRight();
}
});
this.getActionMap().put("moveLeft", new AbstractAction(){
@Override
public void actionPerformed(ActionEvent e){
gm.moveLeft();
}
});
this.requestFocusInWindow();
}
private int mouseX(){
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
return x;
}
private int mouseY(){
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int y = (int) b.getY();
return y;
}
private double calculateAngle(Point a){
int objectX = (int) a.getX();
int objectY = (int) a.getY();
int adjacentSide = mouseX() - objectX;
int oppositeSide = mouseY() - objectY;
double angle = Math.atan(oppositeSide/adjacentSide);
return angle;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
gm.draw(g);
}
}